builder.odin 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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. make_builder_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. make_builder_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. make_builder_len_cap :: proc(len, cap: int, allocator := context.allocator) -> Builder {
  25. return Builder{buf=make([dynamic]byte, len, cap, allocator)}
  26. }
  27. // overload simple `make_builder_*` with or without len / cap parameters
  28. make_builder :: proc{
  29. make_builder_none,
  30. make_builder_len,
  31. make_builder_len_cap,
  32. }
  33. // initialize a builder, default length 0 / cap 16 are done through make
  34. // replaces the existing `buf`
  35. init_builder_none :: proc(b: ^Builder, allocator := context.allocator) {
  36. b.buf = make([dynamic]byte, allocator)
  37. }
  38. // initialize a builder, with a set length `len` and cap 16 byte buffer
  39. // replaces the existing `buf`
  40. init_builder_len :: proc(b: ^Builder, len: int, allocator := context.allocator) {
  41. b.buf = make([dynamic]byte, len, allocator)
  42. }
  43. // initialize a builder, with a set length `len` byte buffer and a custom `cap`
  44. // replaces the existing `buf`
  45. init_builder_len_cap :: proc(b: ^Builder, len, cap: int, allocator := context.allocator) {
  46. b.buf = make([dynamic]byte, len, cap, allocator)
  47. }
  48. // overload simple `init_builder_*` with or without len / ap parameters
  49. init_builder :: proc{
  50. init_builder_none,
  51. init_builder_len,
  52. init_builder_len_cap,
  53. }
  54. @(private)
  55. _builder_stream_vtable := &io.Stream_VTable{
  56. impl_write = proc(s: io.Stream, p: []byte) -> (n: int, err: io.Error) {
  57. b := (^Builder)(s.stream_data)
  58. n = write_bytes(b, p)
  59. if n < len(p) {
  60. err = .EOF
  61. }
  62. return
  63. },
  64. impl_write_byte = proc(s: io.Stream, c: byte) -> (err: io.Error) {
  65. b := (^Builder)(s.stream_data)
  66. n := write_byte(b, c)
  67. if n == 0 {
  68. err = .EOF
  69. }
  70. return
  71. },
  72. impl_size = proc(s: io.Stream) -> i64 {
  73. b := (^Builder)(s.stream_data)
  74. return i64(len(b.buf))
  75. },
  76. impl_destroy = proc(s: io.Stream) -> io.Error {
  77. b := (^Builder)(s.stream_data)
  78. delete(b.buf)
  79. return .None
  80. },
  81. }
  82. // return an `io.Stream` from a builder
  83. to_stream :: proc(b: ^Builder) -> io.Stream {
  84. return io.Stream{stream_vtable=_builder_stream_vtable, stream_data=b}
  85. }
  86. // return an `io.Writer` from a builder
  87. to_writer :: proc(b: ^Builder) -> io.Writer {
  88. return io.to_writer(to_stream(b))
  89. }
  90. // delete and clear the builder byte buffer content
  91. destroy_builder :: proc(b: ^Builder) {
  92. delete(b.buf)
  93. clear(&b.buf)
  94. }
  95. // reserve the builfer byte buffer to a specific cap, when it's higher than before
  96. grow_builder :: proc(b: ^Builder, cap: int) {
  97. reserve(&b.buf, cap)
  98. }
  99. // clear the builder byte buffer content
  100. reset_builder :: proc(b: ^Builder) {
  101. clear(&b.buf)
  102. }
  103. /*
  104. create an empty builder with the same slice length as its cap
  105. uses the `mem.nil_allocator` to avoid allocation and keep a fixed length
  106. used in `fmt.bprint*`
  107. bytes: [8]byte // <-- gets filled
  108. builder := strings.builder_from_bytes(bytes[:])
  109. strings.write_byte(&builder, 'a') -> "a"
  110. strings.write_byte(&builder, 'b') -> "ab"
  111. */
  112. builder_from_bytes :: proc(backing: []byte) -> Builder {
  113. s := transmute(runtime.Raw_Slice)backing
  114. d := runtime.Raw_Dynamic_Array{
  115. data = s.data,
  116. len = 0,
  117. cap = s.len,
  118. allocator = runtime.nil_allocator(),
  119. }
  120. return Builder{
  121. buf = transmute([dynamic]byte)d,
  122. }
  123. }
  124. builder_from_slice :: builder_from_bytes
  125. // cast the builder byte buffer to a string and return it
  126. to_string :: proc(b: Builder) -> string {
  127. return string(b.buf[:])
  128. }
  129. // return the length of the builder byte buffer
  130. builder_len :: proc(b: Builder) -> int {
  131. return len(b.buf)
  132. }
  133. // return the cap of the builder byte buffer
  134. builder_cap :: proc(b: Builder) -> int {
  135. return cap(b.buf)
  136. }
  137. // returns the space left in the builder byte buffer to use up
  138. builder_space :: proc(b: Builder) -> int {
  139. return cap(b.buf) - len(b.buf)
  140. }
  141. /*
  142. appends a byte to the builder, returns the append diff
  143. builder := strings.make_builder()
  144. strings.write_byte(&builder, 'a') // 1
  145. strings.write_byte(&builder, 'b') // 1
  146. strings.write_byte(&builder, 'c') // 1
  147. fmt.println(strings.to_string(builder)) // -> abc
  148. */
  149. write_byte :: proc(b: ^Builder, x: byte) -> (n: int) {
  150. n0 := len(b.buf)
  151. append(&b.buf, x)
  152. n1 := len(b.buf)
  153. return n1-n0
  154. }
  155. /*
  156. appends a slice of bytes to the builder, returns the append diff
  157. builder := strings.make_builder()
  158. bytes := [?]byte { 'a', 'b', 'c' }
  159. strings.write_bytes(&builder, bytes[:]) // 3
  160. fmt.println(strings.to_string(builder)) // -> abc
  161. */
  162. write_bytes :: proc(b: ^Builder, x: []byte) -> (n: int) {
  163. n0 := len(b.buf)
  164. append(&b.buf, ..x)
  165. n1 := len(b.buf)
  166. return n1-n0
  167. }
  168. /*
  169. appends a single rune into the builder, returns written rune size and an `io.Error`
  170. builder := strings.make_builder()
  171. strings.write_rune_builder(&builder, 'ä') // 2 None
  172. strings.write_rune_builder(&builder, 'b') // 1 None
  173. strings.write_rune_builder(&builder, 'c') // 1 None
  174. fmt.println(strings.to_string(builder)) // -> äbc
  175. */
  176. write_rune_builder :: proc(b: ^Builder, r: rune) -> (int, io.Error) {
  177. return io.write_rune(to_writer(b), r)
  178. }
  179. /*
  180. appends a quoted rune into the builder, returns written size
  181. builder := strings.make_builder()
  182. strings.write_string(&builder, "abc") // 3
  183. strings.write_quoted_rune_builder(&builder, 'ä') // 4
  184. strings.write_string(&builder, "abc") // 3
  185. fmt.println(strings.to_string(builder)) // -> abc'ä'abc
  186. */
  187. write_quoted_rune_builder :: proc(b: ^Builder, r: rune) -> (n: int) {
  188. return write_quoted_rune(to_writer(b), r)
  189. }
  190. @(private)
  191. _write_byte :: proc(w: io.Writer, c: byte) -> int {
  192. err := io.write_byte(w, c)
  193. return 1 if err == nil else 0
  194. }
  195. // writer append a quoted rune into the byte buffer, return the written size
  196. write_quoted_rune :: proc(w: io.Writer, r: rune) -> (n: int) {
  197. quote := byte('\'')
  198. n += _write_byte(w, quote)
  199. buf, width := utf8.encode_rune(r)
  200. if width == 1 && r == utf8.RUNE_ERROR {
  201. n += _write_byte(w, '\\')
  202. n += _write_byte(w, 'x')
  203. n += _write_byte(w, DIGITS_LOWER[buf[0]>>4])
  204. n += _write_byte(w, DIGITS_LOWER[buf[0]&0xf])
  205. } else {
  206. i, _ := io.write_escaped_rune(w, r, quote)
  207. n += i
  208. }
  209. n += _write_byte(w, quote)
  210. return
  211. }
  212. // overload for `write_string_*` variants
  213. write_string :: proc{
  214. write_string_builder,
  215. write_string_writer,
  216. }
  217. /*
  218. appends a string to the builder, return the written byte size
  219. builder := strings.make_builder()
  220. strings.write_string(&builder, "a") // 1
  221. strings.write_string(&builder, "bc") // 2
  222. strings.write_string(&builder, "xyz") // 3
  223. fmt.println(strings.to_string(builder)) // -> abcxyz
  224. */
  225. write_string_builder :: proc(b: ^Builder, s: string) -> (n: int) {
  226. return write_string_writer(to_writer(b), s)
  227. }
  228. // appends a string to the writer
  229. write_string_writer :: proc(w: io.Writer, s: string) -> (n: int) {
  230. n, _ = io.write(w, transmute([]byte)s)
  231. return
  232. }
  233. // pops and returns the last byte in the builder
  234. // returns 0 when the builder is empty
  235. pop_byte :: proc(b: ^Builder) -> (r: byte) {
  236. if len(b.buf) == 0 {
  237. return 0
  238. }
  239. r = b.buf[len(b.buf)-1]
  240. d := cast(^runtime.Raw_Dynamic_Array)&b.buf
  241. d.len = max(d.len-1, 0)
  242. return
  243. }
  244. // pops the last rune in the builder and returns the popped rune and its rune width
  245. // returns 0, 0 when the builder is empty
  246. pop_rune :: proc(b: ^Builder) -> (r: rune, width: int) {
  247. if len(b.buf) == 0 {
  248. return 0, 0
  249. }
  250. r, width = utf8.decode_last_rune(b.buf[:])
  251. d := cast(^runtime.Raw_Dynamic_Array)&b.buf
  252. d.len = max(d.len-width, 0)
  253. return
  254. }
  255. @(private)
  256. DIGITS_LOWER := "0123456789abcdefx"
  257. // overload for `write_quoted_string_*` variants
  258. write_quoted_string :: proc{
  259. write_quoted_string_builder,
  260. write_quoted_string_writer,
  261. }
  262. /*
  263. append a quoted string into the builder, return the written byte size
  264. builder := strings.make_builder()
  265. strings.write_quoted_string(&builder, "a") // 3
  266. strings.write_quoted_string(&builder, "bc", '\'') // 4
  267. strings.write_quoted_string(&builder, "xyz") // 5
  268. fmt.println(strings.to_string(builder)) // -> "a"'bc'xyz"
  269. */
  270. write_quoted_string_builder :: proc(b: ^Builder, str: string, quote: byte = '"') -> (n: int) {
  271. n, _ = io.write_quoted_string(to_writer(b), str, quote)
  272. return
  273. }
  274. @(deprecated="prefer io.write_quoted_string")
  275. write_quoted_string_writer :: proc(w: io.Writer, str: string, quote: byte = '"') -> (n: int) {
  276. n, _ = io.write_quoted_string(w, str, quote)
  277. return
  278. }
  279. // overload for `write_encoded_rune_*`
  280. write_encoded_rune :: proc{
  281. write_encoded_rune_builder,
  282. write_encoded_rune_writer,
  283. }
  284. // appends a rune to the builder, optional `write_quote` boolean tag, returns the written rune size
  285. write_encoded_rune_builder :: proc(b: ^Builder, r: rune, write_quote := true) -> (n: int) {
  286. n, _ = io.write_encoded_rune(to_writer(b), r, write_quote)
  287. return
  288. }
  289. @(deprecated="prefer io.write_encoded_rune")
  290. write_encoded_rune_writer :: proc(w: io.Writer, r: rune, write_quote := true) -> (n: int) {
  291. n, _ = io.write_encoded_rune(w, r, write_quote)
  292. return
  293. }
  294. // overload for `write_escaped_rune_*`
  295. write_escaped_rune :: proc{
  296. write_escaped_rune_builder,
  297. write_escaped_rune_writer,
  298. }
  299. // appends a rune to the builder, fully written out in case of escaped runes e.g. '\a' will be written as such
  300. // when `r` and `quote` match and `quote` is `\\` - they will be written as two slashes
  301. // `html_safe` flag in case the runes '<', '>', '&' should be encoded as digits e.g. `\u0026`
  302. write_escaped_rune_builder :: proc(b: ^Builder, r: rune, quote: byte, html_safe := false) -> (n: int) {
  303. n, _ = io.write_escaped_rune(to_writer(b), r, quote, html_safe)
  304. return
  305. }
  306. @(deprecated="prefer io.write_escaped_rune")
  307. write_escaped_rune_writer :: proc(w: io.Writer, r: rune, quote: byte, html_safe := false) -> (n: int) {
  308. n, _ = io.write_escaped_rune(w, r, quote, html_safe)
  309. return
  310. }
  311. // writes a u64 value `i` in `base` = 10 into the builder, returns the written amount of characters
  312. write_u64 :: proc(b: ^Builder, i: u64, base: int = 10) -> (n: int) {
  313. buf: [32]byte
  314. s := strconv.append_bits(buf[:], i, base, false, 64, strconv.digits, nil)
  315. return write_string(b, s)
  316. }
  317. // writes a i64 value `i` in `base` = 10 into the builder, returns the written amount of characters
  318. write_i64 :: proc(b: ^Builder, i: i64, base: int = 10) -> (n: int) {
  319. buf: [32]byte
  320. s := strconv.append_bits(buf[:], u64(i), base, true, 64, strconv.digits, nil)
  321. return write_string(b, s)
  322. }
  323. // writes a uint value `i` in `base` = 10 into the builder, returns the written amount of characters
  324. write_uint :: proc(b: ^Builder, i: uint, base: int = 10) -> (n: int) {
  325. return write_u64(b, u64(i), base)
  326. }
  327. // writes a int value `i` in `base` = 10 into the builder, returns the written amount of characters
  328. write_int :: proc(b: ^Builder, i: int, base: int = 10) -> (n: int) {
  329. return write_i64(b, i64(i), base)
  330. }