text.cpp 25 KB

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