| 1 |
// |
| 2 |
// Copyright(C) 2023 by Ryan Krafnick |
| 3 |
// |
| 4 |
// This program is free software; you can redistribute it and/or |
| 5 |
// modify it under the terms of the GNU General Public License |
| 6 |
// as published by the Free Software Foundation; either version 2 |
| 7 |
// of the License, or (at your option) any later version. |
| 8 |
// |
| 9 |
// This program is distributed in the hope that it will be useful, |
| 10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
// GNU General Public License for more details. |
| 13 |
// |
| 14 |
// DESCRIPTION: |
| 15 |
// DSDA Utility String View |
| 16 |
// |
| 17 |
|
| 18 |
#include <string.h> |
| 19 |
|
| 20 |
#include "string_view.h" |
| 21 |
|
| 22 |
void dsda_InitStringView(dsda_string_view_t* sv, const char* string, size_t size) { |
| 23 |
sv->string = string; |
| 24 |
sv->size = size; |
| 25 |
} |
| 26 |
|
| 27 |
dboolean dsda_IsStringViewEmpty(const dsda_string_view_t* sv) { |
| 28 |
return sv->size == 0; |
| 29 |
} |
| 30 |
|
| 31 |
void dsda_StringViewAtOffset(const dsda_string_view_t* sv, size_t offset, |
| 32 |
dsda_string_view_t* ofs) { |
| 33 |
if (offset > sv->size) { |
| 34 |
dsda_InitStringView(ofs, NULL, 0); |
| 35 |
return; |
| 36 |
} |
| 37 |
|
| 38 |
ofs->string = sv->string + offset; |
| 39 |
ofs->size = sv->size - offset; |
| 40 |
} |
| 41 |
|
| 42 |
dboolean dsda_SplitStringViewAfterChar(const dsda_string_view_t* sv, char c, |
| 43 |
dsda_string_view_t* before, |
| 44 |
dsda_string_view_t* after) |
| 45 |
{ |
| 46 |
const char* cur; |
| 47 |
size_t size; |
| 48 |
|
| 49 |
for (cur = sv->string, size = sv->size; size; ++cur, --size) |
| 50 |
if (*cur == c) { |
| 51 |
if (before) |
| 52 |
dsda_InitStringView(before, sv->string, sv->size - size + 1); |
| 53 |
|
| 54 |
if (after) |
| 55 |
dsda_InitStringView(after, cur + 1, size - 1); |
| 56 |
|
| 57 |
return true; |
| 58 |
} |
| 59 |
|
| 60 |
if (before) |
| 61 |
dsda_InitStringView(before, sv->string, sv->size); |
| 62 |
|
| 63 |
if (after) |
| 64 |
dsda_InitStringView(after, NULL, 0); |
| 65 |
|
| 66 |
return false; |
| 67 |
} |
| 68 |
|
| 69 |
dboolean dsda_SplitStringViewBeforeChar(const dsda_string_view_t* sv, char c, |
| 70 |
dsda_string_view_t* before, |
| 71 |
dsda_string_view_t* after) |
| 72 |
{ |
| 73 |
const char* cur; |
| 74 |
size_t size; |
| 75 |
|
| 76 |
for (cur = sv->string, size = sv->size; size; ++cur, --size) |
| 77 |
if (*cur == c) { |
| 78 |
if (before) |
| 79 |
dsda_InitStringView(before, sv->string, sv->size - size); |
| 80 |
|
| 81 |
if (after) |
| 82 |
dsda_InitStringView(after, cur, size); |
| 83 |
|
| 84 |
return true; |
| 85 |
} |
| 86 |
|
| 87 |
if (before) |
| 88 |
dsda_InitStringView(before, sv->string, sv->size); |
| 89 |
|
| 90 |
if (after) |
| 91 |
dsda_InitStringView(after, NULL, 0); |
| 92 |
|
| 93 |
return false; |
| 94 |
} |
| 95 |
|
| 96 |
dboolean dsda_GetStringViewLine(dsda_string_view_t* sv, dsda_string_view_t* line) { |
| 97 |
if (dsda_IsStringViewEmpty(sv)) { |
| 98 |
dsda_InitStringView(line, NULL, 0); |
| 99 |
|
| 100 |
return false; |
| 101 |
} |
| 102 |
|
| 103 |
dsda_SplitStringViewAfterChar(sv, '\n', line, sv); |
| 104 |
|
| 105 |
return true; |
| 106 |
} |
| 107 |
|
| 108 |
dboolean dsda_StringViewStartsWith(const dsda_string_view_t* sv, const char* prefix) { |
| 109 |
size_t len; |
| 110 |
|
| 111 |
len = strlen(prefix); |
| 112 |
|
| 113 |
return sv->size >= len && !memcmp(sv->string, prefix, len); |
| 114 |
} |
| 115 |
|
| 116 |
void dsda_StringViewAfterChars(const dsda_string_view_t* sv, |
| 117 |
const char* chars, dsda_string_view_t* after) |
| 118 |
{ |
| 119 |
const char* cur; |
| 120 |
size_t size; |
| 121 |
|
| 122 |
for (cur = sv->string, size = sv->size; size && strchr(chars, *cur); ++cur, --size); |
| 123 |
|
| 124 |
dsda_InitStringView(after, cur, size); |
| 125 |
} |