strconv.odin 22 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141
  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. return
  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. }