| [0532954] | 1 | /* dynamic string handling */
|
|---|
| 2 | /* Copyright (c) Olly Betts 1999, 2001, 2012, 2014, 2024
|
|---|
| [9286525] | 3 | *
|
|---|
| [89231c4] | 4 | * This program is free software; you can redistribute it and/or modify
|
|---|
| 5 | * it under the terms of the GNU General Public License as published by
|
|---|
| 6 | * the Free Software Foundation; either version 2 of the License, or
|
|---|
| 7 | * (at your option) any later version.
|
|---|
| [846746e] | 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
|
|---|
| [89231c4] | 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|---|
| 12 | * GNU General Public License for more details.
|
|---|
| [846746e] | 13 | *
|
|---|
| [89231c4] | 14 | * You should have received a copy of the GNU General Public License
|
|---|
| [0b99107] | 15 | * along with this program; if not, see
|
|---|
| 16 | * <https://www.gnu.org/licenses/>.
|
|---|
| [846746e] | 17 | */
|
|---|
| [30d80d2] | 18 |
|
|---|
| [0532954] | 19 | #ifndef SURVEX_INCLUDED_STR_H
|
|---|
| 20 | #define SURVEX_INCLUDED_STR_H
|
|---|
| 21 |
|
|---|
| 22 | #include <string.h>
|
|---|
| [a49a80c0] | 23 | #include <stdlib.h>
|
|---|
| [0532954] | 24 |
|
|---|
| 25 | typedef struct {
|
|---|
| 26 | char *s;
|
|---|
| 27 | int len;
|
|---|
| 28 | int capacity; /* One less than the allocated size! */
|
|---|
| 29 | } string;
|
|---|
| 30 |
|
|---|
| 31 | // string foo = S_INIT;
|
|---|
| 32 | #define S_INIT { NULL, 0, 0 }
|
|---|
| 33 |
|
|---|
| 34 | void s_expand_(string *pstr, int addition);
|
|---|
| 35 |
|
|---|
| 36 | /* Append a block of text with given length. */
|
|---|
| [94d9f64] | 37 | void s_appendlen(string *pstr, const char *s, int s_len);
|
|---|
| [0532954] | 38 |
|
|---|
| [94d9f64] | 39 | void s_appendn(string *pstr, int n, char c);
|
|---|
| [725d3b1] | 40 |
|
|---|
| 41 | /* Append another string. */
|
|---|
| [94d9f64] | 42 | static inline void s_appends(string *pstr, const string *s) {
|
|---|
| 43 | s_appendlen(pstr, s->s, s->len);
|
|---|
| [725d3b1] | 44 | }
|
|---|
| 45 |
|
|---|
| [0532954] | 46 | /* Append a C string. */
|
|---|
| [94d9f64] | 47 | static inline void s_append(string *pstr, const char *s) {
|
|---|
| 48 | s_appendlen(pstr, s, strlen(s));
|
|---|
| [0532954] | 49 | }
|
|---|
| 50 |
|
|---|
| 51 | /* Append a character */
|
|---|
| [94d9f64] | 52 | static inline void s_appendch(string *pstr, char c) {
|
|---|
| [0532954] | 53 | if (pstr->capacity == pstr->len) s_expand_(pstr, 1);
|
|---|
| 54 | pstr->s[pstr->len++] = c;
|
|---|
| 55 | }
|
|---|
| 56 |
|
|---|
| 57 | /* Truncate string to zero length (and ensure it isn't NULL). */
|
|---|
| [04f218f] | 58 | static inline void s_clear(string *pstr) {
|
|---|
| [0532954] | 59 | if (pstr->s == NULL) s_expand_(pstr, 0);
|
|---|
| 60 | pstr->len = 0;
|
|---|
| 61 | }
|
|---|
| 62 |
|
|---|
| 63 | /* Truncate string */
|
|---|
| [04f218f] | 64 | static inline void s_truncate(string *pstr, int new_len) {
|
|---|
| [0532954] | 65 | if (new_len < pstr->len) {
|
|---|
| 66 | pstr->len = new_len;
|
|---|
| 67 | pstr->s[new_len] = '\0';
|
|---|
| 68 | }
|
|---|
| 69 | }
|
|---|
| 70 |
|
|---|
| 71 | /* Release allocated memory. */
|
|---|
| [04f218f] | 72 | static inline void s_free(string *pstr) {
|
|---|
| [ae917b96] | 73 | free(pstr->s);
|
|---|
| [0532954] | 74 | pstr->s = NULL;
|
|---|
| 75 | pstr->len = 0;
|
|---|
| 76 | pstr->capacity = 0;
|
|---|
| 77 | }
|
|---|
| 78 |
|
|---|
| 79 | /* Steal the C string. */
|
|---|
| [04f218f] | 80 | static inline char *s_steal(string *pstr) {
|
|---|
| [0532954] | 81 | char *s = pstr->s;
|
|---|
| [94d9f64] | 82 | s[pstr->len] = '\0';
|
|---|
| [0532954] | 83 | pstr->s = NULL;
|
|---|
| 84 | pstr->len = 0;
|
|---|
| 85 | pstr->capacity = 0;
|
|---|
| 86 | return s;
|
|---|
| 87 | }
|
|---|
| 88 |
|
|---|
| 89 | /* Donate a malloc-ed C string. */
|
|---|
| [04f218f] | 90 | static inline void s_donate(string *pstr, char *s) {
|
|---|
| [ae917b96] | 91 | free(pstr->s);
|
|---|
| [0532954] | 92 | pstr->s = s;
|
|---|
| 93 | pstr->capacity = pstr->len = strlen(s);
|
|---|
| 94 | }
|
|---|
| 95 |
|
|---|
| [04f218f] | 96 | static inline int s_len(const string *pstr) { return pstr->len; }
|
|---|
| [0532954] | 97 |
|
|---|
| [04f218f] | 98 | static inline bool s_empty(const string *pstr) { return pstr->len == 0; }
|
|---|
| [30d80d2] | 99 |
|
|---|
| [94d9f64] | 100 | static inline const char *s_str(string *pstr) {
|
|---|
| 101 | char *s = pstr->s;
|
|---|
| 102 | if (s) s[pstr->len] = '\0';
|
|---|
| 103 | return s;
|
|---|
| 104 | }
|
|---|
| [99d5754] | 105 |
|
|---|
| [04f218f] | 106 | static inline bool s_eqlen(const string *pstr, const char *s, int s_len) {
|
|---|
| [0532954] | 107 | return pstr->len == s_len && memcmp(pstr->s, s, s_len) == 0;
|
|---|
| 108 | }
|
|---|
| [bd2e33a] | 109 |
|
|---|
| [04f218f] | 110 | static inline bool s_eq(const string *pstr, const char *s) {
|
|---|
| [94d9f64] | 111 | return strncmp(pstr->s, s, pstr->len) == 0 && s[pstr->len] == '\0';
|
|---|
| [0532954] | 112 | }
|
|---|
| [99d5754] | 113 |
|
|---|
| [236cfc4] | 114 | static inline char s_back(const string *pstr) {
|
|---|
| [c2fab81e] | 115 | return pstr->s[pstr->len - 1];
|
|---|
| 116 | }
|
|---|
| 117 |
|
|---|
| [0532954] | 118 | #define S_EQ(PSTR, LITERAL) s_eqlen((PSTR), LITERAL, sizeof(LITERAL "") - 1)
|
|---|
| [99d5754] | 119 |
|
|---|
| [0532954] | 120 | #endif // SURVEX_INCLUDED_STR_H
|
|---|