strconv.odin 23 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169
  1. package strconv
  2. import "core:unicode/utf8"
  3. import "decimal"
  4. parse_bool :: proc(s: string, n: ^int = nil) -> (result: bool = false, ok: bool) {
  5. switch s {
  6. case "1", "t", "T", "true", "TRUE", "True":
  7. if n != nil { n^ = len(s) }
  8. return true, true
  9. case "0", "f", "F", "false", "FALSE", "False":
  10. if n != nil { n^ = len(s) }
  11. return false, true
  12. }
  13. return
  14. }
  15. _digit_value :: proc(r: rune) -> int {
  16. ri := int(r)
  17. v: int = 16
  18. switch r {
  19. case '0'..='9': v = ri-'0'
  20. case 'a'..='z': v = ri-'a'+10
  21. case 'A'..='Z': v = ri-'A'+10
  22. }
  23. return v
  24. }
  25. // Parses an integer value from a string, in the given base, without a prefix.
  26. //
  27. // Returns ok=false if no numeric value of the appropriate base could be found,
  28. // or if the input string contained more than just the number.
  29. //
  30. // ```
  31. // n, ok := strconv.parse_i64_of_base("-1234eeee", 10);
  32. // assert(n == -1234 && ok);
  33. // ```
  34. parse_i64_of_base :: proc(str: string, base: int, n: ^int = nil) -> (value: i64, ok: bool) {
  35. assert(base <= 16, "base must be 1-16")
  36. s := str
  37. defer if n != nil { n^ = len(str)-len(s) }
  38. if s == "" {
  39. return
  40. }
  41. neg := false
  42. if len(s) > 1 {
  43. switch s[0] {
  44. case '-':
  45. neg = true
  46. s = s[1:]
  47. case '+':
  48. s = s[1:]
  49. }
  50. }
  51. i := 0
  52. for r in s {
  53. if r == '_' {
  54. i += 1
  55. continue
  56. }
  57. v := i64(_digit_value(r))
  58. if v >= i64(base) {
  59. break
  60. }
  61. value *= i64(base)
  62. value += v
  63. i += 1
  64. }
  65. s = s[i:]
  66. if neg {
  67. value = -value
  68. }
  69. ok = len(s) == 0
  70. return
  71. }
  72. // Parses a integer value from a string, in base 10, unless there's a prefix.
  73. //
  74. // Returns ok=false if a valid integer could not be found,
  75. // or if the input string contained more than just the number.
  76. //
  77. // ```
  78. // n, ok := strconv.parse_i64_maybe_prefixed("1234");
  79. // assert(n == 1234 && ok);
  80. //
  81. // n, ok = strconv.parse_i64_maybe_prefixed("0xeeee");
  82. // assert(n == 0xeeee && ok);
  83. // ```
  84. parse_i64_maybe_prefixed :: proc(str: string, n: ^int = nil) -> (value: i64, ok: bool) {
  85. s := str
  86. defer if n != nil { n^ = len(str)-len(s) }
  87. if s == "" {
  88. return
  89. }
  90. neg := false
  91. if len(s) > 1 {
  92. switch s[0] {
  93. case '-':
  94. neg = true
  95. s = s[1:]
  96. case '+':
  97. s = s[1:]
  98. }
  99. }
  100. base: i64 = 10
  101. if len(s) > 2 && s[0] == '0' {
  102. switch s[1] {
  103. case 'b': base = 2; s = s[2:]
  104. case 'o': base = 8; s = s[2:]
  105. case 'd': base = 10; s = s[2:]
  106. case 'z': base = 12; s = s[2:]
  107. case 'x': base = 16; s = s[2:]
  108. }
  109. }
  110. i := 0
  111. for r in s {
  112. if r == '_' {
  113. i += 1
  114. continue
  115. }
  116. v := i64(_digit_value(r))
  117. if v >= base {
  118. break
  119. }
  120. value *= base
  121. value += v
  122. i += 1
  123. }
  124. s = s[i:]
  125. if neg {
  126. value = -value
  127. }
  128. ok = len(s) == 0
  129. return
  130. }
  131. parse_i64 :: proc{parse_i64_maybe_prefixed, parse_i64_of_base}
  132. // Parses an unsigned integer value from a string, in the given base, and
  133. // without a prefix.
  134. //
  135. // Returns ok=false if no numeric value of the appropriate base could be found,
  136. // or if the input string contained more than just the number.
  137. //
  138. // ```
  139. // n, ok := strconv.parse_u64_of_base("1234eeee", 10);
  140. // assert(n == 1234 && ok);
  141. //
  142. // n, ok = strconv.parse_u64_of_base("5678eeee", 16);
  143. // assert(n == 0x5678eeee && ok);
  144. // ```
  145. parse_u64_of_base :: proc(str: string, base: int, n: ^int = nil) -> (value: u64, ok: bool) {
  146. assert(base <= 16, "base must be 1-16")
  147. s := str
  148. defer if n != nil { n^ = len(str)-len(s) }
  149. if s == "" {
  150. return
  151. }
  152. if len(s) > 1 && s[0] == '+' {
  153. s = s[1:]
  154. }
  155. i := 0
  156. for r in s {
  157. if r == '_' {
  158. i += 1
  159. continue
  160. }
  161. v := u64(_digit_value(r))
  162. if v >= u64(base) {
  163. break
  164. }
  165. value *= u64(base)
  166. value += v
  167. i += 1
  168. }
  169. s = s[i:]
  170. ok = len(s) == 0
  171. return
  172. }
  173. // Parses an unsigned integer value from a string in base 10, unless there's a prefix.
  174. //
  175. // Returns ok=false if a valid integer could not be found, if the value was negative,
  176. // or if the input string contained more than just the number.
  177. //
  178. // ```
  179. // n, ok := strconv.parse_u64_maybe_prefixed("1234");
  180. // assert(n == 1234 && ok);
  181. //
  182. // n, ok = strconv.parse_u64_maybe_prefixed("0xeeee");
  183. // assert(n == 0xeeee && ok);
  184. // ```
  185. parse_u64_maybe_prefixed :: proc(str: string, n: ^int = nil) -> (value: u64, ok: bool) {
  186. s := str
  187. defer if n != nil { n^ = len(str)-len(s) }
  188. if s == "" {
  189. return
  190. }
  191. if len(s) > 1 && s[0] == '+' {
  192. s = s[1:]
  193. }
  194. base := u64(10)
  195. if len(s) > 2 && s[0] == '0' {
  196. switch s[1] {
  197. case 'b': base = 2; s = s[2:]
  198. case 'o': base = 8; s = s[2:]
  199. case 'd': base = 10; s = s[2:]
  200. case 'z': base = 12; s = s[2:]
  201. case 'x': base = 16; s = s[2:]
  202. }
  203. }
  204. i := 0
  205. for r in s {
  206. if r == '_' {
  207. i += 1
  208. continue
  209. }
  210. v := u64(_digit_value(r))
  211. if v >= base {
  212. break
  213. }
  214. value *= base
  215. value += v
  216. i += 1
  217. }
  218. s = s[i:]
  219. ok = len(s) == 0
  220. return
  221. }
  222. parse_u64 :: proc{parse_u64_maybe_prefixed, parse_u64_of_base}
  223. // Parses an integer value from a string in the given base, or
  224. // - if the string has a prefix (e.g: '0x') then that will determine the base;
  225. // - otherwise, assumes base 10.
  226. //
  227. // Returns ok=false if no appropriate value could be found, or if the input string
  228. // contained more than just the number.
  229. //
  230. // ```
  231. // n, ok := strconv.parse_int("1234"); // without prefix, inferred base 10
  232. // assert(n == 1234 && ok);
  233. //
  234. // n, ok = strconv.parse_int("ffff", 16); // without prefix, explicit base
  235. // assert(n == 0xffff && ok);
  236. //
  237. // n, ok = strconv.parse_int("0xffff"); // with prefix and inferred base
  238. // assert(n == 0xffff && ok);
  239. // ```
  240. parse_int :: proc(s: string, base := 0, n: ^int = nil) -> (value: int, ok: bool) {
  241. v: i64 = ---
  242. switch base {
  243. case 0: v, ok = parse_i64_maybe_prefixed(s, n)
  244. case: v, ok = parse_i64_of_base(s, base, n)
  245. }
  246. value = int(v)
  247. return
  248. }
  249. // Parses an unsigned integer value from a string in the given base, or
  250. // - if the string has a prefix (e.g: '0x') then that will determine the base;
  251. // - otherwise, assumes base 10.
  252. //
  253. // Returns ok=false if:
  254. // - no appropriate value could be found; or
  255. // - the value was negative.
  256. // - the input string contained more than just the number.
  257. //
  258. // ```
  259. // n, ok := strconv.parse_uint("1234"); // without prefix, inferred base 10
  260. // assert(n == 1234 && ok);
  261. //
  262. // n, ok = strconv.parse_uint("ffff", 16); // without prefix, explicit base
  263. // assert(n == 0xffff && ok);
  264. //
  265. // n, ok = strconv.parse_uint("0xffff"); // with prefix and inferred base
  266. // assert(n == 0xffff && ok);
  267. // ```
  268. parse_uint :: proc(s: string, base := 0, n: ^int = nil) -> (value: uint, ok: bool) {
  269. v: u64 = ---
  270. switch base {
  271. case 0: v, ok = parse_u64_maybe_prefixed(s, n)
  272. case: v, ok = parse_u64_of_base(s, base, n)
  273. }
  274. value = uint(v)
  275. return
  276. }
  277. // Parses an integer value from a string, in the given base, without a prefix.
  278. //
  279. // Returns ok=false if no numeric value of the appropriate base could be found,
  280. // or if the input string contained more than just the number.
  281. //
  282. // ```
  283. // n, ok := strconv.parse_i128_of_base("-1234eeee", 10);
  284. // assert(n == -1234 && ok);
  285. // ```
  286. parse_i128_of_base :: proc(str: string, base: int, n: ^int = nil) -> (value: i128, ok: bool) {
  287. assert(base <= 16, "base must be 1-16")
  288. s := str
  289. defer if n != nil { n^ = len(str)-len(s) }
  290. if s == "" {
  291. return
  292. }
  293. neg := false
  294. if len(s) > 1 {
  295. switch s[0] {
  296. case '-':
  297. neg = true
  298. s = s[1:]
  299. case '+':
  300. s = s[1:]
  301. }
  302. }
  303. i := 0
  304. for r in s {
  305. if r == '_' {
  306. i += 1
  307. continue
  308. }
  309. v := i128(_digit_value(r))
  310. if v >= i128(base) {
  311. break
  312. }
  313. value *= i128(base)
  314. value += v
  315. i += 1
  316. }
  317. s = s[i:]
  318. if neg {
  319. value = -value
  320. }
  321. ok = len(s) == 0
  322. return
  323. }
  324. // Parses a integer value from a string, in base 10, unless there's a prefix.
  325. //
  326. // Returns ok=false if a valid integer could not be found,
  327. // or if the input string contained more than just the number.
  328. //
  329. // ```
  330. // n, ok := strconv.parse_i128_maybe_prefixed("1234");
  331. // assert(n == 1234 && ok);
  332. //
  333. // n, ok = strconv.parse_i128_maybe_prefixed("0xeeee");
  334. // assert(n == 0xeeee && ok);
  335. // ```
  336. parse_i128_maybe_prefixed :: proc(str: string, n: ^int = nil) -> (value: i128, ok: bool) {
  337. s := str
  338. defer if n != nil { n^ = len(str)-len(s) }
  339. if s == "" {
  340. return
  341. }
  342. neg := false
  343. if len(s) > 1 {
  344. switch s[0] {
  345. case '-':
  346. neg = true
  347. s = s[1:]
  348. case '+':
  349. s = s[1:]
  350. }
  351. }
  352. base: i128 = 10
  353. if len(s) > 2 && s[0] == '0' {
  354. switch s[1] {
  355. case 'b': base = 2; s = s[2:]
  356. case 'o': base = 8; s = s[2:]
  357. case 'd': base = 10; s = s[2:]
  358. case 'z': base = 12; s = s[2:]
  359. case 'x': base = 16; s = s[2:]
  360. }
  361. }
  362. i := 0
  363. for r in s {
  364. if r == '_' {
  365. i += 1
  366. continue
  367. }
  368. v := i128(_digit_value(r))
  369. if v >= base {
  370. break
  371. }
  372. value *= base
  373. value += v
  374. i += 1
  375. }
  376. s = s[i:]
  377. if neg {
  378. value = -value
  379. }
  380. ok = len(s) == 0
  381. return
  382. }
  383. parse_i128 :: proc{parse_i128_maybe_prefixed, parse_i128_of_base}
  384. // Parses an unsigned integer value from a string, in the given base, and
  385. // without a prefix.
  386. //
  387. // Returns ok=false if no numeric value of the appropriate base could be found,
  388. // or if the input string contained more than just the number.
  389. //
  390. // ```
  391. // n, ok := strconv.parse_u128_of_base("1234eeee", 10);
  392. // assert(n == 1234 && ok);
  393. //
  394. // n, ok = strconv.parse_u128_of_base("5678eeee", 16);
  395. // assert(n == 0x5678eeee && ok);
  396. // ```
  397. parse_u128_of_base :: proc(str: string, base: int, n: ^int = nil) -> (value: u128, ok: bool) {
  398. assert(base <= 16, "base must be 1-16")
  399. s := str
  400. defer if n != nil { n^ = len(str)-len(s) }
  401. if s == "" {
  402. return
  403. }
  404. if len(s) > 1 && s[0] == '+' {
  405. s = s[1:]
  406. }
  407. i := 0
  408. for r in s {
  409. if r == '_' {
  410. i += 1
  411. continue
  412. }
  413. v := u128(_digit_value(r))
  414. if v >= u128(base) {
  415. break
  416. }
  417. value *= u128(base)
  418. value += v
  419. i += 1
  420. }
  421. s = s[i:]
  422. ok = len(s) == 0
  423. return
  424. }
  425. // Parses an unsigned integer value from a string in base 10, unless there's a prefix.
  426. //
  427. // Returns ok=false if a valid integer could not be found, if the value was negative,
  428. // or if the input string contained more than just the number.
  429. //
  430. // ```
  431. // n, ok := strconv.parse_u128_maybe_prefixed("1234");
  432. // assert(n == 1234 && ok);
  433. //
  434. // n, ok = strconv.parse_u128_maybe_prefixed("0xeeee");
  435. // assert(n == 0xeeee && ok);
  436. // ```
  437. parse_u128_maybe_prefixed :: proc(str: string, n: ^int = nil) -> (value: u128, ok: bool) {
  438. s := str
  439. defer if n != nil { n^ = len(str)-len(s) }
  440. if s == "" {
  441. return
  442. }
  443. if len(s) > 1 && s[0] == '+' {
  444. s = s[1:]
  445. }
  446. base := u128(10)
  447. if len(s) > 2 && s[0] == '0' {
  448. switch s[1] {
  449. case 'b': base = 2; s = s[2:]
  450. case 'o': base = 8; s = s[2:]
  451. case 'd': base = 10; s = s[2:]
  452. case 'z': base = 12; s = s[2:]
  453. case 'x': base = 16; s = s[2:]
  454. }
  455. }
  456. i := 0
  457. for r in s {
  458. if r == '_' {
  459. i += 1
  460. continue
  461. }
  462. v := u128(_digit_value(r))
  463. if v >= base {
  464. break
  465. }
  466. value *= base
  467. value += v
  468. i += 1
  469. }
  470. s = s[i:]
  471. ok = len(s) == 0
  472. return
  473. }
  474. parse_u128 :: proc{parse_u128_maybe_prefixed, parse_u128_of_base}
  475. @(private)
  476. lower :: #force_inline proc "contextless" (ch: byte) -> byte { return ('a' - 'A') | ch }
  477. // Parses a 32-bit floating point number from a string.
  478. //
  479. // Returns ok=false if a base 10 float could not be found,
  480. // or if the input string contained more than just the number.
  481. //
  482. // ```
  483. // n, ok := strconv.parse_f32("12.34eee");
  484. // assert(n == 12.34 && ok);
  485. //
  486. // n, ok = strconv.parse_f32("12.34");
  487. // assert(n == 12.34 && ok);
  488. // ```
  489. parse_f32 :: proc(s: string, n: ^int = nil) -> (value: f32, ok: bool) {
  490. v: f64 = ---
  491. v, ok = parse_f64(s, n)
  492. return f32(v), ok
  493. }
  494. parse_f64 :: proc(str: string, n: ^int = nil) -> (value: f64, ok: bool) {
  495. nr: int
  496. value, nr, ok = parse_f64_prefix(str)
  497. if ok && len(str) != nr {
  498. ok = false
  499. }
  500. if n != nil { n^ = nr }
  501. return
  502. }
  503. // Parses a 32-bit floating point number from a string.
  504. //
  505. // Returns ok=false if a base 10 float could not be found,
  506. // or if the input string contained more than just the number.
  507. //
  508. // ```
  509. // n, _, ok := strconv.parse_f32("12.34eee");
  510. // assert(n == 12.34 && ok);
  511. //
  512. // n, _, ok = strconv.parse_f32("12.34");
  513. // assert(n == 12.34 && ok);
  514. // ```
  515. parse_f32_prefix :: proc(str: string) -> (value: f32, nr: int, ok: bool) {
  516. f: f64
  517. f, nr, ok = parse_f64_prefix(str)
  518. value = f32(f)
  519. return
  520. }
  521. // Parses a 64-bit floating point number from a string.
  522. //
  523. // Returns ok=false if a base 10 float could not be found,
  524. // or if the input string contained more than just the number.
  525. //
  526. // ```
  527. // n, _, ok := strconv.parse_f32("12.34eee");
  528. // assert(n == 12.34 && ok);
  529. //
  530. // n, _, ok = strconv.parse_f32("12.34");
  531. // assert(n == 12.34 && ok);
  532. // ```
  533. parse_f64_prefix :: proc(str: string) -> (value: f64, nr: int, ok: bool) {
  534. common_prefix_len_ignore_case :: proc "contextless" (s, prefix: string) -> int {
  535. n := len(prefix)
  536. if n > len(s) {
  537. n = len(s)
  538. }
  539. for i in 0..<n {
  540. c := s[i]
  541. if 'A' <= c && c <= 'Z' {
  542. c += 'a' - 'A'
  543. }
  544. if c != prefix[i] {
  545. return i
  546. }
  547. }
  548. return n
  549. }
  550. check_special :: proc "contextless" (s: string) -> (f: f64, n: int, ok: bool) {
  551. s := s
  552. if len(s) > 0 {
  553. sign := 1
  554. nsign := 0
  555. switch s[0] {
  556. case '+', '-':
  557. if s[0] == '-' {
  558. sign = -1
  559. }
  560. nsign = 1
  561. s = s[1:]
  562. fallthrough
  563. case 'i', 'I':
  564. n := common_prefix_len_ignore_case(s, "infinity")
  565. if 3 < n && n < 8 { // "inf" or "infinity"
  566. n = 3
  567. }
  568. if n == 3 || n == 8 {
  569. f = 0h7ff00000_00000000 if sign == 1 else 0hfff00000_00000000
  570. n = nsign + 3
  571. ok = true
  572. return
  573. }
  574. case 'n', 'N':
  575. if common_prefix_len_ignore_case(s, "nan") == 3 {
  576. f = 0h7ff80000_00000001
  577. n = nsign + 3
  578. ok = true
  579. return
  580. }
  581. }
  582. }
  583. return
  584. }
  585. parse_components :: proc "contextless" (s: string) -> (mantissa: u64, exp: int, neg, trunc, hex: bool, i: int, ok: bool) {
  586. if len(s) == 0 {
  587. return
  588. }
  589. switch s[i] {
  590. case '+': i += 1
  591. case '-': i += 1; neg = true
  592. }
  593. base := u64(10)
  594. MAX_MANT_DIGITS := 19
  595. exp_char := byte('e')
  596. // support stupid 0x1.ABp100 hex floats even if Odin doesn't
  597. if i+2 < len(s) && s[i] == '0' && lower(s[i+1]) == 'x' {
  598. base = 16
  599. MAX_MANT_DIGITS = 16
  600. i += 2
  601. exp_char = 'p'
  602. hex = true
  603. }
  604. underscores := false
  605. saw_dot, saw_digits := false, false
  606. nd := 0
  607. nd_mant := 0
  608. decimal_point := 0
  609. loop: for ; i < len(s); i += 1 {
  610. switch c := s[i]; true {
  611. case c == '_':
  612. underscores = true
  613. continue loop
  614. case c == '.':
  615. if saw_dot {
  616. break loop
  617. }
  618. saw_dot = true
  619. decimal_point = nd
  620. continue loop
  621. case '0' <= c && c <= '9':
  622. saw_digits = true
  623. if c == '0' && nd == 0 {
  624. decimal_point -= 1
  625. continue loop
  626. }
  627. nd += 1
  628. if nd_mant < MAX_MANT_DIGITS {
  629. mantissa *= base
  630. mantissa += u64(c - '0')
  631. nd_mant += 1
  632. } else if c != '0' {
  633. trunc = true
  634. }
  635. continue loop
  636. case base == 16 && 'a' <= lower(c) && lower(c) <= 'f':
  637. saw_digits = true
  638. nd += 1
  639. if nd_mant < MAX_MANT_DIGITS {
  640. mantissa *= 16
  641. mantissa += u64(lower(c) - 'a' + 10)
  642. nd_mant += 1
  643. } else {
  644. trunc = true
  645. }
  646. continue loop
  647. }
  648. break loop
  649. }
  650. if !saw_digits {
  651. return
  652. }
  653. if !saw_dot {
  654. decimal_point = nd
  655. }
  656. if base == 16 {
  657. decimal_point *= 4
  658. nd_mant *= 4
  659. }
  660. if i < len(s) && lower(s[i]) == exp_char {
  661. i += 1
  662. if i >= len(s) { return }
  663. exp_sign := 1
  664. switch s[i] {
  665. case '+': i += 1
  666. case '-': i += 1; exp_sign = -1
  667. }
  668. if i >= len(s) || s[i] < '0' || s[i] > '9' {
  669. return
  670. }
  671. e := 0
  672. for ; i < len(s) && ('0' <= s[i] && s[i] <= '9' || s[i] == '_'); i += 1 {
  673. if s[i] == '_' {
  674. underscores = true
  675. continue
  676. }
  677. if e < 1e5 {
  678. e = e*10 + int(s[i]) - '0'
  679. }
  680. }
  681. decimal_point += e * exp_sign
  682. } else if base == 16 {
  683. return
  684. }
  685. if mantissa != 0 {
  686. exp = decimal_point - nd_mant
  687. }
  688. ok = true
  689. return
  690. }
  691. parse_hex :: proc "contextless" (s: string, mantissa: u64, exp: int, neg, trunc: bool) -> (f64, bool) {
  692. info := &_f64_info
  693. mantissa, exp := mantissa, exp
  694. MAX_EXP := 1<<info.expbits + info.bias - 2
  695. MIN_EXP := info.bias + 1
  696. exp += int(info.mantbits)
  697. for mantissa != 0 && mantissa >> (info.mantbits+2) == 0 {
  698. mantissa <<= 1
  699. exp -= 1
  700. }
  701. if trunc {
  702. mantissa |= 1
  703. }
  704. for mantissa != 0 && mantissa >> (info.mantbits+2) == 0 {
  705. mantissa = mantissa>>1 | mantissa&1
  706. exp += 1
  707. }
  708. // denormalize
  709. if mantissa > 1 && exp < MIN_EXP-2 {
  710. mantissa = mantissa>>1 | mantissa&1
  711. exp += 1
  712. }
  713. round := mantissa & 3
  714. mantissa >>= 2
  715. round |= mantissa & 1 // round to even
  716. exp += 2
  717. if round == 3 {
  718. mantissa += 1
  719. if mantissa == 1 << (1 + info.mantbits) {
  720. mantissa >>= 1
  721. exp += 1
  722. }
  723. }
  724. if mantissa>>info.mantbits == 0 {
  725. // zero or denormal
  726. exp = info.bias
  727. }
  728. ok := true
  729. if exp > MAX_EXP {
  730. // infinity or invalid
  731. mantissa = 1<<info.mantbits
  732. exp = MAX_EXP + 1
  733. ok = false
  734. }
  735. bits := mantissa & (1<<info.mantbits - 1)
  736. bits |= u64((exp-info.bias) & (1<<info.expbits - 1)) << info.mantbits
  737. if neg {
  738. bits |= 1 << info.mantbits << info.expbits
  739. }
  740. return transmute(f64)bits, ok
  741. }
  742. if value, nr, ok = check_special(str); ok {
  743. return
  744. }
  745. mantissa: u64
  746. exp: int
  747. neg, trunc, hex: bool
  748. mantissa, exp, neg, trunc, hex, nr = parse_components(str) or_return
  749. if hex {
  750. value, ok = parse_hex(str, mantissa, exp, neg, trunc)
  751. return
  752. }
  753. trunc_block: if !trunc {
  754. @static pow10 := [?]f64{
  755. 1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9,
  756. 1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19,
  757. 1e20, 1e21, 1e22,
  758. }
  759. if mantissa>>_f64_info.mantbits != 0 {
  760. break trunc_block
  761. }
  762. f := f64(mantissa)
  763. if neg {
  764. f = -f
  765. }
  766. switch {
  767. case exp == 0:
  768. return f, nr, true
  769. case exp > 0 && exp <= 15+22:
  770. if exp > 22 {
  771. f *= pow10[exp-22]
  772. exp = 22
  773. }
  774. if f > 1e15 || f < 1e-15 {
  775. break trunc_block
  776. }
  777. return f * pow10[exp], nr, true
  778. case -22 <= exp && exp < 0:
  779. return f / pow10[-exp], nr, true
  780. }
  781. }
  782. d: decimal.Decimal
  783. decimal.set(&d, str[:nr])
  784. b, overflow := decimal_to_float_bits(&d, &_f64_info)
  785. value = transmute(f64)b
  786. ok = !overflow
  787. return
  788. }
  789. append_bool :: proc(buf: []byte, b: bool) -> string {
  790. n := 0
  791. if b {
  792. n = copy(buf, "true")
  793. } else {
  794. n = copy(buf, "false")
  795. }
  796. return string(buf[:n])
  797. }
  798. append_uint :: proc(buf: []byte, u: u64, base: int) -> string {
  799. return append_bits(buf, u, base, false, 8*size_of(uint), digits, nil)
  800. }
  801. append_int :: proc(buf: []byte, i: i64, base: int) -> string {
  802. return append_bits(buf, u64(i), base, true, 8*size_of(int), digits, nil)
  803. }
  804. itoa :: proc(buf: []byte, i: int) -> string {
  805. return append_int(buf, i64(i), 10)
  806. }
  807. atoi :: proc(s: string) -> int {
  808. v, _ := parse_int(s)
  809. return v
  810. }
  811. atof :: proc(s: string) -> f64 {
  812. v, _ := parse_f64(s)
  813. return v
  814. }
  815. ftoa :: append_float
  816. append_float :: proc(buf: []byte, f: f64, fmt: byte, prec, bit_size: int) -> string {
  817. return string(generic_ftoa(buf, f, fmt, prec, bit_size))
  818. }
  819. quote :: proc(buf: []byte, str: string) -> string {
  820. write_byte :: proc(buf: []byte, i: ^int, bytes: ..byte) {
  821. if i^ >= len(buf) {
  822. return
  823. }
  824. n := copy(buf[i^:], bytes[:])
  825. i^ += n
  826. }
  827. if buf == nil {
  828. return ""
  829. }
  830. c :: '"'
  831. i := 0
  832. s := str
  833. write_byte(buf, &i, c)
  834. for width := 0; len(s) > 0; s = s[width:] {
  835. r := rune(s[0])
  836. width = 1
  837. if r >= utf8.RUNE_SELF {
  838. r, width = utf8.decode_rune_in_string(s)
  839. }
  840. if width == 1 && r == utf8.RUNE_ERROR {
  841. write_byte(buf, &i, '\\', 'x')
  842. write_byte(buf, &i, digits[s[0]>>4])
  843. write_byte(buf, &i, digits[s[0]&0xf])
  844. }
  845. if i < len(buf) {
  846. x := quote_rune(buf[i:], r)
  847. i += len(x)
  848. }
  849. }
  850. write_byte(buf, &i, c)
  851. return string(buf[:i])
  852. }
  853. quote_rune :: proc(buf: []byte, r: rune) -> string {
  854. write_byte :: proc(buf: []byte, i: ^int, bytes: ..byte) {
  855. if i^ < len(buf) {
  856. n := copy(buf[i^:], bytes[:])
  857. i^ += n
  858. }
  859. }
  860. write_string :: proc(buf: []byte, i: ^int, s: string) {
  861. if i^ < len(buf) {
  862. n := copy(buf[i^:], s)
  863. i^ += n
  864. }
  865. }
  866. write_rune :: proc(buf: []byte, i: ^int, r: rune) {
  867. if i^ < len(buf) {
  868. b, w := utf8.encode_rune(r)
  869. n := copy(buf[i^:], b[:w])
  870. i^ += n
  871. }
  872. }
  873. if buf == nil {
  874. return ""
  875. }
  876. i := 0
  877. write_byte(buf, &i, '\'')
  878. switch r {
  879. case '\a': write_string(buf, &i, "\\a")
  880. case '\b': write_string(buf, &i, "\\b")
  881. case '\e': write_string(buf, &i, "\\e")
  882. case '\f': write_string(buf, &i, "\\f")
  883. case '\n': write_string(buf, &i, "\\n")
  884. case '\r': write_string(buf, &i, "\\r")
  885. case '\t': write_string(buf, &i, "\\t")
  886. case '\v': write_string(buf, &i, "\\v")
  887. case:
  888. if r < 32 {
  889. write_string(buf, &i, "\\x")
  890. b: [2]byte
  891. s := append_bits(b[:], u64(r), 16, true, 64, digits, nil)
  892. switch len(s) {
  893. case 0: write_string(buf, &i, "00")
  894. case 1: write_rune(buf, &i, '0')
  895. case 2: write_string(buf, &i, s)
  896. }
  897. } else {
  898. write_rune(buf, &i, r)
  899. }
  900. }
  901. write_byte(buf, &i, '\'')
  902. return string(buf[:i])
  903. }
  904. unquote_char :: proc(str: string, quote: byte) -> (r: rune, multiple_bytes: bool, tail_string: string, success: bool) {
  905. hex_to_int :: proc(c: byte) -> int {
  906. switch c {
  907. case '0'..='9': return int(c-'0')
  908. case 'a'..='f': return int(c-'a')+10
  909. case 'A'..='F': return int(c-'A')+10
  910. }
  911. return -1
  912. }
  913. w: int
  914. if str[0] == quote && quote == '"' {
  915. return
  916. } else if str[0] >= 0x80 {
  917. r, w = utf8.decode_rune_in_string(str)
  918. return r, true, str[w:], true
  919. } else if str[0] != '\\' {
  920. return rune(str[0]), false, str[1:], true
  921. }
  922. if len(str) <= 1 {
  923. return
  924. }
  925. s := str
  926. c := s[1]
  927. s = s[2:]
  928. switch c {
  929. case:
  930. return
  931. case 'a': r = '\a'
  932. case 'b': r = '\b'
  933. case 'f': r = '\f'
  934. case 'n': r = '\n'
  935. case 'r': r = '\r'
  936. case 't': r = '\t'
  937. case 'v': r = '\v'
  938. case '\\': r = '\\'
  939. case '"': r = '"'
  940. case '\'': r = '\''
  941. case '0'..='7':
  942. v := int(c-'0')
  943. if len(s) < 2 {
  944. return
  945. }
  946. for i in 0..<len(s) {
  947. d := int(s[i]-'0')
  948. if d < 0 || d > 7 {
  949. return
  950. }
  951. v = (v<<3) | d
  952. }
  953. s = s[2:]
  954. if v > 0xff {
  955. return
  956. }
  957. r = rune(v)
  958. case 'x', 'u', 'U':
  959. count: int
  960. switch c {
  961. case 'x': count = 2
  962. case 'u': count = 4
  963. case 'U': count = 8
  964. }
  965. if len(s) < count {
  966. return
  967. }
  968. for i in 0..<count {
  969. d := hex_to_int(s[i])
  970. if d < 0 {
  971. return
  972. }
  973. r = (r<<4) | rune(d)
  974. }
  975. s = s[count:]
  976. if c == 'x' {
  977. break
  978. }
  979. if r > utf8.MAX_RUNE {
  980. return
  981. }
  982. multiple_bytes = true
  983. }
  984. success = true
  985. tail_string = s
  986. return
  987. }
  988. unquote_string :: proc(lit: string, allocator := context.allocator) -> (res: string, allocated, success: bool) {
  989. contains_rune :: proc(s: string, r: rune) -> int {
  990. for c, offset in s {
  991. if c == r {
  992. return offset
  993. }
  994. }
  995. return -1
  996. }
  997. if len(lit) < 2 {
  998. return
  999. }
  1000. if lit[0] == '`' {
  1001. return lit[1:len(lit)-1], false, true
  1002. }
  1003. s := lit
  1004. quote := '"'
  1005. if s == `""` {
  1006. return "", false, true
  1007. }
  1008. s = s[1:len(s)-1]
  1009. if contains_rune(s, '\n') >= 0 {
  1010. return s, false, false
  1011. }
  1012. if contains_rune(s, '\\') < 0 && contains_rune(s, quote) < 0 {
  1013. if quote == '"' {
  1014. return s, false, true
  1015. }
  1016. }
  1017. context.allocator = allocator
  1018. buf_len := 3*len(s) / 2
  1019. buf := make([]byte, buf_len)
  1020. offset := 0
  1021. for len(s) > 0 {
  1022. r, multiple_bytes, tail_string, ok := unquote_char(s, byte(quote))
  1023. if !ok {
  1024. delete(buf)
  1025. return s, false, false
  1026. }
  1027. s = tail_string
  1028. if r < 0x80 || !multiple_bytes {
  1029. buf[offset] = byte(r)
  1030. offset += 1
  1031. } else {
  1032. b, w := utf8.encode_rune(r)
  1033. copy(buf[offset:], b[:w])
  1034. offset += w
  1035. }
  1036. }
  1037. new_string := string(buf[:offset])
  1038. return new_string, true, true
  1039. }