| 1 |
#ifndef _melder_string_h_ |
| 2 |
#define _melder_string_h_ |
| 3 |
/* MelderString.h |
| 4 |
* |
| 5 |
* Copyright (C) 1992-2023,2025 Paul Boersma |
| 6 |
* |
| 7 |
* This code is free software; you can redistribute it and/or modify |
| 8 |
* it under the terms of the GNU General Public License as published by |
| 9 |
* the Free Software Foundation; either version 3 of the License, or (at |
| 10 |
* your option) any later version. |
| 11 |
* |
| 12 |
* This code is distributed in the hope that it will be useful, but |
| 13 |
* WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 15 |
* See the GNU General Public License for more details. |
| 16 |
* |
| 17 |
* You should have received a copy of the GNU General Public License |
| 18 |
* along with this work. If not, see <http://www.gnu.org/licenses/>. |
| 19 |
*/ |
| 20 |
|
| 21 |
/* |
| 22 |
Strings that: |
| 23 |
- are null-terminated |
| 24 |
- have O(1) access to their length |
| 25 |
- grow as needed |
| 26 |
- can be appended to without scanning for the final null character |
| 27 |
- automatically convert numbers, objects, file names, vectors, and matrices to strings |
| 28 |
*/ |
| 29 |
|
| 30 |
struct MelderString8 { |
| 31 |
int64 length = 0; |
| 32 |
int64 bufferSize = 0; |
| 33 |
char8 *string = nullptr; // a growing buffer, rarely shrunk (can only be freed by MelderString8_free) |
| 34 |
}; |
| 35 |
struct MelderString16 { |
| 36 |
int64 length = 0; |
| 37 |
int64 bufferSize = 0; |
| 38 |
char16 *string = nullptr; // a growing buffer, rarely shrunk (can only be freed by MelderString16_free) |
| 39 |
}; |
| 40 |
struct MelderString { |
| 41 |
int64 length = 0; |
| 42 |
int64 bufferSize = 0; |
| 43 |
char32 *string = nullptr; // a growing buffer, rarely shrunk (can only be freed by MelderString_free) |
| 44 |
}; |
| 45 |
|
| 46 |
void MelderString8_free (MelderString16 *me); // frees the buffer (and sets other attributes to zero) |
| 47 |
void MelderString16_free (MelderString16 *me); // frees the buffer (and sets other attributes to zero) |
| 48 |
void MelderString_free (MelderString *me); // frees the buffer (and sets other attributes to zero) |
| 49 |
void MelderString8_empty (MelderString16 *me); // sets to empty string (buffer shrunk if very large) |
| 50 |
void MelderString16_empty (MelderString16 *me); // sets to empty string (buffer shrunk if very large) |
| 51 |
void MelderString_empty (MelderString *me); // sets to empty string (buffer shrunk if very large) |
| 52 |
void _private_MelderString_expand (MelderString *me, int64 sizeNeeded); // increases the buffer size; there's normally no need to call this |
| 53 |
void MelderString_ncopy (MelderString *me, conststring32 sourceOrNull, int64 n); |
| 54 |
void MelderString_nappend (MelderString *me, conststring32 sourceOrNull, integer n); |
| 55 |
|
| 56 |
inline void _private_MelderString__appendOneStringElement (MelderString *me, conststring32 string) { |
| 57 |
if (string) { |
| 58 |
const char32 *newEndOfStringLocation = stp32cpy (& my string [my length], string); // this will append a null character |
| 59 |
my length = newEndOfStringLocation - & my string [0]; |
| 60 |
} else { |
| 61 |
/* |
| 62 |
Append a null string: do nothing. |
| 63 |
The result will be null-terminated if `me` was null-terminated to start with, |
| 64 |
which is a required invariant. |
| 65 |
*/ |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
template <typename... Arg> |
| 70 |
void MelderString_append (MelderString *me, const Arg... arg) { |
| 71 |
const integer extraLength = MelderArg__length (arg...); |
| 72 |
const integer sizeNeeded = my length + extraLength + 1; |
| 73 |
Melder_assert (sizeNeeded > 0); // this assertion was added to silence an analyzer complaint |
| 74 |
if (sizeNeeded > my bufferSize) |
| 75 |
_private_MelderString_expand (me, sizeNeeded); |
| 76 |
(// fold |
| 77 |
_private_MelderString__appendOneStringElement (me, MelderArg { arg }. _arg) |
| 78 |
, ... |
| 79 |
); |
| 80 |
} |
| 81 |
|
| 82 |
constexpr int64 MelderString_FREE_THRESHOLD_BYTES = 10'000LL; |
| 83 |
|
| 84 |
template <typename... Arg> |
| 85 |
void MelderString_copy (MelderString *me, const Arg... arg) { |
| 86 |
if (my bufferSize * (int64) sizeof (char32) >= MelderString_FREE_THRESHOLD_BYTES) |
| 87 |
MelderString_free (me); |
| 88 |
const integer length = MelderArg__length (arg...); |
| 89 |
const integer sizeNeeded = length + 1; |
| 90 |
Melder_assert (sizeNeeded > 0); // this assertion was added to silence an analyzer complaint |
| 91 |
if (sizeNeeded > my bufferSize) |
| 92 |
_private_MelderString_expand (me, sizeNeeded); |
| 93 |
my length = 0; |
| 94 |
my string [0] = U'\0'; // maintain invariant |
| 95 |
(// fold |
| 96 |
_private_MelderString__appendOneStringElement (me, MelderArg { arg }. _arg) |
| 97 |
, ... |
| 98 |
); |
| 99 |
} |
| 100 |
|
| 101 |
void MelderString16_appendCharacter (MelderString16 *me, char32 character); |
| 102 |
void MelderString_appendCharacter (MelderString *me, char32 character); |
| 103 |
void MelderString_get (MelderString *me, char32 *destination); // performs no boundary checking |
| 104 |
void MelderString_truncate (MelderString *me, integer maximumLength); |
| 105 |
int64 MelderString_allocationCount (); |
| 106 |
int64 MelderString_deallocationCount (); |
| 107 |
int64 MelderString_allocationSize (); |
| 108 |
int64 MelderString_deallocationSize (); |
| 109 |
|
| 110 |
struct autoMelderString : MelderString { |
| 111 |
autoMelderString () { |
| 112 |
// inherited zero initialization suffices |
| 113 |
} |
| 114 |
~autoMelderString () { |
| 115 |
MelderString_free (this); |
| 116 |
} |
| 117 |
}; |
| 118 |
|
| 119 |
/* End of file MelderString.h */ |
| 120 |
#endif |