strconv.odin 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140
  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. // Parses a 64-bit floating point number from a string.
  495. //
  496. // Returns ok=false if a base 10 float could not be found,
  497. // or if the input string contained more than just the number.
  498. //
  499. // ```
  500. // n, ok := strconv.parse_f32("12.34eee");
  501. // assert(n == 12.34 && ok);
  502. //
  503. // n, ok = strconv.parse_f32("12.34");
  504. // assert(n == 12.34 && ok);
  505. // ```
  506. parse_f64 :: proc(str: string, n: ^int = nil) -> (value: f64, ok: bool) {
  507. common_prefix_len_ignore_case :: proc "contextless" (s, prefix: string) -> int {
  508. n := len(prefix)
  509. if n > len(s) {
  510. n = len(s)
  511. }
  512. for i in 0..<n {
  513. c := s[i]
  514. if 'A' <= c && c <= 'Z' {
  515. c += 'a' - 'A'
  516. }
  517. if c != prefix[i] {
  518. return i
  519. }
  520. }
  521. return n
  522. }
  523. check_special :: proc "contextless" (s: string) -> (f: f64, n: int, ok: bool) {
  524. s := s
  525. if len(s) > 0 {
  526. sign := 1
  527. nsign := 0
  528. switch s[0] {
  529. case '+', '-':
  530. if s[0] == '-' {
  531. sign = -1
  532. }
  533. nsign = 1
  534. s = s[1:]
  535. fallthrough
  536. case 'i', 'I':
  537. n := common_prefix_len_ignore_case(s, "infinity")
  538. if 3 < n && n < 8 { // "inf" or "infinity"
  539. n = 3
  540. }
  541. if n == 3 || n == 8 {
  542. f = 0h7ff00000_00000000 if sign == 1 else 0hfff00000_00000000
  543. n = nsign + 3
  544. ok = true
  545. return
  546. }
  547. case 'n', 'N':
  548. if common_prefix_len_ignore_case(s, "nan") == 3 {
  549. f = 0h7ff80000_00000001
  550. n = nsign + 3
  551. ok = true
  552. return
  553. }
  554. }
  555. }
  556. return
  557. }
  558. parse_components :: proc "contextless" (s: string) -> (mantissa: u64, exp: int, neg, trunc, hex: bool, i: int, ok: bool) {
  559. if len(s) == 0 {
  560. return
  561. }
  562. switch s[i] {
  563. case '+': i += 1
  564. case '-': i += 1; neg = true
  565. }
  566. base := u64(10)
  567. MAX_MANT_DIGITS := 19
  568. exp_char := byte('e')
  569. // support stupid 0x1.ABp100 hex floats even if Odin doesn't
  570. if i+2 < len(s) && s[i] == '0' && lower(s[i+1]) == 'x' {
  571. base = 16
  572. MAX_MANT_DIGITS = 16
  573. i += 2
  574. exp_char = 'p'
  575. hex = true
  576. }
  577. underscores := false
  578. saw_dot, saw_digits := false, false
  579. nd := 0
  580. nd_mant := 0
  581. decimal_point := 0
  582. loop: for ; i < len(s); i += 1 {
  583. switch c := s[i]; true {
  584. case c == '_':
  585. underscores = true
  586. continue loop
  587. case c == '.':
  588. if saw_dot {
  589. break loop
  590. }
  591. saw_dot = true
  592. decimal_point = nd
  593. continue loop
  594. case '0' <= c && c <= '9':
  595. saw_digits = true
  596. if c == '0' && nd == 0 {
  597. decimal_point -= 1
  598. continue loop
  599. }
  600. nd += 1
  601. if nd_mant < MAX_MANT_DIGITS {
  602. mantissa *= base
  603. mantissa += u64(c - '0')
  604. nd_mant += 1
  605. } else if c != '0' {
  606. trunc = true
  607. }
  608. continue loop
  609. case base == 16 && 'a' <= lower(c) && lower(c) <= 'f':
  610. saw_digits = true
  611. nd += 1
  612. if nd_mant < MAX_MANT_DIGITS {
  613. MAX_MANT_DIGITS *= 16
  614. MAX_MANT_DIGITS += int(lower(c) - 'a' + 10)
  615. nd_mant += 1
  616. } else {
  617. trunc = true
  618. }
  619. continue loop
  620. }
  621. break loop
  622. }
  623. if !saw_digits {
  624. return
  625. }
  626. if !saw_dot {
  627. decimal_point = nd
  628. }
  629. if base == 16 {
  630. decimal_point *= 4
  631. nd_mant *= 4
  632. }
  633. if i < len(s) && lower(s[i]) == exp_char {
  634. i += 1
  635. if i >= len(s) { return }
  636. exp_sign := 1
  637. switch s[i] {
  638. case '+': i += 1
  639. case '-': i += 1; exp_sign = -1
  640. }
  641. if i >= len(s) || s[i] < '0' || s[i] > '9' {
  642. return
  643. }
  644. e := 0
  645. for ; i < len(s) && ('0' <= s[i] && s[i] <= '9' || s[i] == '_'); i += 1 {
  646. if s[i] == '_' {
  647. underscores = true
  648. continue
  649. }
  650. if e < 1e5 {
  651. e = e*10 + int(s[i]) - '0'
  652. }
  653. }
  654. decimal_point += e * exp_sign
  655. } else if base == 16 {
  656. return
  657. }
  658. if mantissa != 0 {
  659. exp = decimal_point - nd_mant
  660. }
  661. // TODO(bill): check underscore correctness
  662. ok = true
  663. return
  664. }
  665. parse_hex :: proc(s: string, mantissa: u64, exp: int, neg, trunc: bool) -> (f64, bool) {
  666. info := &_f64_info
  667. mantissa, exp := mantissa, exp
  668. MAX_EXP := 1<<info.expbits + info.bias - 2
  669. MIN_EXP := info.bias + 1
  670. exp += int(info.mantbits)
  671. for mantissa != 0 && mantissa >> (info.mantbits+2) == 0 {
  672. mantissa <<= 1
  673. exp -= 1
  674. }
  675. if trunc {
  676. mantissa |= 1
  677. }
  678. for mantissa >> (info.mantbits+2) == 0 {
  679. mantissa = mantissa>>1 | mantissa&1
  680. exp += 1
  681. }
  682. // denormalize
  683. if mantissa > 1 && exp < MIN_EXP-2 {
  684. mantissa = mantissa>>1 | mantissa&1
  685. exp += 1
  686. }
  687. round := mantissa & 3
  688. mantissa >>= 2
  689. round |= mantissa & 1 // round to even
  690. exp += 2
  691. if round == 3 {
  692. mantissa += 1
  693. if mantissa == 1 << (1 + info.mantbits) {
  694. mantissa >>= 1
  695. exp += 1
  696. }
  697. }
  698. if mantissa>>info.mantbits == 0 {
  699. // zero or denormal
  700. exp = info.bias
  701. }
  702. ok := true
  703. if exp > MAX_EXP {
  704. // infinity or invalid
  705. mantissa = 1<<info.mantbits
  706. exp = MAX_EXP + 1
  707. ok = false
  708. }
  709. bits := mantissa & (1<<info.mantbits - 1)
  710. bits |= u64((exp-info.bias) & (1<<info.expbits - 1)) << info.mantbits
  711. if neg {
  712. bits |= 1 << info.mantbits << info.expbits
  713. }
  714. return transmute(f64)bits, ok
  715. }
  716. nr: int
  717. defer if n != nil { n^ = nr }
  718. if value, nr, ok = check_special(str); ok {
  719. return
  720. }
  721. mantissa: u64
  722. exp: int
  723. neg, trunc, hex: bool
  724. mantissa, exp, neg, trunc, hex, nr = parse_components(str) or_return
  725. if hex {
  726. return parse_hex(str, mantissa, exp, neg, trunc)
  727. }
  728. trunc_block: if !trunc {
  729. @static pow10 := [?]f64{
  730. 1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9,
  731. 1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19,
  732. 1e20, 1e21, 1e22,
  733. }
  734. if mantissa>>_f64_info.mantbits != 0 {
  735. break trunc_block
  736. }
  737. f := f64(mantissa)
  738. if neg {
  739. f = -f
  740. }
  741. switch {
  742. case exp == 0:
  743. return f, true
  744. case exp > 0 && exp <= 15+22:
  745. if exp > 22 {
  746. f *= pow10[exp-22]
  747. exp = 22
  748. }
  749. if f > 1e15 || f < 1e-15 {
  750. break trunc_block
  751. }
  752. return f * pow10[exp], true
  753. case -22 <= exp && exp < 0:
  754. return f / pow10[-exp], true
  755. }
  756. }
  757. d: decimal.Decimal
  758. decimal.set(&d, str[:nr])
  759. b, overflow := decimal_to_float_bits(&d, &_f64_info)
  760. value = transmute(f64)b
  761. ok = !overflow
  762. return
  763. }
  764. append_bool :: proc(buf: []byte, b: bool) -> string {
  765. n := 0
  766. if b {
  767. n = copy(buf, "true")
  768. } else {
  769. n = copy(buf, "false")
  770. }
  771. return string(buf[:n])
  772. }
  773. append_uint :: proc(buf: []byte, u: u64, base: int) -> string {
  774. return append_bits(buf, u, base, false, 8*size_of(uint), digits, nil)
  775. }
  776. append_int :: proc(buf: []byte, i: i64, base: int) -> string {
  777. return append_bits(buf, u64(i), base, true, 8*size_of(int), digits, nil)
  778. }
  779. itoa :: proc(buf: []byte, i: int) -> string {
  780. return append_int(buf, i64(i), 10)
  781. }
  782. atoi :: proc(s: string) -> int {
  783. v, _ := parse_int(s)
  784. return v
  785. }
  786. atof :: proc(s: string) -> f64 {
  787. v, _ := parse_f64(s)
  788. return v
  789. }
  790. ftoa :: append_float
  791. append_float :: proc(buf: []byte, f: f64, fmt: byte, prec, bit_size: int) -> string {
  792. return string(generic_ftoa(buf, f, fmt, prec, bit_size))
  793. }
  794. quote :: proc(buf: []byte, str: string) -> string {
  795. write_byte :: proc(buf: []byte, i: ^int, bytes: ..byte) {
  796. if i^ >= len(buf) {
  797. return
  798. }
  799. n := copy(buf[i^:], bytes[:])
  800. i^ += n
  801. }
  802. if buf == nil {
  803. return ""
  804. }
  805. c :: '"'
  806. i := 0
  807. s := str
  808. write_byte(buf, &i, c)
  809. for width := 0; len(s) > 0; s = s[width:] {
  810. r := rune(s[0])
  811. width = 1
  812. if r >= utf8.RUNE_SELF {
  813. r, width = utf8.decode_rune_in_string(s)
  814. }
  815. if width == 1 && r == utf8.RUNE_ERROR {
  816. write_byte(buf, &i, '\\', 'x')
  817. write_byte(buf, &i, digits[s[0]>>4])
  818. write_byte(buf, &i, digits[s[0]&0xf])
  819. }
  820. if i < len(buf) {
  821. x := quote_rune(buf[i:], r)
  822. i += len(x)
  823. }
  824. }
  825. write_byte(buf, &i, c)
  826. return string(buf[:i])
  827. }
  828. quote_rune :: proc(buf: []byte, r: rune) -> string {
  829. write_byte :: proc(buf: []byte, i: ^int, bytes: ..byte) {
  830. if i^ < len(buf) {
  831. n := copy(buf[i^:], bytes[:])
  832. i^ += n
  833. }
  834. }
  835. write_string :: proc(buf: []byte, i: ^int, s: string) {
  836. if i^ < len(buf) {
  837. n := copy(buf[i^:], s)
  838. i^ += n
  839. }
  840. }
  841. write_rune :: proc(buf: []byte, i: ^int, r: rune) {
  842. if i^ < len(buf) {
  843. b, w := utf8.encode_rune(r)
  844. n := copy(buf[i^:], b[:w])
  845. i^ += n
  846. }
  847. }
  848. if buf == nil {
  849. return ""
  850. }
  851. i := 0
  852. write_byte(buf, &i, '\'')
  853. switch r {
  854. case '\a': write_string(buf, &i, "\\a")
  855. case '\b': write_string(buf, &i, "\\b")
  856. case '\e': write_string(buf, &i, "\\e")
  857. case '\f': write_string(buf, &i, "\\f")
  858. case '\n': write_string(buf, &i, "\\n")
  859. case '\r': write_string(buf, &i, "\\r")
  860. case '\t': write_string(buf, &i, "\\t")
  861. case '\v': write_string(buf, &i, "\\v")
  862. case:
  863. if r < 32 {
  864. write_string(buf, &i, "\\x")
  865. b: [2]byte
  866. s := append_bits(b[:], u64(r), 16, true, 64, digits, nil)
  867. switch len(s) {
  868. case 0: write_string(buf, &i, "00")
  869. case 1: write_rune(buf, &i, '0')
  870. case 2: write_string(buf, &i, s)
  871. }
  872. } else {
  873. write_rune(buf, &i, r)
  874. }
  875. }
  876. write_byte(buf, &i, '\'')
  877. return string(buf[:i])
  878. }
  879. unquote_char :: proc(str: string, quote: byte) -> (r: rune, multiple_bytes: bool, tail_string: string, success: bool) {
  880. hex_to_int :: proc(c: byte) -> int {
  881. switch c {
  882. case '0'..='9': return int(c-'0')
  883. case 'a'..='f': return int(c-'a')+10
  884. case 'A'..='F': return int(c-'A')+10
  885. }
  886. return -1
  887. }
  888. w: int
  889. if str[0] == quote && quote == '"' {
  890. return
  891. } else if str[0] >= 0x80 {
  892. r, w = utf8.decode_rune_in_string(str)
  893. return r, true, str[w:], true
  894. } else if str[0] != '\\' {
  895. return rune(str[0]), false, str[1:], true
  896. }
  897. if len(str) <= 1 {
  898. return
  899. }
  900. s := str
  901. c := s[1]
  902. s = s[2:]
  903. switch c {
  904. case:
  905. return
  906. case 'a': r = '\a'
  907. case 'b': r = '\b'
  908. case 'f': r = '\f'
  909. case 'n': r = '\n'
  910. case 'r': r = '\r'
  911. case 't': r = '\t'
  912. case 'v': r = '\v'
  913. case '\\': r = '\\'
  914. case '"': r = '"'
  915. case '\'': r = '\''
  916. case '0'..='7':
  917. v := int(c-'0')
  918. if len(s) < 2 {
  919. return
  920. }
  921. for i in 0..<len(s) {
  922. d := int(s[i]-'0')
  923. if d < 0 || d > 7 {
  924. return
  925. }
  926. v = (v<<3) | d
  927. }
  928. s = s[2:]
  929. if v > 0xff {
  930. return
  931. }
  932. r = rune(v)
  933. case 'x', 'u', 'U':
  934. count: int
  935. switch c {
  936. case 'x': count = 2
  937. case 'u': count = 4
  938. case 'U': count = 8
  939. }
  940. if len(s) < count {
  941. return
  942. }
  943. for i in 0..<count {
  944. d := hex_to_int(s[i])
  945. if d < 0 {
  946. return
  947. }
  948. r = (r<<4) | rune(d)
  949. }
  950. s = s[count:]
  951. if c == 'x' {
  952. break
  953. }
  954. if r > utf8.MAX_RUNE {
  955. return
  956. }
  957. multiple_bytes = true
  958. }
  959. success = true
  960. tail_string = s
  961. return
  962. }
  963. unquote_string :: proc(lit: string, allocator := context.allocator) -> (res: string, allocated, success: bool) {
  964. contains_rune :: proc(s: string, r: rune) -> int {
  965. for c, offset in s {
  966. if c == r {
  967. return offset
  968. }
  969. }
  970. return -1
  971. }
  972. if len(lit) < 2 {
  973. return
  974. }
  975. if lit[0] == '`' {
  976. return lit[1:len(lit)-1], false, true
  977. }
  978. s := lit
  979. quote := '"'
  980. if s == `""` {
  981. return "", false, true
  982. }
  983. s = s[1:len(s)-1]
  984. if contains_rune(s, '\n') >= 0 {
  985. return s, false, false
  986. }
  987. if contains_rune(s, '\\') < 0 && contains_rune(s, quote) < 0 {
  988. if quote == '"' {
  989. return s, false, true
  990. }
  991. }
  992. context.allocator = allocator
  993. buf_len := 3*len(s) / 2
  994. buf := make([]byte, buf_len)
  995. offset := 0
  996. for len(s) > 0 {
  997. r, multiple_bytes, tail_string, ok := unquote_char(s, byte(quote))
  998. if !ok {
  999. delete(buf)
  1000. return s, false, false
  1001. }
  1002. s = tail_string
  1003. if r < 0x80 || !multiple_bytes {
  1004. buf[offset] = byte(r)
  1005. offset += 1
  1006. } else {
  1007. b, w := utf8.encode_rune(r)
  1008. copy(buf[offset:], b[:w])
  1009. offset += w
  1010. }
  1011. }
  1012. new_string := string(buf[:offset])
  1013. return new_string, true, true
  1014. }