| 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> |
| 27 | |
| 28 | bool string_is_empty(const char *data) |
| 29 | { |
| 30 | return data==NULL || *data=='\0'; |
| 31 | } |
| 32 | |
| 33 | bool string_is_equal(const char *a, const char *b) |
| 34 | { |
| 35 | if (!a || !b) |
| 36 | return false; |
| 37 | return (strcmp(a, b) == 0); |
| 38 | } |
| 39 | |
| 40 | bool string_is_equal_noncase(const char *a, const char *b) |
| 41 | { |
| 42 | if (!a || !b) |
| 43 | return false; |
| 44 | return (strcasecmp(a, b) == 0); |
| 45 | } |
| 46 | |
| 47 | char *string_to_upper(char *s) |
| 48 | { |
| 49 | char *cs = (char *)s; |
| 50 | for ( ; *cs != '\0'; cs++) |
| 51 | *cs = toupper(*cs); |
| 52 | return s; |
| 53 | } |
| 54 | |
| 55 | char *string_to_lower(char *s) |
| 56 | { |
| 57 | char *cs = (char *)s; |
| 58 | for ( ; *cs != '\0'; cs++) |
| 59 | *cs = tolower(*cs); |
| 60 | return s; |
| 61 | } |
| 62 | |
| 63 | char *string_ucwords(char *s) |
| 64 | { |
| 65 | char *cs = (char *)s; |
| 66 | for ( ; *cs != '\0'; cs++) |
| 67 | { |
| 68 | if (*cs == ' ') |
| 69 | { |
| 70 | *(cs+1) = toupper(*(cs+1)); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | s[0] = toupper(s[0]); |
| 75 | return s; |
| 76 | } |
| 77 | |
| 78 | char *string_replace_substring(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 strdup(in); |
| 90 | |
| 91 | pattern_len = strlen(pattern); |
| 92 | replacement_len = strlen(replacement); |
| 93 | numhits = 0; |
| 94 | inat = in; |
| 95 | |
| 96 | while ((inat = strstr(inat, pattern))) |
| 97 | { |
| 98 | inat += pattern_len; |
| 99 | numhits++; |
| 100 | } |
| 101 | |
| 102 | outlen = strlen(in) - pattern_len*numhits + replacement_len*numhits; |
| 103 | out = (char *)malloc(outlen+1); |
| 104 | outat = out; |
| 105 | inat = in; |
| 106 | inprev = in; |
| 107 | |
| 108 | while ((inat = strstr(inat, pattern))) |
| 109 | { |
| 110 | memcpy(outat, inprev, inat-inprev); |
| 111 | outat += inat-inprev; |
| 112 | memcpy(outat, replacement, replacement_len); |
| 113 | outat += replacement_len; |
| 114 | inat += pattern_len; |
| 115 | inprev = inat; |
| 116 | } |
| 117 | strcpy(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_left(char *const s) |
| 128 | { |
| 129 | if(s && *s) |
| 130 | { |
| 131 | size_t len = strlen(s); |
| 132 | char *cur = s; |
| 133 | |
| 134 | while(*cur && isspace(*cur)) |
| 135 | ++cur, --len; |
| 136 | |
| 137 | if(s != cur) |
| 138 | memmove(s, cur, len + 1); |
| 139 | |
| 140 | } |
| 141 | |
| 142 | return s; |
| 143 | } |
| 144 | |
| 145 | /* Remove trailing whitespaces */ |
| 146 | char *string_trim_whitespace_right(char *const s) |
| 147 | { |
| 148 | if(s && *s) |
| 149 | { |
| 150 | size_t len = strlen(s); |
| 151 | char *cur = s + len - 1; |
| 152 | |
| 153 | while(cur != s && isspace(*cur)) |
| 154 | --cur, --len; |
| 155 | |
| 156 | cur[isspace(*cur) ? 0 : 1] = '\0'; |
| 157 | } |
| 158 | |
| 159 | return s; |
| 160 | } |
| 161 | |
| 162 | /* Remove leading and trailing whitespaces */ |
| 163 | char *string_trim_whitespace(char *const s) |
| 164 | { |
| 165 | string_trim_whitespace_right(s); /* order matters */ |
| 166 | string_trim_whitespace_left(s); |
| 167 | |
| 168 | return s; |
| 169 | } |