builder.odin 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809
  1. package strings
  2. import "core:runtime"
  3. import "core:unicode/utf8"
  4. import "core:strconv"
  5. import "core:mem"
  6. import "core:io"
  7. /*
  8. Type definition for a procedure that flushes a Builder
  9. Inputs:
  10. - b: A pointer to the Builder
  11. Returns:
  12. A boolean indicating whether the Builder should be reset
  13. */
  14. Builder_Flush_Proc :: #type proc(b: ^Builder) -> (do_reset: bool)
  15. /*
  16. A dynamic byte buffer / string builder with helper procedures
  17. The dynamic array is wrapped inside the struct to be more opaque
  18. You can use `fmt.sbprint*` procedures with a `^strings.Builder` directly
  19. */
  20. Builder :: struct {
  21. buf: [dynamic]byte,
  22. }
  23. /*
  24. Produces a Builder with a default length of 0 and cap of 16
  25. *Allocates Using Provided Allocator*
  26. Inputs:
  27. - allocator: (default is context.allocator)
  28. Returns:
  29. - res: The new Builder
  30. - err: An optional allocator error if one occured, `nil` otherwise
  31. */
  32. builder_make_none :: proc(allocator := context.allocator) -> (res: Builder, err: mem.Allocator_Error) #optional_allocator_error {
  33. return Builder{buf=make([dynamic]byte, allocator) or_return }, nil
  34. }
  35. /*
  36. Produces a Builder with a specified length and cap of max(16,len) byte buffer
  37. *Allocates Using Provided Allocator*
  38. Inputs:
  39. - len: The desired length of the Builder's buffer
  40. - allocator: (default is context.allocator)
  41. Returns:
  42. - res: The new Builder
  43. - err: An optional allocator error if one occured, `nil` otherwise
  44. */
  45. builder_make_len :: proc(len: int, allocator := context.allocator) -> (res: Builder, err: mem.Allocator_Error) #optional_allocator_error {
  46. return Builder{buf=make([dynamic]byte, len, allocator) or_return }, nil
  47. }
  48. /*
  49. Produces a Builder with a specified length and cap
  50. *Allocates Using Provided Allocator*
  51. Inputs:
  52. - len: The desired length of the Builder's buffer
  53. - cap: The desired capacity of the Builder's buffer, cap is max(cap, len)
  54. - allocator: (default is context.allocator)
  55. Returns:
  56. - res: The new Builder
  57. - err: An optional allocator error if one occured, `nil` otherwise
  58. */
  59. builder_make_len_cap :: proc(len, cap: int, allocator := context.allocator) -> (res: Builder, err: mem.Allocator_Error) #optional_allocator_error {
  60. return Builder{buf=make([dynamic]byte, len, cap, allocator) or_return }, nil
  61. }
  62. /*
  63. Produces a String Builder
  64. *Allocates Using Provided Allocator*
  65. Example:
  66. import "core:fmt"
  67. import "core:strings"
  68. builder_make_example :: proc() {
  69. sb := strings.builder_make()
  70. strings.write_byte(&sb, 'a')
  71. strings.write_string(&sb, " slice of ")
  72. strings.write_f64(&sb, 3.14,'g',true) // See `fmt.fmt_float` byte codes
  73. strings.write_string(&sb, " is ")
  74. strings.write_int(&sb, 180)
  75. strings.write_rune(&sb,'°')
  76. the_string :=strings.to_string(sb)
  77. fmt.println(the_string)
  78. }
  79. Output:
  80. a slice of +3.14 is 180°
  81. */
  82. builder_make :: proc{
  83. builder_make_none,
  84. builder_make_len,
  85. builder_make_len_cap,
  86. }
  87. /*
  88. Initializes a Builder with a length of 0 and cap of 16
  89. It replaces the existing `buf`
  90. *Allocates Using Provided Allocator*
  91. Inputs:
  92. - b: A pointer to the Builder
  93. - allocator: (default is context.allocator)
  94. Returns:
  95. - res: A pointer to the initialized Builder
  96. - err: An optional allocator error if one occured, `nil` otherwise
  97. */
  98. builder_init_none :: proc(b: ^Builder, allocator := context.allocator) -> (res: ^Builder, err: mem.Allocator_Error) #optional_allocator_error {
  99. b.buf = make([dynamic]byte, allocator) or_return
  100. return b, nil
  101. }
  102. /*
  103. Initializes a Builder with a specified length and cap, which is max(len,16)
  104. It replaces the existing `buf`
  105. *Allocates Using Provided Allocator*
  106. Inputs:
  107. - b: A pointer to the Builder
  108. - len: The desired length of the Builder's buffer
  109. - allocator: (default is context.allocator)
  110. Returns:
  111. - res: A pointer to the initialized Builder
  112. - err: An optional allocator error if one occured, `nil` otherwise
  113. */
  114. builder_init_len :: proc(b: ^Builder, len: int, allocator := context.allocator) -> (res: ^Builder, err: mem.Allocator_Error) #optional_allocator_error {
  115. b.buf = make([dynamic]byte, len, allocator) or_return
  116. return b, nil
  117. }
  118. /*
  119. Initializes a Builder with a specified length and cap
  120. It replaces the existing `buf`
  121. Inputs:
  122. - b: A pointer to the Builder
  123. - len: The desired length of the Builder's buffer
  124. - cap: The desired capacity of the Builder's buffer, actual max(len,cap)
  125. - allocator: (default is context.allocator)
  126. Returns:
  127. - res: A pointer to the initialized Builder
  128. - err: An optional allocator error if one occured, `nil` otherwise
  129. */
  130. builder_init_len_cap :: proc(b: ^Builder, len, cap: int, allocator := context.allocator) -> (res: ^Builder, err: mem.Allocator_Error) #optional_allocator_error {
  131. b.buf = make([dynamic]byte, len, cap, allocator) or_return
  132. return b, nil
  133. }
  134. // Overload simple `builder_init_*` with or without len / ap parameters
  135. builder_init :: proc{
  136. builder_init_none,
  137. builder_init_len,
  138. builder_init_len_cap,
  139. }
  140. @(private)
  141. _builder_stream_vtable_obj := io.Stream_VTable{
  142. impl_write = proc(s: io.Stream, p: []byte) -> (n: int, err: io.Error) {
  143. b := (^Builder)(s.stream_data)
  144. n = write_bytes(b, p)
  145. if n < len(p) {
  146. err = .EOF
  147. }
  148. return
  149. },
  150. impl_write_byte = proc(s: io.Stream, c: byte) -> (err: io.Error) {
  151. b := (^Builder)(s.stream_data)
  152. n := write_byte(b, c)
  153. if n == 0 {
  154. err = .EOF
  155. }
  156. return
  157. },
  158. impl_size = proc(s: io.Stream) -> i64 {
  159. b := (^Builder)(s.stream_data)
  160. return i64(len(b.buf))
  161. },
  162. impl_destroy = proc(s: io.Stream) -> io.Error {
  163. b := (^Builder)(s.stream_data)
  164. builder_destroy(b)
  165. return .None
  166. },
  167. }
  168. // NOTE(dweiler): Work around a miscompilation bug on Linux still.
  169. @(private)
  170. _builder_stream_vtable := &_builder_stream_vtable_obj
  171. /*
  172. Returns an io.Stream from a Builder
  173. Inputs:
  174. - b: A pointer to the Builder
  175. Returns:
  176. - res: the io.Stream
  177. */
  178. to_stream :: proc(b: ^Builder) -> (res: io.Stream) {
  179. return io.Stream{stream_vtable=_builder_stream_vtable, stream_data=b}
  180. }
  181. /*
  182. Returns an io.Writer from a Builder
  183. Inputs:
  184. - b: A pointer to the Builder
  185. Returns:
  186. - res: The io.Writer
  187. */
  188. to_writer :: proc(b: ^Builder) -> (res: io.Writer) {
  189. return io.to_writer(to_stream(b))
  190. }
  191. /*
  192. Deletes the Builder byte buffer content
  193. Inputs:
  194. - b: A pointer to the Builder
  195. */
  196. builder_destroy :: proc(b: ^Builder) {
  197. delete(b.buf)
  198. b.buf = nil
  199. }
  200. /*
  201. Reserves the Builder byte buffer to a specific capacity, when it's higher than before
  202. Inputs:
  203. - b: A pointer to the Builder
  204. - cap: The desired capacity for the Builder's buffer
  205. */
  206. builder_grow :: proc(b: ^Builder, cap: int) {
  207. reserve(&b.buf, cap)
  208. }
  209. /*
  210. Clears the Builder byte buffer content (sets len to zero)
  211. Inputs:
  212. - b: A pointer to the Builder
  213. */
  214. builder_reset :: proc(b: ^Builder) {
  215. clear(&b.buf)
  216. }
  217. /*
  218. Creates a Builder from a slice of bytes with the same slice length as its capacity. Used in fmt.bprint*
  219. *Uses Nil Allocator - Does NOT allocate*
  220. Inputs:
  221. - backing: A slice of bytes to be used as the backing buffer
  222. Returns:
  223. - res: The new Builder
  224. Example:
  225. import "core:fmt"
  226. import "core:strings"
  227. builder_from_bytes_example :: proc() {
  228. bytes: [8]byte // <-- gets filled
  229. builder := strings.builder_from_bytes(bytes[:])
  230. strings.write_byte(&builder, 'a')
  231. fmt.println(strings.to_string(builder)) // -> "a"
  232. strings.write_byte(&builder, 'b')
  233. fmt.println(strings.to_string(builder)) // -> "ab"
  234. }
  235. Output:
  236. a
  237. ab
  238. */
  239. builder_from_bytes :: proc(backing: []byte) -> (res: Builder) {
  240. return Builder{ buf = mem.buffer_from_slice(backing) }
  241. }
  242. // Alias to `builder_from_bytes`
  243. builder_from_slice :: builder_from_bytes
  244. /*
  245. Casts the Builder byte buffer to a string and returns it
  246. Inputs:
  247. - b: A Builder
  248. Returns:
  249. - res: The contents of the Builder's buffer, as a string
  250. */
  251. to_string :: proc(b: Builder) -> (res: string) {
  252. return string(b.buf[:])
  253. }
  254. /*
  255. Returns the length of the Builder's buffer, in bytes
  256. Inputs:
  257. - b: A Builder
  258. Returns:
  259. - res: The length of the Builder's buffer
  260. */
  261. builder_len :: proc(b: Builder) -> (res: int) {
  262. return len(b.buf)
  263. }
  264. /*
  265. Returns the capacity of the Builder's buffer, in bytes
  266. Inputs:
  267. - b: A Builder
  268. Returns:
  269. - res: The capacity of the Builder's buffer
  270. */
  271. builder_cap :: proc(b: Builder) -> (res: int) {
  272. return cap(b.buf)
  273. }
  274. /*
  275. The free space left in the Builder's buffer, in bytes
  276. Inputs:
  277. - b: A Builder
  278. Returns:
  279. - res: The available space left in the Builder's buffer
  280. */
  281. builder_space :: proc(b: Builder) -> (res: int) {
  282. return cap(b.buf) - len(b.buf)
  283. }
  284. /*
  285. Appends a byte to the Builder and returns the number of bytes appended
  286. Inputs:
  287. - b: A pointer to the Builder
  288. - x: The byte to be appended
  289. Returns:
  290. - n: The number of bytes appended
  291. NOTE: The backing dynamic array may be fixed in capacity or fail to resize, `n` states the number actually written.
  292. Example:
  293. import "core:fmt"
  294. import "core:strings"
  295. write_byte_example :: proc() {
  296. builder := strings.builder_make()
  297. strings.write_byte(&builder, 'a') // 1
  298. strings.write_byte(&builder, 'b') // 1
  299. fmt.println(strings.to_string(builder)) // -> ab
  300. }
  301. Output:
  302. ab
  303. */
  304. write_byte :: proc(b: ^Builder, x: byte) -> (n: int) {
  305. n0 := len(b.buf)
  306. append(&b.buf, x)
  307. n1 := len(b.buf)
  308. return n1-n0
  309. }
  310. /*
  311. Appends a slice of bytes to the Builder and returns the number of bytes appended
  312. Inputs:
  313. - b: A pointer to the Builder
  314. - x: The slice of bytes to be appended
  315. Example:
  316. import "core:fmt"
  317. import "core:strings"
  318. write_bytes_example :: proc() {
  319. builder := strings.builder_make()
  320. bytes := [?]byte { 'a', 'b', 'c' }
  321. strings.write_bytes(&builder, bytes[:]) // 3
  322. fmt.println(strings.to_string(builder)) // -> abc
  323. }
  324. NOTE: The backing dynamic array may be fixed in capacity or fail to resize, `n` states the number actually written.
  325. Returns:
  326. - n: The number of bytes appended
  327. */
  328. write_bytes :: proc(b: ^Builder, x: []byte) -> (n: int) {
  329. n0 := len(b.buf)
  330. append(&b.buf, ..x)
  331. n1 := len(b.buf)
  332. return n1-n0
  333. }
  334. /*
  335. Appends a single rune to the Builder and returns the number of bytes written and an `io.Error`
  336. Inputs:
  337. - b: A pointer to the Builder
  338. - r: The rune to be appended
  339. Returns:
  340. - res: The number of bytes written
  341. - err: An io.Error if one occured, `nil` otherwise
  342. NOTE: The backing dynamic array may be fixed in capacity or fail to resize, `n` states the number actually written.
  343. Example:
  344. import "core:fmt"
  345. import "core:strings"
  346. write_rune_example :: proc() {
  347. builder := strings.builder_make()
  348. strings.write_rune(&builder, 'ä') // 2 None
  349. strings.write_rune(&builder, 'b') // 1 None
  350. fmt.println(strings.to_string(builder)) // -> äb
  351. }
  352. Output:
  353. äb
  354. */
  355. write_rune :: proc(b: ^Builder, r: rune) -> (res: int, err: io.Error) {
  356. return io.write_rune(to_writer(b), r)
  357. }
  358. /*
  359. Appends a quoted rune to the Builder and returns the number of bytes written
  360. Inputs:
  361. - b: A pointer to the Builder
  362. - r: The rune to be appended
  363. Returns:
  364. - n: The number of bytes written
  365. NOTE: The backing dynamic array may be fixed in capacity or fail to resize, `n` states the number actually written.
  366. Example:
  367. import "core:fmt"
  368. import "core:strings"
  369. write_quoted_rune_example :: proc() {
  370. builder := strings.builder_make()
  371. strings.write_string(&builder, "abc") // 3
  372. strings.write_quoted_rune(&builder, 'ä') // 4
  373. strings.write_string(&builder, "abc") // 3
  374. fmt.println(strings.to_string(builder)) // -> abc'ä'abc
  375. }
  376. Output:
  377. abc'ä'abc
  378. */
  379. write_quoted_rune :: proc(b: ^Builder, r: rune) -> (n: int) {
  380. return io.write_quoted_rune(to_writer(b), r)
  381. }
  382. /*
  383. Appends a string to the Builder and returns the number of bytes written
  384. Inputs:
  385. - b: A pointer to the Builder
  386. - s: The string to be appended
  387. Returns:
  388. - n: The number of bytes written
  389. NOTE: The backing dynamic array may be fixed in capacity or fail to resize, `n` states the number actually written.
  390. Example:
  391. import "core:fmt"
  392. import "core:strings"
  393. write_string_example :: proc() {
  394. builder := strings.builder_make()
  395. strings.write_string(&builder, "a") // 1
  396. strings.write_string(&builder, "bc") // 2
  397. fmt.println(strings.to_string(builder)) // -> abc
  398. }
  399. Output:
  400. abc
  401. */
  402. write_string :: proc(b: ^Builder, s: string) -> (n: int) {
  403. n0 := len(b.buf)
  404. append(&b.buf, s)
  405. n1 := len(b.buf)
  406. return n1-n0
  407. }
  408. /*
  409. Pops and returns the last byte in the Builder or 0 when the Builder is empty
  410. Inputs:
  411. - b: A pointer to the Builder
  412. Returns:
  413. - r: The last byte in the Builder or 0 if empty
  414. */
  415. pop_byte :: proc(b: ^Builder) -> (r: byte) {
  416. if len(b.buf) == 0 {
  417. return 0
  418. }
  419. r = b.buf[len(b.buf)-1]
  420. d := cast(^runtime.Raw_Dynamic_Array)&b.buf
  421. d.len = max(d.len-1, 0)
  422. return
  423. }
  424. /*
  425. Pops the last rune in the Builder and returns the popped rune and its rune width or (0, 0) if empty
  426. Inputs:
  427. - b: A pointer to the Builder
  428. Returns:
  429. - r: The popped rune
  430. - width: The rune width or 0 if the builder was empty
  431. */
  432. pop_rune :: proc(b: ^Builder) -> (r: rune, width: int) {
  433. if len(b.buf) == 0 {
  434. return 0, 0
  435. }
  436. r, width = utf8.decode_last_rune(b.buf[:])
  437. d := cast(^runtime.Raw_Dynamic_Array)&b.buf
  438. d.len = max(d.len-width, 0)
  439. return
  440. }
  441. @(private)
  442. DIGITS_LOWER := "0123456789abcdefx"
  443. /*
  444. Inputs:
  445. - b: A pointer to the Builder
  446. - str: The string to be quoted and appended
  447. - quote: The optional quote character (default is double quotes)
  448. Returns:
  449. - n: The number of bytes written
  450. NOTE: The backing dynamic array may be fixed in capacity or fail to resize, `n` states the number actually written.
  451. Example:
  452. import "core:fmt"
  453. import "core:strings"
  454. write_quoted_string_example :: proc() {
  455. builder := strings.builder_make()
  456. strings.write_quoted_string(&builder, "a") // 3
  457. strings.write_quoted_string(&builder, "bc", '\'') // 4
  458. strings.write_quoted_string(&builder, "xyz") // 5
  459. fmt.println(strings.to_string(builder))
  460. }
  461. Output:
  462. "a"'bc'"xyz"
  463. */
  464. write_quoted_string :: proc(b: ^Builder, str: string, quote: byte = '"') -> (n: int) {
  465. n, _ = io.write_quoted_string(to_writer(b), str, quote)
  466. return
  467. }
  468. /*
  469. Appends a rune to the Builder and returns the number of bytes written
  470. Inputs:
  471. - b: A pointer to the Builder
  472. - r: The rune to be appended
  473. - write_quote: Optional boolean flag to wrap in single-quotes (') (default is true)
  474. Returns:
  475. - n: The number of bytes written
  476. NOTE: The backing dynamic array may be fixed in capacity or fail to resize, `n` states the number actually written.
  477. Example:
  478. import "core:fmt"
  479. import "core:strings"
  480. write_encoded_rune_example :: proc() {
  481. builder := strings.builder_make()
  482. strings.write_encoded_rune(&builder, 'a', false) // 1
  483. strings.write_encoded_rune(&builder, '\"', true) // 3
  484. strings.write_encoded_rune(&builder, 'x', false) // 1
  485. fmt.println(strings.to_string(builder))
  486. }
  487. Output:
  488. a'"'x
  489. */
  490. write_encoded_rune :: proc(b: ^Builder, r: rune, write_quote := true) -> (n: int) {
  491. n, _ = io.write_encoded_rune(to_writer(b), r, write_quote)
  492. return
  493. }
  494. /*
  495. Appends an escaped rune to the Builder and returns the number of bytes written
  496. Inputs:
  497. - b: A pointer to the Builder
  498. - r: The rune to be appended
  499. - quote: The quote character
  500. - html_safe: Optional boolean flag to encode '<', '>', '&' as digits (default is false)
  501. **Usage**
  502. - '\a' will be written as such
  503. - `r` and `quote` match and `quote` is `\\` - they will be written as two slashes
  504. - `html_safe` flag in case the runes '<', '>', '&' should be encoded as digits e.g. `\u0026`
  505. NOTE: The backing dynamic array may be fixed in capacity or fail to resize, `n` states the number actually written.
  506. Returns:
  507. - n: The number of bytes written
  508. */
  509. write_escaped_rune :: proc(b: ^Builder, r: rune, quote: byte, html_safe := false) -> (n: int) {
  510. n, _ = io.write_escaped_rune(to_writer(b), r, quote, html_safe)
  511. return
  512. }
  513. /*
  514. Writes a f64 value to the Builder and returns the number of characters written
  515. Inputs:
  516. - b: A pointer to the Builder
  517. - f: The f64 value to be appended
  518. - fmt: The format byte
  519. - prec: The precision
  520. - bit_size: The bit size
  521. - always_signed: Optional boolean flag to always include the sign (default is false)
  522. NOTE: The backing dynamic array may be fixed in capacity or fail to resize, `n` states the number actually written.
  523. Returns:
  524. - n: The number of characters written
  525. */
  526. write_float :: proc(b: ^Builder, f: f64, fmt: byte, prec, bit_size: int, always_signed := false) -> (n: int) {
  527. buf: [384]byte
  528. s := strconv.append_float(buf[:], f, fmt, prec, bit_size)
  529. // If the result starts with a `+` then unless we always want signed results,
  530. // we skip it unless it's followed by an `I` (because of +Inf).
  531. if !always_signed && (buf[0] == '+' && buf[1] != 'I') {
  532. s = s[1:]
  533. }
  534. return write_string(b, s)
  535. }
  536. /*
  537. Writes a f16 value to the Builder and returns the number of characters written
  538. Inputs:
  539. - b: A pointer to the Builder
  540. - f: The f16 value to be appended
  541. - fmt: The format byte
  542. - always_signed: Optional boolean flag to always include the sign
  543. NOTE: The backing dynamic array may be fixed in capacity or fail to resize, `n` states the number actually written.
  544. Returns:
  545. - n: The number of characters written
  546. */
  547. write_f16 :: proc(b: ^Builder, f: f16, fmt: byte, always_signed := false) -> (n: int) {
  548. buf: [384]byte
  549. s := strconv.append_float(buf[:], f64(f), fmt, 2*size_of(f), 8*size_of(f))
  550. if !always_signed && (buf[0] == '+' && buf[1] != 'I') {
  551. s = s[1:]
  552. }
  553. return write_string(b, s)
  554. }
  555. /*
  556. Writes a f32 value to the Builder and returns the number of characters written
  557. Inputs:
  558. - b: A pointer to the Builder
  559. - f: The f32 value to be appended
  560. - fmt: The format byte
  561. - always_signed: Optional boolean flag to always include the sign
  562. Returns:
  563. - n: The number of characters written
  564. NOTE: The backing dynamic array may be fixed in capacity or fail to resize, `n` states the number actually written.
  565. Example:
  566. import "core:fmt"
  567. import "core:strings"
  568. write_f32_example :: proc() {
  569. builder := strings.builder_make()
  570. strings.write_f32(&builder, 3.14159, 'f') // 6
  571. strings.write_string(&builder, " - ") // 3
  572. strings.write_f32(&builder, -0.123, 'e') // 8
  573. fmt.println(strings.to_string(builder)) // -> 3.14159012 - -1.23000003e-01
  574. }
  575. Output:
  576. 3.14159012 - -1.23000003e-01
  577. */
  578. write_f32 :: proc(b: ^Builder, f: f32, fmt: byte, always_signed := false) -> (n: int) {
  579. buf: [384]byte
  580. s := strconv.append_float(buf[:], f64(f), fmt, 2*size_of(f), 8*size_of(f))
  581. if !always_signed && (buf[0] == '+' && buf[1] != 'I') {
  582. s = s[1:]
  583. }
  584. return write_string(b, s)
  585. }
  586. /*
  587. Writes a f32 value to the Builder and returns the number of characters written
  588. Inputs:
  589. - b: A pointer to the Builder
  590. - f: The f32 value to be appended
  591. - fmt: The format byte
  592. - always_signed: Optional boolean flag to always include the sign
  593. NOTE: The backing dynamic array may be fixed in capacity or fail to resize, `n` states the number actually written.
  594. Returns:
  595. - n: The number of characters written
  596. */
  597. write_f64 :: proc(b: ^Builder, f: f64, fmt: byte, always_signed := false) -> (n: int) {
  598. buf: [384]byte
  599. s := strconv.append_float(buf[:], f64(f), fmt, 2*size_of(f), 8*size_of(f))
  600. if !always_signed && (buf[0] == '+' && buf[1] != 'I') {
  601. s = s[1:]
  602. }
  603. return write_string(b, s)
  604. }
  605. /*
  606. Writes a u64 value to the Builder and returns the number of characters written
  607. Inputs:
  608. - b: A pointer to the Builder
  609. - i: The u64 value to be appended
  610. - base: The optional base for the numeric representation
  611. NOTE: The backing dynamic array may be fixed in capacity or fail to resize, `n` states the number actually written.
  612. Returns:
  613. - n: The number of characters written
  614. */
  615. write_u64 :: proc(b: ^Builder, i: u64, base: int = 10) -> (n: int) {
  616. buf: [32]byte
  617. s := strconv.append_bits(buf[:], i, base, false, 64, strconv.digits, nil)
  618. return write_string(b, s)
  619. }
  620. /*
  621. Writes a i64 value to the Builder and returns the number of characters written
  622. Inputs:
  623. - b: A pointer to the Builder
  624. - i: The i64 value to be appended
  625. - base: The optional base for the numeric representation
  626. NOTE: The backing dynamic array may be fixed in capacity or fail to resize, `n` states the number actually written.
  627. Returns:
  628. - n: The number of characters written
  629. */
  630. write_i64 :: proc(b: ^Builder, i: i64, base: int = 10) -> (n: int) {
  631. buf: [32]byte
  632. s := strconv.append_bits(buf[:], u64(i), base, true, 64, strconv.digits, nil)
  633. return write_string(b, s)
  634. }
  635. /*
  636. Writes a uint value to the Builder and returns the number of characters written
  637. Inputs:
  638. - b: A pointer to the Builder
  639. - i: The uint value to be appended
  640. - base: The optional base for the numeric representation
  641. NOTE: The backing dynamic array may be fixed in capacity or fail to resize, `n` states the number actually written.
  642. Returns:
  643. - n: The number of characters written
  644. */
  645. write_uint :: proc(b: ^Builder, i: uint, base: int = 10) -> (n: int) {
  646. return write_u64(b, u64(i), base)
  647. }
  648. /*
  649. Writes a int value to the Builder and returns the number of characters written
  650. Inputs:
  651. - b: A pointer to the Builder
  652. - i: The int value to be appended
  653. - base: The optional base for the numeric representation
  654. NOTE: The backing dynamic array may be fixed in capacity or fail to resize, `n` states the number actually written.
  655. Returns:
  656. - n: The number of characters written
  657. */
  658. write_int :: proc(b: ^Builder, i: int, base: int = 10) -> (n: int) {
  659. return write_i64(b, i64(i), base)
  660. }