text.cpp 23 KB

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