1 /* Copyright  (C) 2010-2018 The RetroArch team
2  *
3  * ---------------------------------------------------------------------------------------
4  * The following license statement only applies to this file (stdstring.c).
5  * ---------------------------------------------------------------------------------------
6  *
7  * Permission is hereby granted, free of charge,
8  * to any person obtaining a copy of this software and associated documentation files (the "Software"),
9  * to deal in the Software without restriction, including without limitation the rights to
10  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
11  * and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
16  * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
19  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21  */
22 
23 #include <stdint.h>
24 #include <ctype.h>
25 
26 #include <string/stdstring.h>
27 #include <encodings/utf.h>
28 
string_to_upper(char * s)29 char *string_to_upper(char *s)
30 {
31    char *cs = (char *)s;
32    for ( ; *cs != '\0'; cs++)
33       *cs = toupper((unsigned char)*cs);
34    return s;
35 }
36 
string_to_lower(char * s)37 char *string_to_lower(char *s)
38 {
39    char *cs = (char *)s;
40    for ( ; *cs != '\0'; cs++)
41       *cs = tolower((unsigned char)*cs);
42    return s;
43 }
44 
string_ucwords(char * s)45 char *string_ucwords(char *s)
46 {
47    char *cs = (char *)s;
48    for ( ; *cs != '\0'; cs++)
49    {
50       if (*cs == ' ')
51          *(cs+1) = toupper((unsigned char)*(cs+1));
52    }
53 
54    s[0] = toupper((unsigned char)s[0]);
55    return s;
56 }
57 
string_replace_substring(const char * in,const char * pattern,const char * replacement)58 char *string_replace_substring(const char *in,
59       const char *pattern, const char *replacement)
60 {
61    size_t numhits, pattern_len, replacement_len, outlen;
62    const char *inat   = NULL;
63    const char *inprev = NULL;
64    char          *out = NULL;
65    char        *outat = NULL;
66 
67    /* if either pattern or replacement is NULL,
68     * duplicate in and let caller handle it. */
69    if (!pattern || !replacement)
70       return strdup(in);
71 
72    pattern_len     = strlen(pattern);
73    replacement_len = strlen(replacement);
74    numhits         = 0;
75    inat            = in;
76 
77    while ((inat = strstr(inat, pattern)))
78    {
79       inat += pattern_len;
80       numhits++;
81    }
82 
83    outlen          = strlen(in) - pattern_len*numhits + replacement_len*numhits;
84    out             = (char *)malloc(outlen+1);
85 
86    if (!out)
87       return NULL;
88 
89    outat           = out;
90    inat            = in;
91    inprev          = in;
92 
93    while ((inat = strstr(inat, pattern)))
94    {
95       memcpy(outat, inprev, inat-inprev);
96       outat += inat-inprev;
97       memcpy(outat, replacement, replacement_len);
98       outat += replacement_len;
99       inat += pattern_len;
100       inprev = inat;
101    }
102    strcpy(outat, inprev);
103 
104    return out;
105 }
106 
107 /* Remove leading whitespaces */
string_trim_whitespace_left(char * const s)108 char *string_trim_whitespace_left(char *const s)
109 {
110    if(s && *s)
111    {
112       size_t len     = strlen(s);
113       char *current  = s;
114 
115       while(*current && isspace((unsigned char)*current))
116       {
117          ++current;
118          --len;
119       }
120 
121       if(s != current)
122          memmove(s, current, len + 1);
123    }
124 
125    return s;
126 }
127 
128 /* Remove trailing whitespaces */
string_trim_whitespace_right(char * const s)129 char *string_trim_whitespace_right(char *const s)
130 {
131    if(s && *s)
132    {
133       size_t len     = strlen(s);
134       char  *current = s + len - 1;
135 
136       while(current != s && isspace((unsigned char)*current))
137       {
138          --current;
139          --len;
140       }
141 
142       current[isspace((unsigned char)*current) ? 0 : 1] = '\0';
143    }
144 
145    return s;
146 }
147 
148 /* Remove leading and trailing whitespaces */
string_trim_whitespace(char * const s)149 char *string_trim_whitespace(char *const s)
150 {
151    string_trim_whitespace_right(s);  /* order matters */
152    string_trim_whitespace_left(s);
153 
154    return s;
155 }
156 
word_wrap(char * buffer,const char * string,int line_width,bool unicode,unsigned max_lines)157 char *word_wrap(char* buffer, const char *string, int line_width, bool unicode, unsigned max_lines)
158 {
159    unsigned i     = 0;
160    unsigned len   = (unsigned)strlen(string);
161    unsigned lines = 1;
162 
163    while (i < len)
164    {
165       unsigned counter;
166       int pos = (int)(&buffer[i] - buffer);
167 
168       /* copy string until the end of the line is reached */
169       for (counter = 1; counter <= (unsigned)line_width; counter++)
170       {
171          const char *character;
172          unsigned char_len;
173          unsigned j = i;
174 
175          /* check if end of string reached */
176          if (i == len)
177          {
178             buffer[i] = 0;
179             return buffer;
180          }
181 
182          character = utf8skip(&string[i], 1);
183          char_len  = (unsigned)(character - &string[i]);
184 
185          if (!unicode)
186             counter += char_len - 1;
187 
188          do
189          {
190             buffer[i] = string[i];
191             char_len--;
192             i++;
193          } while(char_len);
194 
195          /* check for newlines embedded in the original input
196           * and reset the index */
197          if (buffer[j] == '\n')
198          {
199             lines++;
200             counter = 1;
201          }
202       }
203 
204       /* check for whitespace */
205       if (string[i] == ' ')
206       {
207          if ((max_lines == 0 || lines < max_lines))
208          {
209             buffer[i] = '\n';
210             i++;
211             lines++;
212          }
213       }
214       else
215       {
216          int k;
217 
218          /* check for nearest whitespace back in string */
219          for (k = i; k > 0; k--)
220          {
221             if (string[k] != ' ' || (max_lines != 0 && lines >= max_lines))
222                continue;
223 
224             buffer[k] = '\n';
225             /* set string index back to character after this one */
226             i         = k + 1;
227             lines++;
228             break;
229          }
230 
231          if (&buffer[i] - buffer == pos)
232             return buffer;
233       }
234    }
235 
236    buffer[i] = 0;
237 
238    return buffer;
239 }
240 
241 /* Splits string into tokens seperated by 'delim'
242  * > Returned token string must be free()'d
243  * > Returns NULL if token is not found
244  * > After each call, 'str' is set to the position after the
245  *   last found token
246  * > Tokens *include* empty strings
247  * Usage example:
248  *    char *str      = "1,2,3,4,5,6,7,,,10,";
249  *    char **str_ptr = &str;
250  *    char *token    = NULL;
251  *    while((token = string_tokenize(str_ptr, ",")))
252  *    {
253  *        printf("%s\n", token);
254  *        free(token);
255  *        token = NULL;
256  *    }
257  */
string_tokenize(char ** str,const char * delim)258 char* string_tokenize(char **str, const char *delim)
259 {
260    /* Taken from https://codereview.stackexchange.com/questions/216956/strtok-function-thread-safe-supports-empty-tokens-doesnt-change-string# */
261    char *str_ptr    = NULL;
262    char *delim_ptr  = NULL;
263    char *token      = NULL;
264    size_t token_len = 0;
265 
266    /* Sanity checks */
267    if (!str || string_is_empty(delim))
268       return NULL;
269 
270    str_ptr = *str;
271 
272    /* Note: we don't check string_is_empty() here,
273     * empty strings are valid */
274    if (!str_ptr)
275       return NULL;
276 
277    /* Search for delimiter */
278    delim_ptr = strstr(str_ptr, delim);
279 
280    if (delim_ptr)
281       token_len = delim_ptr - str_ptr;
282    else
283       token_len = strlen(str_ptr);
284 
285    /* Allocate token string */
286    token = (char *)malloc((token_len + 1) * sizeof(char));
287 
288    if (!token)
289       return NULL;
290 
291    /* Copy token */
292    strlcpy(token, str_ptr, (token_len + 1) * sizeof(char));
293    token[token_len] = '\0';
294 
295    /* Update input string pointer */
296    *str = delim_ptr ? delim_ptr + strlen(delim) : NULL;
297 
298    return token;
299 }
300 
301 /* Removes every instance of character 'c' from 'str' */
string_remove_all_chars(char * str,char c)302 void string_remove_all_chars(char *str, char c)
303 {
304    char *read_ptr  = NULL;
305    char *write_ptr = NULL;
306 
307    if (string_is_empty(str))
308       return;
309 
310    read_ptr  = str;
311    write_ptr = str;
312 
313    while (*read_ptr != '\0')
314    {
315       *write_ptr = *read_ptr++;
316       write_ptr += (*write_ptr != c) ? 1 : 0;
317    }
318 
319    *write_ptr = '\0';
320 }
321 
322 /* Replaces every instance of character 'find' in 'str'
323  * with character 'replace' */
string_replace_all_chars(char * str,char find,char replace)324 void string_replace_all_chars(char *str, char find, char replace)
325 {
326    char *str_ptr = str;
327 
328    if (string_is_empty(str))
329       return;
330 
331    while((str_ptr = strchr(str_ptr, find)) != NULL)
332       *str_ptr++ = replace;
333 }
334 
335 /* Converts string to unsigned integer.
336  * Returns 0 if string is invalid  */
string_to_unsigned(const char * str)337 unsigned string_to_unsigned(const char *str)
338 {
339    const char *ptr = NULL;
340 
341    if (string_is_empty(str))
342       return 0;
343 
344    for (ptr = str; *ptr != '\0'; ptr++)
345    {
346       if (!isdigit(*ptr))
347          return 0;
348    }
349 
350    return (unsigned)strtoul(str, NULL, 10);
351 }
352 
353 /* Converts hexadecimal string to unsigned integer.
354  * Handles optional leading '0x'.
355  * Returns 0 if string is invalid  */
string_hex_to_unsigned(const char * str)356 unsigned string_hex_to_unsigned(const char *str)
357 {
358    const char *hex_str = str;
359    const char *ptr     = NULL;
360    size_t len;
361 
362    if (string_is_empty(str))
363       return 0;
364 
365    /* Remove leading '0x', if required */
366    len = strlen(str);
367 
368    if (len >= 2)
369       if ((str[0] == '0') &&
370           ((str[1] == 'x') || (str[1] == 'X')))
371          hex_str = str + 2;
372 
373    if (string_is_empty(hex_str))
374       return 0;
375 
376    /* Check for valid characters */
377    for (ptr = hex_str; *ptr != '\0'; ptr++)
378    {
379       if (!isxdigit(*ptr))
380          return 0;
381    }
382 
383    return (unsigned)strtoul(hex_str, NULL, 16);
384 }
385