builder.odin 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814
  1. package strings
  2. import "base: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 an empty Builder
  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, loc := #caller_location) -> (res: Builder, err: mem.Allocator_Error) #optional_allocator_error {
  33. return Builder{buf=make([dynamic]byte, allocator, loc) or_return }, nil
  34. }
  35. /*
  36. Produces a Builder with specified length and capacity `len`.
  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, loc := #caller_location) -> (res: Builder, err: mem.Allocator_Error) #optional_allocator_error {
  46. return Builder{buf=make([dynamic]byte, len, allocator, loc) or_return }, nil
  47. }
  48. /*
  49. Produces a Builder with specified length `len` and capacity `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, loc := #caller_location) -> (res: Builder, err: mem.Allocator_Error) #optional_allocator_error {
  60. return Builder{buf=make([dynamic]byte, len, cap, allocator, loc) 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 an empty Builder
  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, loc := #caller_location) -> (res: ^Builder, err: mem.Allocator_Error) #optional_allocator_error {
  99. b.buf = make([dynamic]byte, allocator, loc) or_return
  100. return b, nil
  101. }
  102. /*
  103. Initializes a Builder with specified length and capacity `len`.
  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, loc := #caller_location) -> (res: ^Builder, err: mem.Allocator_Error) #optional_allocator_error {
  115. b.buf = make([dynamic]byte, len, allocator, loc) or_return
  116. return b, nil
  117. }
  118. /*
  119. Initializes a Builder with specified length `len` and capacity `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, loc := #caller_location) -> (res: ^Builder, err: mem.Allocator_Error) #optional_allocator_error {
  131. b.buf = make([dynamic]byte, len, cap, allocator, loc) 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_proc :: proc(stream_data: rawptr, mode: io.Stream_Mode, p: []byte, offset: i64, whence: io.Seek_From) -> (n: i64, err: io.Error) {
  142. b := (^Builder)(stream_data)
  143. #partial switch mode {
  144. case .Write:
  145. n = i64(write_bytes(b, p))
  146. if n < i64(len(p)) {
  147. err = .EOF
  148. }
  149. return
  150. case .Size:
  151. n = i64(len(b.buf))
  152. return
  153. case .Destroy:
  154. builder_destroy(b)
  155. return
  156. case .Query:
  157. return io.query_utility({.Write, .Size, .Destroy, .Query})
  158. }
  159. return 0, .Empty
  160. }
  161. /*
  162. Returns an io.Stream from a Builder
  163. Inputs:
  164. - b: A pointer to the Builder
  165. Returns:
  166. - res: the io.Stream
  167. */
  168. to_stream :: proc(b: ^Builder) -> (res: io.Stream) {
  169. return io.Stream{procedure=_builder_stream_proc, data=b}
  170. }
  171. /*
  172. Returns an io.Writer from a Builder
  173. Inputs:
  174. - b: A pointer to the Builder
  175. Returns:
  176. - res: The io.Writer
  177. */
  178. to_writer :: proc(b: ^Builder) -> (res: io.Writer) {
  179. return io.to_writer(to_stream(b))
  180. }
  181. /*
  182. Deletes the Builder byte buffer content
  183. Inputs:
  184. - b: A pointer to the Builder
  185. */
  186. builder_destroy :: proc(b: ^Builder) {
  187. delete(b.buf)
  188. b.buf = nil
  189. }
  190. /*
  191. Reserves the Builder byte buffer to a specific capacity, when it's higher than before
  192. Inputs:
  193. - b: A pointer to the Builder
  194. - cap: The desired capacity for the Builder's buffer
  195. */
  196. builder_grow :: proc(b: ^Builder, cap: int) {
  197. reserve(&b.buf, cap)
  198. }
  199. /*
  200. Clears the Builder byte buffer content (sets len to zero)
  201. Inputs:
  202. - b: A pointer to the Builder
  203. */
  204. builder_reset :: proc(b: ^Builder) {
  205. clear(&b.buf)
  206. }
  207. /*
  208. Creates a Builder from a slice of bytes with the same slice length as its capacity. Used in fmt.bprint*
  209. *Uses Nil Allocator - Does NOT allocate*
  210. Inputs:
  211. - backing: A slice of bytes to be used as the backing buffer
  212. Returns:
  213. - res: The new Builder
  214. Example:
  215. import "core:fmt"
  216. import "core:strings"
  217. builder_from_bytes_example :: proc() {
  218. bytes: [8]byte // <-- gets filled
  219. builder := strings.builder_from_bytes(bytes[:])
  220. strings.write_byte(&builder, 'a')
  221. fmt.println(strings.to_string(builder)) // -> "a"
  222. strings.write_byte(&builder, 'b')
  223. fmt.println(strings.to_string(builder)) // -> "ab"
  224. }
  225. Output:
  226. a
  227. ab
  228. */
  229. builder_from_bytes :: proc(backing: []byte) -> (res: Builder) {
  230. return Builder{ buf = mem.buffer_from_slice(backing) }
  231. }
  232. // Alias to `builder_from_bytes`
  233. builder_from_slice :: builder_from_bytes
  234. /*
  235. Casts the Builder byte buffer to a string and returns it
  236. Inputs:
  237. - b: A Builder
  238. Returns:
  239. - res: The contents of the Builder's buffer, as a string
  240. */
  241. to_string :: proc(b: Builder) -> (res: string) {
  242. return string(b.buf[:])
  243. }
  244. /*
  245. Appends a trailing null byte after the end of the current Builder byte buffer and then casts it to a cstring
  246. Inputs:
  247. - b: A pointer to builder
  248. Returns:
  249. - res: A cstring of the Builder's buffer
  250. */
  251. to_cstring :: proc(b: ^Builder) -> (res: cstring) {
  252. append(&b.buf, 0)
  253. pop(&b.buf)
  254. return cstring(raw_data(b.buf))
  255. }
  256. /*
  257. Returns the length of the Builder's buffer, in bytes
  258. Inputs:
  259. - b: A Builder
  260. Returns:
  261. - res: The length of the Builder's buffer
  262. */
  263. builder_len :: proc(b: Builder) -> (res: int) {
  264. return len(b.buf)
  265. }
  266. /*
  267. Returns the capacity of the Builder's buffer, in bytes
  268. Inputs:
  269. - b: A Builder
  270. Returns:
  271. - res: The capacity of the Builder's buffer
  272. */
  273. builder_cap :: proc(b: Builder) -> (res: int) {
  274. return cap(b.buf)
  275. }
  276. /*
  277. The free space left in the Builder's buffer, in bytes
  278. Inputs:
  279. - b: A Builder
  280. Returns:
  281. - res: The available space left in the Builder's buffer
  282. */
  283. builder_space :: proc(b: Builder) -> (res: int) {
  284. return cap(b.buf) - len(b.buf)
  285. }
  286. /*
  287. Appends a byte to the Builder and returns the number of bytes appended
  288. Inputs:
  289. - b: A pointer to the Builder
  290. - x: The byte to be appended
  291. Returns:
  292. - n: The number of bytes appended
  293. NOTE: The backing dynamic array may be fixed in capacity or fail to resize, `n` states the number actually written.
  294. Example:
  295. import "core:fmt"
  296. import "core:strings"
  297. write_byte_example :: proc() {
  298. builder := strings.builder_make()
  299. strings.write_byte(&builder, 'a') // 1
  300. strings.write_byte(&builder, 'b') // 1
  301. fmt.println(strings.to_string(builder)) // -> ab
  302. }
  303. Output:
  304. ab
  305. */
  306. write_byte :: proc(b: ^Builder, x: byte, loc := #caller_location) -> (n: int) {
  307. n0 := len(b.buf)
  308. append(&b.buf, x, loc)
  309. n1 := len(b.buf)
  310. return n1-n0
  311. }
  312. /*
  313. Appends a slice of bytes to the Builder and returns the number of bytes appended
  314. Inputs:
  315. - b: A pointer to the Builder
  316. - x: The slice of bytes to be appended
  317. Example:
  318. import "core:fmt"
  319. import "core:strings"
  320. write_bytes_example :: proc() {
  321. builder := strings.builder_make()
  322. bytes := [?]byte { 'a', 'b', 'c' }
  323. strings.write_bytes(&builder, bytes[:]) // 3
  324. fmt.println(strings.to_string(builder)) // -> abc
  325. }
  326. NOTE: The backing dynamic array may be fixed in capacity or fail to resize, `n` states the number actually written.
  327. Returns:
  328. - n: The number of bytes appended
  329. */
  330. write_bytes :: proc(b: ^Builder, x: []byte, loc := #caller_location) -> (n: int) {
  331. n0 := len(b.buf)
  332. append(&b.buf, ..x, loc=loc)
  333. n1 := len(b.buf)
  334. return n1-n0
  335. }
  336. /*
  337. Appends a single rune to the Builder and returns the number of bytes written and an `io.Error`
  338. Inputs:
  339. - b: A pointer to the Builder
  340. - r: The rune to be appended
  341. Returns:
  342. - res: The number of bytes written
  343. - err: An io.Error if one occured, `nil` otherwise
  344. NOTE: The backing dynamic array may be fixed in capacity or fail to resize, `n` states the number actually written.
  345. Example:
  346. import "core:fmt"
  347. import "core:strings"
  348. write_rune_example :: proc() {
  349. builder := strings.builder_make()
  350. strings.write_rune(&builder, 'ä') // 2 None
  351. strings.write_rune(&builder, 'b') // 1 None
  352. fmt.println(strings.to_string(builder)) // -> äb
  353. }
  354. Output:
  355. äb
  356. */
  357. write_rune :: proc(b: ^Builder, r: rune) -> (res: int, err: io.Error) {
  358. return io.write_rune(to_writer(b), r)
  359. }
  360. /*
  361. Appends a quoted rune to the Builder and returns the number of bytes written
  362. Inputs:
  363. - b: A pointer to the Builder
  364. - r: The rune to be appended
  365. Returns:
  366. - n: The number of bytes written
  367. NOTE: The backing dynamic array may be fixed in capacity or fail to resize, `n` states the number actually written.
  368. Example:
  369. import "core:fmt"
  370. import "core:strings"
  371. write_quoted_rune_example :: proc() {
  372. builder := strings.builder_make()
  373. strings.write_string(&builder, "abc") // 3
  374. strings.write_quoted_rune(&builder, 'ä') // 4
  375. strings.write_string(&builder, "abc") // 3
  376. fmt.println(strings.to_string(builder)) // -> abc'ä'abc
  377. }
  378. Output:
  379. abc'ä'abc
  380. */
  381. write_quoted_rune :: proc(b: ^Builder, r: rune) -> (n: int) {
  382. return io.write_quoted_rune(to_writer(b), r)
  383. }
  384. /*
  385. Appends a string to the Builder and returns the number of bytes written
  386. Inputs:
  387. - b: A pointer to the Builder
  388. - s: The string to be appended
  389. Returns:
  390. - n: The number of bytes written
  391. NOTE: The backing dynamic array may be fixed in capacity or fail to resize, `n` states the number actually written.
  392. Example:
  393. import "core:fmt"
  394. import "core:strings"
  395. write_string_example :: proc() {
  396. builder := strings.builder_make()
  397. strings.write_string(&builder, "a") // 1
  398. strings.write_string(&builder, "bc") // 2
  399. fmt.println(strings.to_string(builder)) // -> abc
  400. }
  401. Output:
  402. abc
  403. */
  404. write_string :: proc(b: ^Builder, s: string) -> (n: int) {
  405. n0 := len(b.buf)
  406. append(&b.buf, s)
  407. n1 := len(b.buf)
  408. return n1-n0
  409. }
  410. /*
  411. Pops and returns the last byte in the Builder or 0 when the Builder is empty
  412. Inputs:
  413. - b: A pointer to the Builder
  414. Returns:
  415. - r: The last byte in the Builder or 0 if empty
  416. */
  417. pop_byte :: proc(b: ^Builder) -> (r: byte) {
  418. if len(b.buf) == 0 {
  419. return 0
  420. }
  421. r = b.buf[len(b.buf)-1]
  422. d := (^runtime.Raw_Dynamic_Array)(&b.buf)
  423. d.len = max(d.len-1, 0)
  424. return
  425. }
  426. /*
  427. Pops the last rune in the Builder and returns the popped rune and its rune width or (0, 0) if empty
  428. Inputs:
  429. - b: A pointer to the Builder
  430. Returns:
  431. - r: The popped rune
  432. - width: The rune width or 0 if the builder was empty
  433. */
  434. pop_rune :: proc(b: ^Builder) -> (r: rune, width: int) {
  435. if len(b.buf) == 0 {
  436. return 0, 0
  437. }
  438. r, width = utf8.decode_last_rune(b.buf[:])
  439. d := (^runtime.Raw_Dynamic_Array)(&b.buf)
  440. d.len = max(d.len-width, 0)
  441. return
  442. }
  443. @(private)
  444. DIGITS_LOWER := "0123456789abcdefx"
  445. /*
  446. Inputs:
  447. - b: A pointer to the Builder
  448. - str: The string to be quoted and appended
  449. - quote: The optional quote character (default is double quotes)
  450. Returns:
  451. - n: The number of bytes written
  452. NOTE: The backing dynamic array may be fixed in capacity or fail to resize, `n` states the number actually written.
  453. Example:
  454. import "core:fmt"
  455. import "core:strings"
  456. write_quoted_string_example :: proc() {
  457. builder := strings.builder_make()
  458. strings.write_quoted_string(&builder, "a") // 3
  459. strings.write_quoted_string(&builder, "bc", '\'') // 4
  460. strings.write_quoted_string(&builder, "xyz") // 5
  461. fmt.println(strings.to_string(builder))
  462. }
  463. Output:
  464. "a"'bc'"xyz"
  465. */
  466. write_quoted_string :: proc(b: ^Builder, str: string, quote: byte = '"') -> (n: int) {
  467. n, _ = io.write_quoted_string(to_writer(b), str, quote)
  468. return
  469. }
  470. /*
  471. Appends a rune to the Builder and returns the number of bytes written
  472. Inputs:
  473. - b: A pointer to the Builder
  474. - r: The rune to be appended
  475. - write_quote: Optional boolean flag to wrap in single-quotes (') (default is true)
  476. Returns:
  477. - n: The number of bytes written
  478. NOTE: The backing dynamic array may be fixed in capacity or fail to resize, `n` states the number actually written.
  479. Example:
  480. import "core:fmt"
  481. import "core:strings"
  482. write_encoded_rune_example :: proc() {
  483. builder := strings.builder_make()
  484. strings.write_encoded_rune(&builder, 'a', false) // 1
  485. strings.write_encoded_rune(&builder, '\"', true) // 3
  486. strings.write_encoded_rune(&builder, 'x', false) // 1
  487. fmt.println(strings.to_string(builder))
  488. }
  489. Output:
  490. a'"'x
  491. */
  492. write_encoded_rune :: proc(b: ^Builder, r: rune, write_quote := true) -> (n: int) {
  493. n, _ = io.write_encoded_rune(to_writer(b), r, write_quote)
  494. return
  495. }
  496. /*
  497. Appends an escaped rune to the Builder and returns the number of bytes written
  498. Inputs:
  499. - b: A pointer to the Builder
  500. - r: The rune to be appended
  501. - quote: The quote character
  502. - html_safe: Optional boolean flag to encode '<', '>', '&' as digits (default is false)
  503. **Usage**
  504. - '\a' will be written as such
  505. - `r` and `quote` match and `quote` is `\\` - they will be written as two slashes
  506. - `html_safe` flag in case the runes '<', '>', '&' should be encoded as digits e.g. `\u0026`
  507. NOTE: The backing dynamic array may be fixed in capacity or fail to resize, `n` states the number actually written.
  508. Returns:
  509. - n: The number of bytes written
  510. */
  511. write_escaped_rune :: proc(b: ^Builder, r: rune, quote: byte, html_safe := false) -> (n: int) {
  512. n, _ = io.write_escaped_rune(to_writer(b), r, quote, html_safe)
  513. return
  514. }
  515. /*
  516. Writes a f64 value to the Builder and returns the number of characters written
  517. Inputs:
  518. - b: A pointer to the Builder
  519. - f: The f64 value to be appended
  520. - fmt: The format byte
  521. - prec: The precision
  522. - bit_size: The bit size
  523. - always_signed: Optional boolean flag to always include the sign (default is false)
  524. NOTE: The backing dynamic array may be fixed in capacity or fail to resize, `n` states the number actually written.
  525. Returns:
  526. - n: The number of characters written
  527. */
  528. write_float :: proc(b: ^Builder, f: f64, fmt: byte, prec, bit_size: int, always_signed := false) -> (n: int) {
  529. buf: [384]byte
  530. s := strconv.append_float(buf[:], f, fmt, prec, bit_size)
  531. // If the result starts with a `+` then unless we always want signed results,
  532. // we skip it unless it's followed by an `I` (because of +Inf).
  533. if !always_signed && (buf[0] == '+' && buf[1] != 'I') {
  534. s = s[1:]
  535. }
  536. return write_string(b, s)
  537. }
  538. /*
  539. Writes a f16 value to the Builder and returns the number of characters written
  540. Inputs:
  541. - b: A pointer to the Builder
  542. - f: The f16 value to be appended
  543. - fmt: The format byte
  544. - always_signed: Optional boolean flag to always include the sign
  545. NOTE: The backing dynamic array may be fixed in capacity or fail to resize, `n` states the number actually written.
  546. Returns:
  547. - n: The number of characters written
  548. */
  549. write_f16 :: proc(b: ^Builder, f: f16, fmt: byte, always_signed := false) -> (n: int) {
  550. buf: [384]byte
  551. s := strconv.append_float(buf[:], f64(f), fmt, 2*size_of(f), 8*size_of(f))
  552. if !always_signed && (buf[0] == '+' && buf[1] != 'I') {
  553. s = s[1:]
  554. }
  555. return write_string(b, s)
  556. }
  557. /*
  558. Writes a f32 value to the Builder and returns the number of characters written
  559. Inputs:
  560. - b: A pointer to the Builder
  561. - f: The f32 value to be appended
  562. - fmt: The format byte
  563. - always_signed: Optional boolean flag to always include the sign
  564. Returns:
  565. - n: The number of characters written
  566. NOTE: The backing dynamic array may be fixed in capacity or fail to resize, `n` states the number actually written.
  567. Example:
  568. import "core:fmt"
  569. import "core:strings"
  570. write_f32_example :: proc() {
  571. builder := strings.builder_make()
  572. strings.write_f32(&builder, 3.14159, 'f') // 6
  573. strings.write_string(&builder, " - ") // 3
  574. strings.write_f32(&builder, -0.123, 'e') // 8
  575. fmt.println(strings.to_string(builder)) // -> 3.14159012 - -1.23000003e-01
  576. }
  577. Output:
  578. 3.14159012 - -1.23000003e-01
  579. */
  580. write_f32 :: proc(b: ^Builder, f: f32, fmt: byte, always_signed := false) -> (n: int) {
  581. buf: [384]byte
  582. s := strconv.append_float(buf[:], f64(f), fmt, 2*size_of(f), 8*size_of(f))
  583. if !always_signed && (buf[0] == '+' && buf[1] != 'I') {
  584. s = s[1:]
  585. }
  586. return write_string(b, s)
  587. }
  588. /*
  589. Writes a f64 value to the Builder and returns the number of characters written
  590. Inputs:
  591. - b: A pointer to the Builder
  592. - f: The f64 value to be appended
  593. - fmt: The format byte
  594. - always_signed: Optional boolean flag to always include the sign
  595. NOTE: The backing dynamic array may be fixed in capacity or fail to resize, `n` states the number actually written.
  596. Returns:
  597. - n: The number of characters written
  598. */
  599. write_f64 :: proc(b: ^Builder, f: f64, fmt: byte, always_signed := false) -> (n: int) {
  600. buf: [384]byte
  601. s := strconv.append_float(buf[:], f64(f), fmt, 2*size_of(f), 8*size_of(f))
  602. if !always_signed && (buf[0] == '+' && buf[1] != 'I') {
  603. s = s[1:]
  604. }
  605. return write_string(b, s)
  606. }
  607. /*
  608. Writes a u64 value to the Builder and returns the number of characters written
  609. Inputs:
  610. - b: A pointer to the Builder
  611. - i: The u64 value to be appended
  612. - base: The optional base for the numeric representation
  613. NOTE: The backing dynamic array may be fixed in capacity or fail to resize, `n` states the number actually written.
  614. Returns:
  615. - n: The number of characters written
  616. */
  617. write_u64 :: proc(b: ^Builder, i: u64, base: int = 10) -> (n: int) {
  618. buf: [32]byte
  619. s := strconv.append_bits(buf[:], i, base, false, 64, strconv.digits, nil)
  620. return write_string(b, s)
  621. }
  622. /*
  623. Writes a i64 value to the Builder and returns the number of characters written
  624. Inputs:
  625. - b: A pointer to the Builder
  626. - i: The i64 value to be appended
  627. - base: The optional base for the numeric representation
  628. NOTE: The backing dynamic array may be fixed in capacity or fail to resize, `n` states the number actually written.
  629. Returns:
  630. - n: The number of characters written
  631. */
  632. write_i64 :: proc(b: ^Builder, i: i64, base: int = 10) -> (n: int) {
  633. buf: [32]byte
  634. s := strconv.append_bits(buf[:], u64(i), base, true, 64, strconv.digits, nil)
  635. return write_string(b, s)
  636. }
  637. /*
  638. Writes a uint value to the Builder and returns the number of characters written
  639. Inputs:
  640. - b: A pointer to the Builder
  641. - i: The uint value to be appended
  642. - base: The optional base for the numeric representation
  643. NOTE: The backing dynamic array may be fixed in capacity or fail to resize, `n` states the number actually written.
  644. Returns:
  645. - n: The number of characters written
  646. */
  647. write_uint :: proc(b: ^Builder, i: uint, base: int = 10) -> (n: int) {
  648. return write_u64(b, u64(i), base)
  649. }
  650. /*
  651. Writes a int value to the Builder and returns the number of characters written
  652. Inputs:
  653. - b: A pointer to the Builder
  654. - i: The int value to be appended
  655. - base: The optional base for the numeric representation
  656. NOTE: The backing dynamic array may be fixed in capacity or fail to resize, `n` states the number actually written.
  657. Returns:
  658. - n: The number of characters written
  659. */
  660. write_int :: proc(b: ^Builder, i: int, base: int = 10) -> (n: int) {
  661. return write_i64(b, i64(i), base)
  662. }