builder.odin 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. package strings
  2. import "core:runtime"
  3. import "core:unicode/utf8"
  4. import "core:strconv"
  5. import "core:io"
  6. Builder_Flush_Proc :: #type proc(b: ^Builder) -> (do_reset: bool)
  7. /*
  8. dynamic byte buffer / string builder with helper procedures
  9. the dynamic array is wrapped inside the struct to be more opaque
  10. you can use `fmt.sbprint*` procedures with a `^strings.Builder` directly
  11. */
  12. Builder :: struct {
  13. buf: [dynamic]byte,
  14. }
  15. // return a builder, default length 0 / cap 16 are done through make
  16. builder_make_none :: proc(allocator := context.allocator) -> Builder {
  17. return Builder{buf=make([dynamic]byte, allocator)}
  18. }
  19. // return a builder, with a set length `len` and cap 16 byte buffer
  20. builder_make_len :: proc(len: int, allocator := context.allocator) -> Builder {
  21. return Builder{buf=make([dynamic]byte, len, allocator)}
  22. }
  23. // return a builder, with a set length `len` byte buffer and a custom `cap`
  24. builder_make_len_cap :: proc(len, cap: int, allocator := context.allocator) -> Builder {
  25. return Builder{buf=make([dynamic]byte, len, cap, allocator)}
  26. }
  27. // overload simple `builder_make_*` with or without len / cap parameters
  28. builder_make :: proc{
  29. builder_make_none,
  30. builder_make_len,
  31. builder_make_len_cap,
  32. }
  33. // initialize a builder, default length 0 / cap 16 are done through make
  34. // replaces the existing `buf`
  35. builder_init_none :: proc(b: ^Builder, allocator := context.allocator) -> ^Builder {
  36. b.buf = make([dynamic]byte, allocator)
  37. return b
  38. }
  39. // initialize a builder, with a set length `len` and cap 16 byte buffer
  40. // replaces the existing `buf`
  41. builder_init_len :: proc(b: ^Builder, len: int, allocator := context.allocator) -> ^Builder {
  42. b.buf = make([dynamic]byte, len, allocator)
  43. return b
  44. }
  45. // initialize a builder, with a set length `len` byte buffer and a custom `cap`
  46. // replaces the existing `buf`
  47. builder_init_len_cap :: proc(b: ^Builder, len, cap: int, allocator := context.allocator) -> ^Builder {
  48. b.buf = make([dynamic]byte, len, cap, allocator)
  49. return b
  50. }
  51. // overload simple `builder_init_*` with or without len / ap parameters
  52. builder_init :: proc{
  53. builder_init_none,
  54. builder_init_len,
  55. builder_init_len_cap,
  56. }
  57. @(private)
  58. _builder_stream_vtable := io.Stream_VTable{
  59. impl_write = proc(s: io.Stream, p: []byte) -> (n: int, err: io.Error) {
  60. b := (^Builder)(s.stream_data)
  61. n = write_bytes(b, p)
  62. if n < len(p) {
  63. err = .EOF
  64. }
  65. return
  66. },
  67. impl_write_byte = proc(s: io.Stream, c: byte) -> (err: io.Error) {
  68. b := (^Builder)(s.stream_data)
  69. n := write_byte(b, c)
  70. if n == 0 {
  71. err = .EOF
  72. }
  73. return
  74. },
  75. impl_size = proc(s: io.Stream) -> i64 {
  76. b := (^Builder)(s.stream_data)
  77. return i64(len(b.buf))
  78. },
  79. impl_destroy = proc(s: io.Stream) -> io.Error {
  80. b := (^Builder)(s.stream_data)
  81. delete(b.buf)
  82. return .None
  83. },
  84. }
  85. // return an `io.Stream` from a builder
  86. to_stream :: proc(b: ^Builder) -> io.Stream {
  87. return io.Stream{stream_vtable=&_builder_stream_vtable, stream_data=b}
  88. }
  89. // return an `io.Writer` from a builder
  90. to_writer :: proc(b: ^Builder) -> io.Writer {
  91. return io.to_writer(to_stream(b))
  92. }
  93. // delete and clear the builder byte buffer content
  94. builder_destroy :: proc(b: ^Builder) {
  95. delete(b.buf)
  96. clear(&b.buf)
  97. }
  98. // reserve the builfer byte buffer to a specific cap, when it's higher than before
  99. builder_grow :: proc(b: ^Builder, cap: int) {
  100. reserve(&b.buf, cap)
  101. }
  102. // clear the builder byte buffer content
  103. builder_reset :: proc(b: ^Builder) {
  104. clear(&b.buf)
  105. }
  106. /*
  107. create an empty builder with the same slice length as its cap
  108. uses the `mem.nil_allocator` to avoid allocation and keep a fixed length
  109. used in `fmt.bprint*`
  110. bytes: [8]byte // <-- gets filled
  111. builder := strings.builder_from_bytes(bytes[:])
  112. strings.write_byte(&builder, 'a') -> "a"
  113. strings.write_byte(&builder, 'b') -> "ab"
  114. */
  115. builder_from_bytes :: proc(backing: []byte) -> Builder {
  116. s := transmute(runtime.Raw_Slice)backing
  117. d := runtime.Raw_Dynamic_Array{
  118. data = s.data,
  119. len = 0,
  120. cap = s.len,
  121. allocator = runtime.nil_allocator(),
  122. }
  123. return Builder{
  124. buf = transmute([dynamic]byte)d,
  125. }
  126. }
  127. builder_from_slice :: builder_from_bytes
  128. // cast the builder byte buffer to a string and return it
  129. to_string :: proc(b: Builder) -> string {
  130. return string(b.buf[:])
  131. }
  132. // return the length of the builder byte buffer
  133. builder_len :: proc(b: Builder) -> int {
  134. return len(b.buf)
  135. }
  136. // return the cap of the builder byte buffer
  137. builder_cap :: proc(b: Builder) -> int {
  138. return cap(b.buf)
  139. }
  140. // returns the space left in the builder byte buffer to use up
  141. builder_space :: proc(b: Builder) -> int {
  142. return cap(b.buf) - len(b.buf)
  143. }
  144. /*
  145. appends a byte to the builder, returns the append diff
  146. builder := strings.builder_make()
  147. strings.write_byte(&builder, 'a') // 1
  148. strings.write_byte(&builder, 'b') // 1
  149. strings.write_byte(&builder, 'c') // 1
  150. fmt.println(strings.to_string(builder)) // -> abc
  151. */
  152. write_byte :: proc(b: ^Builder, x: byte) -> (n: int) {
  153. n0 := len(b.buf)
  154. append(&b.buf, x)
  155. n1 := len(b.buf)
  156. return n1-n0
  157. }
  158. /*
  159. appends a slice of bytes to the builder, returns the append diff
  160. builder := strings.builder_make()
  161. bytes := [?]byte { 'a', 'b', 'c' }
  162. strings.write_bytes(&builder, bytes[:]) // 3
  163. fmt.println(strings.to_string(builder)) // -> abc
  164. */
  165. write_bytes :: proc(b: ^Builder, x: []byte) -> (n: int) {
  166. n0 := len(b.buf)
  167. append(&b.buf, ..x)
  168. n1 := len(b.buf)
  169. return n1-n0
  170. }
  171. /*
  172. appends a single rune into the builder, returns written rune size and an `io.Error`
  173. builder := strings.builder_make()
  174. strings.write_rune(&builder, 'ä') // 2 None
  175. strings.write_rune(&builder, 'b') // 1 None
  176. strings.write_rune(&builder, 'c') // 1 None
  177. fmt.println(strings.to_string(builder)) // -> äbc
  178. */
  179. write_rune :: proc(b: ^Builder, r: rune) -> (int, io.Error) {
  180. return io.write_rune(to_writer(b), r)
  181. }
  182. /*
  183. appends a quoted rune into the builder, returns written size
  184. builder := strings.builder_make()
  185. strings.write_string(&builder, "abc") // 3
  186. strings.write_quoted_rune(&builder, 'ä') // 4
  187. strings.write_string(&builder, "abc") // 3
  188. fmt.println(strings.to_string(builder)) // -> abc'ä'abc
  189. */
  190. write_quoted_rune :: proc(b: ^Builder, r: rune) -> (n: int) {
  191. return io.write_quoted_rune(to_writer(b), r)
  192. }
  193. /*
  194. appends a string to the builder, return the written byte size
  195. builder := strings.builder_make()
  196. strings.write_string(&builder, "a") // 1
  197. strings.write_string(&builder, "bc") // 2
  198. strings.write_string(&builder, "xyz") // 3
  199. fmt.println(strings.to_string(builder)) // -> abcxyz
  200. */
  201. write_string :: proc(b: ^Builder, s: string) -> (n: int) {
  202. n0 := len(b.buf)
  203. append(&b.buf, s)
  204. n1 := len(b.buf)
  205. return n1-n0
  206. }
  207. // pops and returns the last byte in the builder
  208. // returns 0 when the builder is empty
  209. pop_byte :: proc(b: ^Builder) -> (r: byte) {
  210. if len(b.buf) == 0 {
  211. return 0
  212. }
  213. r = b.buf[len(b.buf)-1]
  214. d := cast(^runtime.Raw_Dynamic_Array)&b.buf
  215. d.len = max(d.len-1, 0)
  216. return
  217. }
  218. // pops the last rune in the builder and returns the popped rune and its rune width
  219. // returns 0, 0 when the builder is empty
  220. pop_rune :: proc(b: ^Builder) -> (r: rune, width: int) {
  221. if len(b.buf) == 0 {
  222. return 0, 0
  223. }
  224. r, width = utf8.decode_last_rune(b.buf[:])
  225. d := cast(^runtime.Raw_Dynamic_Array)&b.buf
  226. d.len = max(d.len-width, 0)
  227. return
  228. }
  229. @(private)
  230. DIGITS_LOWER := "0123456789abcdefx"
  231. /*
  232. append a quoted string into the builder, return the written byte size
  233. builder := strings.builder_make()
  234. strings.write_quoted_string(&builder, "a") // 3
  235. strings.write_quoted_string(&builder, "bc", '\'') // 4
  236. strings.write_quoted_string(&builder, "xyz") // 5
  237. fmt.println(strings.to_string(builder)) // -> "a"'bc'xyz"
  238. */
  239. write_quoted_string :: proc(b: ^Builder, str: string, quote: byte = '"') -> (n: int) {
  240. n, _ = io.write_quoted_string(to_writer(b), str, quote)
  241. return
  242. }
  243. // appends a rune to the builder, optional `write_quote` boolean tag, returns the written rune size
  244. write_encoded_rune :: proc(b: ^Builder, r: rune, write_quote := true) -> (n: int) {
  245. n, _ = io.write_encoded_rune(to_writer(b), r, write_quote)
  246. return
  247. }
  248. // appends a rune to the builder, fully written out in case of escaped runes e.g. '\a' will be written as such
  249. // when `r` and `quote` match and `quote` is `\\` - they will be written as two slashes
  250. // `html_safe` flag in case the runes '<', '>', '&' should be encoded as digits e.g. `\u0026`
  251. write_escaped_rune :: proc(b: ^Builder, r: rune, quote: byte, html_safe := false) -> (n: int) {
  252. n, _ = io.write_escaped_rune(to_writer(b), r, quote, html_safe)
  253. return
  254. }
  255. // writes a f64 value into the builder, returns the written amount of characters
  256. write_float :: proc(b: ^Builder, f: f64, fmt: byte, prec, bit_size: int, always_signed := false) -> (n: int) {
  257. buf: [384]byte
  258. s := strconv.append_float(buf[:], f, fmt, prec, bit_size)
  259. // If the result starts with a `+` then unless we always want signed results,
  260. // we skip it unless it's followed by an `I` (because of +Inf).
  261. if !always_signed && (buf[0] == '+' && buf[1] != 'I') {
  262. s = s[1:]
  263. }
  264. return write_string(b, s)
  265. }
  266. // writes a f16 value into the builder, returns the written amount of characters
  267. write_f16 :: proc(b: ^Builder, f: f16, fmt: byte, always_signed := false) -> (n: int) {
  268. buf: [384]byte
  269. s := strconv.append_float(buf[:], f64(f), fmt, 2*size_of(f), 8*size_of(f))
  270. if !always_signed && (buf[0] == '+' && buf[1] != 'I') {
  271. s = s[1:]
  272. }
  273. return write_string(b, s)
  274. }
  275. // writes a f32 value into the builder, returns the written amount of characters
  276. write_f32 :: proc(b: ^Builder, f: f32, fmt: byte, always_signed := false) -> (n: int) {
  277. buf: [384]byte
  278. s := strconv.append_float(buf[:], f64(f), fmt, 2*size_of(f), 8*size_of(f))
  279. if !always_signed && (buf[0] == '+' && buf[1] != 'I') {
  280. s = s[1:]
  281. }
  282. return write_string(b, s)
  283. }
  284. // writes a f64 value into the builder, returns the written amount of characters
  285. write_f64 :: proc(b: ^Builder, f: f64, fmt: byte, always_signed := false) -> (n: int) {
  286. buf: [384]byte
  287. s := strconv.append_float(buf[:], f64(f), fmt, 2*size_of(f), 8*size_of(f))
  288. if !always_signed && (buf[0] == '+' && buf[1] != 'I') {
  289. s = s[1:]
  290. }
  291. return write_string(b, s)
  292. }
  293. // writes a u64 value `i` in `base` = 10 into the builder, returns the written amount of characters
  294. write_u64 :: proc(b: ^Builder, i: u64, base: int = 10) -> (n: int) {
  295. buf: [32]byte
  296. s := strconv.append_bits(buf[:], i, base, false, 64, strconv.digits, nil)
  297. return write_string(b, s)
  298. }
  299. // writes a i64 value `i` in `base` = 10 into the builder, returns the written amount of characters
  300. write_i64 :: proc(b: ^Builder, i: i64, base: int = 10) -> (n: int) {
  301. buf: [32]byte
  302. s := strconv.append_bits(buf[:], u64(i), base, true, 64, strconv.digits, nil)
  303. return write_string(b, s)
  304. }
  305. // writes a uint value `i` in `base` = 10 into the builder, returns the written amount of characters
  306. write_uint :: proc(b: ^Builder, i: uint, base: int = 10) -> (n: int) {
  307. return write_u64(b, u64(i), base)
  308. }
  309. // writes a int value `i` in `base` = 10 into the builder, returns the written amount of characters
  310. write_int :: proc(b: ^Builder, i: int, base: int = 10) -> (n: int) {
  311. return write_i64(b, i64(i), base)
  312. }