diff options
| author | 2026-11-11 05:31:06 +0000 | |
|---|---|---|
| committer | 2026-11-11 06:14:42 +0000 | |
| commit | 9e1d304cac5367c3dbb1ca14700736680bd3a804 (patch) | |
| tree | 31f328747acb934d365d423adfda144818ff5ad4 /02/src/string-view.c | |
| download | lab-01-05-9e1d304cac5367c3dbb1ca14700736680bd3a804.tar.gz | |
Diffstat (limited to '02/src/string-view.c')
| -rw-r--r-- | 02/src/string-view.c | 147 |
1 files changed, 147 insertions, 0 deletions
diff --git a/02/src/string-view.c b/02/src/string-view.c new file mode 100644 index 0000000..ef84f69 --- /dev/null +++ b/02/src/string-view.c | |||
| @@ -0,0 +1,147 @@ | |||
| 1 | #include "string-view.h" | ||
| 2 | #include <string.h> | ||
| 3 | #include <stdlib.h> | ||
| 4 | |||
| 5 | size_t | ||
| 6 | string_view_length (struct string_view sv) | ||
| 7 | { | ||
| 8 | return sv.end - sv.begin; | ||
| 9 | } | ||
| 10 | |||
| 11 | struct string_view | ||
| 12 | string_view_from_cstr (const char *cstr) | ||
| 13 | { | ||
| 14 | return (struct string_view) { | ||
| 15 | .begin = cstr, | ||
| 16 | .end = cstr + strlen (cstr), | ||
| 17 | }; | ||
| 18 | } | ||
| 19 | |||
| 20 | struct string_view | ||
| 21 | duplicate_string_view (struct string_view sv) | ||
| 22 | { | ||
| 23 | size_t len = SVLEN (sv); | ||
| 24 | |||
| 25 | struct string_view result = { | ||
| 26 | .begin = malloc (len), | ||
| 27 | }; | ||
| 28 | result.end = result.begin + len; | ||
| 29 | |||
| 30 | memcpy ((void *) result.begin, sv.begin, len); | ||
| 31 | |||
| 32 | return result; | ||
| 33 | } | ||
| 34 | |||
| 35 | struct string_view | ||
| 36 | duplicate_string_view_as_cstr (struct string_view sv) | ||
| 37 | { | ||
| 38 | size_t len = SVLEN (sv); | ||
| 39 | |||
| 40 | struct string_view result = { | ||
| 41 | .begin = malloc (len + 1), | ||
| 42 | }; | ||
| 43 | result.end = result.begin + len; | ||
| 44 | |||
| 45 | memcpy ((void *) result.begin, sv.begin, len); | ||
| 46 | ((char *) result.begin)[len] = '\0'; | ||
| 47 | |||
| 48 | return result; | ||
| 49 | } | ||
| 50 | |||
| 51 | struct string_view | ||
| 52 | string_view_strdup (const char *cstr) | ||
| 53 | { | ||
| 54 | struct string_view sv; | ||
| 55 | size_t len = strlen (cstr); | ||
| 56 | sv.begin = malloc (len + 1), | ||
| 57 | sv.end = sv.begin + len; | ||
| 58 | |||
| 59 | memcpy ((void *) sv.begin, cstr, len); | ||
| 60 | ((char *) sv.begin)[len] = '\0'; | ||
| 61 | |||
| 62 | return sv; | ||
| 63 | } | ||
| 64 | |||
| 65 | int | ||
| 66 | string_view_compare (struct string_view lhs, struct string_view rhs) | ||
| 67 | { | ||
| 68 | if (SVLEN (lhs) != SVLEN (rhs)) | ||
| 69 | return SVLEN (lhs) - SVLEN (rhs); | ||
| 70 | |||
| 71 | return memcmp (lhs.begin, rhs.begin, SVLEN (lhs)); | ||
| 72 | } | ||
| 73 | |||
| 74 | bool | ||
| 75 | string_view_starts_with (struct string_view sv, struct string_view prefix) | ||
| 76 | { | ||
| 77 | if (SVLEN (sv) < SVLEN (prefix)) | ||
| 78 | return false; | ||
| 79 | |||
| 80 | return memcmp (sv.begin, prefix.begin, SVLEN (prefix)) == 0; | ||
| 81 | } | ||
| 82 | |||
| 83 | struct string_view | ||
| 84 | string_view_dirname (struct string_view sv) | ||
| 85 | { | ||
| 86 | while (sv.end != sv.begin && sv.end[-1] == '/') | ||
| 87 | sv.end -= 1; | ||
| 88 | |||
| 89 | while (sv.end != sv.begin && sv.end[-1] != '/') | ||
| 90 | sv.end -= 1; | ||
| 91 | |||
| 92 | while (sv.end != sv.begin && sv.end[-1] == '/') | ||
| 93 | sv.end -= 1; | ||
| 94 | |||
| 95 | if (sv.end == sv.begin) | ||
| 96 | return SVCSTR ("."); | ||
| 97 | |||
| 98 | return sv; | ||
| 99 | } | ||
| 100 | |||
| 101 | struct string_view | ||
| 102 | string_view_basename (struct string_view sv) | ||
| 103 | { | ||
| 104 | const char *head = sv.end; | ||
| 105 | |||
| 106 | while (head > sv.begin && head[-1] == '/') | ||
| 107 | head -= 1; | ||
| 108 | |||
| 109 | while (head > sv.begin && head[-1] != '/') | ||
| 110 | head -= 1; | ||
| 111 | |||
| 112 | if (head != sv.begin) | ||
| 113 | sv.begin = head; | ||
| 114 | |||
| 115 | return sv; | ||
| 116 | } | ||
| 117 | |||
| 118 | // pops all if c was not found | ||
| 119 | struct string_view | ||
| 120 | string_view_pop_up_to (struct string_view *sv, char c) | ||
| 121 | { | ||
| 122 | const char *head = sv->begin; | ||
| 123 | |||
| 124 | while (head < sv->end && *head != c) | ||
| 125 | head += 1; | ||
| 126 | |||
| 127 | struct string_view result = { .begin = sv->begin }; | ||
| 128 | result.end = sv->begin = head; | ||
| 129 | |||
| 130 | return result; | ||
| 131 | } | ||
| 132 | |||
| 133 | struct string_view | ||
| 134 | string_view_pop_n (struct string_view *sv, size_t n) | ||
| 135 | { | ||
| 136 | struct string_view result = { | ||
| 137 | .begin = sv->begin, | ||
| 138 | .end = sv->begin + n, | ||
| 139 | }; | ||
| 140 | |||
| 141 | if (result.end > sv->end) | ||
| 142 | result.end = sv->end; | ||
| 143 | |||
| 144 | sv->begin = result.end; | ||
| 145 | |||
| 146 | return result; | ||
| 147 | } | ||
