text.cpp 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076
  1. // zlib open source license
  2. //
  3. // Copyright (c) 2017 to 2020 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. // Gets access to private members by making them public for the whole module
  24. #define DFPSR_INTERNAL_ACCESS
  25. #include <fstream>
  26. #include <streambuf>
  27. #include <cstring>
  28. #include <stdexcept>
  29. #include "text.h"
  30. #include "../api/fileAPI.h"
  31. using namespace dsr;
  32. static int64_t strlen_utf32(const char32_t *content) {
  33. int64_t length = 0;
  34. while (content[length] != 0) {
  35. length++;
  36. }
  37. return length;
  38. }
  39. static char toAscii(DsrChar c) {
  40. if (c > 127) {
  41. return '?';
  42. } else {
  43. return c;
  44. }
  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 (int64_t i = 0; i < result.length; i++) {
  61. out.put(toAscii(result.readSection[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 (int64_t i = 0; i < a.length; i++) {
  79. if (a.readSection[i] != b.readSection[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 (int64_t i = 0; i < a.length; i++) {
  91. if (towupper(a.readSection[i]) != towupper(b.readSection[i])) {
  92. return false;
  93. }
  94. }
  95. return true;
  96. }
  97. }
  98. std::ostream& ReadableString::toStream(std::ostream& out) const {
  99. for (int64_t i = 0; i < this->length; i++) {
  100. out.put(toAscii(this->readSection[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 (int64_t 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 (int64_t 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 (int64_t i = 0; i < text.length; i++) {
  129. DsrChar c = text[i];
  130. if (!character_isWhiteSpace(c)) {
  131. result.appendChar(c);
  132. }
  133. }
  134. return result;
  135. }
  136. ReadableString dsr::string_removeOuterWhiteSpace(const ReadableString &text) {
  137. int64_t first = -1;
  138. int64_t last = -1;
  139. for (int64_t i = 0; i < text.length; i++) {
  140. DsrChar c = text[i];
  141. if (!character_isWhiteSpace(c)) {
  142. first = i;
  143. break;
  144. }
  145. }
  146. for (int64_t i = text.length - 1; i >= 0; i--) {
  147. DsrChar c = text[i];
  148. if (!character_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 (int64_t 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. int64_t firstQuote = string_findFirst(mangledText, '\"');
  196. int64_t 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 (int64_t 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. static void uintToString_arabic(String& target, uint64_t value) {
  254. static const int bufferSize = 20;
  255. DsrChar digits[bufferSize];
  256. int64_t 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. static void 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. static void 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. int64_t decimalCount = 0;
  290. int64_t lastValueIndex = -1;
  291. for (size_t c = 0; c < 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 (int64_t 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 (int64_t i = 0; i < SOURCE.length; i++) { \
  310. TARGET[i] = toAscii(SOURCE[i]); \
  311. } \
  312. TARGET[SOURCE.length] = '\0';
  313. // A function definition for receiving a stream of bytes
  314. // Instead of using std's messy inheritance
  315. using ByteWriterFunction = std::function<void(uint8_t value)>;
  316. // A function definition for receiving a stream of UTF-32 characters
  317. // Instead of using std's messy inheritance
  318. using UTF32WriterFunction = std::function<void(DsrChar character)>;
  319. // Filter out unwanted characters for improved portability
  320. static void feedCharacter(const UTF32WriterFunction &reciever, DsrChar character) {
  321. if (character != U'\0' && character != U'\r') {
  322. reciever(character);
  323. }
  324. }
  325. // Appends the content of buffer as a BOM-free Latin-1 file into target
  326. static void feedStringFromFileBuffer_Latin1(const UTF32WriterFunction &reciever, const uint8_t* buffer, int64_t fileLength) {
  327. for (int64_t i = 0; i < fileLength; i++) {
  328. DsrChar character = (DsrChar)(buffer[i]);
  329. feedCharacter(reciever, character);
  330. }
  331. }
  332. // Appends the content of buffer as a BOM-free UTF-8 file into target
  333. static void feedStringFromFileBuffer_UTF8(const UTF32WriterFunction &reciever, const uint8_t* buffer, int64_t fileLength) {
  334. for (int64_t i = 0; i < fileLength; i++) {
  335. uint8_t byteA = buffer[i];
  336. if (byteA < (uint32_t)0b10000000) {
  337. // Single byte (1xxxxxxx)
  338. feedCharacter(reciever, (DsrChar)byteA);
  339. } else {
  340. uint32_t character = 0;
  341. int extraBytes = 0;
  342. if (byteA >= (uint32_t)0b11000000) { // At least two leading ones
  343. if (byteA < (uint32_t)0b11100000) { // Less than three leading ones
  344. character = byteA & (uint32_t)0b00011111;
  345. extraBytes = 1;
  346. } else if (byteA < (uint32_t)0b11110000) { // Less than four leading ones
  347. character = byteA & (uint32_t)0b00001111;
  348. extraBytes = 2;
  349. } else if (byteA < (uint32_t)0b11111000) { // Less than five leading ones
  350. character = byteA & (uint32_t)0b00000111;
  351. extraBytes = 3;
  352. } else {
  353. // Invalid UTF-8 format
  354. throwError(U"Invalid UTF-8 multi-chatacter beginning with 0b111111xx!");
  355. }
  356. } else {
  357. // Invalid UTF-8 format
  358. throwError(U"Invalid UTF-8 multi-chatacter beginning with 0b10xxxxxx!");
  359. }
  360. while (extraBytes > 0) {
  361. i += 1; uint32_t nextByte = buffer[i];
  362. character = (character << 6) | (nextByte & 0b00111111);
  363. extraBytes--;
  364. }
  365. feedCharacter(reciever, (DsrChar)character);
  366. }
  367. }
  368. }
  369. template <bool LittleEndian>
  370. uint16_t read16bits(const uint8_t* buffer, int64_t startOffset) {
  371. uint16_t byteA = buffer[startOffset];
  372. uint16_t byteB = buffer[startOffset + 1];
  373. if (LittleEndian) {
  374. return (byteB << 8) | byteA;
  375. } else {
  376. return (byteA << 8) | byteB;
  377. }
  378. }
  379. // Appends the content of buffer as a BOM-free UTF-16 file into target
  380. template <bool LittleEndian>
  381. static void feedStringFromFileBuffer_UTF16(const UTF32WriterFunction &reciever, const uint8_t* buffer, int64_t fileLength) {
  382. for (int64_t i = 0; i < fileLength; i += 2) {
  383. // Read the first 16-bit word
  384. uint16_t wordA = read16bits<LittleEndian>(buffer, i);
  385. // Check if another word is needed
  386. // Assuming that wordA >= 0x0000 and wordA <= 0xFFFF as uint16_t,
  387. // we can just check if it's within the range reserved for 32-bit encoding
  388. if (wordA <= 0xD7FF || wordA >= 0xE000) {
  389. // Not in the reserved range, just a single 16-bit character
  390. feedCharacter(reciever, (DsrChar)wordA);
  391. } else {
  392. // The given range was reserved and therefore using 32 bits
  393. i += 2;
  394. uint16_t wordB = read16bits<LittleEndian>(buffer, i);
  395. uint32_t higher10Bits = wordA & (uint32_t)0b1111111111;
  396. uint32_t lower10Bits = wordB & (uint32_t)0b1111111111;
  397. feedCharacter(reciever, (DsrChar)(((higher10Bits << 10) | lower10Bits) + (uint32_t)0x10000));
  398. }
  399. }
  400. }
  401. // Appends the content of buffer as a text file of unknown format into target
  402. static void feedStringFromFileBuffer(const UTF32WriterFunction &reciever, const uint8_t* buffer, int64_t fileLength) {
  403. // After removing the BOM bytes, the rest can be seen as a BOM-free text file with a known format
  404. if (fileLength >= 3 && buffer[0] == 0xEF && buffer[1] == 0xBB && buffer[2] == 0xBF) { // UTF-8
  405. feedStringFromFileBuffer_UTF8(reciever, buffer + 3, fileLength - 3);
  406. } else if (fileLength >= 2 && buffer[0] == 0xFE && buffer[1] == 0xFF) { // UTF-16 BE
  407. feedStringFromFileBuffer_UTF16<false>(reciever, buffer + 2, fileLength - 2);
  408. } else if (fileLength >= 2 && buffer[0] == 0xFF && buffer[1] == 0xFE) { // UTF-16 LE
  409. feedStringFromFileBuffer_UTF16<true>(reciever, buffer + 2, fileLength - 2);
  410. } else if (fileLength >= 4 && buffer[0] == 0x00 && buffer[1] == 0x00 && buffer[2] == 0xFE && buffer[3] == 0xFF) { // UTF-32 BE
  411. //feedStringFromFileBuffer_UTF32BE(receiver, buffer + 4, fileLength - 4);
  412. throwError(U"UTF-32 BE format is not yet supported!\n");
  413. } else if (fileLength >= 4 && buffer[0] == 0xFF && buffer[1] == 0xFE && buffer[2] == 0x00 && buffer[3] == 0x00) { // UTF-32 LE
  414. //feedStringFromFileBuffer_UTF32BE(receiver, buffer + 4, fileLength - 4);
  415. throwError(U"UTF-32 LE format is not yet supported!\n");
  416. } else if (fileLength >= 3 && buffer[0] == 0xF7 && buffer[1] == 0x64 && buffer[2] == 0x4C) { // UTF-1
  417. //feedStringFromFileBuffer_UTF1(receiver, buffer + 3, fileLength - 3);
  418. throwError(U"UTF-1 format is not yet supported!\n");
  419. } else if (fileLength >= 3 && buffer[0] == 0x0E && buffer[1] == 0xFE && buffer[2] == 0xFF) { // SCSU
  420. //feedStringFromFileBuffer_SCSU(receiver, buffer + 3, fileLength - 3);
  421. throwError(U"SCSU format is not yet supported!\n");
  422. } else if (fileLength >= 3 && buffer[0] == 0xFB && buffer[1] == 0xEE && buffer[2] == 0x28) { // BOCU
  423. //feedStringFromFileBuffer_BOCU-1(receiver, buffer + 3, fileLength - 3);
  424. throwError(U"BOCU-1 format is not yet supported!\n");
  425. } else if (fileLength >= 4 && buffer[0] == 0x2B && buffer[1] == 0x2F && buffer[2] == 0x76) { // UTF-7
  426. // Ignoring fourth byte with the dialect of UTF-7 when just showing the error message
  427. throwError(U"UTF-7 format is not yet supported!\n");
  428. } else {
  429. // No BOM detected, assuming Latin-1 (because it directly corresponds to a unicode sub-set)
  430. feedStringFromFileBuffer_Latin1(reciever, buffer, fileLength);
  431. }
  432. }
  433. String dsr::string_loadFromMemory(Buffer fileContent) {
  434. String result;
  435. // Measure the size of the result by scanning the content in advance
  436. int64_t characterCount = 0;
  437. UTF32WriterFunction measurer = [&characterCount](DsrChar character) {
  438. characterCount++;
  439. };
  440. feedStringFromFileBuffer(measurer, buffer_dangerous_getUnsafeData(fileContent), buffer_getSize(fileContent));
  441. // Pre-allocate the correct amount of memory based on the simulation
  442. result.reserve(characterCount);
  443. // Stream output to the result string
  444. UTF32WriterFunction reciever = [&result](DsrChar character) {
  445. result.appendChar(character);
  446. };
  447. feedStringFromFileBuffer(reciever, buffer_dangerous_getUnsafeData(fileContent), buffer_getSize(fileContent));
  448. return result;
  449. }
  450. // Loads a text file of unknown format
  451. // Removes carriage-return characters to make processing easy with only line-feed for breaking lines
  452. String dsr::string_load(const ReadableString& filename, bool mustExist) {
  453. Buffer encoded = file_loadBuffer(filename, mustExist);
  454. if (!buffer_exists(encoded)) {
  455. return String();
  456. } else {
  457. return string_loadFromMemory(encoded);
  458. }
  459. }
  460. template <CharacterEncoding characterEncoding>
  461. static void encodeCharacter(const ByteWriterFunction &receiver, DsrChar character) {
  462. if (characterEncoding == CharacterEncoding::Raw_Latin1) {
  463. // Replace any illegal characters with questionmarks
  464. if (character > 255) { character = U'?'; }
  465. receiver(character);
  466. } else if (characterEncoding == CharacterEncoding::BOM_UTF8) {
  467. // Replace any illegal characters with questionmarks
  468. if (character > 0x10FFFF) { character = U'?'; }
  469. if (character < (1 << 7)) {
  470. // 0xxxxxxx
  471. receiver(character);
  472. } else if (character < (1 << 11)) {
  473. // 110xxxxx 10xxxxxx
  474. receiver((uint32_t)0b11000000 | ((character & ((uint32_t)0b11111 << 6)) >> 6));
  475. receiver((uint32_t)0b10000000 | (character & (uint32_t)0b111111));
  476. } else if (character < (1 << 16)) {
  477. // 1110xxxx 10xxxxxx 10xxxxxx
  478. receiver((uint32_t)0b11100000 | ((character & ((uint32_t)0b1111 << 12)) >> 12));
  479. receiver((uint32_t)0b10000000 | ((character & ((uint32_t)0b111111 << 6)) >> 6));
  480. receiver((uint32_t)0b10000000 | (character & (uint32_t)0b111111));
  481. } else if (character < (1 << 21)) {
  482. // 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
  483. receiver((uint32_t)0b11110000 | ((character & ((uint32_t)0b111 << 18)) >> 18));
  484. receiver((uint32_t)0b10000000 | ((character & ((uint32_t)0b111111 << 12)) >> 12));
  485. receiver((uint32_t)0b10000000 | ((character & ((uint32_t)0b111111 << 6)) >> 6));
  486. receiver((uint32_t)0b10000000 | (character & (uint32_t)0b111111));
  487. }
  488. } else { // Assuming UTF-16
  489. if (character > 0x10FFFF) { character = U'?'; }
  490. if (character <= 0xD7FF || (character >= 0xE000 && character <= 0xFFFF)) {
  491. // xxxxxxxx xxxxxxxx (Limited range)
  492. uint32_t higher8Bits = (character & (uint32_t)0b1111111100000000) >> 8;
  493. uint32_t lower8Bits = character & (uint32_t)0b0000000011111111;
  494. if (characterEncoding == CharacterEncoding::BOM_UTF16BE) {
  495. receiver(higher8Bits);
  496. receiver(lower8Bits);
  497. } else { // Assuming UTF-16 LE
  498. receiver(lower8Bits);
  499. receiver(higher8Bits);
  500. }
  501. } else if (character >= 0x010000 && character <= 0x10FFFF) {
  502. // 110110xxxxxxxxxx 110111xxxxxxxxxx
  503. uint32_t code = character - (uint32_t)0x10000;
  504. uint32_t byteA = ((code & (uint32_t)0b11000000000000000000) >> 18) | (uint32_t)0b11011000;
  505. uint32_t byteB = (code & (uint32_t)0b00111111110000000000) >> 10;
  506. uint32_t byteC = ((code & (uint32_t)0b00000000001100000000) >> 8) | (uint32_t)0b11011100;
  507. uint32_t byteD = code & (uint32_t)0b00000000000011111111;
  508. if (characterEncoding == CharacterEncoding::BOM_UTF16BE) {
  509. receiver(byteA);
  510. receiver(byteB);
  511. receiver(byteC);
  512. receiver(byteD);
  513. } else { // Assuming UTF-16 LE
  514. receiver(byteB);
  515. receiver(byteA);
  516. receiver(byteD);
  517. receiver(byteC);
  518. }
  519. }
  520. }
  521. }
  522. // Template for encoding a whole string
  523. template <CharacterEncoding characterEncoding, LineEncoding lineEncoding>
  524. static void encodeText(const ByteWriterFunction &receiver, String content) {
  525. // Write byte order marks
  526. if (characterEncoding == CharacterEncoding::BOM_UTF8) {
  527. receiver(0xEF);
  528. receiver(0xBB);
  529. receiver(0xBF);
  530. } else if (characterEncoding == CharacterEncoding::BOM_UTF16BE) {
  531. receiver(0xFE);
  532. receiver(0xFF);
  533. } else if (characterEncoding == CharacterEncoding::BOM_UTF16LE) {
  534. receiver(0xFF);
  535. receiver(0xFE);
  536. }
  537. // Write encoded content
  538. for (int64_t i = 0; i < string_length(content); i++) {
  539. DsrChar character = content[i];
  540. if (character == U'\n') {
  541. if (lineEncoding == LineEncoding::CrLf) {
  542. encodeCharacter<characterEncoding>(receiver, U'\r');
  543. encodeCharacter<characterEncoding>(receiver, U'\n');
  544. } else { // Assuming that lineEncoding == LineEncoding::Lf
  545. encodeCharacter<characterEncoding>(receiver, U'\n');
  546. }
  547. } else {
  548. encodeCharacter<characterEncoding>(receiver, character);
  549. }
  550. }
  551. }
  552. // Macro for converting run-time arguments into template arguments for encodeText
  553. #define ENCODE_TEXT(RECEIVER, CONTENT, CHAR_ENCODING, LINE_ENCODING) \
  554. if (CHAR_ENCODING == CharacterEncoding::Raw_Latin1) { \
  555. if (LINE_ENCODING == LineEncoding::CrLf) { \
  556. encodeText<CharacterEncoding::Raw_Latin1, LineEncoding::CrLf>(RECEIVER, CONTENT); \
  557. } else if (LINE_ENCODING == LineEncoding::Lf) { \
  558. encodeText<CharacterEncoding::Raw_Latin1, LineEncoding::Lf>(RECEIVER, CONTENT); \
  559. } \
  560. } else if (CHAR_ENCODING == CharacterEncoding::BOM_UTF8) { \
  561. if (LINE_ENCODING == LineEncoding::CrLf) { \
  562. encodeText<CharacterEncoding::BOM_UTF8, LineEncoding::CrLf>(RECEIVER, CONTENT); \
  563. } else if (LINE_ENCODING == LineEncoding::Lf) { \
  564. encodeText<CharacterEncoding::BOM_UTF8, LineEncoding::Lf>(RECEIVER, CONTENT); \
  565. } \
  566. } else if (CHAR_ENCODING == CharacterEncoding::BOM_UTF16BE) { \
  567. if (LINE_ENCODING == LineEncoding::CrLf) { \
  568. encodeText<CharacterEncoding::BOM_UTF16BE, LineEncoding::CrLf>(RECEIVER, CONTENT); \
  569. } else if (LINE_ENCODING == LineEncoding::Lf) { \
  570. encodeText<CharacterEncoding::BOM_UTF16BE, LineEncoding::Lf>(RECEIVER, CONTENT); \
  571. } \
  572. } else if (CHAR_ENCODING == CharacterEncoding::BOM_UTF16LE) { \
  573. if (LINE_ENCODING == LineEncoding::CrLf) { \
  574. encodeText<CharacterEncoding::BOM_UTF16LE, LineEncoding::CrLf>(RECEIVER, CONTENT); \
  575. } else if (LINE_ENCODING == LineEncoding::Lf) { \
  576. encodeText<CharacterEncoding::BOM_UTF16LE, LineEncoding::Lf>(RECEIVER, CONTENT); \
  577. } \
  578. }
  579. // Encoding to a buffer before saving all at once as a binary file.
  580. // This tells the operating system how big the file is in advance and prevent the worst case of stalling for minutes!
  581. void dsr::string_save(const ReadableString& filename, const ReadableString& content, CharacterEncoding characterEncoding, LineEncoding lineEncoding) {
  582. Buffer buffer = string_saveToMemory(content, characterEncoding, lineEncoding);
  583. if (buffer_exists(buffer)) {
  584. file_saveBuffer(filename, buffer);
  585. }
  586. }
  587. Buffer dsr::string_saveToMemory(const ReadableString& content, CharacterEncoding characterEncoding, LineEncoding lineEncoding) {
  588. int64_t byteCount = 0;
  589. ByteWriterFunction counter = [&byteCount](uint8_t value) {
  590. byteCount++;
  591. };
  592. ENCODE_TEXT(counter, content, characterEncoding, lineEncoding);
  593. Buffer result = buffer_create(byteCount);
  594. SafePointer<uint8_t> byteWriter = buffer_getSafeData<uint8_t>(result, "Buffer for string encoding");
  595. ByteWriterFunction receiver = [&byteWriter](uint8_t value) {
  596. *byteWriter = value;
  597. byteWriter += 1;
  598. };
  599. ENCODE_TEXT(receiver, content, characterEncoding, lineEncoding);
  600. return result;
  601. }
  602. DsrChar ReadableString::operator[] (int64_t index) const {
  603. if (index < 0 || index >= this->length) {
  604. return U'\0';
  605. } else {
  606. return this->readSection[index];
  607. }
  608. }
  609. ReadableString::ReadableString() {}
  610. ReadableString::~ReadableString() {}
  611. ReadableString::ReadableString(const DsrChar *content)
  612. : readSection(content), length(strlen_utf32(content)) {}
  613. // Not the fastest constructor, but won't bloat the public header
  614. // Hopefully most compilers know how to optimize this
  615. static ReadableString createSubString(const DsrChar *content, int64_t length) {
  616. ReadableString result;
  617. result.readSection = content;
  618. result.length = length;
  619. return result;
  620. }
  621. String::String() {}
  622. String::String(const char* source) { this->append(source); }
  623. String::String(const char32_t* source) { this->append(source); }
  624. String::String(const std::string& source) { this->append(source); }
  625. String::String(const ReadableString& source) { this->append(source); }
  626. String::String(const String& source) { this->append(source); }
  627. String::String(Buffer buffer, DsrChar *content, int64_t length)
  628. : ReadableString(createSubString(content, length)), buffer(buffer), writeSection(content) {}
  629. int64_t String::capacity() {
  630. if (this->buffer.get() == nullptr) {
  631. return 0;
  632. } else {
  633. // Get the parent allocation
  634. uint8_t* parentBuffer = buffer_dangerous_getUnsafeData(this->buffer);
  635. // Get the offset from the parent
  636. intptr_t offset = (uint8_t*)this->writeSection - parentBuffer;
  637. // Subtract offset from the buffer size to get the remaining space
  638. return (buffer_getSize(this->buffer) - offset) / sizeof(DsrChar);
  639. }
  640. }
  641. static int32_t getNewBufferSize(int32_t minimumSize) {
  642. if (minimumSize <= 128) {
  643. return 128;
  644. } else if (minimumSize <= 512) {
  645. return 512;
  646. } else if (minimumSize <= 2048) {
  647. return 2048;
  648. } else if (minimumSize <= 8192) {
  649. return 8192;
  650. } else if (minimumSize <= 32768) {
  651. return 32768;
  652. } else if (minimumSize <= 131072) {
  653. return 131072;
  654. } else if (minimumSize <= 524288) {
  655. return 524288;
  656. } else if (minimumSize <= 2097152) {
  657. return 2097152;
  658. } else if (minimumSize <= 8388608) {
  659. return 8388608;
  660. } else if (minimumSize <= 33554432) {
  661. return 33554432;
  662. } else if (minimumSize <= 134217728) {
  663. return 134217728;
  664. } else if (minimumSize <= 536870912) {
  665. return 536870912;
  666. } else {
  667. return 2147483647;
  668. }
  669. }
  670. void String::reallocateBuffer(int64_t newLength, bool preserve) {
  671. // Holding oldData alive while copying to the new buffer
  672. Buffer oldBuffer = this->buffer;
  673. const char32_t* oldData = this->readSection;
  674. this->buffer = buffer_create(getNewBufferSize(newLength * sizeof(DsrChar)));
  675. this->readSection = this->writeSection = reinterpret_cast<char32_t*>(buffer_dangerous_getUnsafeData(this->buffer));
  676. if (preserve && oldData) {
  677. memcpy(this->writeSection, oldData, this->length * sizeof(DsrChar));
  678. }
  679. }
  680. // Call before writing to the buffer
  681. // This hides that Strings share buffers when assigning by value or taking partial strings
  682. void String::cloneIfShared() {
  683. if (this->buffer.use_count() > 1) {
  684. this->reallocateBuffer(this->length, true);
  685. }
  686. }
  687. void String::expand(int64_t newLength, bool affectUsedLength) {
  688. if (newLength > this->length) {
  689. if (newLength > this->capacity()) {
  690. this->reallocateBuffer(newLength, true);
  691. }
  692. }
  693. if (affectUsedLength) {
  694. this->length = newLength;
  695. }
  696. }
  697. void String::reserve(int64_t minimumLength) {
  698. this->expand(minimumLength, false);
  699. }
  700. void String::write(int64_t index, DsrChar value) {
  701. this->cloneIfShared();
  702. if (index < 0 || index >= this->length) {
  703. // TODO: Give a warning
  704. } else {
  705. this->writeSection[index] = value;
  706. }
  707. }
  708. void String::clear() {
  709. this->length = 0;
  710. }
  711. // This macro has to be used because a static template wouldn't be able to inherit access to private methods from the target class.
  712. // Better to use a macro without type safety in the implementation than to expose yet another template in a global header.
  713. // Proof that appending to one string doesn't affect another:
  714. // If it has to reallocate
  715. // * Then it will have its own buffer without conflicts
  716. // If it doesn't have to reallocate
  717. // If it shares the buffer
  718. // If source is empty
  719. // * Then no risk of overwriting neighbor strings if we don't write
  720. // If source isn't empty
  721. // * Then the buffer will be cloned when the first character is written
  722. // If it doesn't share the buffer
  723. // * Then no risk of writing
  724. #define APPEND(TARGET, SOURCE, LENGTH, MASK) { \
  725. int64_t oldLength = (TARGET)->length; \
  726. (TARGET)->expand(oldLength + (int64_t)(LENGTH), true); \
  727. for (int64_t i = 0; i < (int64_t)(LENGTH); i++) { \
  728. (TARGET)->write(oldLength + i, ((SOURCE)[i]) & MASK); \
  729. } \
  730. }
  731. // TODO: See if ascii litterals can be checked for values above 127 in compile-time
  732. void String::append(const char* source) { APPEND(this, source, strlen(source), 0xFF); }
  733. // TODO: Use memcpy when appending input of the same format
  734. void String::append(const ReadableString& source) { APPEND(this, source, source.length, 0xFFFFFFFF); }
  735. void String::append(const char32_t* source) { APPEND(this, source, strlen_utf32(source), 0xFFFFFFFF); }
  736. void String::append(const std::string& source) { APPEND(this, source.c_str(), (int64_t)source.size(), 0xFF); }
  737. void String::appendChar(DsrChar source) { APPEND(this, &source, 1, 0xFFFFFFFF); }
  738. String& dsr::string_toStreamIndented(String& target, const Printable& source, const ReadableString& indentation) {
  739. return source.toStreamIndented(target, indentation);
  740. }
  741. String& dsr::string_toStreamIndented(String& target, const char* value, const ReadableString& indentation) {
  742. target.append(indentation);
  743. target.append(value);
  744. return target;
  745. }
  746. String& dsr::string_toStreamIndented(String& target, const ReadableString& value, const ReadableString& indentation) {
  747. target.append(indentation);
  748. target.append(value);
  749. return target;
  750. }
  751. String& dsr::string_toStreamIndented(String& target, const char32_t* value, const ReadableString& indentation) {
  752. target.append(indentation);
  753. target.append(value);
  754. return target;
  755. }
  756. String& dsr::string_toStreamIndented(String& target, const std::string& value, const ReadableString& indentation) {
  757. target.append(indentation);
  758. target.append(value);
  759. return target;
  760. }
  761. String& dsr::string_toStreamIndented(String& target, const float& value, const ReadableString& indentation) {
  762. target.append(indentation);
  763. doubleToString_arabic(target, (double)value);
  764. return target;
  765. }
  766. String& dsr::string_toStreamIndented(String& target, const double& value, const ReadableString& indentation) {
  767. target.append(indentation);
  768. doubleToString_arabic(target, value);
  769. return target;
  770. }
  771. String& dsr::string_toStreamIndented(String& target, const int64_t& value, const ReadableString& indentation) {
  772. target.append(indentation);
  773. intToString_arabic(target, value);
  774. return target;
  775. }
  776. String& dsr::string_toStreamIndented(String& target, const uint64_t& value, const ReadableString& indentation) {
  777. target.append(indentation);
  778. uintToString_arabic(target, value);
  779. return target;
  780. }
  781. String& dsr::string_toStreamIndented(String& target, const int32_t& value, const ReadableString& indentation) {
  782. target.append(indentation);
  783. intToString_arabic(target, (int64_t)value);
  784. return target;
  785. }
  786. String& dsr::string_toStreamIndented(String& target, const uint32_t& value, const ReadableString& indentation) {
  787. target.append(indentation);
  788. uintToString_arabic(target, (uint64_t)value);
  789. return target;
  790. }
  791. String& dsr::string_toStreamIndented(String& target, const int16_t& value, const ReadableString& indentation) {
  792. target.append(indentation);
  793. intToString_arabic(target, (int64_t)value);
  794. return target;
  795. }
  796. String& dsr::string_toStreamIndented(String& target, const uint16_t& value, const ReadableString& indentation) {
  797. target.append(indentation);
  798. uintToString_arabic(target, (uint64_t)value);
  799. return target;
  800. }
  801. String& dsr::string_toStreamIndented(String& target, const int8_t& value, const ReadableString& indentation) {
  802. target.append(indentation);
  803. intToString_arabic(target, (int64_t)value);
  804. return target;
  805. }
  806. String& dsr::string_toStreamIndented(String& target, const uint8_t& value, const ReadableString& indentation) {
  807. target.append(indentation);
  808. uintToString_arabic(target, (uint64_t)value);
  809. return target;
  810. }
  811. void dsr::throwErrorMessage(const String& message) {
  812. throw std::runtime_error(message.toStdString());
  813. }
  814. void dsr::string_split_callback(std::function<void(ReadableString)> action, const ReadableString& source, DsrChar separator) {
  815. int64_t sectionStart = 0;
  816. for (int64_t i = 0; i < source.length; i++) {
  817. DsrChar c = source[i];
  818. if (c == separator) {
  819. action(string_exclusiveRange(source, sectionStart, i));
  820. sectionStart = i + 1;
  821. }
  822. }
  823. if (source.length > sectionStart) {
  824. action(string_exclusiveRange(source, sectionStart, source.length));;
  825. }
  826. }
  827. void dsr::string_split_inPlace(List<ReadableString> &target, const ReadableString& source, DsrChar separator, bool appendResult) {
  828. if (!appendResult) {
  829. target.clear();
  830. }
  831. string_split_callback([&target](ReadableString section){
  832. target.push(section);
  833. }, source, separator);
  834. }
  835. List<ReadableString> dsr::string_split(const ReadableString& source, DsrChar separator) {
  836. List<ReadableString> result;
  837. string_split_inPlace(result, source, separator);
  838. return result;
  839. }
  840. int64_t dsr::string_toInteger(const ReadableString& source) {
  841. int64_t result;
  842. bool negated;
  843. result = 0;
  844. negated = false;
  845. for (int64_t i = 0; i < source.length; i++) {
  846. DsrChar c = source[i];
  847. if (c == '-' || c == '~') {
  848. negated = !negated;
  849. } else if (c >= '0' && c <= '9') {
  850. result = (result * 10) + (int)(c - '0');
  851. } else if (c == ',' || c == '.') {
  852. // Truncate any decimals by ignoring them
  853. break;
  854. }
  855. }
  856. if (negated) {
  857. return -result;
  858. } else {
  859. return result;
  860. }
  861. }
  862. double dsr::string_toDouble(const ReadableString& source) {
  863. double result;
  864. bool negated;
  865. bool reachedDecimal;
  866. int64_t digitDivider;
  867. result = 0.0;
  868. negated = false;
  869. reachedDecimal = false;
  870. digitDivider = 1;
  871. for (int64_t i = 0; i < source.length; i++) {
  872. DsrChar c = source[i];
  873. if (c == '-' || c == '~') {
  874. negated = !negated;
  875. } else if (c >= '0' && c <= '9') {
  876. if (reachedDecimal) {
  877. digitDivider = digitDivider * 10;
  878. result = result + ((double)(c - '0') / (double)digitDivider);
  879. } else {
  880. result = (result * 10) + (double)(c - '0');
  881. }
  882. } else if (c == ',' || c == '.') {
  883. reachedDecimal = true;
  884. }
  885. }
  886. if (negated) {
  887. return -result;
  888. } else {
  889. return result;
  890. }
  891. }
  892. int64_t dsr::string_length(const ReadableString& source) {
  893. return source.length;
  894. }
  895. int64_t dsr::string_findFirst(const ReadableString& source, DsrChar toFind, int64_t startIndex) {
  896. for (int64_t i = startIndex; i < source.length; i++) {
  897. if (source[i] == toFind) {
  898. return i;
  899. }
  900. }
  901. return -1;
  902. }
  903. int64_t dsr::string_findLast(const ReadableString& source, DsrChar toFind) {
  904. for (int64_t i = source.length - 1; i >= 0; i--) {
  905. if (source[i] == toFind) {
  906. return i;
  907. }
  908. }
  909. return -1;
  910. }
  911. ReadableString dsr::string_exclusiveRange(const ReadableString& source, int64_t inclusiveStart, int64_t exclusiveEnd) {
  912. // Return empty string for each complete miss
  913. if (inclusiveStart >= source.length || exclusiveEnd <= 0) { return ReadableString(); }
  914. // Automatically clamping to valid range
  915. if (inclusiveStart < 0) { inclusiveStart = 0; }
  916. if (exclusiveEnd > source.length) { exclusiveEnd = source.length; }
  917. // Return the overlapping interval
  918. return createSubString(&(source.readSection[inclusiveStart]), exclusiveEnd - inclusiveStart);
  919. }
  920. ReadableString dsr::string_inclusiveRange(const ReadableString& source, int64_t inclusiveStart, int64_t inclusiveEnd) {
  921. return string_exclusiveRange(source, inclusiveStart, inclusiveEnd + 1);
  922. }
  923. ReadableString dsr::string_before(const ReadableString& source, int64_t exclusiveEnd) {
  924. return string_exclusiveRange(source, 0, exclusiveEnd);
  925. }
  926. ReadableString dsr::string_until(const ReadableString& source, int64_t inclusiveEnd) {
  927. return string_inclusiveRange(source, 0, inclusiveEnd);
  928. }
  929. ReadableString dsr::string_from(const ReadableString& source, int64_t inclusiveStart) {
  930. return string_exclusiveRange(source, inclusiveStart, source.length);
  931. }
  932. ReadableString dsr::string_after(const ReadableString& source, int64_t exclusiveStart) {
  933. return string_from(source, exclusiveStart + 1);
  934. }
  935. bool dsr::character_isDigit(DsrChar c) {
  936. return c >= U'0' && c <= U'9';
  937. }
  938. bool dsr::character_isIntegerCharacter(DsrChar c) {
  939. return c == U'-' || character_isDigit(c);
  940. }
  941. bool dsr::character_isValueCharacter(DsrChar c) {
  942. return c == U'.' || character_isIntegerCharacter(c);
  943. }
  944. bool dsr::character_isWhiteSpace(DsrChar c) {
  945. return c == U' ' || c == U'\t' || c == U'\v' || c == U'\f' || c == U'\n' || c == U'\r';
  946. }
  947. // Macros for implementing regular expressions with a greedy approach consuming the first match
  948. // Optional accepts 0 or 1 occurence
  949. // Forced accepts 1 occurence
  950. // Star accepts 0..N occurence
  951. // Plus accepts 1..N occurence
  952. #define CHARACTER_OPTIONAL(CHARACTER) if (source[readIndex] == CHARACTER) { readIndex++; }
  953. #define CHARACTER_FORCED(CHARACTER) if (source[readIndex] == CHARACTER) { readIndex++; } else { return false; }
  954. #define CHARACTER_STAR(CHARACTER) while (source[readIndex] == CHARACTER) { readIndex++; }
  955. #define CHARACTER_PLUS(CHARACTER) CHARACTER_FORCED(CHARACTER) CHARACTER_STAR(CHARACTER)
  956. #define PATTERN_OPTIONAL(PATTERN) if (character_is##PATTERN(source[readIndex])) { readIndex++; }
  957. #define PATTERN_FORCED(PATTERN) if (character_is##PATTERN(source[readIndex])) { readIndex++; } else { return false; }
  958. #define PATTERN_STAR(PATTERN) while (character_is##PATTERN(source[readIndex])) { readIndex++; }
  959. #define PATTERN_PLUS(PATTERN) PATTERN_FORCED(PATTERN) PATTERN_STAR(PATTERN)
  960. // The greedy approach works here, because there's no ambiguity
  961. bool dsr::string_isInteger(const ReadableString& source, bool allowWhiteSpace) {
  962. int64_t readIndex = 0;
  963. if (allowWhiteSpace) {
  964. PATTERN_STAR(WhiteSpace);
  965. }
  966. CHARACTER_OPTIONAL(U'-');
  967. // At least one digit required
  968. PATTERN_PLUS(IntegerCharacter);
  969. if (allowWhiteSpace) {
  970. PATTERN_STAR(WhiteSpace);
  971. }
  972. return true;
  973. }
  974. // To avoid consuming the all digits on Digit* before reaching Digit+ when there is no decimal, whole integers are judged by string_isInteger
  975. bool dsr::string_isDouble(const ReadableString& source, bool allowWhiteSpace) {
  976. // Solving the UnsignedDouble <- Digit+ | Digit* '.' Digit+ ambiguity is done easiest by checking if there's a decimal before handling the white-space and negation
  977. if (string_findFirst(source, U'.') == -1) {
  978. // No decimal detected
  979. return string_isInteger(source, allowWhiteSpace);
  980. } else {
  981. int64_t readIndex = 0;
  982. if (allowWhiteSpace) {
  983. PATTERN_STAR(WhiteSpace);
  984. }
  985. // Double <- UnsignedDouble | '-' UnsignedDouble
  986. CHARACTER_OPTIONAL(U'-');
  987. // UnsignedDouble <- Digit* '.' Digit+
  988. // Any number of integer digits
  989. PATTERN_STAR(IntegerCharacter);
  990. // Only dot for decimal
  991. CHARACTER_FORCED(U'.')
  992. // At least one decimal digit
  993. PATTERN_PLUS(IntegerCharacter);
  994. if (allowWhiteSpace) {
  995. PATTERN_STAR(WhiteSpace);
  996. }
  997. return true;
  998. }
  999. }