stringAPI.h 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  1. // zlib open source license
  2. //
  3. // Copyright (c) 2017 to 2025 David Forsgren Piuva
  4. //
  5. // This software is provided 'as-is', without any express or implied
  6. // warranty. In no event will the authors be held liable for any damages
  7. // arising from the use of this software.
  8. //
  9. // Permission is granted to anyone to use this software for any purpose,
  10. // including commercial applications, and to alter it and redistribute it
  11. // freely, subject to the following restrictions:
  12. //
  13. // 1. The origin of this software must not be misrepresented; you must not
  14. // claim that you wrote the original software. If you use this software
  15. // in a product, an acknowledgment in the product documentation would be
  16. // appreciated but is not required.
  17. //
  18. // 2. Altered source versions must be plainly marked as such, and must not be
  19. // misrepresented as being the original software.
  20. //
  21. // 3. This notice may not be removed or altered from any source
  22. // distribution.
  23. #ifndef DFPSR_API_STRING
  24. #define DFPSR_API_STRING
  25. #include <cstdint>
  26. #include <functional>
  27. #include "bufferAPI.h"
  28. #include "../base/SafePointer.h"
  29. #include "../base/DsrTraits.h"
  30. #include "../collection/List.h"
  31. // Define DSR_INTERNAL_ACCESS before any include to get internal access to exposed types
  32. #ifdef DSR_INTERNAL_ACCESS
  33. #define IMPL_ACCESS public
  34. #else
  35. #define IMPL_ACCESS protected
  36. #endif
  37. namespace dsr {
  38. using DsrChar = char32_t;
  39. // Text files support loading UTF-8/16 BE/LE with BOM or Latin-1 without BOM
  40. enum class CharacterEncoding {
  41. Raw_Latin1, // U+00 to U+FF
  42. BOM_UTF8, // U+00000000 to U+0010FFFF
  43. BOM_UTF16BE, // U+00000000 to U+0000D7FF, U+0000E000 to U+0010FFFF
  44. BOM_UTF16LE // U+00000000 to U+0000D7FF, U+0000E000 to U+0010FFFF
  45. };
  46. // Carriage-return is removed when loading text files to prevent getting double lines
  47. // A line-feed without a line-feed character is nonsense
  48. // LineEncoding allow re-adding carriage-return before or after each line-break when saving
  49. enum class LineEncoding {
  50. CrLf, // Microsoft Windows compatible (Can also be read on other platforms by ignoring carriage return)
  51. Lf // Linux and Macintosh compatible (Might not work on non-portable text editors on Microsoft Windows)
  52. };
  53. class String;
  54. // Helper type for strings.
  55. struct Impl_CharacterView {
  56. DsrChar *data = nullptr;
  57. intptr_t length = 0;
  58. Impl_CharacterView() {}
  59. Impl_CharacterView(Handle<DsrChar> characters)
  60. : data(characters.getUnsafe()), length(characters.getElementCount()) {}
  61. Impl_CharacterView(const DsrChar *data, intptr_t length)
  62. : data(const_cast<DsrChar *>(data)), length(length) {
  63. if (data == nullptr) this->length = 0;
  64. }
  65. inline DsrChar *getUnchecked() const {
  66. return const_cast<DsrChar*>(this->data);
  67. }
  68. inline DsrChar operator [] (intptr_t index) const {
  69. if (index < 0 || index >= this->length) {
  70. return U'\0';
  71. } else {
  72. return this->data[index];
  73. }
  74. }
  75. inline void writeCharacter(intptr_t index, DsrChar character) {
  76. if (index < 0 || index >= this->length) {
  77. // TODO: Throw an error without causing bottomless recursion.
  78. } else {
  79. this->data[index] = character;
  80. }
  81. }
  82. inline SafePointer<DsrChar> getSafe(const char *name) const {
  83. return SafePointer<DsrChar>(name, this->getUnchecked(), this->length * sizeof(DsrChar));
  84. }
  85. };
  86. // Replacing String with a ReadableString reference for input arguments can make passing of U"" literals faster,
  87. // because String is not allowed to assume anything about how long the literal will be available.
  88. // Unlike String, it cannot be constructed from a "" literal, because it is not allowed to heap allocate new memory
  89. // for the conversion, only hold existing buffers alive with reference counting when casted from String.
  90. class ReadableString {
  91. IMPL_ACCESS:
  92. // A reference counted pointer to the buffer to allow passing strings around without having to clone the buffer each time
  93. // ReadableString only uses it for reference counting but String use it for reallocating
  94. Handle<DsrChar> characters;
  95. // Pointing to a subset of the buffer or memory that is not shared.
  96. Impl_CharacterView view;
  97. // TODO: Merge the pointer and length into a new View type for unified bound checks. Then remove the writer pointer.
  98. //SafePointer<const DsrChar> reader;
  99. //intptr_t length = 0;
  100. public:
  101. // TODO: Inline the [] operator for faster reading of characters.
  102. // Use the padded read internally, because the old version was hard-coded for buffers padded to default alignment.
  103. // Returning the character by value prevents writing to memory that might be a constant literal or shared with other strings
  104. inline DsrChar operator[] (intptr_t index) const {
  105. return this->view[index];
  106. }
  107. public:
  108. // Empty string U""
  109. ReadableString() {}
  110. // Implicit casting from U"text"
  111. ReadableString(const DsrChar *content);
  112. ReadableString(Handle<DsrChar> characters, Impl_CharacterView view)
  113. : characters(characters), view(view) {}
  114. // Create from String by sharing the buffer
  115. ReadableString(const String& source);
  116. // Destructor
  117. ~ReadableString() {} // Do not override the non-virtual destructor.
  118. };
  119. // A safe and simple string type
  120. // Can be constructed from ascii literals "", but U"" will preserve unicode characters.
  121. // Can be used without ReadableString, but ReadableString can be wrapped over U"" literals without allocation
  122. // UTF-32
  123. // Endianness is native
  124. // No combined characters allowed, use precomposed instead, so that the strings can guarantee a fixed character size
  125. class String : public ReadableString {
  126. //IMPL_ACCESS:
  127. // TODO: Have a single pointer to the data in ReadableString and let the API be responsible for type safety.
  128. //SafePointer<DsrChar> writer;
  129. public:
  130. // Constructors
  131. String();
  132. String(const char* source);
  133. String(const DsrChar* source);
  134. String(const ReadableString& source);
  135. String(const String& source);
  136. };
  137. // Used as format tags around numbers passed to string_append or string_combine
  138. // New types can implement printing to String by making wrappers from this class
  139. class Printable {
  140. public:
  141. // The method for appending the printable object into the target string
  142. virtual String& toStreamIndented(String& target, const ReadableString& indentation) const = 0;
  143. String& toStream(String& target) const;
  144. String toStringIndented(const ReadableString& indentation) const;
  145. String toString() const;
  146. virtual ~Printable();
  147. };
  148. // Used to generate fixed size ascii strings, which is useful when heap allocations are not possible
  149. // or you need a safe format until you know which encoding a system call needs to support Unicode.
  150. template <intptr_t SIZE>
  151. struct FixedAscii {
  152. char characters[SIZE];
  153. // Create a fixed size ascii string from a null terminated ascii string.
  154. // Crops if text is too long.
  155. FixedAscii(const char *text) {
  156. bool terminated = false;
  157. for (intptr_t i = 0; i < SIZE - 1; i++) {
  158. char c = text[i];
  159. if (c == '\0') {
  160. terminated = true;
  161. }
  162. if (terminated) {
  163. this->characters[i] = '\0';
  164. } else if (c > 127) {
  165. this->characters[i] = '?';
  166. } else {
  167. this->characters[i] = c;
  168. }
  169. }
  170. this->characters[SIZE - 1] = '\0';
  171. }
  172. FixedAscii(const ReadableString &text) {
  173. bool terminated = false;
  174. for (intptr_t i = 0; i < SIZE - 1; i++) {
  175. char c = text[i];
  176. if (c == '\0') {
  177. terminated = true;
  178. }
  179. if (terminated) {
  180. this->characters[i] = '\0';
  181. } else if (c > 127) {
  182. this->characters[i] = '?';
  183. } else {
  184. this->characters[i] = c;
  185. }
  186. }
  187. this->characters[SIZE - 1] = '\0';
  188. }
  189. const char * getPointer() const {
  190. return characters;
  191. }
  192. };
  193. // Helper functions to resolve ambiguity without constexpr if statements in C++ 14.
  194. String& impl_toStreamIndented_ascii(String& target, const char *value, const ReadableString& indentation);
  195. String& impl_toStreamIndented_utf32(String& target, const char32_t *value, const ReadableString& indentation);
  196. String& impl_toStreamIndented_readable(String& target, const ReadableString &value, const ReadableString& indentation);
  197. String& impl_toStreamIndented_double(String& target, const double &value, const ReadableString& indentation);
  198. String& impl_toStreamIndented_int64(String& target, const int64_t &value, const ReadableString& indentation);
  199. String& impl_toStreamIndented_uint64(String& target, const uint64_t &value, const ReadableString& indentation);
  200. // Resolving ambiguity without access to constexpr in if statements by disabling type safety with unsafeCast.
  201. template <typename T, DSR_ENABLE_IF(
  202. DSR_UTF32_LITERAL(T)
  203. || DSR_ASCII_LITERAL(T)
  204. || DSR_INHERITS_FROM(T, Printable)
  205. || DSR_SAME_TYPE(T, String)
  206. || DSR_SAME_TYPE(T, ReadableString)
  207. || DSR_SAME_TYPE(T, float)
  208. || DSR_SAME_TYPE(T, double)
  209. || DSR_SAME_TYPE(T, char)
  210. || DSR_SAME_TYPE(T, char32_t)
  211. || DSR_SAME_TYPE(T, bool)
  212. || DSR_SAME_TYPE(T, short)
  213. || DSR_SAME_TYPE(T, int)
  214. || DSR_SAME_TYPE(T, long)
  215. || DSR_SAME_TYPE(T, long long)
  216. || DSR_SAME_TYPE(T, unsigned short)
  217. || DSR_SAME_TYPE(T, unsigned int)
  218. || DSR_SAME_TYPE(T, unsigned long)
  219. || DSR_SAME_TYPE(T, unsigned long long)
  220. || DSR_SAME_TYPE(T, uint8_t)
  221. || DSR_SAME_TYPE(T, uint16_t)
  222. || DSR_SAME_TYPE(T, uint32_t)
  223. || DSR_SAME_TYPE(T, uint64_t)
  224. || DSR_SAME_TYPE(T, int8_t)
  225. || DSR_SAME_TYPE(T, int16_t)
  226. || DSR_SAME_TYPE(T, int32_t)
  227. || DSR_SAME_TYPE(T, int64_t))>
  228. inline String& string_toStreamIndented(String& target, const T &value, const ReadableString& indentation) {
  229. if (DSR_UTF32_LITERAL(T)) {
  230. impl_toStreamIndented_utf32(target, unsafeCast<char32_t*>(value), indentation);
  231. } else if (DSR_ASCII_LITERAL(T)) {
  232. impl_toStreamIndented_ascii(target, unsafeCast<char*>(value), indentation);
  233. } else if (DSR_INHERITS_FROM(T, Printable)) {
  234. unsafeCast<Printable>(value).toStreamIndented(target, indentation);
  235. } else if (DSR_SAME_TYPE(T, String)) {
  236. impl_toStreamIndented_readable(target, unsafeCast<String>(value), indentation);
  237. } else if (DSR_SAME_TYPE(T, ReadableString)) {
  238. impl_toStreamIndented_readable(target, unsafeCast<ReadableString>(value), indentation);
  239. } else if (DSR_SAME_TYPE(T, float)) {
  240. impl_toStreamIndented_double(target, (double)unsafeCast<float>(value), indentation);
  241. } else if (DSR_SAME_TYPE(T, double)) {
  242. impl_toStreamIndented_double(target, unsafeCast<double>(value), indentation);
  243. } else if (DSR_SAME_TYPE(T, char)) {
  244. impl_toStreamIndented_readable(target, indentation, U"");
  245. string_appendChar(target, unsafeCast<char>(value));
  246. } else if (DSR_SAME_TYPE(T, char32_t)) {
  247. impl_toStreamIndented_readable(target, indentation, U"");
  248. string_appendChar(target, unsafeCast<char32_t>(value));
  249. } else if (DSR_SAME_TYPE(T, bool)) {
  250. impl_toStreamIndented_utf32(target, unsafeCast<bool>(value) ? U"true" : U"false", indentation);
  251. } else if (DSR_SAME_TYPE(T, uint8_t)) {
  252. impl_toStreamIndented_uint64(target, (uint64_t)unsafeCast<uint8_t>(value), indentation);
  253. } else if (DSR_SAME_TYPE(T, uint16_t)) {
  254. impl_toStreamIndented_uint64(target, (uint64_t)unsafeCast<uint16_t>(value), indentation);
  255. } else if (DSR_SAME_TYPE(T, uint32_t)) {
  256. impl_toStreamIndented_uint64(target, (uint64_t)unsafeCast<uint32_t>(value), indentation);
  257. } else if (DSR_SAME_TYPE(T, uint64_t)) {
  258. impl_toStreamIndented_uint64(target, unsafeCast<uint64_t>(value), indentation);
  259. } else if (DSR_SAME_TYPE(T, int8_t)) {
  260. impl_toStreamIndented_int64(target, (int64_t)unsafeCast<int8_t>(value), indentation);
  261. } else if (DSR_SAME_TYPE(T, int16_t)) {
  262. impl_toStreamIndented_int64(target, (int64_t)unsafeCast<int16_t>(value), indentation);
  263. } else if (DSR_SAME_TYPE(T, int32_t)) {
  264. impl_toStreamIndented_int64(target, (int64_t)unsafeCast<int32_t>(value), indentation);
  265. } else if (DSR_SAME_TYPE(T, int64_t)) {
  266. impl_toStreamIndented_int64(target, unsafeCast<int64_t>(value), indentation);
  267. } else if (DSR_SAME_TYPE(T, short)) {
  268. impl_toStreamIndented_int64(target, (int64_t)unsafeCast<short>(value), indentation);
  269. } else if (DSR_SAME_TYPE(T, int)) {
  270. impl_toStreamIndented_int64(target, (int64_t)unsafeCast<int>(value), indentation);
  271. } else if (DSR_SAME_TYPE(T, long)) {
  272. impl_toStreamIndented_int64(target, (int64_t)unsafeCast<long>(value), indentation);
  273. } else if (DSR_SAME_TYPE(T, long long)) {
  274. static_assert(sizeof(long long) == 8, U"You need to implement integer printing for integers larger than 64 bits, or printing long long will be truncated!");
  275. impl_toStreamIndented_int64(target, (int64_t)unsafeCast<long long>(value), indentation);
  276. } else if (DSR_SAME_TYPE(T, unsigned short)) {
  277. impl_toStreamIndented_int64(target, (int64_t)unsafeCast<unsigned short>(value), indentation);
  278. } else if (DSR_SAME_TYPE(T, unsigned int)) {
  279. impl_toStreamIndented_int64(target, (int64_t)unsafeCast<unsigned int>(value), indentation);
  280. } else if (DSR_SAME_TYPE(T, unsigned long)) {
  281. impl_toStreamIndented_int64(target, (int64_t)unsafeCast<unsigned long>(value), indentation);
  282. } else if (DSR_SAME_TYPE(T, unsigned long long)) {
  283. static_assert(sizeof(unsigned long long) == 8, U"You need to implement integer printing for integers larger than 64 bits, or printing unsigned long long will be truncated!");
  284. impl_toStreamIndented_int64(target, (int64_t)unsafeCast<unsigned long long>(value), indentation);
  285. }
  286. return target;
  287. }
  288. template<typename T>
  289. String string_toStringIndented(const T& source, const ReadableString& indentation) {
  290. String result;
  291. string_toStreamIndented(result, source, indentation);
  292. return result;
  293. }
  294. template<typename T>
  295. String string_toString(const T& source) {
  296. String result;
  297. string_toStreamIndented(result, source, U"");
  298. return result;
  299. }
  300. // ---------------- Procedural API ----------------
  301. // Sets the target string's length to zero.
  302. // Because this opens up to appending new text where sub-string may already share the buffer,
  303. // this operation will reallocate the buffer if shared with other strings.
  304. void string_clear(String& target);
  305. // Post-condition: Returns the length of source.
  306. // Example: string_length(U"ABC") == 3
  307. intptr_t string_length(const ReadableString& source);
  308. // Post-condition: Returns the base-zero index of source's first occurence of toFind, starting from startIndex. Returns -1 if not found.
  309. // Example: string_findFirst(U"ABCABCABC", U'A') == 0
  310. // Example: string_findFirst(U"ABCABCABC", U'B') == 1
  311. // Example: string_findFirst(U"ABCABCABC", U'C') == 2
  312. // Example: string_findFirst(U"ABCABCABC", U'D') == -1
  313. intptr_t string_findFirst(const ReadableString& source, DsrChar toFind, intptr_t startIndex = 0);
  314. // Post-condition: Returns the base-zero index of source's last occurence of toFind. Returns -1 if not found.
  315. // Example: string_findLast(U"ABCABCABC", U'A') == 6
  316. // Example: string_findLast(U"ABCABCABC", U'B') == 7
  317. // Example: string_findLast(U"ABCABCABC", U'C') == 8
  318. // Example: string_findLast(U"ABCABCABC", U'D') == -1
  319. intptr_t string_findLast(const ReadableString& source, DsrChar toFind);
  320. // Post-condition: Returns a sub-string of source from before the character at inclusiveStart to before the character at exclusiveEnd
  321. // Example: string_exclusiveRange(U"0123456789", 2, 4) == U"23"
  322. ReadableString string_exclusiveRange(const ReadableString& source, intptr_t inclusiveStart, intptr_t exclusiveEnd);
  323. // Post-condition: Returns a sub-string of source from before the character at inclusiveStart to after the character at inclusiveEnd
  324. // Example: string_inclusiveRange(U"0123456789", 2, 4) == U"234"
  325. ReadableString string_inclusiveRange(const ReadableString& source, intptr_t inclusiveStart, intptr_t inclusiveEnd);
  326. // Post-condition: Returns a sub-string of source from the start to before the character at exclusiveEnd
  327. // Example: string_before(U"0123456789", 5) == U"01234"
  328. ReadableString string_before(const ReadableString& source, intptr_t exclusiveEnd);
  329. // Post-condition: Returns a sub-string of source from the start to after the character at inclusiveEnd
  330. // Example: string_until(U"0123456789", 5) == U"012345"
  331. ReadableString string_until(const ReadableString& source, intptr_t inclusiveEnd);
  332. // Post-condition: Returns a sub-string of source from before the character at inclusiveStart to the end
  333. // Example: string_from(U"0123456789", 5) == U"56789"
  334. ReadableString string_from(const ReadableString& source, intptr_t inclusiveStart);
  335. // Post-condition: Returns a sub-string of source from after the character at exclusiveStart to the end
  336. // Example: string_after(U"0123456789", 5) == U"6789"
  337. ReadableString string_after(const ReadableString& source, intptr_t exclusiveStart);
  338. // Split source into a list of strings.
  339. // Post-condition:
  340. // Returns a list of strings from source by splitting along separator.
  341. // If removeWhiteSpace is true then surrounding white-space will be removed, otherwise all white-space is kept.
  342. // The separating characters are excluded from the resulting strings.
  343. // The number of strings returned in the list will equal the number of separating characters plus one, so the result may contain empty strings.
  344. // Each string in the list clones content to its own dynamic buffer. Use string_split_callback if you don't need long term storage.
  345. List<String> string_split(const ReadableString& source, DsrChar separator, bool removeWhiteSpace = false);
  346. // Split a string without needing a list to store the result.
  347. // Use string_splitCount on the same source and separator if you need to know the element count in advance.
  348. // Side-effects:
  349. // Calls action for each sub-string divided by separator in source given as the separatedText argument.
  350. void string_split_callback(std::function<void(ReadableString separatedText)> action, const ReadableString& source, DsrChar separator, bool removeWhiteSpace = false);
  351. // An alternative overload for having a very long lambda at the end.
  352. inline void string_split_callback(const ReadableString& source, DsrChar separator, bool removeWhiteSpace, std::function<void(ReadableString separatedText)> action) {
  353. string_split_callback(action, source, separator, removeWhiteSpace);
  354. }
  355. // Split source using separator, only to return the number of splits.
  356. // Useful for pre-allocation.
  357. intptr_t string_splitCount(const ReadableString& source, DsrChar separator);
  358. // Post-condition: Returns true iff c is a digit.
  359. // Digit <- '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9'
  360. bool character_isDigit(DsrChar c);
  361. // Post-condition: Returns true iff c is an integer character.
  362. // IntegerCharacter <- '-' | '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9'
  363. bool character_isIntegerCharacter(DsrChar c);
  364. // Post-condition: Returns true iff c is a value character.
  365. // ValueCharacter <- '.' | '-' | '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9'
  366. bool character_isValueCharacter(DsrChar c);
  367. // Post-condition: Returns true iff c is a white-space character.
  368. // WhiteSpace <- ' ' | '\t' | '\v' | '\f' | '\n' | '\r'
  369. // Null terminators are excluded, because it's reserved for out of bound results.
  370. bool character_isWhiteSpace(DsrChar c);
  371. // Post-condition: Returns true iff source is a valid integer. IntegerAllowingWhiteSpace is also allowed iff allowWhiteSpace is true.
  372. // UnsignedInteger <- Digit+
  373. // Integer <- UnsignedInteger | '-' UnsignedInteger
  374. // IntegerAllowingWhiteSpace <- WhiteSpace* Integer WhiteSpace*
  375. bool string_isInteger(const ReadableString& source, bool allowWhiteSpace = true);
  376. // Post-condition: Returns true iff source is a valid integer or decimal number. DoubleAllowingWhiteSpace is also allowed iff allowWhiteSpace is true.
  377. // UnsignedDouble <- Digit+ | Digit* '.' Digit+
  378. // Double <- UnsignedDouble | '-' UnsignedDouble
  379. // DoubleAllowingWhiteSpace <- WhiteSpace* Double WhiteSpace*
  380. // Only dots are allowed as decimals.
  381. // Because being able to read files from another country without crashes is a lot more important than a detail that most people don't even notice.
  382. // Automatic nationalization made sense when most applications were written in-house before the internet existed.
  383. bool string_isDouble(const ReadableString& source, bool allowWhiteSpace = true);
  384. // Pre-condition: source must be a valid integer according to string_isInteger. Otherwise unexpected characters are simply ignored.
  385. // Post-condition: Returns the integer representation of source.
  386. // The result is signed, because the input might unexpectedly have a negation sign.
  387. // The result is large, so that one can easily check the range before assigning to a smaller integer type.
  388. int64_t string_toInteger(const ReadableString& source);
  389. // Side-effect: Appends value as a base ten integer at the end of target.
  390. void string_fromUnsigned(String& target, uint64_t value);
  391. // Post-condition: Returns value written as a base ten integer.
  392. inline String string_fromUnsigned(int64_t value) {
  393. String result; string_fromUnsigned(result, value); return result;
  394. }
  395. // Side-effect: Appends value as a base ten integer at the end of target.
  396. void string_fromSigned(String& target, int64_t value, DsrChar negationCharacter = U'-');
  397. // Post-condition: Returns value written as a base ten integer.
  398. inline String string_fromSigned(int64_t value, DsrChar negationCharacter = U'-') {
  399. String result; string_fromSigned(result, value, negationCharacter); return result;
  400. }
  401. // Pre-condition: source must be a valid double according to string_isDouble. Otherwise unexpected characters are simply ignored.
  402. // Post-condition: Returns the double precision floating-point representation of source.
  403. double string_toDouble(const ReadableString& source);
  404. // Side-effect: Appends value as a base ten decimal number at the end of target.
  405. void string_fromDouble(String& target, double value, int decimalCount = 6, bool removeTrailingZeroes = true, DsrChar decimalCharacter = U'.', DsrChar negationCharacter = U'-');
  406. // Post-condition: Returns value written as a base ten decimal number.
  407. inline String string_fromDouble(double value, int decimalCount = 6, bool removeTrailingZeroes = true, DsrChar decimalCharacter = U'.', DsrChar negationCharacter = U'-') {
  408. String result; string_fromDouble(result, value, decimalCount, removeTrailingZeroes, decimalCharacter, negationCharacter); return result;
  409. }
  410. // Loading will try to find a byte order mark and can handle UTF-8 and UTF-16.
  411. // Failure to find a byte order mark will assume that the file's content is raw Latin-1,
  412. // because automatic detection would cause random behaviour.
  413. // For portability, carriage return characters are removed,
  414. // but will be generated again using the default CrLf line encoding of string_save.
  415. // Post-condition:
  416. // Returns the content of the file referred to be filename.
  417. // If mustExist is true, then failure to load will throw an exception.
  418. // If mustExist is false, then failure to load will return an empty string.
  419. // If you want to handle files that are not found in a different way,
  420. // it is easy to use buffer_load and string_loadFromMemory separatelly.
  421. String string_load(const ReadableString& filename, bool mustExist = true);
  422. // Decode a text file from a buffer, which can be loaded using buffer_load.
  423. String string_loadFromMemory(Buffer fileContent);
  424. // Decode a null terminated string without BOM, by specifying which format it was encoded in.
  425. // Pre-conditions:
  426. // data does not start with any byte-order-mark (BOM).
  427. // data must be null terminated with '\0' in whatever format is being used. Otherwise you may have random crashes
  428. // Post-condition:
  429. // Returns a string decoded from the raw data.
  430. String string_dangerous_decodeFromData(const void* data, CharacterEncoding encoding);
  431. // Side-effect: Saves content to filename using the selected character and line encodings.
  432. // Post-condition: Returns true on success and false on failure.
  433. // Do not add carriage return characters yourself into strings, for these will be added automatically in the CrLf mode.
  434. // The internal String type should only use UTF-32 with single line feeds for breaking lines.
  435. // This makes text processing algorithms a lot cleaner when a character or line break is always one element.
  436. // UTF-8 with BOM is default by being both compact and capable of storing 21 bits of unicode.
  437. bool string_save(const ReadableString& filename, const ReadableString& content,
  438. CharacterEncoding characterEncoding = CharacterEncoding::BOM_UTF8,
  439. LineEncoding lineEncoding = LineEncoding::CrLf
  440. );
  441. // Encode the string and keep the raw buffer instead of saving it to a file.
  442. // Disabling writeByteOrderMark can be done when the result is casted to a native string for platform specific APIs, where a BOM is not allowed.
  443. // Enabling writeNullTerminator should be done when using the result as a pointer, so that the length is known when the buffer does not have padding.
  444. Buffer string_saveToMemory(const ReadableString& content,
  445. CharacterEncoding characterEncoding = CharacterEncoding::BOM_UTF8,
  446. LineEncoding lineEncoding = LineEncoding::CrLf,
  447. bool writeByteOrderMark = true,
  448. bool writeNullTerminator = false
  449. );
  450. // Post-condition: Returns true iff strings a and b are exactly equal.
  451. bool string_match(const ReadableString& a, const ReadableString& b);
  452. // Post-condition: Returns true iff strings a and b are roughly equal using a case insensitive match.
  453. bool string_caseInsensitiveMatch(const ReadableString& a, const ReadableString& b);
  454. // While string_match should be preferred over == for code readability and consistency with string_caseInsensitiveMatch,
  455. // the equality operator might be called automatically from template methods when a template type is a string.
  456. inline bool operator==(const ReadableString& a, const ReadableString& b) { return string_match(a, b); }
  457. inline bool operator!=(const ReadableString& a, const ReadableString& b) { return !string_match(a, b); }
  458. // Post-condition: Returns text converted to upper case.
  459. String string_upperCase(const ReadableString &text);
  460. // Post-condition: Returns text converted to lower case.
  461. String string_lowerCase(const ReadableString &text);
  462. // Post-condition: Returns a sub-set of text without surrounding white-space (space, tab and carriage-return).
  463. ReadableString string_removeOuterWhiteSpace(const ReadableString &text);
  464. // Post-condition: Returns rawText wrapped in a quote.
  465. // Special characters are included using escape characters, so that one can quote multiple lines but store it easily.
  466. String string_mangleQuote(const ReadableString &rawText);
  467. // Pre-condition: mangledText must be enclosed in double quotes and special characters must use escape characters (tabs in quotes are okay though).
  468. // Post-condition: Returns mangledText with quotes removed and excape tokens interpreted.
  469. String string_unmangleQuote(const ReadableString& mangledText);
  470. // Post-condition: Returns the number of strings using the same buffer, including itself.
  471. uintptr_t string_getBufferUseCount(const ReadableString& text);
  472. // Ensures safely that at least minimumLength characters can he held in the buffer
  473. void string_reserve(String& target, intptr_t minimumLength);
  474. // Append/push one character (to avoid integer to string conversion)
  475. void string_appendChar(String& target, DsrChar value);
  476. // Append elements
  477. inline void string_append(String& target) {}
  478. template<typename HEAD, typename... TAIL>
  479. inline void string_append(String& target, HEAD head, TAIL&&... tail) {
  480. string_toStreamIndented(target, head, U"");
  481. string_append(target, tail...);
  482. }
  483. // Combine a number of strings, characters and numbers
  484. // If an input type is rejected, create a Printable object to wrap around it
  485. template<typename... ARGS>
  486. inline String string_combine(ARGS&&... args) {
  487. String result;
  488. string_append(result, args...);
  489. return result;
  490. }
  491. // ---------------- Infix syntax ----------------
  492. // Operations
  493. inline String operator+ (const ReadableString& a, const ReadableString& b) { return string_combine(a, b); }
  494. inline String operator+ (const DsrChar* a, const ReadableString& b) { return string_combine(a, b); }
  495. inline String operator+ (const ReadableString& a, const DsrChar* b) { return string_combine(a, b); }
  496. inline String operator+ (const String& a, const String& b) { return string_combine(a, b); }
  497. inline String operator+ (const DsrChar* a, const String& b) { return string_combine(a, b); }
  498. inline String operator+ (const String& a, const DsrChar* b) { return string_combine(a, b); }
  499. inline String operator+ (const String& a, const ReadableString& b) { return string_combine(a, b); }
  500. inline String operator+ (const ReadableString& a, const String& b) { return string_combine(a, b); }
  501. // ---------------- Message handling ----------------
  502. enum class MessageType {
  503. Error, // Terminate as quickly as possible after saving and informing the user.
  504. Warning, // Inform the user but let the caller continue.
  505. StandardPrinting, // Print text to the terminal.
  506. DebugPrinting // Print debug information to the terminal, if debug mode is active.
  507. };
  508. // Get a reference to the thread-local buffer used for printing messages.
  509. // Can be combined with string_clear, string_append and string_sendMessage to send long messages in a thread-safe way.
  510. // Clear, fill and send.
  511. String &string_getPrintBuffer();
  512. // Send a message
  513. void string_sendMessage(const ReadableString &message, MessageType type);
  514. // Send a message directly to the default message handler, ignoring string_assignMessageHandler.
  515. void string_sendMessage_default(const ReadableString &message, MessageType type);
  516. // Get a message
  517. // Pre-condition:
  518. // The action function must throw an exception or terminate the program when given an error, otherwise string_sendMessage will throw an exception about failing to do so.
  519. // Do not call string_sendMessage directly or indirectly from within action, use string_sendMessage_default instead to avoid infinite recursion.
  520. // Terminating the program as soon as possible is ideal, but one might want to save a backup or show what went wrong in a graphical interface before terminating.
  521. // Do not throw and catch errors as if they were warnings, because throwing and catching creates a partial transaction, potentially violating type invariants.
  522. // Better to use warnings and let the sender of the warning figure out how to abort the action safely.
  523. void string_assignMessageHandler(std::function<void(const ReadableString &message, MessageType type)> action);
  524. // Undo string_assignMessageHandler, so that any messages will be handled the default way again.
  525. void string_unassignMessageHandler();
  526. // Throw an error, which must terminate the application or throw an error
  527. template<typename... ARGS>
  528. void throwError(ARGS... args) {
  529. String *target = &(string_getPrintBuffer());
  530. string_clear(*target);
  531. string_append(*target, args...);
  532. string_sendMessage(*target, MessageType::Error);
  533. }
  534. // Send a warning, which might throw an exception, terminate the application or anything else that the application requests using string_handleMessages
  535. template<typename... ARGS>
  536. void sendWarning(ARGS... args) {
  537. String *target = &(string_getPrintBuffer());
  538. string_clear(*target);
  539. string_append(*target, args...);
  540. string_sendMessage(*target, MessageType::Warning);
  541. }
  542. // Print information to the terminal or something else listening for messages using string_handleMessages
  543. template<typename... ARGS>
  544. void printText(ARGS... args) {
  545. String *target = &(string_getPrintBuffer());
  546. string_clear(*target);
  547. string_append(*target, args...);
  548. string_sendMessage(*target, MessageType::StandardPrinting);
  549. }
  550. // Debug messages are automatically disabled in release mode, so that you don't have to worry about accidentally releasing a program with poor performance from constantly printing to the terminal
  551. // Useful for selectively printing the most important information accumulated over time
  552. // Less useful for profiling, because the debug mode is slower than the release mode
  553. #ifdef NDEBUG
  554. // Supress debugText in release mode
  555. template<typename... ARGS>
  556. void debugText(ARGS... args) {}
  557. #else
  558. // Print debugText in debug mode
  559. template<typename... ARGS>
  560. void debugText(ARGS... args) {
  561. String *target = &(string_getPrintBuffer());
  562. string_clear(*target);
  563. string_append(*target, args...);
  564. string_sendMessage(*target, MessageType::DebugPrinting);
  565. }
  566. #endif
  567. }
  568. #endif