![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
e
Dependencies: NeoStrip USBDevice mbed
string_functions.h@0:5b9f87a086ce, 2015-09-22 (annotated)
- Committer:
- baraki
- Date:
- Tue Sep 22 21:49:44 2015 +0000
- Revision:
- 0:5b9f87a086ce
for use with blue mbed
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
baraki | 0:5b9f87a086ce | 1 | #ifndef INCL_STRING_FUNCTIONS |
baraki | 0:5b9f87a086ce | 2 | #define INCL_STRING_FUNCTIONS |
baraki | 0:5b9f87a086ce | 3 | |
baraki | 0:5b9f87a086ce | 4 | #include <stdio.h> |
baraki | 0:5b9f87a086ce | 5 | #include <stdlib.h> |
baraki | 0:5b9f87a086ce | 6 | |
baraki | 0:5b9f87a086ce | 7 | inline int length(const char* source); |
baraki | 0:5b9f87a086ce | 8 | inline int indexOf(const char* source, const char* target); |
baraki | 0:5b9f87a086ce | 9 | inline bool contains(const char* source, const char* target); |
baraki | 0:5b9f87a086ce | 10 | inline void substring(const char* source, int startIndex, int endIndex, char* dest); |
baraki | 0:5b9f87a086ce | 11 | inline void substring(const char* source, int startIndex, char* dest); |
baraki | 0:5b9f87a086ce | 12 | inline void substring(const char* source, int startIndex, int endIndex, char* dest, int destLength); |
baraki | 0:5b9f87a086ce | 13 | inline void trim(char* source, int sourceLength); |
baraki | 0:5b9f87a086ce | 14 | inline bool equals(const char* first, const char* second); |
baraki | 0:5b9f87a086ce | 15 | inline bool equalsIgnoreCase(const char* first, const char* second); |
baraki | 0:5b9f87a086ce | 16 | inline void strcpy(const char* source, char* dest, int destLength); |
baraki | 0:5b9f87a086ce | 17 | inline void concat(const char* first, const char* second, char* dest, int destLength); |
baraki | 0:5b9f87a086ce | 18 | inline void concatInt(const char* first, int second, char* dest, int destLength); |
baraki | 0:5b9f87a086ce | 19 | |
baraki | 0:5b9f87a086ce | 20 | |
baraki | 0:5b9f87a086ce | 21 | |
baraki | 0:5b9f87a086ce | 22 | |
baraki | 0:5b9f87a086ce | 23 | |
baraki | 0:5b9f87a086ce | 24 | //============================================================================================ |
baraki | 0:5b9f87a086ce | 25 | // Gets the length of the character array |
baraki | 0:5b9f87a086ce | 26 | //============================================================================================ |
baraki | 0:5b9f87a086ce | 27 | int length(const char* source) |
baraki | 0:5b9f87a086ce | 28 | { |
baraki | 0:5b9f87a086ce | 29 | int length = 0; |
baraki | 0:5b9f87a086ce | 30 | for(; source[length] != '\0'; length++); |
baraki | 0:5b9f87a086ce | 31 | return length; |
baraki | 0:5b9f87a086ce | 32 | } |
baraki | 0:5b9f87a086ce | 33 | |
baraki | 0:5b9f87a086ce | 34 | //============================================================================================ |
baraki | 0:5b9f87a086ce | 35 | // Gets the index of the given string, or -1 if not found |
baraki | 0:5b9f87a086ce | 36 | //============================================================================================ |
baraki | 0:5b9f87a086ce | 37 | int indexOf(const char* source, const char* target) |
baraki | 0:5b9f87a086ce | 38 | { |
baraki | 0:5b9f87a086ce | 39 | int targetLength = length(target); |
baraki | 0:5b9f87a086ce | 40 | int sourceLength = length(source); |
baraki | 0:5b9f87a086ce | 41 | int index = -1; |
baraki | 0:5b9f87a086ce | 42 | for(int i = 0; i <= sourceLength - targetLength && index == -1; i++) |
baraki | 0:5b9f87a086ce | 43 | { |
baraki | 0:5b9f87a086ce | 44 | bool foundTarget = true; |
baraki | 0:5b9f87a086ce | 45 | for(int n = 0; n < targetLength && i+n < sourceLength; n++) |
baraki | 0:5b9f87a086ce | 46 | { |
baraki | 0:5b9f87a086ce | 47 | if(source[i+n] != target[n]) |
baraki | 0:5b9f87a086ce | 48 | foundTarget = false; |
baraki | 0:5b9f87a086ce | 49 | } |
baraki | 0:5b9f87a086ce | 50 | if(foundTarget) |
baraki | 0:5b9f87a086ce | 51 | index = i; |
baraki | 0:5b9f87a086ce | 52 | } |
baraki | 0:5b9f87a086ce | 53 | return index; |
baraki | 0:5b9f87a086ce | 54 | } |
baraki | 0:5b9f87a086ce | 55 | |
baraki | 0:5b9f87a086ce | 56 | //============================================================================================ |
baraki | 0:5b9f87a086ce | 57 | // Checks if the given string is contained in the source string |
baraki | 0:5b9f87a086ce | 58 | //============================================================================================ |
baraki | 0:5b9f87a086ce | 59 | bool contains(const char* source, const char* target) |
baraki | 0:5b9f87a086ce | 60 | { |
baraki | 0:5b9f87a086ce | 61 | return indexOf(source, target) >= 0; |
baraki | 0:5b9f87a086ce | 62 | } |
baraki | 0:5b9f87a086ce | 63 | |
baraki | 0:5b9f87a086ce | 64 | //============================================================================================ |
baraki | 0:5b9f87a086ce | 65 | // Returns a substring of the character array |
baraki | 0:5b9f87a086ce | 66 | // First is inclusive, second is exclusive |
baraki | 0:5b9f87a086ce | 67 | // Returns itself if bounds are bad |
baraki | 0:5b9f87a086ce | 68 | // Assumes beginning / end if either bound is negative |
baraki | 0:5b9f87a086ce | 69 | //============================================================================================ |
baraki | 0:5b9f87a086ce | 70 | void substring(const char* source, int startIndex, int endIndex, char* dest) |
baraki | 0:5b9f87a086ce | 71 | { |
baraki | 0:5b9f87a086ce | 72 | substring(source, startIndex, endIndex, dest, 20); |
baraki | 0:5b9f87a086ce | 73 | } |
baraki | 0:5b9f87a086ce | 74 | |
baraki | 0:5b9f87a086ce | 75 | //============================================================================================ |
baraki | 0:5b9f87a086ce | 76 | // Returns a substring of the character array |
baraki | 0:5b9f87a086ce | 77 | // First is inclusive, second is exclusive |
baraki | 0:5b9f87a086ce | 78 | // Returns itself if bounds are bad |
baraki | 0:5b9f87a086ce | 79 | // Assumes beginning / end if either bound is negative |
baraki | 0:5b9f87a086ce | 80 | //============================================================================================ |
baraki | 0:5b9f87a086ce | 81 | void substring(const char* source, int startIndex, char* dest) |
baraki | 0:5b9f87a086ce | 82 | { |
baraki | 0:5b9f87a086ce | 83 | substring(source, startIndex, length(source), dest, 30); |
baraki | 0:5b9f87a086ce | 84 | } |
baraki | 0:5b9f87a086ce | 85 | |
baraki | 0:5b9f87a086ce | 86 | //============================================================================================ |
baraki | 0:5b9f87a086ce | 87 | // Returns a substring of the character array |
baraki | 0:5b9f87a086ce | 88 | // First is inclusive, second is exclusive |
baraki | 0:5b9f87a086ce | 89 | // Returns itself if bounds are bad |
baraki | 0:5b9f87a086ce | 90 | // Assumes beginning / end if either bound is negative |
baraki | 0:5b9f87a086ce | 91 | //============================================================================================ |
baraki | 0:5b9f87a086ce | 92 | void substring(const char* source, int startIndex, int endIndex, char* dest, int destLength) |
baraki | 0:5b9f87a086ce | 93 | { |
baraki | 0:5b9f87a086ce | 94 | char temp[destLength]; |
baraki | 0:5b9f87a086ce | 95 | if(startIndex < 0) |
baraki | 0:5b9f87a086ce | 96 | startIndex = 0; |
baraki | 0:5b9f87a086ce | 97 | if(endIndex < 0) |
baraki | 0:5b9f87a086ce | 98 | endIndex = 0; |
baraki | 0:5b9f87a086ce | 99 | if(endIndex < startIndex) |
baraki | 0:5b9f87a086ce | 100 | { |
baraki | 0:5b9f87a086ce | 101 | dest[0] = '\0'; |
baraki | 0:5b9f87a086ce | 102 | return; |
baraki | 0:5b9f87a086ce | 103 | } |
baraki | 0:5b9f87a086ce | 104 | if(endIndex >= length(source)) |
baraki | 0:5b9f87a086ce | 105 | endIndex = length(source); |
baraki | 0:5b9f87a086ce | 106 | if(destLength < endIndex - startIndex + 1) |
baraki | 0:5b9f87a086ce | 107 | { |
baraki | 0:5b9f87a086ce | 108 | dest[0] = '\0'; |
baraki | 0:5b9f87a086ce | 109 | return; |
baraki | 0:5b9f87a086ce | 110 | } |
baraki | 0:5b9f87a086ce | 111 | for(int i = 0; i < endIndex - startIndex; i++) |
baraki | 0:5b9f87a086ce | 112 | { |
baraki | 0:5b9f87a086ce | 113 | temp[i] = source[startIndex + i]; |
baraki | 0:5b9f87a086ce | 114 | } |
baraki | 0:5b9f87a086ce | 115 | for(int i = 0; i < endIndex - startIndex; i++) |
baraki | 0:5b9f87a086ce | 116 | dest[i] = temp[i]; |
baraki | 0:5b9f87a086ce | 117 | dest[endIndex - startIndex] = '\0'; |
baraki | 0:5b9f87a086ce | 118 | } |
baraki | 0:5b9f87a086ce | 119 | |
baraki | 0:5b9f87a086ce | 120 | //============================================================================================ |
baraki | 0:5b9f87a086ce | 121 | // Removing leading and trailing spaces or new lines |
baraki | 0:5b9f87a086ce | 122 | //============================================================================================ |
baraki | 0:5b9f87a086ce | 123 | void trim(char* source, int sourceLength) |
baraki | 0:5b9f87a086ce | 124 | { |
baraki | 0:5b9f87a086ce | 125 | char temp[sourceLength]; |
baraki | 0:5b9f87a086ce | 126 | int startIndex = 0; |
baraki | 0:5b9f87a086ce | 127 | int endIndex = length(source)-1; |
baraki | 0:5b9f87a086ce | 128 | for(; startIndex < length(source) && (source[startIndex] == ' ' || source[startIndex] == '\n' || source[startIndex] == '\t'); startIndex++); |
baraki | 0:5b9f87a086ce | 129 | for(; endIndex >= 0 && (source[endIndex] == ' ' || source[endIndex] == '\n' || source[startIndex] == '\t'); endIndex--); |
baraki | 0:5b9f87a086ce | 130 | endIndex++; |
baraki | 0:5b9f87a086ce | 131 | substring(source, startIndex, endIndex, temp, sizeof(temp)-1); |
baraki | 0:5b9f87a086ce | 132 | strcpy(temp, source, sourceLength-1); |
baraki | 0:5b9f87a086ce | 133 | } |
baraki | 0:5b9f87a086ce | 134 | |
baraki | 0:5b9f87a086ce | 135 | //============================================================================================ |
baraki | 0:5b9f87a086ce | 136 | // Tests if two character arrays are equal |
baraki | 0:5b9f87a086ce | 137 | //============================================================================================ |
baraki | 0:5b9f87a086ce | 138 | bool equals(const char* first, const char* second) |
baraki | 0:5b9f87a086ce | 139 | { |
baraki | 0:5b9f87a086ce | 140 | if(length(first) != length(second)) |
baraki | 0:5b9f87a086ce | 141 | return false; |
baraki | 0:5b9f87a086ce | 142 | for(int i = 0; i < length(first); i++) |
baraki | 0:5b9f87a086ce | 143 | { |
baraki | 0:5b9f87a086ce | 144 | if(first[i] != second[i]) |
baraki | 0:5b9f87a086ce | 145 | return false; |
baraki | 0:5b9f87a086ce | 146 | } |
baraki | 0:5b9f87a086ce | 147 | return true; |
baraki | 0:5b9f87a086ce | 148 | } |
baraki | 0:5b9f87a086ce | 149 | |
baraki | 0:5b9f87a086ce | 150 | //============================================================================================ |
baraki | 0:5b9f87a086ce | 151 | // Tests if two character arrays are equal, ignoring case |
baraki | 0:5b9f87a086ce | 152 | //============================================================================================ |
baraki | 0:5b9f87a086ce | 153 | bool equalsIgnoreCase(const char* first, const char* second) |
baraki | 0:5b9f87a086ce | 154 | { |
baraki | 0:5b9f87a086ce | 155 | if(length(first) != length(second)) |
baraki | 0:5b9f87a086ce | 156 | return false; |
baraki | 0:5b9f87a086ce | 157 | for(int i = 0; i < length(first); i++) |
baraki | 0:5b9f87a086ce | 158 | { |
baraki | 0:5b9f87a086ce | 159 | int firstChar = first[i]; |
baraki | 0:5b9f87a086ce | 160 | int secondChar = second[i]; |
baraki | 0:5b9f87a086ce | 161 | // Make them lowercase |
baraki | 0:5b9f87a086ce | 162 | if(firstChar >= 'A' && firstChar <= 'Z') |
baraki | 0:5b9f87a086ce | 163 | firstChar += 'a' - 'A'; |
baraki | 0:5b9f87a086ce | 164 | if(secondChar >= 'A' && secondChar <= 'Z') |
baraki | 0:5b9f87a086ce | 165 | secondChar += 'a' - 'A'; |
baraki | 0:5b9f87a086ce | 166 | if(firstChar != secondChar) |
baraki | 0:5b9f87a086ce | 167 | return false; |
baraki | 0:5b9f87a086ce | 168 | } |
baraki | 0:5b9f87a086ce | 169 | return true; |
baraki | 0:5b9f87a086ce | 170 | } |
baraki | 0:5b9f87a086ce | 171 | |
baraki | 0:5b9f87a086ce | 172 | //============================================================================================ |
baraki | 0:5b9f87a086ce | 173 | // Copies one array into the other |
baraki | 0:5b9f87a086ce | 174 | //============================================================================================ |
baraki | 0:5b9f87a086ce | 175 | void strcpy(const char* source, char* dest, int destLength) |
baraki | 0:5b9f87a086ce | 176 | { |
baraki | 0:5b9f87a086ce | 177 | if(destLength < length(source) + 1) |
baraki | 0:5b9f87a086ce | 178 | { |
baraki | 0:5b9f87a086ce | 179 | dest[0] = '\0'; |
baraki | 0:5b9f87a086ce | 180 | return; |
baraki | 0:5b9f87a086ce | 181 | } |
baraki | 0:5b9f87a086ce | 182 | for(int i = 0; i < length(source); i++) |
baraki | 0:5b9f87a086ce | 183 | { |
baraki | 0:5b9f87a086ce | 184 | dest[i] = source[i]; |
baraki | 0:5b9f87a086ce | 185 | } |
baraki | 0:5b9f87a086ce | 186 | dest[length(source)] = '\0'; |
baraki | 0:5b9f87a086ce | 187 | } |
baraki | 0:5b9f87a086ce | 188 | |
baraki | 0:5b9f87a086ce | 189 | //============================================================================================ |
baraki | 0:5b9f87a086ce | 190 | // Concatenates two character arrays |
baraki | 0:5b9f87a086ce | 191 | //============================================================================================ |
baraki | 0:5b9f87a086ce | 192 | void concat(const char* first, const char* second, char* dest, int destLength) |
baraki | 0:5b9f87a086ce | 193 | { |
baraki | 0:5b9f87a086ce | 194 | char temp[destLength]; |
baraki | 0:5b9f87a086ce | 195 | if(destLength < length(first) + length(second) + 1) |
baraki | 0:5b9f87a086ce | 196 | { |
baraki | 0:5b9f87a086ce | 197 | dest[0] = '\0'; |
baraki | 0:5b9f87a086ce | 198 | return; |
baraki | 0:5b9f87a086ce | 199 | } |
baraki | 0:5b9f87a086ce | 200 | for(int i = 0; i < length(first); i++) |
baraki | 0:5b9f87a086ce | 201 | temp[i] = first[i]; |
baraki | 0:5b9f87a086ce | 202 | for(int i = 0; i < length(second); i++) |
baraki | 0:5b9f87a086ce | 203 | temp[i + length(first)] = second[i]; |
baraki | 0:5b9f87a086ce | 204 | temp[length(second) + length(first)] = '\0'; |
baraki | 0:5b9f87a086ce | 205 | for(int i = 0; i < length(second) + length(first); i++) |
baraki | 0:5b9f87a086ce | 206 | dest[i] = temp[i]; |
baraki | 0:5b9f87a086ce | 207 | dest[length(second) + length(first)] = '\0'; |
baraki | 0:5b9f87a086ce | 208 | } |
baraki | 0:5b9f87a086ce | 209 | |
baraki | 0:5b9f87a086ce | 210 | //============================================================================================ |
baraki | 0:5b9f87a086ce | 211 | // Concatenates a character array with an integer |
baraki | 0:5b9f87a086ce | 212 | //============================================================================================ |
baraki | 0:5b9f87a086ce | 213 | void concatInt(const char* first, int second, char* dest, int destLength) |
baraki | 0:5b9f87a086ce | 214 | { |
baraki | 0:5b9f87a086ce | 215 | char secondChar[10]; |
baraki | 0:5b9f87a086ce | 216 | sprintf(secondChar,"%d",second); //itoa(second, secondChar, 10); |
baraki | 0:5b9f87a086ce | 217 | concat(first, secondChar, dest, destLength); |
baraki | 0:5b9f87a086ce | 218 | } |
baraki | 0:5b9f87a086ce | 219 | |
baraki | 0:5b9f87a086ce | 220 | #endif |