| 1 | #cxx . . $ `<file>/melder/melder_str32.h`/ifndef _melder_str32_h_ |
| 2 | #define _melder_str32_h_cxx . . $ `melder/melder_str32.h:2:9`! |
| 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 | integercxx . . $ integer# Melder8_lengthcxx . . $ Melder8_length(3c95bd14d5d5d286). (conststring8cxx . . $ conststring8# stringOrNull); // can assert on 32-bit platforms if the string is longer than 2^31-1 characters |
| 22 | |
| 23 | integercxx . . $ integer# Melder16_lengthcxx . . $ Melder16_length(7c057485137234bf). (conststring16cxx . . $ conststring16# stringOrNull) noexcept; |
| 24 | mutablestring16cxx . . $ mutablestring16# str16cpycxx . . $ str16cpy(cee0347b89119601). (mutablestring16cxx . . $ mutablestring16# target, conststring16cxx . . $ conststring16# source) noexcept; |
| 25 | |
| 26 | integercxx . . $ integer# constexpr Melder_lengthcxx . . $ Melder_length(3fae411214b133ad). (const conststring32cxx . . $ conststring32# stringOrNull) noexcept { |
| 27 | if (! stringOrNull) |
| 28 | return 0; |
| 29 | const char32cxx . . $ char32# *p = & stringOrNull [0]; |
| 30 | while (*p != U'\0') |
| 31 | ++ p; |
| 32 | return p - stringOrNull; |
| 33 | } |
| 34 | mutablestring32cxx . . $ mutablestring32# str32cpycxx . . $ str32cpy(6ab934322573b179). (mutablestring32cxx . . $ mutablestring32# target, conststring32cxx . . $ conststring32# source) noexcept; |
| 35 | mutablestring32cxx . . $ mutablestring32# str32catcxx . . $ str32cat(6ab934322573b179). (mutablestring32cxx . . $ mutablestring32# target, conststring32cxx . . $ conststring32# source) noexcept; |
| 36 | char32cxx . . $ char32# * stp32cpycxx . . $ stp32cpy(6ab934322573b179). (mutablestring32cxx . . $ mutablestring32# target, conststring32cxx . . $ conststring32# source) noexcept; |
| 37 | mutablestring32cxx . . $ mutablestring32# str32ncpycxx . . $ str32ncpy(a0fb29e7f5bbd1dc). (mutablestring32cxx . . $ mutablestring32# target, conststring32cxx . . $ conststring32# source, integercxx . . $ 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 str32cmpcxx . . $ str32cmp(937dacc22d2a7919). (conststring32cxx . . $ conststring32# string1, conststring32cxx . . $ conststring32# string2) noexcept; |
| 50 | int Melder_cmpcxx . . $ Melder_cmp(937dacc22d2a7919). (conststring32cxx . . $ conststring32# string1orNull, conststring32cxx . . $ 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 str32collcxx . . $ str32coll(937dacc22d2a7919). (conststring32cxx . . $ conststring32# string1, conststring32cxx . . $ conststring32# string2) noexcept; |
| 68 | int Melder_collcxx . . $ Melder_coll(937dacc22d2a7919). (conststring32cxx . . $ conststring32# string1orNull, conststring32cxx . . $ 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_numberAwarecxx . . $ str32coll_numberAware(8be1707edab15857). (conststring32cxx . . $ conststring32# string1, conststring32cxx . . $ conststring32# string2, bool caseAware = false) noexcept; |
| 111 | int Melder_coll_numberAwarecxx . . $ Melder_coll_numberAware(937dacc22d2a7919). (conststring32cxx . . $ conststring32# string1orNull, conststring32cxx . . $ conststring32# string2orNull) noexcept; |
| 112 | int str32cmp_caseInsensitivecxx . . $ str32cmp_caseInsensitive(937dacc22d2a7919). (conststring32cxx . . $ conststring32# string1, conststring32cxx . . $ conststring32# string2) noexcept; |
| 113 | int Melder_cmp_caseInsensitivecxx . . $ Melder_cmp_caseInsensitive(937dacc22d2a7919). (conststring32cxx . . $ conststring32# string1orNull, conststring32cxx . . $ conststring32# string2orNull) noexcept; |
| 114 | int str32cmp_optionallyCaseSensitivecxx . . $ str32cmp_optionallyCaseSensitive(8be1707edab15857). (conststring32cxx . . $ conststring32# string1, conststring32cxx . . $ conststring32# string2, bool caseSensitive) noexcept; |
| 115 | |
| 116 | int str32ncmpcxx . . $ str32ncmp(e53dc8530ec1035d). (conststring32cxx . . $ conststring32# string1, conststring32cxx . . $ conststring32# string2, integercxx . . $ integer# n) noexcept; |
| 117 | int Melder_ncmpcxx . . $ Melder_ncmp(e53dc8530ec1035d). (conststring32cxx . . $ conststring32# string1orNull, conststring32cxx . . $ conststring32# string2orNull, integercxx . . $ integer# n) noexcept; |
| 118 | int str32ncmp_caseInsensitivecxx . . $ str32ncmp_caseInsensitive(e53dc8530ec1035d). (conststring32cxx . . $ conststring32# string1, conststring32cxx . . $ conststring32# string2, integercxx . . $ integer# n) noexcept; |
| 119 | int Melder_ncmp_caseInsensitivecxx . . $ Melder_ncmp_caseInsensitive(e53dc8530ec1035d). (conststring32cxx . . $ conststring32# string1orNull, conststring32cxx . . $ conststring32# string2orNull, integercxx . . $ integer# n) noexcept; |
| 120 | int str32ncmp_optionallyCaseSensitivecxx . . $ str32ncmp_optionallyCaseSensitive(41c907af11e46a9a). (conststring32cxx . . $ conststring32# string1, conststring32cxx . . $ conststring32# string2, integercxx . . $ integer# n, bool caseSensitive) noexcept; |
| 121 | |
| 122 | #define str32equcxx . . $ `melder/melder_str32.h:122:9`! ! str32cmp |
| 123 | #define str32nequcxx . . $ `melder/melder_str32.h:123:9`! ! str32ncmp |
| 124 | #define Melder_equcxx . . $ `melder/melder_str32.h:124:9`! ! Melder_cmp |
| 125 | #define str32equ_caseInsensitivecxx . . $ `melder/melder_str32.h:125:9`! ! str32cmp_caseInsensitive |
| 126 | #define str32nequ_caseInsensitivecxx . . $ `melder/melder_str32.h:126:9`! ! str32ncmp_caseInsensitive |
| 127 | #define Melder_equ_caseInsensitivecxx . . $ `melder/melder_str32.h:127:9`! ! Melder_cmp_caseInsensitive |
| 128 | #define str32equ_optionallyCaseSensitivecxx . . $ `melder/melder_str32.h:128:9`! ! str32cmp_optionallyCaseSensitive |
| 129 | #define str32nequ_optionallyCaseSensitivecxx . . $ `melder/melder_str32.h:129:9`! ! str32ncmp_optionallyCaseSensitive |
| 130 | bool Melder_equ_firstCharacterCaseInsensitivecxx . . $ Melder_equ_firstCharacterCaseInsensitive(6eccf49f7b301074). (conststring32cxx . . $ conststring32# string1orNull, conststring32cxx . . $ conststring32# string2orNull) noexcept; |
| 131 | #define Melder_nequcxx . . $ `melder/melder_str32.h:131:9`! ! Melder_ncmp |
| 132 | #define Melder_nequ_caseInsensitivecxx . . $ `melder/melder_str32.h:132:9`! ! Melder_ncmp_caseInsensitive |
| 133 | |
| 134 | inline char32cxx . . $ char32# * str32chrcxx . . $ str32chr(328a9ede0fba853a). (conststring32cxx . . $ conststring32# string, char32cxx . . $ char32# kar) noexcept { |
| 135 | for (; *string != kar; ++ string) |
| 136 | if (*string == U'\0') |
| 137 | return nullptr; |
| 138 | return (char32cxx . . $ char32# *) string; |
| 139 | } |
| 140 | inline char32cxx . . $ char32# * str32chr_caseInsensitivecxx . . $ str32chr_caseInsensitive(328a9ede0fba853a). (conststring32cxx . . $ conststring32# string, char32cxx . . $ char32# kar) noexcept { |
| 141 | kar = Melder_toLowerCasecxx . . $ Melder_toLowerCase(cd1afca5b9692c51). (kar); |
| 142 | for (; Melder_toLowerCasecxx . . $ Melder_toLowerCase(cd1afca5b9692c51). (*string) != kar; ++ string) |
| 143 | if (*string == U'\0') |
| 144 | return nullptr; |
| 145 | return (char32cxx . . $ char32# *) string; |
| 146 | } |
| 147 | inline char32cxx . . $ char32# * str32rchrcxx . . $ str32rchr(328a9ede0fba853a). (conststring32cxx . . $ conststring32# string, char32cxx . . $ char32# kar) noexcept { |
| 148 | char32cxx . . $ char32# *result = nullptr; |
| 149 | for (; *string != U'\0'; ++ string) |
| 150 | if (*string == kar) |
| 151 | result = (char32cxx . . $ char32# *) string; |
| 152 | return result; |
| 153 | } |
| 154 | inline char32cxx . . $ char32# * str32rchr_caseInsensitivecxx . . $ str32rchr_caseInsensitive(328a9ede0fba853a). (conststring32cxx . . $ conststring32# string, char32cxx . . $ char32# kar) noexcept { |
| 155 | kar = Melder_toLowerCasecxx . . $ Melder_toLowerCase(cd1afca5b9692c51). (kar); |
| 156 | char32cxx . . $ char32# *result = nullptr; |
| 157 | for (; *string != U'\0'; ++ string) |
| 158 | if (Melder_toLowerCasecxx . . $ Melder_toLowerCase(cd1afca5b9692c51). (*string) == kar) |
| 159 | result = (char32cxx . . $ char32# *) string; |
| 160 | return result; |
| 161 | } |
| 162 | char32cxx . . $ char32# * str32strcxx . . $ str32str(2de17cd8f7c584a6). (conststring32cxx . . $ conststring32# string, conststring32cxx . . $ conststring32# find) noexcept; |
| 163 | char32cxx . . $ char32# * str32str_caseInsensitivecxx . . $ str32str_caseInsensitive(2de17cd8f7c584a6). (conststring32cxx . . $ conststring32# string, conststring32cxx . . $ conststring32# find) noexcept; |
| 164 | char32cxx . . $ char32# * str32str_optionallyCaseSensitivecxx . . $ str32str_optionallyCaseSensitive(7e4d3d07cdd0a044). (conststring32cxx . . $ conststring32# string, conststring32cxx . . $ conststring32# find, bool caseSensitive) noexcept; |
| 165 | |
| 166 | inline integercxx . . $ integer# str32spncxx . . $ str32spn(3d7336bffa454768). (conststring32cxx . . $ conststring32# string1, conststring32cxx . . $ conststring32# string2) noexcept { |
| 167 | const char32cxx . . $ char32# *p = & string1 [0]; |
| 168 | char32cxx . . $ char32# kar1, kar2; |
| 169 | cont: |
| 170 | kar1 = * p ++; |
| 171 | for (const char32cxx . . $ 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 |