text.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835
  1. // zlib open source license
  2. //
  3. // Copyright (c) 2017 to 2019 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. #include "text.h"
  24. #include <fstream>
  25. #include <streambuf>
  26. #include <cstring>
  27. #include <stdexcept>
  28. using namespace dsr;
  29. static int strlen_utf16(const char32_t *content) {
  30. int length = 0;
  31. while (content[length] != 0) {
  32. length++;
  33. }
  34. return length;
  35. }
  36. static char toAscii(DsrChar c) {
  37. if (c > 127) {
  38. return '?';
  39. } else {
  40. return c;
  41. }
  42. }
  43. static bool isWhiteSpace(DsrChar c) {
  44. return c <= U' ' || c == U'\t' || c == U'\r';
  45. }
  46. int ReadableString::findFirst(DsrChar toFind, int startIndex) const {
  47. for (int i = startIndex; i < this->length(); i++) {
  48. if (this->readSection[i] == toFind) {
  49. return i;
  50. }
  51. }
  52. return -1;
  53. }
  54. int ReadableString::findLast(DsrChar toFind) const {
  55. for (int i = this->length() - 1; i >= 0; i--) {
  56. if (this->readSection[i] == toFind) {
  57. return i;
  58. }
  59. }
  60. return -1;
  61. }
  62. ReadableString ReadableString::exclusiveRange(int inclusiveStart, int exclusiveEnd) const {
  63. return this->getRange(inclusiveStart, exclusiveEnd - inclusiveStart);
  64. }
  65. ReadableString ReadableString::inclusiveRange(int inclusiveStart, int inclusiveEnd) const {
  66. return this->getRange(inclusiveStart, inclusiveEnd + 1 - inclusiveStart);
  67. }
  68. ReadableString ReadableString::before(int exclusiveEnd) const {
  69. return this->exclusiveRange(0, exclusiveEnd);
  70. }
  71. ReadableString ReadableString::until(int inclusiveEnd) const {
  72. return this->inclusiveRange(0, inclusiveEnd);
  73. }
  74. ReadableString ReadableString::from(int inclusiveStart) const {
  75. return this->exclusiveRange(inclusiveStart, this->length());
  76. }
  77. ReadableString ReadableString::after(int exclusiveStart) const {
  78. return this->from(exclusiveStart + 1);
  79. }
  80. String& Printable::toStream(String& target) const {
  81. return this->toStreamIndented(target, U"");
  82. }
  83. String Printable::toStringIndented(const ReadableString& indentation) const {
  84. String result;
  85. this->toStreamIndented(result, indentation);
  86. return result;
  87. }
  88. String Printable::toString() const {
  89. return this->toStringIndented(U"");
  90. }
  91. std::ostream& Printable::toStreamIndented(std::ostream& out, const ReadableString& indentation) const {
  92. String result;
  93. this->toStreamIndented(result, indentation);
  94. for (int i = 0; i < result.length(); i++) {
  95. out.put(toAscii(result.read(i)));
  96. }
  97. return out;
  98. }
  99. std::ostream& Printable::toStream(std::ostream& out) const {
  100. return this->toStreamIndented(out, U"");
  101. }
  102. std::string Printable::toStdString() const {
  103. std::ostringstream result;
  104. this->toStream(result);
  105. return result.str();
  106. }
  107. Printable::~Printable() {}
  108. bool dsr::string_match(const ReadableString& a, const ReadableString& b) {
  109. if (a.length() != b.length()) {
  110. return false;
  111. } else {
  112. for (int i = 0; i < a.length(); i++) {
  113. if (a.read(i) != b.read(i)) {
  114. return false;
  115. }
  116. }
  117. return true;
  118. }
  119. }
  120. bool dsr::string_caseInsensitiveMatch(const ReadableString& a, const ReadableString& b) {
  121. if (a.length() != b.length()) {
  122. return false;
  123. } else {
  124. for (int i = 0; i < a.length(); i++) {
  125. if (towupper(a.read(i)) != towupper(b.read(i))) {
  126. return false;
  127. }
  128. }
  129. return true;
  130. }
  131. }
  132. std::ostream& ReadableString::toStream(std::ostream& out) const {
  133. for (int i = 0; i < this->length(); i++) {
  134. out.put(toAscii(this->read(i)));
  135. }
  136. return out;
  137. }
  138. std::string ReadableString::toStdString() const {
  139. std::ostringstream result;
  140. this->toStream(result);
  141. return result.str();
  142. }
  143. String dsr::string_upperCase(const ReadableString &text) {
  144. String result;
  145. result.reserve(text.length());
  146. for (int i = 0; i < text.length(); i++) {
  147. result.appendChar(towupper(text[i]));
  148. }
  149. return result;
  150. }
  151. String dsr::string_lowerCase(const ReadableString &text) {
  152. String result;
  153. result.reserve(text.length());
  154. for (int i = 0; i < text.length(); i++) {
  155. result.appendChar(towlower(text[i]));
  156. }
  157. return result;
  158. }
  159. String dsr::string_removeAllWhiteSpace(const ReadableString &text) {
  160. String result;
  161. result.reserve(text.length());
  162. for (int i = 0; i < text.length(); i++) {
  163. DsrChar c = text[i];
  164. if (!isWhiteSpace(c)) {
  165. result.appendChar(c);
  166. }
  167. }
  168. return result;
  169. }
  170. ReadableString dsr::string_removeOuterWhiteSpace(const ReadableString &text) {
  171. int first = -1;
  172. int last = -1;
  173. for (int i = 0; i < text.length(); i++) {
  174. DsrChar c = text[i];
  175. if (!isWhiteSpace(c)) {
  176. first = i;
  177. break;
  178. }
  179. }
  180. for (int i = text.length() - 1; i >= 0; i--) {
  181. DsrChar c = text[i];
  182. if (!isWhiteSpace(c)) {
  183. last = i;
  184. break;
  185. }
  186. }
  187. if (first == -1) {
  188. // Only white space
  189. return ReadableString();
  190. } else {
  191. // Subset
  192. return text.inclusiveRange(first, last);
  193. }
  194. }
  195. int64_t dsr::string_parseInteger(const ReadableString& content) {
  196. return content.toInteger();
  197. }
  198. double dsr::string_parseDouble(const ReadableString& content) {
  199. return content.toDouble();
  200. }
  201. String dsr::string_mangleQuote(const ReadableString &rawText) {
  202. String result;
  203. result.reserve(rawText.length() + 2);
  204. result.appendChar(U'\"'); // Begin quote
  205. for (int i = 0; i < rawText.length(); i++) {
  206. DsrChar c = rawText[i];
  207. if (c == U'\"') { // Double quote
  208. result.append(U"\\\"");
  209. } else if (c == U'\\') { // Backslash
  210. result.append(U"\\\\");
  211. } else if (c == U'\a') { // Audible bell
  212. result.append(U"\\a");
  213. } else if (c == U'\b') { // Backspace
  214. result.append(U"\\b");
  215. } else if (c == U'\f') { // Form feed
  216. result.append(U"\\f");
  217. } else if (c == U'\n') { // Line feed
  218. result.append(U"\\n");
  219. } else if (c == U'\r') { // Carriage return
  220. result.append(U"\\r");
  221. } else if (c == U'\t') { // Horizontal tab
  222. result.append(U"\\t");
  223. } else if (c == U'\v') { // Vertical tab
  224. result.append(U"\\v");
  225. } else if (c == U'\0') { // Null terminator
  226. result.append(U"\\0");
  227. } else {
  228. result.appendChar(c);
  229. }
  230. }
  231. result.appendChar(U'\"'); // End quote
  232. return result;
  233. }
  234. String dsr::string_unmangleQuote(const ReadableString& mangledText) {
  235. int firstQuote = mangledText.findFirst('\"');
  236. int lastQuote = mangledText.findLast('\"');
  237. String result;
  238. if (firstQuote == -1 || lastQuote == -1 || firstQuote == lastQuote) {
  239. throwError(U"Cannot unmangle using string_unmangleQuote without beginning and ending with quote signs!\n", mangledText, "\n");
  240. } else {
  241. for (int i = firstQuote + 1; i < lastQuote; i++) {
  242. DsrChar c = mangledText[i];
  243. if (c == U'\\') { // Escape character
  244. DsrChar c2 = mangledText[i + 1];
  245. if (c2 == U'\"') { // Double quote
  246. result.appendChar(U'\"');
  247. } else if (c2 == U'\\') { // Back slash
  248. result.appendChar(U'\\');
  249. } else if (c2 == U'a') { // Audible bell
  250. result.appendChar(U'\a');
  251. } else if (c2 == U'b') { // Backspace
  252. result.appendChar(U'\b');
  253. } else if (c2 == U'f') { // Form feed
  254. result.appendChar(U'\f');
  255. } else if (c2 == U'n') { // Line feed
  256. result.appendChar(U'\n');
  257. } else if (c2 == U'r') { // Carriage return
  258. result.appendChar(U'\r');
  259. } else if (c2 == U't') { // Horizontal tab
  260. result.appendChar(U'\t');
  261. } else if (c2 == U'v') { // Vertical tab
  262. result.appendChar(U'\v');
  263. } else if (c2 == U'0') { // Null terminator
  264. result.appendChar(U'\0');
  265. }
  266. i++; // Consume both characters
  267. } else {
  268. // Detect bad input
  269. if (c == U'\"') { // Double quote
  270. throwError(U"Unmangled double quote sign detected in string_unmangleQuote!\n", mangledText, "\n");
  271. } else if (c == U'\\') { // Back slash
  272. throwError(U"Unmangled back slash detected in string_unmangleQuote!\n", mangledText, "\n");
  273. } else if (c == U'\a') { // Audible bell
  274. throwError(U"Unmangled audible bell detected in string_unmangleQuote!\n", mangledText, "\n");
  275. } else if (c == U'\b') { // Backspace
  276. throwError(U"Unmangled backspace detected in string_unmangleQuote!\n", mangledText, "\n");
  277. } else if (c == U'\f') { // Form feed
  278. throwError(U"Unmangled form feed detected in string_unmangleQuote!\n", mangledText, "\n");
  279. } else if (c == U'\n') { // Line feed
  280. throwError(U"Unmangled line feed detected in string_unmangleQuote!\n", mangledText, "\n");
  281. } else if (c == U'\r') { // Carriage return
  282. throwError(U"Unmangled carriage return detected in string_unmangleQuote!\n", mangledText, "\n");
  283. } else if (c == U'\0') { // Null terminator
  284. throwError(U"Unmangled null terminator detected in string_unmangleQuote!\n", mangledText, "\n");
  285. } else {
  286. result.appendChar(c);
  287. }
  288. }
  289. }
  290. }
  291. return result;
  292. }
  293. void dsr::uintToString_arabic(String& target, uint64_t value) {
  294. static const int bufferSize = 20;
  295. DsrChar digits[bufferSize];
  296. int usedSize = 0;
  297. if (value == 0) {
  298. target.appendChar(U'0');
  299. } else {
  300. while (usedSize < bufferSize) {
  301. DsrChar digit = U'0' + (value % 10u);
  302. digits[usedSize] = digit;
  303. usedSize++;
  304. value /= 10u;
  305. if (value == 0) {
  306. break;
  307. }
  308. }
  309. while (usedSize > 0) {
  310. usedSize--;
  311. target.appendChar(digits[usedSize]);
  312. }
  313. }
  314. }
  315. void dsr::intToString_arabic(String& target, int64_t value) {
  316. if (value >= 0) {
  317. uintToString_arabic(target, (uint64_t)value);
  318. } else {
  319. target.appendChar(U'-');
  320. uintToString_arabic(target, (uint64_t)(-value));
  321. }
  322. }
  323. // TODO: Implement own version to ensure that nothing strange is happening from buggy std implementations
  324. void dsr::doubleToString_arabic(String& target, double value) {
  325. std::ostringstream buffer;
  326. buffer << std::fixed << value; // Generate using a fixed number of decimals
  327. std::string result = buffer.str();
  328. // Remove trailing zero decimal digits
  329. int decimalCount = 0;
  330. int lastValueIndex = -1;
  331. for (int c = 0; c < (int)result.length(); c++) {
  332. if (result[c] == '.') {
  333. decimalCount++;
  334. } else if (result[c] == ',') {
  335. result[c] = '.'; // Convert nationalized french decimal serialization into international decimals
  336. decimalCount++;
  337. } else if (decimalCount > 0 && result[c] >= '1' && result[c] <= '9') {
  338. lastValueIndex = c;
  339. } else if (decimalCount == 0 && result[c] >= '0' && result[c] <= '9') {
  340. lastValueIndex = c;
  341. }
  342. }
  343. for (int c = 0; c <= lastValueIndex; c++) {
  344. target.appendChar(result[c]);
  345. }
  346. }
  347. #define TO_RAW_ASCII(TARGET, SOURCE) \
  348. char TARGET[SOURCE.length() + 1]; \
  349. for (int i = 0; i < SOURCE.length(); i++) { \
  350. TARGET[i] = toAscii(SOURCE[i]); \
  351. } \
  352. TARGET[SOURCE.length()] = '\0';
  353. String dsr::string_load(const ReadableString& filename, bool mustExist) {
  354. // TODO: Load files using Unicode filenames
  355. TO_RAW_ASCII(asciiFilename, filename);
  356. std::ifstream inputFile(asciiFilename);
  357. if (inputFile.is_open()) {
  358. std::stringstream outputBuffer;
  359. // TODO: Feed directly to String
  360. outputBuffer << inputFile.rdbuf();
  361. std::string content = outputBuffer.str();
  362. String result;
  363. result.reserve(content.size());
  364. for (int i = 0; i < (int)(content.size()); i++) {
  365. result.appendChar(content[i]);
  366. }
  367. inputFile.close();
  368. return result;
  369. } else {
  370. if (mustExist) {
  371. throwError("Failed to load ", filename, "\n");
  372. }
  373. // If the file cound not be found and opened, a null string is returned
  374. return String();
  375. }
  376. }
  377. void dsr::string_save(const ReadableString& filename, const ReadableString& content) {
  378. // TODO: Load files using Unicode filenames
  379. TO_RAW_ASCII(asciiFilename, filename);
  380. TO_RAW_ASCII(asciiContent, content);
  381. std::ofstream outputFile;
  382. outputFile.open(asciiFilename);
  383. if (outputFile.is_open()) {
  384. outputFile << asciiContent;
  385. outputFile.close();
  386. } else {
  387. throwError("Failed to save ", filename, "\n");
  388. }
  389. }
  390. const char32_t* dsr::file_separator() {
  391. #ifdef _WIN32
  392. return U"\\";
  393. #else
  394. return U"/";
  395. #endif
  396. }
  397. int ReadableString::length() const {
  398. return this->sectionLength;
  399. }
  400. bool ReadableString::checkBound(int start, int length, bool warning) const {
  401. if (start < 0 || start + length > this->length()) {
  402. if (warning) {
  403. String message;
  404. string_append(message, U"\n");
  405. string_append(message, U" _____________________ Sub-string bound exception! _____________________\n");
  406. string_append(message, U"/\n");
  407. string_append(message, U"| Characters from ", start, U" to ", (start + length - 1), U" are out of bound!\n");
  408. string_append(message, U"| In source string of 0..", (this->length() - 1), U".\n");
  409. string_append(message, U"\\_______________________________________________________________________\n");
  410. throwError(message);
  411. }
  412. return false;
  413. } else {
  414. return true;
  415. }
  416. }
  417. DsrChar ReadableString::read(int index) const {
  418. if (index < 0 || index >= this->sectionLength) {
  419. return '\0';
  420. } else {
  421. return this->readSection[index];
  422. }
  423. }
  424. DsrChar ReadableString::operator[] (int index) const { return this->read(index); }
  425. ReadableString::ReadableString() {}
  426. ReadableString::~ReadableString() {}
  427. ReadableString::ReadableString(const DsrChar *content, int sectionLength)
  428. : readSection(content), sectionLength(sectionLength) {}
  429. ReadableString::ReadableString(const DsrChar *content)
  430. : readSection(content), sectionLength(strlen_utf16(content)) {}
  431. String::String() {}
  432. String::String(const char* source) { this->append(source); }
  433. String::String(const char32_t* source) { this->append(source); }
  434. String::String(const std::string& source) { this->append(source); }
  435. String::String(const ReadableString& source) { this->append(source); }
  436. String::String(const String& source) { this->append(source); }
  437. String::String(std::shared_ptr<Buffer> buffer, DsrChar *content, int sectionLength)
  438. : ReadableString(content, sectionLength), buffer(buffer), writeSection(content) {}
  439. int String::capacity() {
  440. if (this->buffer.get() == nullptr) {
  441. return 0;
  442. } else {
  443. // Get the parent allocation
  444. uint8_t* parentBuffer = this->buffer->getUnsafeData();
  445. // Get the offset from the parent
  446. intptr_t offset = (uint8_t*)this->writeSection - parentBuffer;
  447. // Subtract offset from the buffer size to get the remaining space
  448. return (this->buffer->size - offset) / sizeof(DsrChar);
  449. }
  450. }
  451. ReadableString ReadableString::getRange(int start, int length) const {
  452. if (length < 1) {
  453. return ReadableString();
  454. } else if (this->checkBound(start, length)) {
  455. return ReadableString(&(this->readSection[start]), length);
  456. } else {
  457. return ReadableString();
  458. }
  459. }
  460. ReadableString String::getRange(int start, int length) const {
  461. if (length < 1) {
  462. return ReadableString();
  463. } else if (this->checkBound(start, length)) {
  464. return String(this->buffer, &(this->writeSection[start]), length);
  465. } else {
  466. return ReadableString();
  467. }
  468. }
  469. static int32_t getNewBufferSize(int32_t minimumSize) {
  470. if (minimumSize <= 128) {
  471. return 128;
  472. } else if (minimumSize <= 512) {
  473. return 512;
  474. } else if (minimumSize <= 2048) {
  475. return 2048;
  476. } else if (minimumSize <= 8192) {
  477. return 8192;
  478. } else if (minimumSize <= 32768) {
  479. return 32768;
  480. } else if (minimumSize <= 131072) {
  481. return 131072;
  482. } else if (minimumSize <= 524288) {
  483. return 524288;
  484. } else if (minimumSize <= 2097152) {
  485. return 2097152;
  486. } else if (minimumSize <= 8388608) {
  487. return 8388608;
  488. } else if (minimumSize <= 33554432) {
  489. return 33554432;
  490. } else if (minimumSize <= 134217728) {
  491. return 134217728;
  492. } else if (minimumSize <= 536870912) {
  493. return 536870912;
  494. } else {
  495. return 2147483647;
  496. }
  497. }
  498. void String::reallocateBuffer(int32_t newLength, bool preserve) {
  499. // Holding oldData alive while copying to the new buffer
  500. std::shared_ptr<Buffer> oldBuffer = this->buffer;
  501. const char32_t* oldData = this->readSection;
  502. this->buffer = std::make_shared<Buffer>(getNewBufferSize(newLength * sizeof(DsrChar)));
  503. this->readSection = this->writeSection = reinterpret_cast<char32_t*>(this->buffer->getUnsafeData());
  504. if (preserve && oldData) {
  505. memcpy(this->writeSection, oldData, this->sectionLength * sizeof(DsrChar));
  506. }
  507. }
  508. // Call before writing to the buffer
  509. // This hides that Strings share buffers when assigning by value or taking partial strings
  510. void String::cloneIfShared() {
  511. if (this->buffer.use_count() > 1) {
  512. this->reallocateBuffer(this->sectionLength, true);
  513. }
  514. }
  515. void String::expand(int32_t newLength, bool affectUsedLength) {
  516. if (newLength > this->sectionLength) {
  517. if (newLength > this->capacity()) {
  518. this->reallocateBuffer(newLength, true);
  519. }
  520. }
  521. if (affectUsedLength) {
  522. this->sectionLength = newLength;
  523. }
  524. }
  525. void String::reserve(int32_t minimumLength) {
  526. this->expand(minimumLength, false);
  527. }
  528. void String::write(int index, DsrChar value) {
  529. this->cloneIfShared();
  530. if (index < 0 || index >= this->sectionLength) {
  531. // TODO: Give a warning
  532. } else {
  533. this->writeSection[index] = value;
  534. }
  535. }
  536. void String::clear() {
  537. this->sectionLength = 0;
  538. }
  539. // This macro has to be used because a static template wouldn't be able to inherit access to private methods from the target class.
  540. // Better to use a macro without type safety in the implementation than to expose yet another template in a global header.
  541. // Proof that appending to one string doesn't affect another:
  542. // If it has to reallocate
  543. // * Then it will have its own buffer without conflicts
  544. // If it doesn't have to reallocate
  545. // If it shares the buffer
  546. // If source is empty
  547. // * Then no risk of overwriting neighbor strings if we don't write
  548. // If source isn't empty
  549. // * Then the buffer will be cloned when the first character is written
  550. // If it doesn't share the buffer
  551. // * Then no risk of writing
  552. #define APPEND(TARGET, SOURCE, LENGTH) { \
  553. int oldLength = (TARGET)->length(); \
  554. (TARGET)->expand(oldLength + (int)(LENGTH), true); \
  555. for (int i = 0; i < (int)(LENGTH); i++) { \
  556. (TARGET)->write(oldLength + i, (SOURCE)[i]); \
  557. } \
  558. }
  559. // TODO: See if ascii litterals can be checked for values above 127 in compile-time
  560. void String::append(const char* source) { APPEND(this, source, strlen(source)); }
  561. // TODO: Use memcpy when appending input of the same format
  562. void String::append(const ReadableString& source) { APPEND(this, source, source.length()); }
  563. void String::append(const char32_t* source) { APPEND(this, source, strlen_utf16(source)); }
  564. void String::append(const std::string& source) { APPEND(this, source.c_str(), (int)source.size()); }
  565. void String::appendChar(DsrChar source) { APPEND(this, &source, 1); }
  566. String& dsr::string_toStreamIndented(String& target, const Printable& source, const ReadableString& indentation) {
  567. return source.toStreamIndented(target, indentation);
  568. }
  569. String& dsr::string_toStreamIndented(String& target, const char* value, const ReadableString& indentation) {
  570. target.append(indentation);
  571. target.append(value);
  572. return target;
  573. }
  574. String& dsr::string_toStreamIndented(String& target, const ReadableString& value, const ReadableString& indentation) {
  575. target.append(indentation);
  576. target.append(value);
  577. return target;
  578. }
  579. String& dsr::string_toStreamIndented(String& target, const char32_t* value, const ReadableString& indentation) {
  580. target.append(indentation);
  581. target.append(value);
  582. return target;
  583. }
  584. String& dsr::string_toStreamIndented(String& target, const std::string& value, const ReadableString& indentation) {
  585. target.append(indentation);
  586. target.append(value);
  587. return target;
  588. }
  589. String& dsr::string_toStreamIndented(String& target, const float& value, const ReadableString& indentation) {
  590. target.append(indentation);
  591. doubleToString_arabic(target, (double)value);
  592. return target;
  593. }
  594. String& dsr::string_toStreamIndented(String& target, const double& value, const ReadableString& indentation) {
  595. target.append(indentation);
  596. doubleToString_arabic(target, value);
  597. return target;
  598. }
  599. String& dsr::string_toStreamIndented(String& target, const int64_t& value, const ReadableString& indentation) {
  600. target.append(indentation);
  601. intToString_arabic(target, value);
  602. return target;
  603. }
  604. String& dsr::string_toStreamIndented(String& target, const uint64_t& value, const ReadableString& indentation) {
  605. target.append(indentation);
  606. uintToString_arabic(target, value);
  607. return target;
  608. }
  609. String& dsr::string_toStreamIndented(String& target, const int32_t& value, const ReadableString& indentation) {
  610. target.append(indentation);
  611. intToString_arabic(target, (int64_t)value);
  612. return target;
  613. }
  614. String& dsr::string_toStreamIndented(String& target, const uint32_t& value, const ReadableString& indentation) {
  615. target.append(indentation);
  616. uintToString_arabic(target, (uint64_t)value);
  617. return target;
  618. }
  619. String& dsr::string_toStreamIndented(String& target, const int16_t& value, const ReadableString& indentation) {
  620. target.append(indentation);
  621. intToString_arabic(target, (int64_t)value);
  622. return target;
  623. }
  624. String& dsr::string_toStreamIndented(String& target, const uint16_t& value, const ReadableString& indentation) {
  625. target.append(indentation);
  626. uintToString_arabic(target, (uint64_t)value);
  627. return target;
  628. }
  629. String& dsr::string_toStreamIndented(String& target, const int8_t& value, const ReadableString& indentation) {
  630. target.append(indentation);
  631. intToString_arabic(target, (int64_t)value);
  632. return target;
  633. }
  634. String& dsr::string_toStreamIndented(String& target, const uint8_t& value, const ReadableString& indentation) {
  635. target.append(indentation);
  636. uintToString_arabic(target, (uint64_t)value);
  637. return target;
  638. }
  639. void dsr::throwErrorMessage(const String& message) {
  640. throw std::runtime_error(message.toStdString());
  641. }
  642. void dsr::string_split_inPlace(List<ReadableString> &target, const ReadableString& source, DsrChar separator, bool appendResult) {
  643. if (!appendResult) {
  644. target.clear();
  645. }
  646. int sectionStart = 0;
  647. for (int i = 0; i < source.length(); i++) {
  648. DsrChar c = source[i];
  649. if (c == separator) {
  650. target.push(source.exclusiveRange(sectionStart, i));
  651. sectionStart = i + 1;
  652. }
  653. }
  654. if (source.length() > sectionStart) {
  655. target.push(source.exclusiveRange(sectionStart, source.length()));;
  656. }
  657. }
  658. List<ReadableString> dsr::string_split(const ReadableString& source, DsrChar separator) {
  659. List<ReadableString> result;
  660. string_split_inPlace(result, source, separator);
  661. return result;
  662. }
  663. // TODO: Delete
  664. int64_t ReadableString::toInteger() const {
  665. int64_t result;
  666. bool negated;
  667. result = 0;
  668. negated = false;
  669. for (int i = 0; i < this->length(); i++) {
  670. DsrChar c = this->readSection[i];
  671. if (c == '-' || c == '~') {
  672. negated = !negated;
  673. } else if (c >= '0' && c <= '9') {
  674. result = (result * 10) + (int)(c - '0');
  675. } else if (c == ',' || c == '.') {
  676. // Truncate any decimals by ignoring them
  677. break;
  678. }
  679. }
  680. if (negated) {
  681. return -result;
  682. } else {
  683. return result;
  684. }
  685. }
  686. // TODO: Delete
  687. double ReadableString::toDouble() const {
  688. double result;
  689. bool negated;
  690. bool reachedDecimal;
  691. int digitDivider;
  692. result = 0.0;
  693. negated = false;
  694. reachedDecimal = false;
  695. digitDivider = 1;
  696. for (int i = 0; i < this->length(); i++) {
  697. DsrChar c = this->readSection[i];
  698. if (c == '-' || c == '~') {
  699. negated = !negated;
  700. } else if (c >= '0' && c <= '9') {
  701. if (reachedDecimal) {
  702. digitDivider = digitDivider * 10;
  703. result = result + ((double)(c - '0') / (double)digitDivider);
  704. } else {
  705. result = (result * 10) + (double)(c - '0');
  706. }
  707. } else if (c == ',' || c == '.') {
  708. reachedDecimal = true;
  709. }
  710. }
  711. if (negated) {
  712. return -result;
  713. } else {
  714. return result;
  715. }
  716. }
  717. int64_t dsr::string_toInteger(const ReadableString& source) {
  718. int64_t result;
  719. bool negated;
  720. result = 0;
  721. negated = false;
  722. for (int i = 0; i < source.length(); i++) {
  723. DsrChar c = source[i];
  724. if (c == '-' || c == '~') {
  725. negated = !negated;
  726. } else if (c >= '0' && c <= '9') {
  727. result = (result * 10) + (int)(c - '0');
  728. } else if (c == ',' || c == '.') {
  729. // Truncate any decimals by ignoring them
  730. break;
  731. }
  732. }
  733. if (negated) {
  734. return -result;
  735. } else {
  736. return result;
  737. }
  738. }
  739. double dsr::string_toDouble(const ReadableString& source) {
  740. double result;
  741. bool negated;
  742. bool reachedDecimal;
  743. int digitDivider;
  744. result = 0.0;
  745. negated = false;
  746. reachedDecimal = false;
  747. digitDivider = 1;
  748. for (int i = 0; i < source.length(); i++) {
  749. DsrChar c = source[i];
  750. if (c == '-' || c == '~') {
  751. negated = !negated;
  752. } else if (c >= '0' && c <= '9') {
  753. if (reachedDecimal) {
  754. digitDivider = digitDivider * 10;
  755. result = result + ((double)(c - '0') / (double)digitDivider);
  756. } else {
  757. result = (result * 10) + (double)(c - '0');
  758. }
  759. } else if (c == ',' || c == '.') {
  760. reachedDecimal = true;
  761. }
  762. }
  763. if (negated) {
  764. return -result;
  765. } else {
  766. return result;
  767. }
  768. }