| 1 | /* |
| 2 | Authors: |
| 3 | Pavel Březina <[email protected]> |
| 4 | |
| 5 | Copyright (C) 2018 Red Hat |
| 6 | |
| 7 | This program is free software; you can redistribute it and/or modify |
| 8 | it under the terms of the GNU General Public License as published by |
| 9 | the Free Software Foundation; either version 3 of the License, or |
| 10 | (at your option) any later version. |
| 11 | |
| 12 | This program is distributed in the hope that it will be useful, |
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | GNU General Public License for more details. |
| 16 | |
| 17 | You should have received a copy of the GNU General Public License |
| 18 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | */ |
| 20 | |
| 21 | #include <ctype.h> |
| 22 | #include <string.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <errno.h> |
| 25 | #include <regex.h> |
| 26 | |
| 27 | #include "common/common.h"cxx . . $ `<file>/src/common/common.h`/ |
| 28 | #include "lib/util/string.h"cxx . . $ `<file>/src/lib/util/string.h`/ |
| 29 | #include "lib/util/string_array.h"cxx . . $ `<file>/src/lib/util/string_array.h`/ |
| 30 | |
| 31 | boolcxx . . $ `<external>/13015445228558014418/stdbool.h:36:9`! |
| 32 | string_is_emptycxx . . $ string_is_empty(c5f7da30cbc0f4e9).(const char *str) |
| 33 | { |
| 34 | return str == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`! || str[0] == '\0'; |
| 35 | } |
| 36 | |
| 37 | const char * |
| 38 | string_trim_left_constcxx . . $ string_trim_left_const(fb58d2e09a2a1ce6).(const char *str) |
| 39 | { |
| 40 | if (str == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) { |
| 41 | return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; |
| 42 | } |
| 43 | |
| 44 | while (isspacecxx . . $ `<external>/17904084882347303253/ctype.h:197:10`!(*str)) { |
| 45 | str++; |
| 46 | } |
| 47 | |
| 48 | return str; |
| 49 | } |
| 50 | |
| 51 | char * |
| 52 | string_trim_leftcxx . . $ string_trim_left(a8b8e32347182212).(const char *str) |
| 53 | { |
| 54 | str = string_trim_left_constcxx . . $ string_trim_left_const(fb58d2e09a2a1ce6).(str); |
| 55 | if (str == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) { |
| 56 | return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; |
| 57 | } |
| 58 | |
| 59 | return strdupcxx . . $ strdup(a8b8e32347182212).(str); |
| 60 | } |
| 61 | |
| 62 | char * |
| 63 | string_trim_rightcxx . . $ string_trim_right(a8b8e32347182212).(const char *str) |
| 64 | { |
| 65 | const char *end; |
| 66 | |
| 67 | if (str == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) { |
| 68 | return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; |
| 69 | } |
| 70 | |
| 71 | end = str + strlencxx . . $ strlen(751346f8406fd082).(str) - 1; |
| 72 | while (end > str && isspacecxx . . $ `<external>/17904084882347303253/ctype.h:197:10`!(*end)) { |
| 73 | end--; |
| 74 | } |
| 75 | |
| 76 | return strndupcxx . . $ strndup(eabdfdef77619b3d).(str, end - str + 1); |
| 77 | } |
| 78 | |
| 79 | char * |
| 80 | string_trimcxx . . $ string_trim(a8b8e32347182212).(const char *str) |
| 81 | { |
| 82 | return string_trim_rightcxx . . $ string_trim_right(a8b8e32347182212).(string_trim_left_constcxx . . $ string_trim_left_const(fb58d2e09a2a1ce6).(str)); |
| 83 | } |
| 84 | |
| 85 | char * |
| 86 | string_trim_noemptycxx . . $ string_trim_noempty(a8b8e32347182212).(const char *str) |
| 87 | { |
| 88 | char *trimmed = string_trimcxx . . $ string_trim(a8b8e32347182212).(str); |
| 89 | |
| 90 | if (trimmed != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`! && trimmed[0] == '\0') { |
| 91 | freecxx . . $ free(5d22bdacc48458e8).(trimmed); |
| 92 | return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; |
| 93 | } |
| 94 | |
| 95 | return trimmed; |
| 96 | } |
| 97 | |
| 98 | static errno_tcxx . . $ errno_t# |
| 99 | string_explode_get_tokencxx . . $ string_explode_get_token(5b23ae25b0d77d72).(const char *str, |
| 100 | size_t len, |
| 101 | unsigned int flags, |
| 102 | char **_token) |
| 103 | { |
| 104 | char *token; |
| 105 | char *tmp; |
| 106 | |
| 107 | token = strndupcxx . . $ strndup(eabdfdef77619b3d).(str, len); |
| 108 | if (token == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) { |
| 109 | return ENOMEMcxx . . $ `<external>/8987895939074593727/errno-base.h:16:9`!; |
| 110 | } |
| 111 | |
| 112 | if (flags & STRING_EXPLODE_TRIM_LEFTcxx . . $ `src/lib/util/string.h:29:9`! && flags & STRING_EXPLODE_TRIM_RIGHTcxx . . $ `src/lib/util/string.h:30:9`!) { |
| 113 | tmp = string_trimcxx . . $ string_trim(a8b8e32347182212).(token); |
| 114 | freecxx . . $ free(5d22bdacc48458e8).(token); |
| 115 | if (tmp == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) { |
| 116 | return ENOMEMcxx . . $ `<external>/8987895939074593727/errno-base.h:16:9`!; |
| 117 | } |
| 118 | |
| 119 | token = tmp; |
| 120 | } else if (flags & STRING_EXPLODE_TRIM_LEFTcxx . . $ `src/lib/util/string.h:29:9`!) { |
| 121 | tmp = string_trim_leftcxx . . $ string_trim_left(a8b8e32347182212).(token); |
| 122 | freecxx . . $ free(5d22bdacc48458e8).(token); |
| 123 | if (tmp == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) { |
| 124 | return ENOMEMcxx . . $ `<external>/8987895939074593727/errno-base.h:16:9`!; |
| 125 | } |
| 126 | |
| 127 | token = tmp; |
| 128 | } else if (flags & STRING_EXPLODE_TRIM_RIGHTcxx . . $ `src/lib/util/string.h:30:9`!) { |
| 129 | tmp = string_trim_rightcxx . . $ string_trim_right(a8b8e32347182212).(token); |
| 130 | freecxx . . $ free(5d22bdacc48458e8).(token); |
| 131 | if (tmp == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) { |
| 132 | return ENOMEMcxx . . $ `<external>/8987895939074593727/errno-base.h:16:9`!; |
| 133 | } |
| 134 | |
| 135 | token = tmp; |
| 136 | } |
| 137 | |
| 138 | if (flags & STRING_EXPLODE_SKIP_EMPTYcxx . . $ `src/lib/util/string.h:31:9`! && string_is_emptycxx . . $ string_is_empty(c5f7da30cbc0f4e9).(token)) { |
| 139 | *_token = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; |
| 140 | freecxx . . $ free(5d22bdacc48458e8).(token); |
| 141 | return EOKcxx . . $ `src/common/errno_t.h:34:9`!; |
| 142 | } |
| 143 | |
| 144 | if (flags & STRING_EXPLODE_SKIP_COMMENTcxx . . $ `src/lib/util/string.h:32:9`! && token[0] == '#') { |
| 145 | *_token = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; |
| 146 | freecxx . . $ free(5d22bdacc48458e8).(token); |
| 147 | return EOKcxx . . $ `src/common/errno_t.h:34:9`!; |
| 148 | } |
| 149 | |
| 150 | *_token = token; |
| 151 | |
| 152 | return EOKcxx . . $ `src/common/errno_t.h:34:9`!; |
| 153 | } |
| 154 | |
| 155 | static char ** |
| 156 | string_explode_add_valuecxx . . $ string_explode_add_value(c179ef6cf17dcda2).(char **array, |
| 157 | const char *value, |
| 158 | size_t len, |
| 159 | unsigned int flags) |
| 160 | { |
| 161 | char *token; |
| 162 | errno_tcxx . . $ errno_t# ret; |
| 163 | |
| 164 | ret = string_explode_get_tokencxx . . $ string_explode_get_token(5b23ae25b0d77d72).(value, len, flags, &token); |
| 165 | if (ret != EOKcxx . . $ `src/common/errno_t.h:34:9`!) { |
| 166 | string_array_freecxx . . $ string_array_free(3ed91490e421178f).(array); |
| 167 | return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; |
| 168 | } |
| 169 | |
| 170 | if (token == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) { |
| 171 | return array; |
| 172 | } |
| 173 | |
| 174 | array = string_array_add_valuecxx . . $ string_array_add_value(db56f6d53b8c0a98).(array, token, falsecxx . . $ `<external>/13015445228558014418/stdbool.h:38:9`!); |
| 175 | freecxx . . $ free(5d22bdacc48458e8).(token); |
| 176 | |
| 177 | return array; |
| 178 | } |
| 179 | |
| 180 | char ** |
| 181 | string_explodecxx . . $ string_explode(4a6f5072951f8444).(const char *str, char delimiter, unsigned int flags) |
| 182 | { |
| 183 | const char *remainder; |
| 184 | const char *pos; |
| 185 | char **array; |
| 186 | size_t len; |
| 187 | |
| 188 | array = string_array_createcxx . . $ string_array_create(4d3fbc76ec7ad0ba).(1); |
| 189 | if (array == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) { |
| 190 | return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; |
| 191 | } |
| 192 | |
| 193 | remainder = str; |
| 194 | while ((pos = strchrcxx . . $ strchr(2050848944e00d54).(remainder, delimiter)) != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) { |
| 195 | len = pos - remainder; |
| 196 | array = string_explode_add_valuecxx . . $ string_explode_add_value(c179ef6cf17dcda2).(array, remainder, len, flags); |
| 197 | if (array == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) { |
| 198 | return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; |
| 199 | } |
| 200 | |
| 201 | remainder = pos + 1; |
| 202 | } |
| 203 | |
| 204 | if (string_is_emptycxx . . $ string_is_empty(c5f7da30cbc0f4e9).(remainder)) { |
| 205 | /* Add empty line if string end with delimiter. */ |
| 206 | if (remainder != str && *(remainder - 1) == delimiter |
| 207 | && !(flags & STRING_EXPLODE_SKIP_EMPTYcxx . . $ `src/lib/util/string.h:31:9`!)) { |
| 208 | return string_array_add_valuecxx . . $ string_array_add_value(db56f6d53b8c0a98).(array, "", falsecxx . . $ `<external>/13015445228558014418/stdbool.h:38:9`!); |
| 209 | } |
| 210 | |
| 211 | return array; |
| 212 | } |
| 213 | |
| 214 | return string_explode_add_valuecxx . . $ string_explode_add_value(c179ef6cf17dcda2).(array, remainder, strlencxx . . $ strlen(751346f8406fd082).(remainder), flags); |
| 215 | } |
| 216 | |
| 217 | char * |
| 218 | string_implodecxx . . $ string_implode(427dff5c35d3d96b).(const char **array, char delimiter) |
| 219 | { |
| 220 | const char delimiter_str[] = {delimiter, '\0'}; |
| 221 | size_t len = 0; |
| 222 | char *tmp; |
| 223 | char *str; |
| 224 | int i; |
| 225 | |
| 226 | for (i = 0; array[i] != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; i++) { |
| 227 | len += strlencxx . . $ strlen(751346f8406fd082).(array[i]) + 1; |
| 228 | } |
| 229 | |
| 230 | if (len == 0) { |
| 231 | return strdupcxx . . $ strdup(a8b8e32347182212).(""); |
| 232 | } |
| 233 | |
| 234 | str = malloc_zero_arraycxx . . $ `src/common/common.h:41:9`!(char, len + 1); |
| 235 | if (str == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) { |
| 236 | return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; |
| 237 | } |
| 238 | |
| 239 | tmp = str; |
| 240 | for (i = 0; array[i + 1] != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; i++) { |
| 241 | strcatcxx . . $ strcat(b7b3a83273496486).(tmp, array[i]); |
| 242 | strcatcxx . . $ strcat(b7b3a83273496486).(tmp, delimiter_str); |
| 243 | tmp += strlencxx . . $ strlen(751346f8406fd082).(array[i]) + 1; |
| 244 | } |
| 245 | |
| 246 | strcatcxx . . $ strcat(b7b3a83273496486).(tmp, array[i]); |
| 247 | |
| 248 | return str; |
| 249 | } |
| 250 | |
| 251 | void |
| 252 | string_replace_positioncxx . . $ string_replace_position(d10b6c01f1aebf31).(char *str, size_t start, size_t end, const char *with) |
| 253 | { |
| 254 | size_t len; |
| 255 | size_t i; |
| 256 | |
| 257 | len = strlencxx . . $ strlen(751346f8406fd082).(with); |
| 258 | |
| 259 | if (len > end - start) { |
| 260 | return; |
| 261 | } |
| 262 | |
| 263 | memcpycxx . . $ memcpy(cc817cb175f282d).(str + start, with, len); |
| 264 | |
| 265 | for (i = start + len; i < end; i++) { |
| 266 | str[i] = '\0'; |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | void |
| 271 | string_remove_linecxx . . $ string_remove_line(dc09b413e6ab05e).(char *beginning, char *str, size_t inner_position) |
| 272 | { |
| 273 | char *left; |
| 274 | |
| 275 | /* str may not be the beginning of the line so we need to refer |
| 276 | * to iterate until we reach the beginning */ |
| 277 | for (left = str + inner_position; left != beginning; left--) { |
| 278 | if (*(left - 1) == '\n') { |
| 279 | break; |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | /* Remove the whole line that is in front of our string and then iterate |
| 284 | * to the line end or string end. */ |
| 285 | for (; left < str || *left != '\0'; left++) { |
| 286 | if (*left == '\n') { |
| 287 | *left = '\0'; |
| 288 | break; |
| 289 | } |
| 290 | |
| 291 | *left = '\0'; |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | void |
| 296 | string_remove_rangecxx . . $ string_remove_range(223f2ae9eb7061ff).(char *str, size_t from, size_t to) |
| 297 | { |
| 298 | char *pos; |
| 299 | |
| 300 | for (pos = str + from; *pos != '\0' && pos - str != to; pos++) { |
| 301 | *pos = '\0'; |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | void |
| 306 | string_remove_remaindercxx . . $ string_remove_remainder(a14cc78460091691).(char *str, size_t from) |
| 307 | { |
| 308 | char *pos; |
| 309 | |
| 310 | for (pos = str + from; *pos != '\0'; pos++) { |
| 311 | *pos = '\0'; |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | void |
| 316 | string_replace_shakecxx . . $ string_replace_shake(a14cc78460091691).(char *str, size_t original_length) |
| 317 | { |
| 318 | size_t pos; |
| 319 | size_t i; |
| 320 | |
| 321 | for (i = 0, pos = 0; i < original_length; i++) { |
| 322 | if (str[i] != '\0') { |
| 323 | str[pos] = str[i]; |
| 324 | pos++; |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | for (; pos < original_length; pos++) { |
| 329 | str[pos] = '\0'; |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | static int |
| 334 | min3cxx . . $ min3(64d4c609941711dd).(unsigned int a, unsigned int b, unsigned int c) |
| 335 | { |
| 336 | if (a < b && a < c) { |
| 337 | return a; |
| 338 | } else if (b < a && b < c) { |
| 339 | return b; |
| 340 | } |
| 341 | |
| 342 | return c; |
| 343 | } |
| 344 | |
| 345 | int |
| 346 | string_levenshteincxx . . $ string_levenshtein(5ae29d726ae57f88).(const char *a, const char *b) |
| 347 | { |
| 348 | unsigned int len_a = strlencxx . . $ strlen(751346f8406fd082).(a); |
| 349 | unsigned int len_b = strlencxx . . $ strlen(751346f8406fd082).(b); |
| 350 | unsigned int x; |
| 351 | unsigned int y; |
| 352 | unsigned int last_diag; |
| 353 | unsigned int old_diag; |
| 354 | unsigned int column[len_a + 1]; |
| 355 | |
| 356 | memsetcxx . . $ memset(d1de1e4fb215f1c6).(column, 0, (len_a + 1) * sizeof(unsigned int)); |
| 357 | |
| 358 | for (y = 1; y <= len_a; y++) { |
| 359 | column[y] = y; |
| 360 | } |
| 361 | |
| 362 | for (x = 1; x <= len_b; x++) { |
| 363 | column[0] = x; |
| 364 | for (y = 1, last_diag = x - 1; y <= len_a; y++) { |
| 365 | old_diag = column[y]; |
| 366 | column[y] = min3cxx . . $ min3(64d4c609941711dd).(column[y] + 1, column[y - 1] + 1, |
| 367 | last_diag + (a[y - 1] == b[x - 1] ? 0 : 1)); |
| 368 | last_diag = old_diag; |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | return column[len_a]; |
| 373 | } |