| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
#include <ctype.h> |
| 5 |
#include <stdio.h> |
| 6 |
#include <string.h> |
| 7 |
|
| 8 |
#include "stringhelper.h" |
| 9 |
|
| 10 |
|
| 11 |
int get_num_from_string(const char *s) { |
| 12 |
int num; |
| 13 |
|
| 14 |
while (*s) { |
| 15 |
if (isdigit(*s)) |
| 16 |
break; |
| 17 |
s++; |
| 18 |
} |
| 19 |
|
| 20 |
if (!*s) |
| 21 |
return -1; |
| 22 |
|
| 23 |
if (!sscanf(s, "%d", &num)) |
| 24 |
return 0; |
| 25 |
|
| 26 |
return num; |
| 27 |
} |
| 28 |
|
| 29 |
|
| 30 |
void get_two_num_from_string(const char *s, int *a, int *b) { |
| 31 |
*a = -1; |
| 32 |
*b = -1; |
| 33 |
|
| 34 |
while (*s) { |
| 35 |
if (isdigit(*s)) |
| 36 |
break; |
| 37 |
s++; |
| 38 |
} |
| 39 |
|
| 40 |
if (!*s) |
| 41 |
return; |
| 42 |
|
| 43 |
if (!sscanf(s, "%d", a)) |
| 44 |
return; |
| 45 |
|
| 46 |
while (*s) { |
| 47 |
if (!isdigit(*s)) |
| 48 |
break; |
| 49 |
s++; |
| 50 |
} |
| 51 |
|
| 52 |
while (*s) { |
| 53 |
if (isdigit(*s)) |
| 54 |
break; |
| 55 |
s++; |
| 56 |
} |
| 57 |
|
| 58 |
if (!sscanf(s, "%d", b)) |
| 59 |
return; |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
int string_ends_with(const char *s, const char *suffix) { |
| 64 |
if (!s || !suffix) |
| 65 |
return 0; |
| 66 |
int s_len = strlen(s); |
| 67 |
int suffix_len = strlen(suffix); |
| 68 |
if (s_len < suffix_len) |
| 69 |
return 0; |
| 70 |
return strcmp(s + s_len - suffix_len, suffix) == 0; |
| 71 |
} |