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