liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 1 | #include <stdio.h> |
| 2 | #include <string.h> |
| 3 | #include <ctype.h> |
| 4 | |
| 5 | #include <mbtk_str.h> |
| 6 | |
| 7 | /* |
| 8 | * Converts all of the characters in this String to lower. |
| 9 | * |
| 10 | * Parameters: |
| 11 | * src - The string should be converted. |
| 12 | * dest - The lowercase string. |
| 13 | * len - The length of result string.Must be "strlen(src) + 1" |
| 14 | * Returns: |
| 15 | * The string, converted to lowercase,or NULL for fail. |
| 16 | */ |
| 17 | void* |
| 18 | str_tolower |
| 19 | ( |
| 20 | const void *src, |
| 21 | void *dest, |
| 22 | size_t len |
| 23 | ) |
| 24 | { |
| 25 | const char *s = (char*)src; |
| 26 | char *d = (char*)dest; |
| 27 | if(!s |
| 28 | || !d |
| 29 | || (strlen(d) + 1 > len)){ |
| 30 | return NULL; |
| 31 | } |
| 32 | |
| 33 | char* temp = d; |
| 34 | while(*s){ |
| 35 | *temp++ = tolower(*s++); |
| 36 | } |
| 37 | *temp = '\0'; |
| 38 | |
| 39 | return dest; |
| 40 | } |
| 41 | |
| 42 | /* |
| 43 | * Converts all of the characters in this String to upper case. |
| 44 | * |
| 45 | * Parameters: |
| 46 | * src - The string should be converted. |
| 47 | * dest - The uppercase string. |
| 48 | * len - The length of result string.Must be "strlen(str_ptr) + 1" |
| 49 | * Returns: |
| 50 | * The string, converted to uppercase.or NULL for fail. |
| 51 | */ |
| 52 | void* |
| 53 | str_toupper |
| 54 | ( |
| 55 | const void *src, |
| 56 | void *dest, |
| 57 | size_t len |
| 58 | ) |
| 59 | { |
| 60 | const char *s = (char*)src; |
| 61 | char *d = (char*)dest; |
| 62 | if(!s |
| 63 | || !d |
| 64 | || (strlen(d) + 1 > len)){ |
| 65 | return NULL; |
| 66 | } |
| 67 | char* temp = d; |
| 68 | while(*s){ |
| 69 | *temp++ = toupper(*s++); |
| 70 | } |
| 71 | *temp = '\0'; |
| 72 | return dest; |
| 73 | } |
| 74 | |
| 75 | /* |
| 76 | * Remove the head and tail spaces. |
| 77 | */ |
| 78 | void* |
| 79 | str_trim |
| 80 | ( |
| 81 | const void* str, |
| 82 | void *result, |
| 83 | size_t len |
| 84 | ) |
| 85 | { |
| 86 | if(str == NULL || result == NULL |
| 87 | || len <= strlen((char*)str)) |
| 88 | { |
| 89 | return NULL; |
| 90 | } |
| 91 | |
| 92 | char* str_ptr = (char*)str; |
| 93 | while(*str_ptr && (*str_ptr == ' ' || *str_ptr == '\t' || *str_ptr == '\n' || *str_ptr == '\r')){ |
| 94 | str_ptr++; |
| 95 | } |
| 96 | |
| 97 | memset(result,0x0,len); |
| 98 | if(*str_ptr && strlen(str_ptr) > 0) |
| 99 | { |
| 100 | memset(result,0x0,len); |
| 101 | memcpy(result,str_ptr,strlen(str_ptr)); |
| 102 | str_ptr = (char*)result + strlen((char*)result) - 1; |
| 103 | while(*str_ptr && (*str_ptr == ' ' || *str_ptr == '\t' || *str_ptr == '\n' || *str_ptr == '\r')){ |
| 104 | *str_ptr = '\0'; |
| 105 | str_ptr--; |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | return result; |
| 110 | } |
| 111 | |
| 112 | /* |
| 113 | * Returns true if and only if this string contains the specified sequence of char values. |
| 114 | * |
| 115 | * Parameters: |
| 116 | * str - The substring to search from. |
| 117 | * sub_str - The substring to search for. |
| 118 | * Returns: |
| 119 | * True if str contains sub_str, false otherwise. |
| 120 | */ |
| 121 | bool |
| 122 | str_contains |
| 123 | ( |
| 124 | const void* str, |
| 125 | const void* sub_str |
| 126 | ) |
| 127 | { |
| 128 | const char *s = (char*)str; |
| 129 | const char *s_sub = (char*)sub_str; |
| 130 | |
| 131 | if(!s || !s_sub){ |
| 132 | return FALSE; |
| 133 | } |
| 134 | |
| 135 | return str_indexof(s,s_sub)==-1?FALSE:TRUE; |
| 136 | } |
| 137 | |
| 138 | /* |
| 139 | * Returns the index within this string of the first occurrence of the specified substring. |
| 140 | * If no such substring, then -1 is returned. |
| 141 | * |
| 142 | * Parameters: |
| 143 | * str - The substring to search from. |
| 144 | * sub_str - The substring to search for. |
| 145 | * Returns: |
| 146 | * The index of the first occurrence of the specified substring, |
| 147 | * or -1 if there is no such occurrence. |
| 148 | */ |
| 149 | ssize_t |
| 150 | str_indexof |
| 151 | ( |
| 152 | const void* str, |
| 153 | const void* sub_str |
| 154 | ) |
| 155 | { |
| 156 | const char *s = (char*)str; |
| 157 | const char *s_sub = (char*)sub_str; |
| 158 | |
| 159 | if(!s || !s_sub){ |
| 160 | return -1; |
| 161 | } |
| 162 | char* position = strstr(s,s_sub); |
| 163 | if(!position){ |
| 164 | return -1; |
| 165 | }else{ |
| 166 | return position - s; |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | /* |
| 171 | * Returns a new string that is a substring of this string. The substring begins |
| 172 | * at the specified beginIndex and extends to the character at index endIndex - 1. |
| 173 | * Thus the length of the substring is endIndex-beginIndex. |
| 174 | * |
| 175 | * Examples: |
| 176 | * "hamburger".substring(4, 8) returns "urge" |
| 177 | * "smiles".substring(1, 5) returns "mile" |
| 178 | * |
| 179 | * Parameters: |
| 180 | * begin_index The beginning index, inclusive. |
| 181 | * end_index The ending index, exclusive. |
| 182 | * Returns: |
| 183 | * The specified substring or NULL. |
| 184 | */ |
| 185 | void* |
| 186 | str_substring |
| 187 | ( |
| 188 | const void* str, |
| 189 | size_t begin_index, |
| 190 | size_t end_index, |
| 191 | void *sub_str, |
| 192 | size_t len |
| 193 | ) |
| 194 | { |
| 195 | const char* s = (char*)str; |
| 196 | char *result = (char*)sub_str; |
| 197 | if(!s |
| 198 | || !result |
| 199 | || begin_index >= end_index |
| 200 | || begin_index >= strlen(s) |
| 201 | || end_index - begin_index + 1 > len){ |
| 202 | return NULL; |
| 203 | } |
| 204 | |
| 205 | if(end_index > strlen(s)){ |
| 206 | end_index = strlen(s); |
| 207 | } |
| 208 | |
| 209 | memcpy(result,s + begin_index,end_index - begin_index); |
| 210 | result[end_index - begin_index] = '\0'; |
| 211 | |
| 212 | return result; |
| 213 | } |
| 214 | |
| 215 | bool str_startwith(const char* str, const void* prefix) |
| 216 | { |
| 217 | if (!str || !(char*) prefix) { |
| 218 | return FALSE; |
| 219 | } |
| 220 | if (strlen((char*) str) < strlen((char*) prefix)) { |
| 221 | return FALSE; |
| 222 | } |
| 223 | return str_indexof(str, (char*) prefix) ? FALSE : TRUE; |
| 224 | } |
| 225 | |
| 226 | void* strstr_hex(char *haystack,int haystack_len, |
| 227 | const char *needle,int needle_len) |
| 228 | { |
| 229 | int index = 0; |
| 230 | if(haystack_len < needle_len) |
| 231 | return NULL; |
| 232 | |
| 233 | while(index <= haystack_len - needle_len) { |
| 234 | if(!memcmp(haystack + index,needle,needle_len)){ |
| 235 | return haystack + index; |
| 236 | } |
| 237 | index++; |
| 238 | } |
| 239 | |
| 240 | return NULL; |
| 241 | } |
| 242 | |
| 243 | bool str_empty(const void *str) |
| 244 | { |
| 245 | if (str && strlen((char*)str) > 0) |
| 246 | return false; |
| 247 | |
| 248 | return true; |
| 249 | } |
| 250 | |