String stuff that should be in stdlib but isn't.

Dependents:   X10Svr SSDP_Server

Committer:
WiredHome
Date:
Wed Feb 27 18:22:04 2019 +0000
Revision:
3:bc30d348f5b4
Parent:
1:65bc379d8cd0
Fix the venerable off-by-one error; if the two strings are adjacent it was detecting a false overlap.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
WiredHome 0:6d899ce93ea0 1 /// @file SW_String.h This is a small collection of string utilities that
WiredHome 0:6d899ce93ea0 2 /// are not consistent across compilers.
WiredHome 0:6d899ce93ea0 3 ///
WiredHome 0:6d899ce93ea0 4 /// @note Copyright © 2016 by Smartware Computing, all rights reserved.
WiredHome 0:6d899ce93ea0 5 /// This software may be used to derive new software, as long as
WiredHome 0:6d899ce93ea0 6 /// this copyright statement remains in the source file.
WiredHome 0:6d899ce93ea0 7 /// @author David Smart
WiredHome 0:6d899ce93ea0 8 ///
WiredHome 0:6d899ce93ea0 9 #include "string.h"
WiredHome 0:6d899ce93ea0 10
WiredHome 1:65bc379d8cd0 11
WiredHome 1:65bc379d8cd0 12 /// A more secure version of strcat
WiredHome 1:65bc379d8cd0 13 ///
WiredHome 1:65bc379d8cd0 14 /// This function is like a wrapper on strcat, to first validate the concatination
WiredHome 1:65bc379d8cd0 15 /// and then if all parameters appear good, it will call strcat. It will not
WiredHome 1:65bc379d8cd0 16 /// permit overlapping source and destination.
WiredHome 1:65bc379d8cd0 17 ///
WiredHome 1:65bc379d8cd0 18 /// If there is an error, no concatination is performed.
WiredHome 1:65bc379d8cd0 19 ///
WiredHome 1:65bc379d8cd0 20 /// @note This has a different return value than the normal strcat.
WiredHome 1:65bc379d8cd0 21 ///
WiredHome 1:65bc379d8cd0 22 /// @param[out] dst is a pointer to the start of the destination buffer (not necessarily
WiredHome 1:65bc379d8cd0 23 /// where the next string will appear).
WiredHome 1:65bc379d8cd0 24 /// @param[in] dstSize defines the size of the destination buffer.
WiredHome 1:65bc379d8cd0 25 /// @param[in] src is a pointer to the source.
WiredHome 1:65bc379d8cd0 26 ///
WiredHome 1:65bc379d8cd0 27 /// @returns
WiredHome 1:65bc379d8cd0 28 /// - 0 = no error
WiredHome 1:65bc379d8cd0 29 /// - -1 = destination pointer invalid
WiredHome 1:65bc379d8cd0 30 /// - -2 = source is too big to append into the destination
WiredHome 1:65bc379d8cd0 31 /// - -3 = overlap between src and dst
WiredHome 1:65bc379d8cd0 32 ///
WiredHome 1:65bc379d8cd0 33 int strcat_s(char * dst, size_t dstSize, const char * src);
WiredHome 1:65bc379d8cd0 34
WiredHome 1:65bc379d8cd0 35 /// A more secure version of strcpy
WiredHome 1:65bc379d8cd0 36 ///
WiredHome 1:65bc379d8cd0 37 /// This function is like a wrapper on strcpy, to first validate the concatination
WiredHome 1:65bc379d8cd0 38 /// and then if all parameters appear good, it will call strcpy. It will not
WiredHome 1:65bc379d8cd0 39 /// permit overlapping source and destination.
WiredHome 1:65bc379d8cd0 40 ///
WiredHome 1:65bc379d8cd0 41 /// If there is an error, no copy is performed.
WiredHome 1:65bc379d8cd0 42 ///
WiredHome 1:65bc379d8cd0 43 /// @note This has a different return value than the normal strcpy.
WiredHome 1:65bc379d8cd0 44 ///
WiredHome 1:65bc379d8cd0 45 /// @param[out] dst is a pointer to the start of the destination buffer.
WiredHome 1:65bc379d8cd0 46 /// @param[in] dstSize defines the size of the destination buffer.
WiredHome 1:65bc379d8cd0 47 /// @param[in] src is a pointer to the source.
WiredHome 1:65bc379d8cd0 48 ///
WiredHome 1:65bc379d8cd0 49 /// @returns
WiredHome 1:65bc379d8cd0 50 /// - 0 = no error
WiredHome 1:65bc379d8cd0 51 /// - -1 = destination pointer invalid
WiredHome 1:65bc379d8cd0 52 /// - -2 = source is too big to append into the destination
WiredHome 1:65bc379d8cd0 53 /// - -3 = overlap between src and dst
WiredHome 1:65bc379d8cd0 54 ///
WiredHome 1:65bc379d8cd0 55 int strcpy_s(char * dst, size_t dstSize, const char * src);
WiredHome 1:65bc379d8cd0 56
WiredHome 0:6d899ce93ea0 57 /// sw_tolower exists because not all compiler libraries have this function
WiredHome 0:6d899ce93ea0 58 ///
WiredHome 0:6d899ce93ea0 59 /// This takes a character and if it is upper-case, it converts it to
WiredHome 0:6d899ce93ea0 60 /// lower-case and returns it.
WiredHome 0:6d899ce93ea0 61 ///
WiredHome 0:6d899ce93ea0 62 /// @note an alternate means would be a 256-entry lookup table. Very fast...
WiredHome 0:6d899ce93ea0 63 ///
WiredHome 0:6d899ce93ea0 64 /// @param a is the character to convert
WiredHome 0:6d899ce93ea0 65 /// @returns the lower case equivalent to a
WiredHome 0:6d899ce93ea0 66 ///
WiredHome 0:6d899ce93ea0 67 char sw_tolower(char a);
WiredHome 0:6d899ce93ea0 68
WiredHome 0:6d899ce93ea0 69 /// sw_strnicmp exists because not all compiler libraries have this function.
WiredHome 0:6d899ce93ea0 70 ///
WiredHome 0:6d899ce93ea0 71 /// In a case-insensitive compare, evaluate 'n' characters of the left and
WiredHome 0:6d899ce93ea0 72 /// right referenced strings.
WiredHome 0:6d899ce93ea0 73 ///
WiredHome 0:6d899ce93ea0 74 /// @note Some compilers have strnicmp, others _strnicmp, and others have C++
WiredHome 0:6d899ce93ea0 75 /// methods, which is outside the scope of this C-portable set of functions.
WiredHome 0:6d899ce93ea0 76 ///
WiredHome 0:6d899ce93ea0 77 /// @param l is a pointer to the string on the left
WiredHome 0:6d899ce93ea0 78 /// @param r is a pointer to the string on the right
WiredHome 0:6d899ce93ea0 79 /// @param n is the number of characters to compare
WiredHome 0:6d899ce93ea0 80 /// @returns -1 if l < r
WiredHome 0:6d899ce93ea0 81 /// @returns 0 if l == r
WiredHome 0:6d899ce93ea0 82 /// @returns +1 if l > r
WiredHome 0:6d899ce93ea0 83 ///
WiredHome 0:6d899ce93ea0 84 int sw_strnicmp(const char *l, const char *r, size_t n);
WiredHome 0:6d899ce93ea0 85
WiredHome 0:6d899ce93ea0 86 /// sw_stristr exists because not all compiler libraries have this function.
WiredHome 0:6d899ce93ea0 87 ///
WiredHome 0:6d899ce93ea0 88 /// In a case-insenstive search, try to find the needle in the haystack.
WiredHome 0:6d899ce93ea0 89 ///
WiredHome 0:6d899ce93ea0 90 /// @param haystack is a pointer to string being searched
WiredHome 0:6d899ce93ea0 91 /// @param needle is a pointer to a string to find
WiredHome 0:6d899ce93ea0 92 /// @returns a pointer to the found needle in the haystack, or NULL
WiredHome 0:6d899ce93ea0 93 ///
WiredHome 0:6d899ce93ea0 94 const char * sw_stristr(const char * haystack, const char * needle);
WiredHome 0:6d899ce93ea0 95
WiredHome 0:6d899ce93ea0 96
WiredHome 0:6d899ce93ea0 97 /// sw_stristr exists because not all compiler libraries have this function.
WiredHome 0:6d899ce93ea0 98 ///
WiredHome 0:6d899ce93ea0 99 /// In a case-insenstive search, try to find the needle in the haystack.
WiredHome 0:6d899ce93ea0 100 ///
WiredHome 0:6d899ce93ea0 101 /// @param haystack is a pointer to string being searched
WiredHome 0:6d899ce93ea0 102 /// @param needle is a pointer to a string to find
WiredHome 0:6d899ce93ea0 103 /// @returns a pointer to the found needle in the haystack, or NULL
WiredHome 0:6d899ce93ea0 104 ///
WiredHome 0:6d899ce93ea0 105 char * sw_stristr(char * haystack, const char * needle);