| 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 |
#ifndef __DSDA_UTILITY_STRING_VIEW__ |
| 19 |
#define __DSDA_UTILITY_STRING_VIEW__ |
| 20 |
|
| 21 |
#include "doomtype.h" |
| 22 |
|
| 23 |
// Non-allocated, read-only view into a string, like C++ string_view |
| 24 |
typedef struct { |
| 25 |
const char* string; |
| 26 |
size_t size; |
| 27 |
} dsda_string_view_t; |
| 28 |
|
| 29 |
void dsda_InitStringView(dsda_string_view_t* sv, const char* string, size_t size); |
| 30 |
|
| 31 |
dboolean dsda_IsStringViewEmpty(const dsda_string_view_t* sv); |
| 32 |
|
| 33 |
void dsda_StringViewAtOffset(const dsda_string_view_t* sv, size_t offset, dsda_string_view_t* ofs); |
| 34 |
|
| 35 |
// Splits `sv` at the first instance of `c`, putting everything up to and including it in `before` |
| 36 |
// and everything after in `after`. `before` or `after` may safely alias `sv`. If `c` does not |
| 37 |
// occur in `sv`, `before` is set to `sv` and `after` is set to empty. Returns `true` if |
| 38 |
// an occurence was found. |
| 39 |
dboolean dsda_SplitStringViewAfterChar(const dsda_string_view_t* sv, char c, |
| 40 |
dsda_string_view_t* before, |
| 41 |
dsda_string_view_t* after); |
| 42 |
|
| 43 |
// Like the above, except `c` is included in `after` if found |
| 44 |
dboolean dsda_SplitStringViewBeforeChar(const dsda_string_view_t* sv, char c, |
| 45 |
dsda_string_view_t* before, |
| 46 |
dsda_string_view_t* after); |
| 47 |
|
| 48 |
// Sets `line` to the current line in `sv`, including any trailing '\r' or '\n' |
| 49 |
// and advances `sv` to the next line. |
| 50 |
// If the string does not end with a '\n', `line` is set to `sv` and `sv` is emptied. |
| 51 |
// Returns `false` if `sv` is empty. |
| 52 |
dboolean dsda_GetStringViewLine(dsda_string_view_t* sv, dsda_string_view_t* line); |
| 53 |
|
| 54 |
dboolean dsda_StringViewStartsWith(const dsda_string_view_t* sv, const char* prefix); |
| 55 |
|
| 56 |
void dsda_StringViewAfterChars(const dsda_string_view_t* sv, const char* chars, |
| 57 |
dsda_string_view_t* after); |
| 58 |
|
| 59 |
#endif |