builder.odin 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800
  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 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, 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 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, 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 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, 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 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, 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 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, 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 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, 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. Returns the length of the Builder's buffer, in bytes
  246. Inputs:
  247. - b: A Builder
  248. Returns:
  249. - res: The length of the Builder's buffer
  250. */
  251. builder_len :: proc(b: Builder) -> (res: int) {
  252. return len(b.buf)
  253. }
  254. /*
  255. Returns the capacity of the Builder's buffer, in bytes
  256. Inputs:
  257. - b: A Builder
  258. Returns:
  259. - res: The capacity of the Builder's buffer
  260. */
  261. builder_cap :: proc(b: Builder) -> (res: int) {
  262. return cap(b.buf)
  263. }
  264. /*
  265. The free space left in the Builder's buffer, in bytes
  266. Inputs:
  267. - b: A Builder
  268. Returns:
  269. - res: The available space left in the Builder's buffer
  270. */
  271. builder_space :: proc(b: Builder) -> (res: int) {
  272. return cap(b.buf) - len(b.buf)
  273. }
  274. /*
  275. Appends a byte to the Builder and returns the number of bytes appended
  276. Inputs:
  277. - b: A pointer to the Builder
  278. - x: The byte to be appended
  279. Returns:
  280. - n: The number of bytes appended
  281. NOTE: The backing dynamic array may be fixed in capacity or fail to resize, `n` states the number actually written.
  282. Example:
  283. import "core:fmt"
  284. import "core:strings"
  285. write_byte_example :: proc() {
  286. builder := strings.builder_make()
  287. strings.write_byte(&builder, 'a') // 1
  288. strings.write_byte(&builder, 'b') // 1
  289. fmt.println(strings.to_string(builder)) // -> ab
  290. }
  291. Output:
  292. ab
  293. */
  294. write_byte :: proc(b: ^Builder, x: byte) -> (n: int) {
  295. n0 := len(b.buf)
  296. append(&b.buf, x)
  297. n1 := len(b.buf)
  298. return n1-n0
  299. }
  300. /*
  301. Appends a slice of bytes to the Builder and returns the number of bytes appended
  302. Inputs:
  303. - b: A pointer to the Builder
  304. - x: The slice of bytes to be appended
  305. Example:
  306. import "core:fmt"
  307. import "core:strings"
  308. write_bytes_example :: proc() {
  309. builder := strings.builder_make()
  310. bytes := [?]byte { 'a', 'b', 'c' }
  311. strings.write_bytes(&builder, bytes[:]) // 3
  312. fmt.println(strings.to_string(builder)) // -> abc
  313. }
  314. NOTE: The backing dynamic array may be fixed in capacity or fail to resize, `n` states the number actually written.
  315. Returns:
  316. - n: The number of bytes appended
  317. */
  318. write_bytes :: proc(b: ^Builder, x: []byte) -> (n: int) {
  319. n0 := len(b.buf)
  320. append(&b.buf, ..x)
  321. n1 := len(b.buf)
  322. return n1-n0
  323. }
  324. /*
  325. Appends a single rune to the Builder and returns the number of bytes written and an `io.Error`
  326. Inputs:
  327. - b: A pointer to the Builder
  328. - r: The rune to be appended
  329. Returns:
  330. - res: The number of bytes written
  331. - err: An io.Error if one occured, `nil` otherwise
  332. NOTE: The backing dynamic array may be fixed in capacity or fail to resize, `n` states the number actually written.
  333. Example:
  334. import "core:fmt"
  335. import "core:strings"
  336. write_rune_example :: proc() {
  337. builder := strings.builder_make()
  338. strings.write_rune(&builder, 'ä') // 2 None
  339. strings.write_rune(&builder, 'b') // 1 None
  340. fmt.println(strings.to_string(builder)) // -> äb
  341. }
  342. Output:
  343. äb
  344. */
  345. write_rune :: proc(b: ^Builder, r: rune) -> (res: int, err: io.Error) {
  346. return io.write_rune(to_writer(b), r)
  347. }
  348. /*
  349. Appends a quoted rune to the Builder and returns the number of bytes written
  350. Inputs:
  351. - b: A pointer to the Builder
  352. - r: The rune to be appended
  353. Returns:
  354. - n: The number of bytes written
  355. NOTE: The backing dynamic array may be fixed in capacity or fail to resize, `n` states the number actually written.
  356. Example:
  357. import "core:fmt"
  358. import "core:strings"
  359. write_quoted_rune_example :: proc() {
  360. builder := strings.builder_make()
  361. strings.write_string(&builder, "abc") // 3
  362. strings.write_quoted_rune(&builder, 'ä') // 4
  363. strings.write_string(&builder, "abc") // 3
  364. fmt.println(strings.to_string(builder)) // -> abc'ä'abc
  365. }
  366. Output:
  367. abc'ä'abc
  368. */
  369. write_quoted_rune :: proc(b: ^Builder, r: rune) -> (n: int) {
  370. return io.write_quoted_rune(to_writer(b), r)
  371. }
  372. /*
  373. Appends a string to the Builder and returns the number of bytes written
  374. Inputs:
  375. - b: A pointer to the Builder
  376. - s: The string to be appended
  377. Returns:
  378. - n: The number of bytes written
  379. NOTE: The backing dynamic array may be fixed in capacity or fail to resize, `n` states the number actually written.
  380. Example:
  381. import "core:fmt"
  382. import "core:strings"
  383. write_string_example :: proc() {
  384. builder := strings.builder_make()
  385. strings.write_string(&builder, "a") // 1
  386. strings.write_string(&builder, "bc") // 2
  387. fmt.println(strings.to_string(builder)) // -> abc
  388. }
  389. Output:
  390. abc
  391. */
  392. write_string :: proc(b: ^Builder, s: string) -> (n: int) {
  393. n0 := len(b.buf)
  394. append(&b.buf, s)
  395. n1 := len(b.buf)
  396. return n1-n0
  397. }
  398. /*
  399. Pops and returns the last byte in the Builder or 0 when the Builder is empty
  400. Inputs:
  401. - b: A pointer to the Builder
  402. Returns:
  403. - r: The last byte in the Builder or 0 if empty
  404. */
  405. pop_byte :: proc(b: ^Builder) -> (r: byte) {
  406. if len(b.buf) == 0 {
  407. return 0
  408. }
  409. r = b.buf[len(b.buf)-1]
  410. d := cast(^runtime.Raw_Dynamic_Array)&b.buf
  411. d.len = max(d.len-1, 0)
  412. return
  413. }
  414. /*
  415. Pops the last rune in the Builder and returns the popped rune and its rune width or (0, 0) if empty
  416. Inputs:
  417. - b: A pointer to the Builder
  418. Returns:
  419. - r: The popped rune
  420. - width: The rune width or 0 if the builder was empty
  421. */
  422. pop_rune :: proc(b: ^Builder) -> (r: rune, width: int) {
  423. if len(b.buf) == 0 {
  424. return 0, 0
  425. }
  426. r, width = utf8.decode_last_rune(b.buf[:])
  427. d := cast(^runtime.Raw_Dynamic_Array)&b.buf
  428. d.len = max(d.len-width, 0)
  429. return
  430. }
  431. @(private)
  432. DIGITS_LOWER := "0123456789abcdefx"
  433. /*
  434. Inputs:
  435. - b: A pointer to the Builder
  436. - str: The string to be quoted and appended
  437. - quote: The optional quote character (default is double quotes)
  438. Returns:
  439. - n: The number of bytes written
  440. NOTE: The backing dynamic array may be fixed in capacity or fail to resize, `n` states the number actually written.
  441. Example:
  442. import "core:fmt"
  443. import "core:strings"
  444. write_quoted_string_example :: proc() {
  445. builder := strings.builder_make()
  446. strings.write_quoted_string(&builder, "a") // 3
  447. strings.write_quoted_string(&builder, "bc", '\'') // 4
  448. strings.write_quoted_string(&builder, "xyz") // 5
  449. fmt.println(strings.to_string(builder))
  450. }
  451. Output:
  452. "a"'bc'"xyz"
  453. */
  454. write_quoted_string :: proc(b: ^Builder, str: string, quote: byte = '"') -> (n: int) {
  455. n, _ = io.write_quoted_string(to_writer(b), str, quote)
  456. return
  457. }
  458. /*
  459. Appends a rune to the Builder and returns the number of bytes written
  460. Inputs:
  461. - b: A pointer to the Builder
  462. - r: The rune to be appended
  463. - write_quote: Optional boolean flag to wrap in single-quotes (') (default is true)
  464. Returns:
  465. - n: The number of bytes written
  466. NOTE: The backing dynamic array may be fixed in capacity or fail to resize, `n` states the number actually written.
  467. Example:
  468. import "core:fmt"
  469. import "core:strings"
  470. write_encoded_rune_example :: proc() {
  471. builder := strings.builder_make()
  472. strings.write_encoded_rune(&builder, 'a', false) // 1
  473. strings.write_encoded_rune(&builder, '\"', true) // 3
  474. strings.write_encoded_rune(&builder, 'x', false) // 1
  475. fmt.println(strings.to_string(builder))
  476. }
  477. Output:
  478. a'"'x
  479. */
  480. write_encoded_rune :: proc(b: ^Builder, r: rune, write_quote := true) -> (n: int) {
  481. n, _ = io.write_encoded_rune(to_writer(b), r, write_quote)
  482. return
  483. }
  484. /*
  485. Appends an escaped rune to the Builder and returns the number of bytes written
  486. Inputs:
  487. - b: A pointer to the Builder
  488. - r: The rune to be appended
  489. - quote: The quote character
  490. - html_safe: Optional boolean flag to encode '<', '>', '&' as digits (default is false)
  491. **Usage**
  492. - '\a' will be written as such
  493. - `r` and `quote` match and `quote` is `\\` - they will be written as two slashes
  494. - `html_safe` flag in case the runes '<', '>', '&' should be encoded as digits e.g. `\u0026`
  495. NOTE: The backing dynamic array may be fixed in capacity or fail to resize, `n` states the number actually written.
  496. Returns:
  497. - n: The number of bytes written
  498. */
  499. write_escaped_rune :: proc(b: ^Builder, r: rune, quote: byte, html_safe := false) -> (n: int) {
  500. n, _ = io.write_escaped_rune(to_writer(b), r, quote, html_safe)
  501. return
  502. }
  503. /*
  504. Writes a f64 value to the Builder and returns the number of characters written
  505. Inputs:
  506. - b: A pointer to the Builder
  507. - f: The f64 value to be appended
  508. - fmt: The format byte
  509. - prec: The precision
  510. - bit_size: The bit size
  511. - always_signed: Optional boolean flag to always include the sign (default is false)
  512. NOTE: The backing dynamic array may be fixed in capacity or fail to resize, `n` states the number actually written.
  513. Returns:
  514. - n: The number of characters written
  515. */
  516. write_float :: proc(b: ^Builder, f: f64, fmt: byte, prec, bit_size: int, always_signed := false) -> (n: int) {
  517. buf: [384]byte
  518. s := strconv.append_float(buf[:], f, fmt, prec, bit_size)
  519. // If the result starts with a `+` then unless we always want signed results,
  520. // we skip it unless it's followed by an `I` (because of +Inf).
  521. if !always_signed && (buf[0] == '+' && buf[1] != 'I') {
  522. s = s[1:]
  523. }
  524. return write_string(b, s)
  525. }
  526. /*
  527. Writes a f16 value to the Builder and returns the number of characters written
  528. Inputs:
  529. - b: A pointer to the Builder
  530. - f: The f16 value to be appended
  531. - fmt: The format byte
  532. - always_signed: Optional boolean flag to always include the sign
  533. NOTE: The backing dynamic array may be fixed in capacity or fail to resize, `n` states the number actually written.
  534. Returns:
  535. - n: The number of characters written
  536. */
  537. write_f16 :: proc(b: ^Builder, f: f16, fmt: byte, always_signed := false) -> (n: int) {
  538. buf: [384]byte
  539. s := strconv.append_float(buf[:], f64(f), fmt, 2*size_of(f), 8*size_of(f))
  540. if !always_signed && (buf[0] == '+' && buf[1] != 'I') {
  541. s = s[1:]
  542. }
  543. return write_string(b, s)
  544. }
  545. /*
  546. Writes a f32 value to the Builder and returns the number of characters written
  547. Inputs:
  548. - b: A pointer to the Builder
  549. - f: The f32 value to be appended
  550. - fmt: The format byte
  551. - always_signed: Optional boolean flag to always include the sign
  552. Returns:
  553. - n: The number of characters written
  554. NOTE: The backing dynamic array may be fixed in capacity or fail to resize, `n` states the number actually written.
  555. Example:
  556. import "core:fmt"
  557. import "core:strings"
  558. write_f32_example :: proc() {
  559. builder := strings.builder_make()
  560. strings.write_f32(&builder, 3.14159, 'f') // 6
  561. strings.write_string(&builder, " - ") // 3
  562. strings.write_f32(&builder, -0.123, 'e') // 8
  563. fmt.println(strings.to_string(builder)) // -> 3.14159012 - -1.23000003e-01
  564. }
  565. Output:
  566. 3.14159012 - -1.23000003e-01
  567. */
  568. write_f32 :: proc(b: ^Builder, f: f32, fmt: byte, always_signed := false) -> (n: int) {
  569. buf: [384]byte
  570. s := strconv.append_float(buf[:], f64(f), fmt, 2*size_of(f), 8*size_of(f))
  571. if !always_signed && (buf[0] == '+' && buf[1] != 'I') {
  572. s = s[1:]
  573. }
  574. return write_string(b, s)
  575. }
  576. /*
  577. Writes a f32 value to the Builder and returns the number of characters written
  578. Inputs:
  579. - b: A pointer to the Builder
  580. - f: The f32 value to be appended
  581. - fmt: The format byte
  582. - always_signed: Optional boolean flag to always include the sign
  583. NOTE: The backing dynamic array may be fixed in capacity or fail to resize, `n` states the number actually written.
  584. Returns:
  585. - n: The number of characters written
  586. */
  587. write_f64 :: proc(b: ^Builder, f: f64, fmt: byte, always_signed := false) -> (n: int) {
  588. buf: [384]byte
  589. s := strconv.append_float(buf[:], f64(f), fmt, 2*size_of(f), 8*size_of(f))
  590. if !always_signed && (buf[0] == '+' && buf[1] != 'I') {
  591. s = s[1:]
  592. }
  593. return write_string(b, s)
  594. }
  595. /*
  596. Writes a u64 value to the Builder and returns the number of characters written
  597. Inputs:
  598. - b: A pointer to the Builder
  599. - i: The u64 value to be appended
  600. - base: The optional base for the numeric representation
  601. NOTE: The backing dynamic array may be fixed in capacity or fail to resize, `n` states the number actually written.
  602. Returns:
  603. - n: The number of characters written
  604. */
  605. write_u64 :: proc(b: ^Builder, i: u64, base: int = 10) -> (n: int) {
  606. buf: [32]byte
  607. s := strconv.append_bits(buf[:], i, base, false, 64, strconv.digits, nil)
  608. return write_string(b, s)
  609. }
  610. /*
  611. Writes a i64 value to the Builder and returns the number of characters written
  612. Inputs:
  613. - b: A pointer to the Builder
  614. - i: The i64 value to be appended
  615. - base: The optional base for the numeric representation
  616. NOTE: The backing dynamic array may be fixed in capacity or fail to resize, `n` states the number actually written.
  617. Returns:
  618. - n: The number of characters written
  619. */
  620. write_i64 :: proc(b: ^Builder, i: i64, base: int = 10) -> (n: int) {
  621. buf: [32]byte
  622. s := strconv.append_bits(buf[:], u64(i), base, true, 64, strconv.digits, nil)
  623. return write_string(b, s)
  624. }
  625. /*
  626. Writes a uint value to the Builder and returns the number of characters written
  627. Inputs:
  628. - b: A pointer to the Builder
  629. - i: The uint value to be appended
  630. - base: The optional base for the numeric representation
  631. NOTE: The backing dynamic array may be fixed in capacity or fail to resize, `n` states the number actually written.
  632. Returns:
  633. - n: The number of characters written
  634. */
  635. write_uint :: proc(b: ^Builder, i: uint, base: int = 10) -> (n: int) {
  636. return write_u64(b, u64(i), base)
  637. }
  638. /*
  639. Writes a int value to the Builder and returns the number of characters written
  640. Inputs:
  641. - b: A pointer to the Builder
  642. - i: The int value to be appended
  643. - base: The optional base for the numeric representation
  644. NOTE: The backing dynamic array may be fixed in capacity or fail to resize, `n` states the number actually written.
  645. Returns:
  646. - n: The number of characters written
  647. */
  648. write_int :: proc(b: ^Builder, i: int, base: int = 10) -> (n: int) {
  649. return write_i64(b, i64(i), base)
  650. }