builder.odin 21 KB

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