Debian Codegraph @

libretro-common/string/stdstring.c

raw symbols SCIP
1 /* Copyright (C) 2010-2016 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>cxx . . $ `<file>/libretro-common/include/string/stdstring.h`/
27
28 boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! string_is_emptycxx . . $ string_is_empty(c5f7da30cbc0f4e9).(const char *data)
29 {
30 return data==NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`! || *data=='\0';
31 }
32
33 boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! string_is_equalcxx . . $ string_is_equal(b7a7aa4a824b7595).(const char *a, const char *b)
34 {
35 if (!a || !b)
36 return falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!;
37 return (strcmpcxx . . $ strcmp(5ae29d726ae57f88).(a, b) == 0);
38 }
39
40 boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! string_is_equal_noncasecxx . . $ string_is_equal_noncase(b7a7aa4a824b7595).(const char *a, const char *b)
41 {
42 if (!a || !b)
43 return falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!;
44 return (strcasecmpcxx . . $ strcasecmp(5ae29d726ae57f88).(a, b) == 0);
45 }
46
47 char *string_to_uppercxx . . $ string_to_upper(7a9722d75b371431).(char *s)
48 {
49 char *cs = (char *)s;
50 for ( ; *cs != '\0'; cs++)
51 *cs = touppercxx . . $ `<external>/17904084882347303253/ctype.h:221:11`!(*cs);
52 return s;
53 }
54
55 char *string_to_lowercxx . . $ string_to_lower(7a9722d75b371431).(char *s)
56 {
57 char *cs = (char *)s;
58 for ( ; *cs != '\0'; cs++)
59 *cs = tolowercxx . . $ `<external>/17904084882347303253/ctype.h:220:11`!(*cs);
60 return s;
61 }
62
63 char *string_ucwordscxx . . $ string_ucwords(7a9722d75b371431).(char *s)
64 {
65 char *cs = (char *)s;
66 for ( ; *cs != '\0'; cs++)
67 {
68 if (*cs == ' ')
69 {
70 *(cs+1) = touppercxx . . $ `<external>/17904084882347303253/ctype.h:221:11`!(*(cs+1));
71 }
72 }
73
74 s[0] = touppercxx . . $ `<external>/17904084882347303253/ctype.h:221:11`!(s[0]);
75 return s;
76 }
77
78 char *string_replace_substringcxx . . $ string_replace_substring(11d6a3b64d462fd0).(const char *in,
79 const char *pattern, const char *replacement)
80 {
81 size_t numhits, pattern_len, replacement_len, outlen;
82 const char *inat;
83 const char *inprev;
84 char *out, *outat;
85
86 /* if either pattern or replacement is NULL,
87 * duplicate in and let caller handle it. */
88 if (!pattern || !replacement)
89 return strdupcxx . . $ strdup(a8b8e32347182212).(in);
90
91 pattern_len = strlencxx . . $ strlen(751346f8406fd082).(pattern);
92 replacement_len = strlencxx . . $ strlen(751346f8406fd082).(replacement);
93 numhits = 0;
94 inat = in;
95
96 while ((inat = strstrcxx . . $ strstr(a713fe7b9a57cf96).(inat, pattern)))
97 {
98 inat += pattern_len;
99 numhits++;
100 }
101
102 outlen = strlencxx . . $ strlen(751346f8406fd082).(in) - pattern_len*numhits + replacement_len*numhits;
103 out = (char *)malloccxx . . $ malloc(bfcfa4d6b7f7ef64).(outlen+1);
104 outat = out;
105 inat = in;
106 inprev = in;
107
108 while ((inat = strstrcxx . . $ strstr(a713fe7b9a57cf96).(inat, pattern)))
109 {
110 memcpycxx . . $ memcpy(cc817cb175f282d).(outat, inprev, inat-inprev);
111 outat += inat-inprev;
112 memcpycxx . . $ memcpy(cc817cb175f282d).(outat, replacement, replacement_len);
113 outat += replacement_len;
114 inat += pattern_len;
115 inprev = inat;
116 }
117 strcpycxx . . $ strcpy(b7b3a83273496486).(outat, inprev);
118
119 return out;
120 }
121
122 /* Non-GPL licensed versions of whitespace trimming:
123 * http://stackoverflow.com/questions/656542/trim-a-string-in-c
124 */
125
126 /* Remove leading whitespaces */
127 char *string_trim_whitespace_leftcxx . . $ string_trim_whitespace_left(7a9722d75b371431).(char *const s)
128 {
129 if(s && *s)
130 {
131 size_t len = strlencxx . . $ strlen(751346f8406fd082).(s);
132 char *cur = s;
133
134 while(*cur && isspacecxx . . $ `<external>/17904084882347303253/ctype.h:197:10`!(*cur))
135 ++cur, --len;
136
137 if(s != cur)
138 memmovecxx . . $ memmove(cc817cb175f282d).(s, cur, len + 1);
139
140 }
141
142 return s;
143 }
144
145 /* Remove trailing whitespaces */
146 char *string_trim_whitespace_rightcxx . . $ string_trim_whitespace_right(7a9722d75b371431).(char *const s)
147 {
148 if(s && *s)
149 {
150 size_t len = strlencxx . . $ strlen(751346f8406fd082).(s);
151 char *cur = s + len - 1;
152
153 while(cur != s && isspacecxx . . $ `<external>/17904084882347303253/ctype.h:197:10`!(*cur))
154 --cur, --len;
155
156 cur[isspacecxx . . $ `<external>/17904084882347303253/ctype.h:197:10`!(*cur) ? 0 : 1] = '\0';
157 }
158
159 return s;
160 }
161
162 /* Remove leading and trailing whitespaces */
163 char *string_trim_whitespacecxx . . $ string_trim_whitespace(7a9722d75b371431).(char *const s)
164 {
165 string_trim_whitespace_rightcxx . . $ string_trim_whitespace_right(7a9722d75b371431).(s); /* order matters */
166 string_trim_whitespace_leftcxx . . $ string_trim_whitespace_left(7a9722d75b371431).(s);
167
168 return s;
169 }