| 1 |
#ifndef _melder_str32_h_ |
| 2 |
#define _melder_str32_h_ |
| 3 |
/* melder_str32.h |
| 4 |
* |
| 5 |
* Copyright (C) 1992-2018,2020-2023 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 2 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 |
integer Melder8_length (conststring8 stringOrNull); // can assert on 32-bit platforms if the string is longer than 2^31-1 characters |
| 22 |
|
| 23 |
integer Melder16_length (conststring16 stringOrNull) noexcept; |
| 24 |
mutablestring16 str16cpy (mutablestring16 target, conststring16 source) noexcept; |
| 25 |
|
| 26 |
integer constexpr Melder_length (const conststring32 stringOrNull) noexcept { |
| 27 |
if (! stringOrNull) |
| 28 |
return 0; |
| 29 |
const char32 *p = & stringOrNull [0]; |
| 30 |
while (*p != U'\0') |
| 31 |
++ p; |
| 32 |
return p - stringOrNull; |
| 33 |
} |
| 34 |
mutablestring32 str32cpy (mutablestring32 target, conststring32 source) noexcept; |
| 35 |
mutablestring32 str32cat (mutablestring32 target, conststring32 source) noexcept; |
| 36 |
char32 * stp32cpy (mutablestring32 target, conststring32 source) noexcept; |
| 37 |
mutablestring32 str32ncpy (mutablestring32 target, conststring32 source, integer n) noexcept; |
| 38 |
|
| 39 |
/* |
| 40 |
str32cmp() mimics the standard library strcmp() function. |
| 41 |
As str32cmp() works on strings with 32-bit *unsigned* characters, |
| 42 |
we had to decide whether str32cmp() should work correctly even for characters that have bit 31 set. |
| 43 |
We decided that it should. |
| 44 |
str32cmp() returns -1 if `string1` sorts before `string2`, 0 if `string1` and `string2` are equal, |
| 45 |
and +1 if `string1` sorts after `string2` (there is no fourth option, because we assume total ordering). |
| 46 |
As std::strcmp(), str32cmp() exhibits undefined behaviour if `string1` and/or `string2` is null. |
| 47 |
By contrast, Melder_cmp() does allow null strings, by regarding them as equal to the empty string. |
| 48 |
*/ |
| 49 |
int str32cmp (conststring32 string1, conststring32 string2) noexcept; |
| 50 |
int Melder_cmp (conststring32 string1orNull, conststring32 string2orNull) noexcept; |
| 51 |
|
| 52 |
/* |
| 53 |
Are strings identical? |
| 54 |
|
| 55 |
str32equ() simply computes whether str32cmp() would return 0 or not. |
| 56 |
*/ |
| 57 |
//bool str32equ (conststring32 string1, conststring32 string2) noexcept; |
| 58 |
//bool Melder_equ (conststring32 string1orNull, conststring32 string2orNull) noexcept; |
| 59 |
|
| 60 |
/* |
| 61 |
What is the sorting order of two strings? |
| 62 |
|
| 63 |
str32coll() simply calls str32cmp() and returns its result. |
| 64 |
It may look like str32coll() is superfluous, but with additional options, |
| 65 |
as below, str32cmp_XXX() and str32coll_XXX() will diverge. |
| 66 |
*/ |
| 67 |
int str32coll (conststring32 string1, conststring32 string2) noexcept; |
| 68 |
int Melder_coll (conststring32 string1orNull, conststring32 string2orNull) noexcept; |
| 69 |
|
| 70 |
/* |
| 71 |
str32coll_numberAware() sorts "hello5" before "hello21", |
| 72 |
i.e. every sequence of digits sorts as its integer value. |
| 73 |
|
| 74 |
SPECIAL CASE: LEADING ZEROES |
| 75 |
|
| 76 |
For some different digit sequences, the integer value will be the same, |
| 77 |
namely whenever there are leading zeroes. That is, "hello005" is equivalent |
| 78 |
to "hello5" and should therefore sort before "hello5a", "hello6" and "hello10". |
| 79 |
Within equivalence classes, sorting is determined by the number of leading zeroes; |
| 80 |
thus, "hello005" sorts after "hello5", and "hello05" sorts in between the two. |
| 81 |
With multiple digit sequences, sorting is determined by the *global* number |
| 82 |
of leading zeroes, so that "hello05a5" sorts before "hello5a005". |
| 83 |
One could have a case of unequal strings that have the same global number of |
| 84 |
leading zeroes; in that case, sorting is determined by the *local* number of |
| 85 |
leading zeroes; for instance, within the equivalence class "hello5a5", |
| 86 |
"hello5a005" comes before "hello05a05", which comes before "hello005a5", |
| 87 |
while "hello05a5" comes before all three, and "hello5a0005 comes after all three. |
| 88 |
|
| 89 |
CAN THIS HANDLE NEGATIVE NUMBERS? |
| 90 |
|
| 91 |
No, it cannot, because of an ambiguity between hyphens and minus signs. |
| 92 |
After all, "hello-8" could have to be interpreted as 'hello, version 8', |
| 93 |
with the hyphen separating the word from the number. If that is indeed |
| 94 |
the interpretation, "hello-9" should sort after "hello-8", and this is what happens. |
| 95 |
Basically, str32coll_numberAware() simply regards hyphens as non-digits. |
| 96 |
|
| 97 |
CAN THIS HANDLE FLOATING-POINT NUMBERS? |
| 98 |
|
| 99 |
No, it cannot, because of an ambiguity between separating dots and decimal points. |
| 100 |
After all, "file8.2" could have to be interpreted as 'chapter 8, section 2' |
| 101 |
and in that case will have to sort before "file8.11". |
| 102 |
If you want to interpret 8.2 and 8.11 as floating-point numbers, |
| 103 |
which sort in the opposite order from chapter–section semantics, |
| 104 |
you will have to make sure that the number of digits after the decimal point |
| 105 |
is the same for both strings, so that "file8.11" will sort before "file8.20". |
| 106 |
By supporting integers, we do support *fixed*-point numbers: |
| 107 |
even without special trickery, "file8.11" will nicely sort before "file14.03". |
| 108 |
Basically, str32coll_numberAware() simply regards dots as non-digits. |
| 109 |
*/ |
| 110 |
int str32coll_numberAware (conststring32 string1, conststring32 string2, bool caseAware = false) noexcept; |
| 111 |
int Melder_coll_numberAware (conststring32 string1orNull, conststring32 string2orNull) noexcept; |
| 112 |
int str32cmp_caseInsensitive (conststring32 string1, conststring32 string2) noexcept; |
| 113 |
int Melder_cmp_caseInsensitive (conststring32 string1orNull, conststring32 string2orNull) noexcept; |
| 114 |
int str32cmp_optionallyCaseSensitive (conststring32 string1, conststring32 string2, bool caseSensitive) noexcept; |
| 115 |
|
| 116 |
int str32ncmp (conststring32 string1, conststring32 string2, integer n) noexcept; |
| 117 |
int Melder_ncmp (conststring32 string1orNull, conststring32 string2orNull, integer n) noexcept; |
| 118 |
int str32ncmp_caseInsensitive (conststring32 string1, conststring32 string2, integer n) noexcept; |
| 119 |
int Melder_ncmp_caseInsensitive (conststring32 string1orNull, conststring32 string2orNull, integer n) noexcept; |
| 120 |
int str32ncmp_optionallyCaseSensitive (conststring32 string1, conststring32 string2, integer n, bool caseSensitive) noexcept; |
| 121 |
|
| 122 |
#define str32equ ! str32cmp |
| 123 |
#define str32nequ ! str32ncmp |
| 124 |
#define Melder_equ ! Melder_cmp |
| 125 |
#define str32equ_caseInsensitive ! str32cmp_caseInsensitive |
| 126 |
#define str32nequ_caseInsensitive ! str32ncmp_caseInsensitive |
| 127 |
#define Melder_equ_caseInsensitive ! Melder_cmp_caseInsensitive |
| 128 |
#define str32equ_optionallyCaseSensitive ! str32cmp_optionallyCaseSensitive |
| 129 |
#define str32nequ_optionallyCaseSensitive ! str32ncmp_optionallyCaseSensitive |
| 130 |
bool Melder_equ_firstCharacterCaseInsensitive (conststring32 string1orNull, conststring32 string2orNull) noexcept; |
| 131 |
#define Melder_nequ ! Melder_ncmp |
| 132 |
#define Melder_nequ_caseInsensitive ! Melder_ncmp_caseInsensitive |
| 133 |
|
| 134 |
inline char32 * str32chr (conststring32 string, char32 kar) noexcept { |
| 135 |
for (; *string != kar; ++ string) |
| 136 |
if (*string == U'\0') |
| 137 |
return nullptr; |
| 138 |
return (char32 *) string; |
| 139 |
} |
| 140 |
inline char32 * str32chr_caseInsensitive (conststring32 string, char32 kar) noexcept { |
| 141 |
kar = Melder_toLowerCase (kar); |
| 142 |
for (; Melder_toLowerCase (*string) != kar; ++ string) |
| 143 |
if (*string == U'\0') |
| 144 |
return nullptr; |
| 145 |
return (char32 *) string; |
| 146 |
} |
| 147 |
inline char32 * str32rchr (conststring32 string, char32 kar) noexcept { |
| 148 |
char32 *result = nullptr; |
| 149 |
for (; *string != U'\0'; ++ string) |
| 150 |
if (*string == kar) |
| 151 |
result = (char32 *) string; |
| 152 |
return result; |
| 153 |
} |
| 154 |
inline char32 * str32rchr_caseInsensitive (conststring32 string, char32 kar) noexcept { |
| 155 |
kar = Melder_toLowerCase (kar); |
| 156 |
char32 *result = nullptr; |
| 157 |
for (; *string != U'\0'; ++ string) |
| 158 |
if (Melder_toLowerCase (*string) == kar) |
| 159 |
result = (char32 *) string; |
| 160 |
return result; |
| 161 |
} |
| 162 |
char32 * str32str (conststring32 string, conststring32 find) noexcept; |
| 163 |
char32 * str32str_caseInsensitive (conststring32 string, conststring32 find) noexcept; |
| 164 |
char32 * str32str_optionallyCaseSensitive (conststring32 string, conststring32 find, bool caseSensitive) noexcept; |
| 165 |
|
| 166 |
inline integer str32spn (conststring32 string1, conststring32 string2) noexcept { |
| 167 |
const char32 *p = & string1 [0]; |
| 168 |
char32 kar1, kar2; |
| 169 |
cont: |
| 170 |
kar1 = * p ++; |
| 171 |
for (const char32 *q = & string2 [0]; (kar2 = * q ++) != U'\0';) |
| 172 |
if (kar2 == kar1) |
| 173 |
goto cont; |
| 174 |
return p - 1 - string1; |
| 175 |
} |
| 176 |
|
| 177 |
/* End of file melder_str32.h */ |
| 178 |
#endif |