| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359 |
- // zlib open source license
- //
- // Copyright (c) 2017 to 2025 David Forsgren Piuva
- //
- // This software is provided 'as-is', without any express or implied
- // warranty. In no event will the authors be held liable for any damages
- // arising from the use of this software.
- //
- // Permission is granted to anyone to use this software for any purpose,
- // including commercial applications, and to alter it and redistribute it
- // freely, subject to the following restrictions:
- //
- // 1. The origin of this software must not be misrepresented; you must not
- // claim that you wrote the original software. If you use this software
- // in a product, an acknowledgment in the product documentation would be
- // appreciated but is not required.
- //
- // 2. Altered source versions must be plainly marked as such, and must not be
- // misrepresented as being the original software.
- //
- // 3. This notice may not be removed or altered from any source
- // distribution.
- // Gets access to private members by making them public for the whole module
- #define DSR_INTERNAL_ACCESS
- #include <iostream>
- #include <sstream>
- #include <fstream>
- #include <streambuf>
- #include <thread>
- #include <mutex>
- #include <stdexcept>
- #include <cmath>
- #include "stringAPI.h"
- #include "../api/fileAPI.h"
- #include "../settings.h"
- using namespace dsr;
- // The print buffer keeps its buffer size from previous printing to avoid reallocating memory every time something is printed.
- // It is stored separatelly for each calling thread to avoid conflicts.
- static thread_local String printBuffer;
- String &dsr::string_getPrintBuffer() {
- return printBuffer;
- }
- static void atomic_append_ascii(String &target, const char* source);
- static void atomic_append_readable(String &target, const ReadableString& source);
- static void atomic_append_utf32(String &target, const DsrChar* source);
- static intptr_t strlen_utf32(const DsrChar *content) {
- intptr_t length = 0;
- while (content[length] != 0) {
- length++;
- }
- return length;
- }
- static char toAscii(DsrChar c) {
- if (c > 127) {
- return '?';
- } else {
- return c;
- }
- }
- ReadableString::ReadableString(const DsrChar *content)
- : view(content, strlen_utf32(content)) {}
- String::String() {}
- String::String(const char* source) { atomic_append_ascii(*this, source); }
- String::String(const DsrChar* source) { atomic_append_utf32(*this, source); }
- String& Printable::toStream(String& target) const {
- return this->toStreamIndented(target, U"");
- }
- String Printable::toStringIndented(const ReadableString& indentation) const {
- String result;
- this->toStreamIndented(result, indentation);
- return result;
- }
- String Printable::toString() const {
- return this->toStringIndented(U"");
- }
- Printable::~Printable() {}
- // TODO: Handle the remaining Unicode characters after ȳ (563).
- DsrChar dsr::character_upperCase(DsrChar character) {
- if (U'a' <= character && character <= U'z') { // a (97) to z (122) Ascii
- return character - (U'a' - U'A');
- } else if (U'à' <= character && character <= U'ö') { // à (224) to ö (246) Latin-1
- return character - (U'à' - U'À');
- } else if (U'ø' <= character && character <= U'þ') { // ø (248) to þ (254) Latin-1
- return character - (U'ø' - U'Ø');
- } else if (character == U'ÿ') { // ÿ (255) Latin Extended-A
- return U'Ÿ'; // Ÿ (376)
- } else if (U'Ā' <= character && character <= U'ķ') { // Ā (256) to ķ (311) Latin Extended-A
- return character & ~DsrChar(1);
- } else if (U'Ĺ' <= character && character <= U'ň' && !(character & 1)) { // Even from Ĺ (313) to ň (328) Latin Extended-A
- return character - 1;
- } else if (U'Ŋ' <= character && character <= U'ŷ') { // Ŋ (330) to ŷ (375) Latin Extended-A
- return character & ~DsrChar(1);
- } else if (character == U'ź') { // ź (378) Latin Extended-A
- return U'Ź'; // Ź (377)
- } else if (character == U'ż') { // ż (380) Latin Extended-A
- return U'Ż'; // Ż (379)
- } else if (character == U'ž') { // ž (382) Latin Extended-A
- return U'Ž'; // Ž (381)
- } else if (character == U'ƃ') { // ƃ (387) Latin Extended-B
- return U'Ƃ'; // Ƃ (386)
- } else if (character == U'ƅ') { // ƅ (389) Latin Extended-B
- return U'Ƅ'; // Ƅ (388)
- } else if (character == U'ƈ') { // ƈ (392) Latin Extended-B
- return U'Ƈ'; // Ƈ (391)
- } else if (character == U'ƌ') { // ƌ (396) Latin Extended-B
- return U'Ƌ'; // Ƌ (395)
- } else if (character == U'ƒ') { // ƒ (402) Latin Extended-B
- return U'Ƒ'; // Ƒ (401)
- } else if (character == U'ƙ') { // ƙ (409) Latin Extended-B
- return U'Ƙ'; // Ƙ (408)
- } else if (character == U'ơ') { // ơ (417) Latin Extended-B
- return U'Ơ'; // Ơ (416)
- } else if (character == U'ƣ') { // ƣ (419) Latin Extended-B
- return U'Ƣ'; // Ƣ (418)
- } else if (character == U'ƥ') { // ƥ (421) Latin Extended-B
- return U'Ƥ'; // Ƥ (420)
- } else if (character == U'ƨ') { // ƨ (424) Latin Extended-B
- return U'Ƨ'; // Ƨ (423)
- } else if (character == U'ƭ') { // ƭ (429) Latin Extended-B
- return U'Ƭ'; // Ƭ (428)
- } else if (character == U'ư') { // ư (432) Latin Extended-B
- return U'Ư'; // Ư (431)
- } else if (character == U'ƴ') { // ƴ (436) Latin Extended-B
- return U'Ƴ'; // Ƴ (435)
- } else if (character == U'ƶ') { // ƶ (438) Latin Extended-B
- return U'Ƶ'; // Ƶ (437)
- } else if (character == U'ƹ') { // ƹ (441) Latin Extended-B
- return U'Ƹ'; // Ƹ (440)
- } else if (character == U'ƽ') { // ƽ (445) Latin Extended-B
- return U'Ƽ'; // Ƽ (444)
- } else if (character == U'dž' || character == U'Dž') { // dž, Dž (454, 453) Latin Extended-B
- return U'DŽ'; // DŽ (454)
- } else if (character == U'lj' || character == U'Lj') { // lj, Lj (457, 456) Latin Extended-B
- return U'LJ'; // LJ (457)
- } else if (character == U'nj' || character == U'Nj') { // nj, Nj (460, 459) Latin Extended-B
- return U'NJ'; // NJ (460)
- } else if (U'Ǎ' <= character && character <= U'ǜ' && !(character & 1)) { // Even from Ǎ (461) to ǜ (476) Latin Extended-B Pinyin
- return character - 1;
- // Unhandled: ǝ (477)
- } else if (U'Ǟ' <= character && character <= U'ǯ') { // Ǟ (478) to ǯ (495) Latin Extended-B
- return character & ~DsrChar(1);
- // Unhandled: ǰ (496)
- } else if (character == U'dz' || character == U'Dz') { // dž, Dž (499, 498) Latin Extended-B
- return U'DZ'; // DŽ (497)
- } else if (character == U'ǵ') { // ǵ (501) Latin Extended-B
- return U'Ǵ'; // Ǵ (500)
- // Unhandled: Ƕ Ƿ
- } else if (U'Ǹ' <= character && character <= U'ȳ') { // Ǹ (504) to ȳ (563) Latin Extended-B
- return character & ~DsrChar(1);
- } else {
- return character;
- }
- }
- DsrChar dsr::character_lowerCase(DsrChar character) {
- if (U'A' <= character && character <= U'Z') { // A (65) to Z (90) Ascii
- return character + (U'a' - U'A');
- } else if (U'À' <= character && character <= U'Ö') { // À (192) to Ö (214) Latin-1
- return character + (U'à' - U'À');
- } else if (U'Ø' <= character && character <= U'Þ') { // Ø (216) to Þ (222) Latin-1
- return character + (U'ø' - U'Ø');
- } else if (character == U'Ÿ') { // Ÿ (376) Latin Extended-A
- return U'ÿ'; // ÿ (255)
- } else if (U'Ā' <= character && character <= U'ķ') { // Ā (256) to ķ (311) Latin Extended-A
- return character | DsrChar(1);
- } else if (U'Ĺ' <= character && character <= U'ň' && character & 1) { // Odd from Ĺ (313) to ň (328) Latin Extended-A
- return character + 1;
- } else if (U'Ŋ' <= character && character <= U'ŷ') { // Ŋ (330) to ŷ (375) Latin Extended-A
- return character | DsrChar(1);
- } else if (character == U'Ź') { // Ź (377) Latin Extended-A
- return U'ź'; // ź (378)
- } else if (character == U'Ż') { // Ż (379) Latin Extended-A
- return U'ż'; // ż (380)
- } else if (character == U'Ž') { // Ž (381) Latin Extended-A
- return U'ž'; // ž (382)
- } else if (character == U'Ƃ') { // Ƃ (386) Latin Extended-B
- return U'ƃ'; // ƃ (387)
- } else if (character == U'Ƅ') { // Ƅ (388) Latin Extended-B
- return U'ƅ'; // ƅ (389)
- } else if (character == U'Ƈ') { // Ƈ (391) Latin Extended-B
- return U'ƈ'; // ƈ (392)
- } else if (character == U'Ƌ') { // Ƌ (395) Latin Extended-B
- return U'ƌ'; // ƌ (396)
- } else if (character == U'Ƒ') { // Ƒ (401) Latin Extended-B
- return U'ƒ'; // ƒ (402)
- } else if (character == U'Ƙ') { // Ƙ (408) Latin Extended-B
- return U'ƙ'; // ƙ (409)
- } else if (character == U'Ơ') { // Ơ (416) Latin Extended-B
- return U'ơ'; // ơ (417)
- } else if (character == U'Ƣ') { // Ƣ (418) Latin Extended-B
- return U'ƣ'; // ƣ (419)
- } else if (character == U'Ƥ') { // Ƥ (420) Latin Extended-B
- return U'ƥ'; // ƥ (421)
- } else if (character == U'Ƨ') { // Ƨ (423) Latin Extended-B
- return U'ƨ'; // ƨ (424)
- } else if (character == U'Ƭ') { // Ƭ (428) Latin Extended-B
- return U'ƭ'; // ƭ (429)
- } else if (character == U'Ư') { // Ư (431) Latin Extended-B
- return U'ư'; // ư (432)
- } else if (character == U'Ƴ') { // Ƴ (435) Latin Extended-B
- return U'ƴ'; // ƴ (436)
- } else if (character == U'Ƶ') { // Ƶ (437) Latin Extended-B
- return U'ƶ'; // ƶ (438)
- } else if (character == U'Ƹ') { // Ƹ (440) Latin Extended-B
- return U'ƹ'; // ƹ (441)
- } else if (character == U'Ƽ') { // Ƽ (444) Latin Extended-B
- return U'ƽ'; // ƽ (445)
- } else if (character == U'DŽ' || character == U'Dž') { // DŽ, Dž (452, 453) Latin Extended-B
- return U'dž'; // dž (454)
- } else if (character == U'LJ' || character == U'Lj') { // LJ, Lj (455, 456) Latin Extended-B
- return U'lj'; // lj (457)
- } else if (character == U'NJ' || character == U'Nj') { // NJ, Nj (458, 459) Latin Extended-B
- return U'nj'; // nj (460)
- } else if (U'Ǎ' <= character && character <= U'ǜ' && character & 1) { // Odd from Ǎ (461) to ǜ (476) Latin Extended-B Pinyin
- return character + 1;
- } else if (U'Ǟ' <= character && character <= U'ǯ') { // Ǟ (478) to ǯ (495) Latin Extended-B
- return character & DsrChar(1);
- } else if (character == U'DZ' || character == U'Dz') { // DŽ, Dž (497, 498) Latin Extended-B
- return U'dz'; // DŽdz499)
- } else if (character == U'Ǵ') { // Ǵ (500) Latin Extended-B
- return U'ǵ'; // ǵ (501)
- } else if (U'Ǹ' <= character && character <= U'ȳ') { // Ǹ (504) to ȳ (563) Latin Extended-B
- return character & DsrChar(1);
- } else {
- return character;
- }
- }
- String dsr::string_upperCase(const ReadableString &text) {
- String result;
- string_reserve(result, text.view.length);
- for (intptr_t i = 0; i < text.view.length; i++) {
- string_appendChar(result, character_upperCase(text[i]));
- }
- return result;
- }
- String dsr::string_lowerCase(const ReadableString &text) {
- String result;
- string_reserve(result, text.view.length);
- for (intptr_t i = 0; i < text.view.length; i++) {
- string_appendChar(result, character_lowerCase(text[i]));
- }
- return result;
- }
- bool dsr::string_match(const ReadableString& a, const ReadableString& b) {
- if (a.view.length != b.view.length) {
- return false;
- } else {
- for (intptr_t i = 0; i < a.view.length; i++) {
- if (a[i] != b[i]) {
- return false;
- }
- }
- return true;
- }
- }
- bool dsr::string_caseInsensitiveMatch(const ReadableString& a, const ReadableString& b) {
- if (a.view.length != b.view.length) {
- return false;
- } else {
- for (intptr_t i = 0; i < a.view.length; i++) {
- if (character_upperCase(a[i]) != character_upperCase(b[i])) {
- return false;
- }
- }
- return true;
- }
- }
- static intptr_t findFirstNonWhite(const ReadableString &text) {
- for (intptr_t i = 0; i < text.view.length; i++) {
- DsrChar c = text[i];
- if (!character_isWhiteSpace(c)) {
- return i;
- }
- }
- return -1;
- }
- static intptr_t findLastNonWhite(const ReadableString &text) {
- for (intptr_t i = text.view.length - 1; i >= 0; i--) {
- DsrChar c = text[i];
- if (!character_isWhiteSpace(c)) {
- return i;
- }
- }
- return -1;
- }
- // Allow passing literals without allocating heap memory for the result
- ReadableString dsr::string_removeOuterWhiteSpace(const ReadableString &text) {
- intptr_t first = findFirstNonWhite(text);
- intptr_t last = findLastNonWhite(text);
- if (first == -1) {
- // Only white space
- return ReadableString();
- } else {
- // Subset
- return string_inclusiveRange(text, first, last);
- }
- }
- String dsr::string_mangleQuote(const ReadableString &rawText) {
- String result;
- string_reserve(result, rawText.view.length + 2);
- string_appendChar(result, U'\"'); // Begin quote
- for (intptr_t i = 0; i < rawText.view.length; i++) {
- DsrChar c = rawText[i];
- if (c == U'\"') { // Double quote
- string_append(result, U"\\\"");
- } else if (c == U'\\') { // Backslash
- string_append(result, U"\\\\");
- } else if (c == U'\a') { // Audible bell
- string_append(result, U"\\a");
- } else if (c == U'\b') { // Backspace
- string_append(result, U"\\b");
- } else if (c == U'\f') { // Form feed
- string_append(result, U"\\f");
- } else if (c == U'\n') { // Line feed
- string_append(result, U"\\n");
- } else if (c == U'\r') { // Carriage return
- string_append(result, U"\\r");
- } else if (c == U'\t') { // Horizontal tab
- string_append(result, U"\\t");
- } else if (c == U'\v') { // Vertical tab
- string_append(result, U"\\v");
- } else if (c == U'\0') { // Null terminator
- string_append(result, U"\\0");
- } else {
- string_appendChar(result, c);
- }
- }
- string_appendChar(result, U'\"'); // End quote
- return result;
- }
- String dsr::string_unmangleQuote(const ReadableString& mangledText) {
- intptr_t firstQuote = string_findFirst(mangledText, '\"');
- intptr_t lastQuote = string_findLast(mangledText, '\"');
- String result;
- if (firstQuote == -1 || lastQuote == -1 || firstQuote == lastQuote) {
- throwError(U"Cannot unmangle using string_unmangleQuote without beginning and ending with quote signs!\n", mangledText, U"\n");
- } else {
- for (intptr_t i = firstQuote + 1; i < lastQuote; i++) {
- DsrChar c = mangledText[i];
- if (c == U'\\') { // Escape character
- DsrChar c2 = mangledText[i + 1];
- if (c2 == U'\"') { // Double quote
- string_appendChar(result, U'\"');
- } else if (c2 == U'\\') { // Back slash
- string_appendChar(result, U'\\');
- } else if (c2 == U'a') { // Audible bell
- string_appendChar(result, U'\a');
- } else if (c2 == U'b') { // Backspace
- string_appendChar(result, U'\b');
- } else if (c2 == U'f') { // Form feed
- string_appendChar(result, U'\f');
- } else if (c2 == U'n') { // Line feed
- string_appendChar(result, U'\n');
- } else if (c2 == U'r') { // Carriage return
- string_appendChar(result, U'\r');
- } else if (c2 == U't') { // Horizontal tab
- string_appendChar(result, U'\t');
- } else if (c2 == U'v') { // Vertical tab
- string_appendChar(result, U'\v');
- } else if (c2 == U'0') { // Null terminator
- string_appendChar(result, U'\0');
- }
- i++; // Consume both characters
- } else {
- // Detect bad input
- if (c == U'\"') { // Double quote
- throwError(U"Unmangled double quote sign detected in string_unmangleQuote!\n", mangledText, U"\n");
- } else if (c == U'\a') { // Audible bell
- throwError(U"Unmangled audible bell detected in string_unmangleQuote!\n", mangledText, U"\n");
- } else if (c == U'\b') { // Backspace
- throwError(U"Unmangled backspace detected in string_unmangleQuote!\n", mangledText, U"\n");
- } else if (c == U'\f') { // Form feed
- throwError(U"Unmangled form feed detected in string_unmangleQuote!\n", mangledText, U"\n");
- } else if (c == U'\n') { // Line feed
- throwError(U"Unmangled line feed detected in string_unmangleQuote!\n", mangledText, U"\n");
- } else if (c == U'\r') { // Carriage return
- throwError(U"Unmangled carriage return detected in string_unmangleQuote!\n", mangledText, U"\n");
- } else if (c == U'\0') { // Null terminator
- throwError(U"Unmangled null terminator detected in string_unmangleQuote!\n", mangledText, U"\n");
- } else {
- string_appendChar(result, c);
- }
- }
- }
- }
- return result;
- }
- void dsr::string_fromUnsigned(String& target, uint64_t value) {
- static const int bufferSize = 20;
- DsrChar digits[bufferSize];
- int64_t usedSize = 0;
- if (value == 0) {
- string_appendChar(target, U'0');
- } else {
- while (usedSize < bufferSize) {
- DsrChar digit = U'0' + (value % 10u);
- digits[usedSize] = digit;
- usedSize++;
- value /= 10u;
- if (value == 0) {
- break;
- }
- }
- while (usedSize > 0) {
- usedSize--;
- string_appendChar(target, digits[usedSize]);
- }
- }
- }
- void dsr::string_fromSigned(String& target, int64_t value, DsrChar negationCharacter) {
- if (value >= 0) {
- string_fromUnsigned(target, (uint64_t)value);
- } else {
- string_appendChar(target, negationCharacter);
- string_fromUnsigned(target, (uint64_t)(-value));
- }
- }
- static const int MAX_DECIMALS = 16;
- static double decimalMultipliers[MAX_DECIMALS] = {
- 10.0,
- 100.0,
- 1000.0,
- 10000.0,
- 100000.0,
- 1000000.0,
- 10000000.0,
- 100000000.0,
- 1000000000.0,
- 10000000000.0,
- 100000000000.0,
- 1000000000000.0,
- 10000000000000.0,
- 100000000000000.0,
- 1000000000000000.0,
- 10000000000000000.0
- };
- static double roundingOffsets[MAX_DECIMALS] = {
- 0.05,
- 0.005,
- 0.0005,
- 0.00005,
- 0.000005,
- 0.0000005,
- 0.00000005,
- 0.000000005,
- 0.0000000005,
- 0.00000000005,
- 0.000000000005,
- 0.0000000000005,
- 0.00000000000005,
- 0.000000000000005,
- 0.0000000000000005,
- 0.00000000000000005
- };
- static uint64_t decimalLimits[MAX_DECIMALS] = {
- 9,
- 99,
- 999,
- 9999,
- 99999,
- 999999,
- 9999999,
- 99999999,
- 999999999,
- 9999999999,
- 99999999999,
- 999999999999,
- 9999999999999,
- 99999999999999,
- 999999999999999,
- 9999999999999999
- };
- void dsr::string_fromDouble(String& target, double value, int decimalCount, bool removeTrailingZeroes, DsrChar decimalCharacter, DsrChar negationCharacter) {
- if (decimalCount < 1) decimalCount = 1;
- if (decimalCount > MAX_DECIMALS) decimalCount = MAX_DECIMALS;
- double remainder = value;
- // Get negation
- if (remainder < 0.0) {
- string_appendChar(target, negationCharacter);
- remainder = -remainder;
- }
- // Apply an offset to make the following truncation round to the closest printable decimal.
- int offsetIndex = decimalCount - 1;
- remainder += roundingOffsets[offsetIndex];
- // Get whole part
- uint64_t whole = (uint64_t)remainder;
- string_fromUnsigned(target, whole);
- // Remove the whole part from the remainder.
- remainder = remainder - whole;
- // Print the decimal
- string_appendChar(target, decimalCharacter);
- // Get decimals
- uint64_t scaledDecimals = uint64_t(remainder * decimalMultipliers[offsetIndex]);
- // Limit decimals to all nines prevent losing a whole unit from fraction overflow.
- uint64_t limit = decimalLimits[offsetIndex];
- if (scaledDecimals > limit) scaledDecimals = limit;
- DsrChar digits[MAX_DECIMALS]; // Using 0 to decimalCount - 1
- int writeIndex = decimalCount - 1;
- for (int d = 0; d < decimalCount; d++) {
- int digit = scaledDecimals % 10;
- digits[writeIndex] = U'0' + digit;
- scaledDecimals = scaledDecimals / 10;
- writeIndex--;
- }
- if (removeTrailingZeroes) {
- // Find the last non-zero decimal, but keep at least one zero.
- int lastValue = 0;
- for (int d = 0; d < decimalCount; d++) {
- if (digits[d] != U'0') lastValue = d;
- }
- // Print until the last value or the only zero.
- for (int d = 0; d <= lastValue; d++) {
- string_appendChar(target, digits[d]);
- }
- } else {
- // Print fixed decimals.
- for (int d = 0; d < decimalCount; d++) {
- string_appendChar(target, digits[d]);
- }
- }
- }
- #define TO_RAW_ASCII(TARGET, SOURCE) \
- char TARGET[SOURCE.view.length + 1]; \
- for (intptr_t i = 0; i < SOURCE.view.length; i++) { \
- TARGET[i] = toAscii(SOURCE[i]); \
- } \
- TARGET[SOURCE.view.length] = '\0';
- // A function definition for receiving a stream of bytes
- // Instead of using std's messy inheritance
- using ByteWriterFunction = std::function<void(uint8_t value)>;
- // A function definition for receiving a stream of UTF-32 characters
- // Instead of using std's messy inheritance
- using UTF32WriterFunction = std::function<void(DsrChar character)>;
- // Filter out unwanted characters for improved portability
- static void feedCharacter(const UTF32WriterFunction &receiver, DsrChar character) {
- if (character != U'\0' && character != U'\r') {
- receiver(character);
- }
- }
- // Appends the content of buffer as a BOM-free Latin-1 file into target
- // fileLength is ignored when nullTerminated is true
- template <bool nullTerminated>
- static void feedStringFromFileBuffer_Latin1(const UTF32WriterFunction &receiver, const uint8_t* buffer, intptr_t fileLength = 0) {
- for (intptr_t i = 0; i < fileLength || nullTerminated; i++) {
- DsrChar character = (DsrChar)(buffer[i]);
- if (nullTerminated && character == 0) { return; }
- feedCharacter(receiver, character);
- }
- }
- // Appends the content of buffer as a BOM-free UTF-8 file into target
- // fileLength is ignored when nullTerminated is true
- template <bool nullTerminated>
- static void feedStringFromFileBuffer_UTF8(const UTF32WriterFunction &receiver, const uint8_t* buffer, intptr_t fileLength = 0) {
- for (intptr_t i = 0; i < fileLength || nullTerminated; i++) {
- uint8_t byteA = buffer[i];
- if (byteA < (uint32_t)0b10000000) {
- // Single byte (1xxxxxxx)
- if (nullTerminated && byteA == 0) { return; }
- feedCharacter(receiver, (DsrChar)byteA);
- } else {
- uint32_t character = 0;
- int extraBytes = 0;
- if (byteA >= (uint32_t)0b11000000) { // At least two leading ones
- if (byteA < (uint32_t)0b11100000) { // Less than three leading ones
- character = byteA & (uint32_t)0b00011111;
- extraBytes = 1;
- } else if (byteA < (uint32_t)0b11110000) { // Less than four leading ones
- character = byteA & (uint32_t)0b00001111;
- extraBytes = 2;
- } else if (byteA < (uint32_t)0b11111000) { // Less than five leading ones
- character = byteA & (uint32_t)0b00000111;
- extraBytes = 3;
- } else {
- // Invalid UTF-8 format
- throwError(U"Invalid UTF-8 multi-chatacter beginning with 0b111111xx!");
- }
- } else {
- // Invalid UTF-8 format
- throwError(U"Invalid UTF-8 multi-chatacter beginning with 0b10xxxxxx!");
- }
- while (extraBytes > 0) {
- i += 1; uint32_t nextByte = buffer[i];
- character = (character << 6) | (nextByte & 0b00111111);
- extraBytes--;
- }
- feedCharacter(receiver, (DsrChar)character);
- }
- }
- }
- template <bool LittleEndian>
- uint16_t read16bits(const uint8_t* buffer, intptr_t startOffset) {
- uint16_t byteA = buffer[startOffset];
- uint16_t byteB = buffer[startOffset + 1];
- if (LittleEndian) {
- return (byteB << 8) | byteA;
- } else {
- return (byteA << 8) | byteB;
- }
- }
- // Appends the content of buffer as a BOM-free UTF-16 file into target as UTF-32
- // fileLength is ignored when nullTerminated is true
- template <bool LittleEndian, bool nullTerminated>
- static void feedStringFromFileBuffer_UTF16(const UTF32WriterFunction &receiver, const uint8_t* buffer, intptr_t fileLength = 0) {
- for (intptr_t i = 0; i < fileLength || nullTerminated; i += 2) {
- // Read the first 16-bit word
- uint16_t wordA = read16bits<LittleEndian>(buffer, i);
- // Check if another word is needed
- // Assuming that wordA >= 0x0000 and wordA <= 0xFFFF as uint16_t,
- // we can just check if it's within the range reserved for 32-bit encoding
- if (wordA <= 0xD7FF || wordA >= 0xE000) {
- // Not in the reserved range, just a single 16-bit character
- if (nullTerminated && wordA == 0) { return; }
- feedCharacter(receiver, (DsrChar)wordA);
- } else {
- // The given range was reserved and therefore using 32 bits
- i += 2;
- uint16_t wordB = read16bits<LittleEndian>(buffer, i);
- uint32_t higher10Bits = wordA & (uint32_t)0b1111111111;
- uint32_t lower10Bits = wordB & (uint32_t)0b1111111111;
- DsrChar finalChar = (DsrChar)(((higher10Bits << 10) | lower10Bits) + (uint32_t)0x10000);
- feedCharacter(receiver, finalChar);
- }
- }
- }
- // Sends the decoded UTF-32 characters from the encoded buffer into target.
- // The text encoding should be specified using a BOM at the start of buffer, otherwise Latin-1 is assumed.
- static void feedStringFromFileBuffer(const UTF32WriterFunction &receiver, const uint8_t* buffer, intptr_t fileLength) {
- // After removing the BOM bytes, the rest can be seen as a BOM-free text file with a known format
- if (fileLength >= 3 && buffer[0] == 0xEF && buffer[1] == 0xBB && buffer[2] == 0xBF) { // UTF-8
- feedStringFromFileBuffer_UTF8<false>(receiver, buffer + 3, fileLength - 3);
- } else if (fileLength >= 2 && buffer[0] == 0xFE && buffer[1] == 0xFF) { // UTF-16 BE
- feedStringFromFileBuffer_UTF16<false, false>(receiver, buffer + 2, fileLength - 2);
- } else if (fileLength >= 2 && buffer[0] == 0xFF && buffer[1] == 0xFE) { // UTF-16 LE
- feedStringFromFileBuffer_UTF16<true, false>(receiver, buffer + 2, fileLength - 2);
- } else if (fileLength >= 4 && buffer[0] == 0x00 && buffer[1] == 0x00 && buffer[2] == 0xFE && buffer[3] == 0xFF) { // UTF-32 BE
- //feedStringFromFileBuffer_UTF32BE(receiver, buffer + 4, fileLength - 4);
- throwError(U"UTF-32 BE format is not yet supported!\n");
- } else if (fileLength >= 4 && buffer[0] == 0xFF && buffer[1] == 0xFE && buffer[2] == 0x00 && buffer[3] == 0x00) { // UTF-32 LE
- //feedStringFromFileBuffer_UTF32BE(receiver, buffer + 4, fileLength - 4);
- throwError(U"UTF-32 LE format is not yet supported!\n");
- } else if (fileLength >= 3 && buffer[0] == 0xF7 && buffer[1] == 0x64 && buffer[2] == 0x4C) { // UTF-1
- //feedStringFromFileBuffer_UTF1(receiver, buffer + 3, fileLength - 3);
- throwError(U"UTF-1 format is not yet supported!\n");
- } else if (fileLength >= 3 && buffer[0] == 0x0E && buffer[1] == 0xFE && buffer[2] == 0xFF) { // SCSU
- //feedStringFromFileBuffer_SCSU(receiver, buffer + 3, fileLength - 3);
- throwError(U"SCSU format is not yet supported!\n");
- } else if (fileLength >= 3 && buffer[0] == 0xFB && buffer[1] == 0xEE && buffer[2] == 0x28) { // BOCU
- //feedStringFromFileBuffer_BOCU-1(receiver, buffer + 3, fileLength - 3);
- throwError(U"BOCU-1 format is not yet supported!\n");
- } else if (fileLength >= 4 && buffer[0] == 0x2B && buffer[1] == 0x2F && buffer[2] == 0x76) { // UTF-7
- // Ignoring fourth byte with the dialect of UTF-7 when just showing the error message
- throwError(U"UTF-7 format is not yet supported!\n");
- } else {
- // No BOM detected, assuming Latin-1 (because it directly corresponds to a unicode sub-set)
- feedStringFromFileBuffer_Latin1<false>(receiver, buffer, fileLength);
- }
- }
- // Sends the decoded UTF-32 characters from the encoded null terminated buffer into target.
- // buffer may not contain any BOM, and must be null terminated in the specified encoding.
- static void feedStringFromRawData(const UTF32WriterFunction &receiver, const uint8_t* buffer, CharacterEncoding encoding) {
- if (encoding == CharacterEncoding::Raw_Latin1) {
- feedStringFromFileBuffer_Latin1<true>(receiver, buffer);
- } else if (encoding == CharacterEncoding::BOM_UTF8) {
- feedStringFromFileBuffer_UTF8<true>(receiver, buffer);
- } else if (encoding == CharacterEncoding::BOM_UTF16BE) {
- feedStringFromFileBuffer_UTF16<false, true>(receiver, buffer);
- } else if (encoding == CharacterEncoding::BOM_UTF16LE) {
- feedStringFromFileBuffer_UTF16<true, true>(receiver, buffer);
- } else {
- throwError(U"Unhandled encoding in feedStringFromRawData!\n");
- }
- }
- String dsr::string_dangerous_decodeFromData(const void* data, CharacterEncoding encoding) {
- String result;
- // Measure the size of the result by scanning the content in advance
- intptr_t characterCount = 0;
- UTF32WriterFunction measurer = [&characterCount](DsrChar character) {
- characterCount++;
- };
- feedStringFromRawData(measurer, (const uint8_t*)data, encoding);
- // Pre-allocate the correct amount of memory based on the simulation
- string_reserve(result, characterCount);
- // Stream output to the result string
- UTF32WriterFunction receiver = [&result](DsrChar character) {
- string_appendChar(result, character);
- };
- feedStringFromRawData(receiver, (const uint8_t*)data, encoding);
- return result;
- }
- String dsr::string_loadFromMemory(Buffer fileContent) {
- String result;
- // Measure the size of the result by scanning the content in advance
- intptr_t characterCount = 0;
- UTF32WriterFunction measurer = [&characterCount](DsrChar character) {
- characterCount++;
- };
- feedStringFromFileBuffer(measurer, fileContent.getUnsafe(), fileContent.getUsedSize());
- // Pre-allocate the correct amount of memory based on the simulation
- string_reserve(result, characterCount);
- // Stream output to the result string
- UTF32WriterFunction receiver = [&result](DsrChar character) {
- string_appendChar(result, character);
- };
- feedStringFromFileBuffer(receiver, fileContent.getUnsafe(), fileContent.getUsedSize());
- return result;
- }
- // Loads a text file of unknown format
- // Removes carriage-return characters to make processing easy with only line-feed for breaking lines
- String dsr::string_load(const ReadableString& filename, bool mustExist) {
- Buffer encoded = file_loadBuffer(filename, mustExist);
- if (!buffer_exists(encoded)) {
- return String();
- } else {
- return string_loadFromMemory(encoded);
- }
- }
- template <CharacterEncoding characterEncoding>
- static void encodeCharacter(const ByteWriterFunction &receiver, DsrChar character) {
- if (characterEncoding == CharacterEncoding::Raw_Latin1) {
- // Replace any illegal characters with questionmarks
- if (character > 255) { character = U'?'; }
- receiver(character);
- } else if (characterEncoding == CharacterEncoding::BOM_UTF8) {
- // Replace any illegal characters with questionmarks
- if (character > 0x10FFFF) { character = U'?'; }
- if (character < (1 << 7)) {
- // 0xxxxxxx
- receiver(character);
- } else if (character < (1 << 11)) {
- // 110xxxxx 10xxxxxx
- receiver((uint32_t)0b11000000 | ((character & ((uint32_t)0b11111 << 6)) >> 6));
- receiver((uint32_t)0b10000000 | (character & (uint32_t)0b111111));
- } else if (character < (1 << 16)) {
- // 1110xxxx 10xxxxxx 10xxxxxx
- receiver((uint32_t)0b11100000 | ((character & ((uint32_t)0b1111 << 12)) >> 12));
- receiver((uint32_t)0b10000000 | ((character & ((uint32_t)0b111111 << 6)) >> 6));
- receiver((uint32_t)0b10000000 | (character & (uint32_t)0b111111));
- } else if (character < (1 << 21)) {
- // 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
- receiver((uint32_t)0b11110000 | ((character & ((uint32_t)0b111 << 18)) >> 18));
- receiver((uint32_t)0b10000000 | ((character & ((uint32_t)0b111111 << 12)) >> 12));
- receiver((uint32_t)0b10000000 | ((character & ((uint32_t)0b111111 << 6)) >> 6));
- receiver((uint32_t)0b10000000 | (character & (uint32_t)0b111111));
- }
- } else { // Assuming UTF-16
- if (character > 0x10FFFF) { character = U'?'; }
- if (character <= 0xD7FF || (character >= 0xE000 && character <= 0xFFFF)) {
- // xxxxxxxx xxxxxxxx (Limited range)
- uint32_t higher8Bits = (character & (uint32_t)0b1111111100000000) >> 8;
- uint32_t lower8Bits = character & (uint32_t)0b0000000011111111;
- if (characterEncoding == CharacterEncoding::BOM_UTF16BE) {
- receiver(higher8Bits);
- receiver(lower8Bits);
- } else { // Assuming UTF-16 LE
- receiver(lower8Bits);
- receiver(higher8Bits);
- }
- } else if (character >= 0x010000 && character <= 0x10FFFF) {
- // 110110xxxxxxxxxx 110111xxxxxxxxxx
- uint32_t code = character - (uint32_t)0x10000;
- uint32_t byteA = ((code & (uint32_t)0b11000000000000000000) >> 18) | (uint32_t)0b11011000;
- uint32_t byteB = (code & (uint32_t)0b00111111110000000000) >> 10;
- uint32_t byteC = ((code & (uint32_t)0b00000000001100000000) >> 8) | (uint32_t)0b11011100;
- uint32_t byteD = code & (uint32_t)0b00000000000011111111;
- if (characterEncoding == CharacterEncoding::BOM_UTF16BE) {
- receiver(byteA);
- receiver(byteB);
- receiver(byteC);
- receiver(byteD);
- } else { // Assuming UTF-16 LE
- receiver(byteB);
- receiver(byteA);
- receiver(byteD);
- receiver(byteC);
- }
- }
- }
- }
- // Template for encoding a whole string
- template <CharacterEncoding characterEncoding, LineEncoding lineEncoding>
- static void encodeText(const ByteWriterFunction &receiver, String content, bool writeBOM, bool writeNullTerminator) {
- if (writeBOM) {
- // Write byte order marks
- if (characterEncoding == CharacterEncoding::BOM_UTF8) {
- receiver(0xEF);
- receiver(0xBB);
- receiver(0xBF);
- } else if (characterEncoding == CharacterEncoding::BOM_UTF16BE) {
- receiver(0xFE);
- receiver(0xFF);
- } else if (characterEncoding == CharacterEncoding::BOM_UTF16LE) {
- receiver(0xFF);
- receiver(0xFE);
- }
- }
- // Write encoded content
- for (intptr_t i = 0; i < string_length(content); i++) {
- DsrChar character = content[i];
- if (character == U'\n') {
- if (lineEncoding == LineEncoding::CrLf) {
- encodeCharacter<characterEncoding>(receiver, U'\r');
- encodeCharacter<characterEncoding>(receiver, U'\n');
- } else { // Assuming that lineEncoding == LineEncoding::Lf
- encodeCharacter<characterEncoding>(receiver, U'\n');
- }
- } else {
- encodeCharacter<characterEncoding>(receiver, character);
- }
- }
- if (writeNullTerminator) {
- // Terminate internal strings with \0 to prevent getting garbage data after unpadded buffers
- if (characterEncoding == CharacterEncoding::BOM_UTF16BE || characterEncoding == CharacterEncoding::BOM_UTF16LE) {
- receiver(0);
- receiver(0);
- } else {
- receiver(0);
- }
- }
- }
- // Macro for converting run-time arguments into template arguments for encodeText
- #define ENCODE_TEXT(RECEIVER, CONTENT, CHAR_ENCODING, LINE_ENCODING, WRITE_BOM, WRITE_NULL_TERMINATOR) \
- if (CHAR_ENCODING == CharacterEncoding::Raw_Latin1) { \
- if (LINE_ENCODING == LineEncoding::CrLf) { \
- encodeText<CharacterEncoding::Raw_Latin1, LineEncoding::CrLf>(RECEIVER, CONTENT, false, WRITE_NULL_TERMINATOR); \
- } else if (LINE_ENCODING == LineEncoding::Lf) { \
- encodeText<CharacterEncoding::Raw_Latin1, LineEncoding::Lf>(RECEIVER, CONTENT, false, WRITE_NULL_TERMINATOR); \
- } \
- } else if (CHAR_ENCODING == CharacterEncoding::BOM_UTF8) { \
- if (LINE_ENCODING == LineEncoding::CrLf) { \
- encodeText<CharacterEncoding::BOM_UTF8, LineEncoding::CrLf>(RECEIVER, CONTENT, WRITE_BOM, WRITE_NULL_TERMINATOR); \
- } else if (LINE_ENCODING == LineEncoding::Lf) { \
- encodeText<CharacterEncoding::BOM_UTF8, LineEncoding::Lf>(RECEIVER, CONTENT, WRITE_BOM, WRITE_NULL_TERMINATOR); \
- } \
- } else if (CHAR_ENCODING == CharacterEncoding::BOM_UTF16BE) { \
- if (LINE_ENCODING == LineEncoding::CrLf) { \
- encodeText<CharacterEncoding::BOM_UTF16BE, LineEncoding::CrLf>(RECEIVER, CONTENT, WRITE_BOM, WRITE_NULL_TERMINATOR); \
- } else if (LINE_ENCODING == LineEncoding::Lf) { \
- encodeText<CharacterEncoding::BOM_UTF16BE, LineEncoding::Lf>(RECEIVER, CONTENT, WRITE_BOM, WRITE_NULL_TERMINATOR); \
- } \
- } else if (CHAR_ENCODING == CharacterEncoding::BOM_UTF16LE) { \
- if (LINE_ENCODING == LineEncoding::CrLf) { \
- encodeText<CharacterEncoding::BOM_UTF16LE, LineEncoding::CrLf>(RECEIVER, CONTENT, WRITE_BOM, WRITE_NULL_TERMINATOR); \
- } else if (LINE_ENCODING == LineEncoding::Lf) { \
- encodeText<CharacterEncoding::BOM_UTF16LE, LineEncoding::Lf>(RECEIVER, CONTENT, WRITE_BOM, WRITE_NULL_TERMINATOR); \
- } \
- }
- // Encoding to a buffer before saving all at once as a binary file.
- // This tells the operating system how big the file is in advance and prevent the worst case of stalling for minutes!
- bool dsr::string_save(const ReadableString& filename, const ReadableString& content, CharacterEncoding characterEncoding, LineEncoding lineEncoding) {
- Buffer buffer = string_saveToMemory(content, characterEncoding, lineEncoding);
- if (buffer_exists(buffer)) {
- return file_saveBuffer(filename, buffer);
- } else {
- return false;
- }
- }
- Buffer dsr::string_saveToMemory(const ReadableString& content, CharacterEncoding characterEncoding, LineEncoding lineEncoding, bool writeByteOrderMark, bool writeNullTerminator) {
- intptr_t byteCount = 0;
- ByteWriterFunction counter = [&byteCount](uint8_t value) {
- byteCount++;
- };
- ENCODE_TEXT(counter, content, characterEncoding, lineEncoding, writeByteOrderMark, writeNullTerminator);
- Buffer result = buffer_create(byteCount).setName("Buffer holding an encoded string");
- SafePointer<uint8_t> byteWriter = buffer_getSafeData<uint8_t>(result, "Buffer for string encoding");
- ByteWriterFunction receiver = [&byteWriter](uint8_t value) {
- *byteWriter = value;
- byteWriter += 1;
- };
- ENCODE_TEXT(receiver, content, characterEncoding, lineEncoding, writeByteOrderMark, writeNullTerminator);
- return result;
- }
- static uintptr_t getStartOffset(const ReadableString &source) {
- // Get the allocation
- const uint8_t* origin = (uint8_t*)(source.characters.getUnsafe());
- const uint8_t* start = (uint8_t*)(source.view.getUnchecked());
- assert(start <= origin);
- // Get the offset from the parent
- return (start - origin) / sizeof(DsrChar);
- }
- #ifdef SAFE_POINTER_CHECKS
- static void serializeCharacterBuffer(PrintCharacter target, void const * const allocation, uintptr_t maxLength) {
- uintptr_t characterCount = heap_getUsedSize(allocation) / sizeof(DsrChar);
- target(U'\"');
- for (uintptr_t c = 0; c < characterCount; c++) {
- if (c == maxLength) {
- target(U'\"');
- target(U'.');
- target(U'.');
- target(U'.');
- return;
- }
- target(((DsrChar *)allocation)[c]);
- }
- target(U'\"');
- }
- #endif
- static Handle<DsrChar> allocateCharacters(intptr_t minimumLength) {
- // Allocate memory.
- Handle<DsrChar> result = handle_createArray<DsrChar>(AllocationInitialization::Uninitialized, minimumLength).setName("String characters");
- #ifdef SAFE_POINTER_CHECKS
- setAllocationSerialization(result.getUnsafe(), &serializeCharacterBuffer);
- #endif
- // Check how much space we got.
- uintptr_t availableSpace = heap_getAllocationSize(result.getUnsafe());
- // Expand to use all available memory in the allocation.
- uintptr_t newSize = heap_setUsedSize(result.getUnsafe(), availableSpace);
- // Clear the memory to zeroes, just to be safe against non-deterministic bugs.
- safeMemorySet(result.getSafe("Cleared String pointer"), 0, newSize);
- return result;
- }
- // Replaces the buffer with a new buffer holding at least minimumLength characters
- // Guarantees that the new buffer is not shared by other strings, so that it may be written to freely
- static void reallocateBuffer(String &target, intptr_t minimumLength, bool preserve) {
- // Holding oldData alive while copying to the new buffer
- Handle<DsrChar> oldBuffer = target.characters; // Kept for reference counting only, do not remove.
- Impl_CharacterView oldData = target.view;
- target.characters = allocateCharacters(minimumLength);
- target.view = Impl_CharacterView(target.characters.getUnsafe(), oldData.length);
- if (preserve && oldData.length > 0) {
- safeMemoryCopy(target.view.getSafe("New characters being copied from an old buffer"), oldData.getSafe("Old characters being copied to a new buffer"), oldData.length * sizeof(DsrChar));
- }
- }
- // Call before writing to the buffer.
- // This hides that Strings share buffers when assigning by value or taking partial strings.
- static void cloneIfNeeded(String &target) {
- // If there is no buffer or the buffer is shared, it needs to allocate its own buffer.
- if (target.characters.isNull() || target.characters.getUseCount() > 1) {
- reallocateBuffer(target, target.view.length, true);
- }
- }
- void dsr::string_clear(String& target) {
- // We we start writing from the beginning, then we must have our own allocation to avoid overwriting the characters in other strings.
- cloneIfNeeded(target);
- target.view.length = 0;
- }
- // The number of DsrChar characters that can be contained in the allocation before reaching the buffer's end
- // This doesn't imply that it's always okay to write to the remaining space, because the buffer may be shared
- static intptr_t getCapacity(const ReadableString &source) {
- if (source.characters.isNotNull()) {
- uintptr_t bufferElements = source.characters.getElementCount();
- // Subtract offset from the buffer size to get the remaining space
- return bufferElements - getStartOffset(source);
- } else {
- return 0;
- }
- }
- static void expand(String &target, intptr_t newLength, bool affectUsedLength) {
- cloneIfNeeded(target);
- if (newLength > target.view.length) {
- if (newLength > getCapacity(target)) {
- reallocateBuffer(target, newLength, true);
- }
- if (affectUsedLength) {
- target.view.length = newLength;
- }
- }
- }
- void dsr::string_reserve(String& target, intptr_t minimumLength) {
- expand(target, minimumLength, false);
- }
- // This macro has to be used because a static template wouldn't be able to inherit access to private methods from the target class.
- // Better to use a macro without type safety in the implementation than to expose yet another template in a global header.
- // Proof that appending to one string doesn't affect another:
- // If it has to reallocate
- // * Then it will have its own buffer without conflicts
- // If it doesn't have to reallocate
- // If it shares the buffer
- // If source is empty
- // * Then no risk of overwriting neighbor strings if we don't write
- // If source isn't empty
- // * Then the buffer will be cloned when the first character is written
- // If it doesn't share the buffer
- // * Then no risk of writing
- #define APPEND(TARGET, SOURCE, LENGTH, MASK) { \
- intptr_t oldLength = (TARGET).view.length; \
- expand((TARGET), oldLength + (intptr_t)(LENGTH), true); \
- for (intptr_t i = 0; i < (intptr_t)(LENGTH); i++) { \
- (TARGET).view.writeCharacter(oldLength + i, ((SOURCE)[i]) & MASK); \
- } \
- }
- // TODO: See if ascii litterals can be checked for values above 127 in compile-time
- static void atomic_append_ascii(String &target, const char* source) { APPEND(target, source, strlen(source), 0xFF); }
- // TODO: Use memcpy when appending input of the same format
- static void atomic_append_readable(String &target, const ReadableString& source) { APPEND(target, source, source.view.length, 0xFFFFFFFF); }
- static void atomic_append_utf32(String &target, const DsrChar* source) { APPEND(target, source, strlen_utf32(source), 0xFFFFFFFF); }
- void dsr::string_appendChar(String& target, DsrChar value) { APPEND(target, &value, 1, 0xFFFFFFFF); }
- String& dsr::impl_toStreamIndented_ascii(String& target, const char *value, const ReadableString& indentation) {
- atomic_append_readable(target, indentation);
- atomic_append_ascii(target, value);
- return target;
- }
- String& dsr::impl_toStreamIndented_utf32(String& target, const char32_t *value, const ReadableString& indentation) {
- atomic_append_readable(target, indentation);
- atomic_append_utf32(target, value);
- return target;
- }
- String& dsr::impl_toStreamIndented_readable(String& target, const ReadableString& value, const ReadableString& indentation) {
- atomic_append_readable(target, indentation);
- atomic_append_readable(target, value);
- return target;
- }
- String& dsr::impl_toStreamIndented_double(String& target, const double &value, const ReadableString& indentation) {
- atomic_append_readable(target, indentation);
- string_fromDouble(target, (double)value);
- return target;
- }
- String& dsr::impl_toStreamIndented_int64(String& target, const int64_t &value, const ReadableString& indentation) {
- atomic_append_readable(target, indentation);
- string_fromSigned(target, value);
- return target;
- }
- String& dsr::impl_toStreamIndented_uint64(String& target, const uint64_t &value, const ReadableString& indentation) {
- atomic_append_readable(target, indentation);
- string_fromUnsigned(target, value);
- return target;
- }
- // The print mutex makes sure that messages from multiple threads don't get mixed up.
- static std::mutex printMutex;
- static std::ostream& toStream(std::ostream& out, const ReadableString &source) {
- for (intptr_t i = 0; i < source.view.length; i++) {
- out.put(toAscii(source.view[i]));
- }
- return out;
- }
- static const std::function<void(const ReadableString &message, MessageType type)> defaultMessageAction = [](const ReadableString &message, MessageType type) {
- if (type == MessageType::Error) {
- #ifdef DSR_HARD_EXIT_ON_ERROR
- // Print the error.
- toStream(std::cerr, message);
- // Free all heap allocations.
- heap_hardExitCleaning();
- // Terminate with a non-zero value to indicate failure.
- std::exit(1);
- #else
- Buffer ascii = string_saveToMemory(message, CharacterEncoding::Raw_Latin1, LineEncoding::CrLf, false, true);
- throw std::runtime_error((char*)ascii.getUnsafe());
- #endif
- } else {
- printMutex.lock();
- toStream(std::cout, message);
- printMutex.unlock();
- }
- };
- static std::function<void(const ReadableString &message, MessageType type)> globalMessageAction = defaultMessageAction;
- void dsr::string_sendMessage(const ReadableString &message, MessageType type) {
- globalMessageAction(message, type);
- }
- void dsr::string_sendMessage_default(const ReadableString &message, MessageType type) {
- defaultMessageAction(message, type);
- }
- void dsr::string_assignMessageHandler(std::function<void(const ReadableString &message, MessageType type)> newHandler) {
- globalMessageAction = newHandler;
- }
- void dsr::string_unassignMessageHandler() {
- globalMessageAction = defaultMessageAction;
- }
- void dsr::string_split_callback(std::function<void(ReadableString separatedText)> action, const ReadableString& source, DsrChar separator, bool removeWhiteSpace) {
- intptr_t sectionStart = 0;
- for (intptr_t i = 0; i < source.view.length; i++) {
- DsrChar c = source[i];
- if (c == separator) {
- ReadableString element = string_exclusiveRange(source, sectionStart, i);
- if (removeWhiteSpace) {
- action(string_removeOuterWhiteSpace(element));
- } else {
- action(element);
- }
- sectionStart = i + 1;
- }
- }
- if (source.view.length > sectionStart) {
- if (removeWhiteSpace) {
- action(string_removeOuterWhiteSpace(string_exclusiveRange(source, sectionStart, source.view.length)));
- } else {
- action(string_exclusiveRange(source, sectionStart, source.view.length));
- }
- }
- }
- static String createSubString(const Handle<DsrChar> &characters, const Impl_CharacterView &view) {
- String result;
- result.characters = characters;
- result.view = view;
- return result;
- }
- List<String> dsr::string_split(const ReadableString& source, DsrChar separator, bool removeWhiteSpace) {
- List<String> result;
- if (source.view.length > 0) {
- // Re-use the existing buffer
- String commonBuffer = createSubString(source.characters, source.view);
- // Source is allocated as String
- string_split_callback([&result, removeWhiteSpace](String element) {
- if (removeWhiteSpace) {
- result.push(string_removeOuterWhiteSpace(element));
- } else {
- result.push(element);
- }
- }, commonBuffer, separator, removeWhiteSpace);
- }
- return result;
- }
- intptr_t dsr::string_splitCount(const ReadableString& source, DsrChar separator) {
- intptr_t result = 0;
- string_split_callback([&result](ReadableString element) {
- result++;
- }, source, separator);
- return result;
- }
- int64_t dsr::string_toInteger(const ReadableString& source) {
- int64_t result;
- bool negated;
- result = 0;
- negated = false;
- for (intptr_t i = 0; i < source.view.length; i++) {
- DsrChar c = source[i];
- if (c == '-' || c == '~') {
- negated = !negated;
- } else if (c >= '0' && c <= '9') {
- result = (result * 10) + (int)(c - '0');
- } else if (c == ',' || c == '.') {
- // Truncate any decimals by ignoring them
- break;
- }
- }
- if (negated) {
- return -result;
- } else {
- return result;
- }
- }
- double dsr::string_toDouble(const ReadableString& source) {
- double result;
- bool negated;
- bool reachedDecimal;
- int64_t digitDivider;
- result = 0.0;
- negated = false;
- reachedDecimal = false;
- digitDivider = 1;
- for (intptr_t i = 0; i < source.view.length; i++) {
- DsrChar c = source[i];
- if (c == '-' || c == '~') {
- negated = !negated;
- } else if (c >= '0' && c <= '9') {
- if (reachedDecimal) {
- digitDivider = digitDivider * 10;
- result = result + ((double)(c - '0') / (double)digitDivider);
- } else {
- result = (result * 10) + (double)(c - '0');
- }
- } else if (c == ',' || c == '.') {
- reachedDecimal = true;
- } else if (c == 'e' || c == 'E') {
- // Apply the exponent after 'e'.
- result *= std::pow(10.0, string_toInteger(string_after(source, i)));
- // Skip remaining characters.
- i = source.view.length;
- }
- }
- if (negated) {
- return -result;
- } else {
- return result;
- }
- }
- intptr_t dsr::string_length(const ReadableString& source) {
- return source.view.length;
- }
- intptr_t dsr::string_findFirst(const ReadableString& source, DsrChar toFind, intptr_t startIndex) {
- for (intptr_t i = startIndex; i < source.view.length; i++) {
- if (source[i] == toFind) {
- return i;
- }
- }
- return -1;
- }
- intptr_t dsr::string_findLast(const ReadableString& source, DsrChar toFind) {
- for (intptr_t i = source.view.length - 1; i >= 0; i--) {
- if (source[i] == toFind) {
- return i;
- }
- }
- return -1;
- }
- ReadableString dsr::string_exclusiveRange(const ReadableString& source, intptr_t inclusiveStart, intptr_t exclusiveEnd) {
- // Return empty string for each complete miss
- if (inclusiveStart >= source.view.length || exclusiveEnd <= 0) { return ReadableString(); }
- // Automatically clamping to valid range
- if (inclusiveStart < 0) { inclusiveStart = 0; }
- if (exclusiveEnd > source.view.length) { exclusiveEnd = source.view.length; }
- // Return the overlapping interval
- return createSubString(source.characters, Impl_CharacterView(source.view.getUnchecked() + inclusiveStart, exclusiveEnd - inclusiveStart));
- }
- ReadableString dsr::string_inclusiveRange(const ReadableString& source, intptr_t inclusiveStart, intptr_t inclusiveEnd) {
- return string_exclusiveRange(source, inclusiveStart, inclusiveEnd + 1);
- }
- ReadableString dsr::string_before(const ReadableString& source, intptr_t exclusiveEnd) {
- return string_exclusiveRange(source, 0, exclusiveEnd);
- }
- ReadableString dsr::string_until(const ReadableString& source, intptr_t inclusiveEnd) {
- return string_inclusiveRange(source, 0, inclusiveEnd);
- }
- ReadableString dsr::string_from(const ReadableString& source, intptr_t inclusiveStart) {
- return string_exclusiveRange(source, inclusiveStart, source.view.length);
- }
- ReadableString dsr::string_after(const ReadableString& source, intptr_t exclusiveStart) {
- return string_from(source, exclusiveStart + 1);
- }
- bool dsr::character_isDigit(DsrChar c) {
- return c >= U'0' && c <= U'9';
- }
- bool dsr::character_isIntegerCharacter(DsrChar c) {
- return c == U'-' || character_isDigit(c);
- }
- bool dsr::character_isValueCharacter(DsrChar c) {
- return c == U'.' || character_isIntegerCharacter(c);
- }
- bool dsr::character_isWhiteSpace(DsrChar c) {
- return c == U' ' || c == U'\t' || c == U'\v' || c == U'\f' || c == U'\n' || c == U'\r';
- }
- // Macros for implementing regular expressions with a greedy approach consuming the first match
- // Optional accepts 0 or 1 occurence
- // Forced accepts 1 occurence
- // Star accepts 0..N occurence
- // Plus accepts 1..N occurence
- #define CHARACTER_OPTIONAL(CHARACTER) if (source[readIndex] == CHARACTER) { readIndex++; }
- #define CHARACTER_FORCED(CHARACTER) if (source[readIndex] == CHARACTER) { readIndex++; } else { return false; }
- #define CHARACTER_STAR(CHARACTER) while (source[readIndex] == CHARACTER) { readIndex++; }
- #define CHARACTER_PLUS(CHARACTER) CHARACTER_FORCED(CHARACTER) CHARACTER_STAR(CHARACTER)
- #define PATTERN_OPTIONAL(PATTERN) if (character_is##PATTERN(source[readIndex])) { readIndex++; }
- #define PATTERN_FORCED(PATTERN) if (character_is##PATTERN(source[readIndex])) { readIndex++; } else { return false; }
- #define PATTERN_STAR(PATTERN) while (character_is##PATTERN(source[readIndex])) { readIndex++; }
- #define PATTERN_PLUS(PATTERN) PATTERN_FORCED(PATTERN) PATTERN_STAR(PATTERN)
- // The greedy approach works here, because there's no ambiguity
- bool dsr::string_isInteger(const ReadableString& source, bool allowWhiteSpace) {
- intptr_t readIndex = 0;
- if (allowWhiteSpace) {
- PATTERN_STAR(WhiteSpace);
- }
- CHARACTER_OPTIONAL(U'-');
- // At least one digit required
- PATTERN_PLUS(IntegerCharacter);
- if (allowWhiteSpace) {
- PATTERN_STAR(WhiteSpace);
- }
- return readIndex == source.view.length;
- }
- // To avoid consuming the all digits on Digit* before reaching Digit+ when there is no decimal, whole integers are judged by string_isInteger
- bool dsr::string_isDouble(const ReadableString& source, bool allowWhiteSpace) {
- // Solving the UnsignedDouble <- Digit+ | Digit* '.' Digit+ ambiguity is done easiest by checking if there's a decimal before handling the white-space and negation
- if (string_findFirst(source, U'.') == -1) {
- // No decimal detected
- return string_isInteger(source, allowWhiteSpace);
- } else {
- intptr_t readIndex = 0;
- if (allowWhiteSpace) {
- PATTERN_STAR(WhiteSpace);
- }
- // Double <- UnsignedDouble | '-' UnsignedDouble
- CHARACTER_OPTIONAL(U'-');
- // UnsignedDouble <- Digit* '.' Digit+
- // Any number of integer digits
- PATTERN_STAR(IntegerCharacter);
- // Only dot for decimal
- CHARACTER_FORCED(U'.')
- // At least one decimal digit
- PATTERN_PLUS(IntegerCharacter);
- if (allowWhiteSpace) {
- PATTERN_STAR(WhiteSpace);
- }
- return readIndex == source.view.length;
- }
- }
- uintptr_t dsr::string_getBufferUseCount(const ReadableString& text) {
- return text.characters.getUseCount();
- }
|