builder.odin 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. package strings
  2. import "core:mem"
  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_slice(bytes[:])
  109. strings.write_byte(&builder, 'a') -> "a"
  110. strings.write_byte(&builder, 'b') -> "ab"
  111. */
  112. builder_from_slice :: proc(backing: []byte) -> Builder {
  113. s := transmute(mem.Raw_Slice)backing
  114. d := mem.Raw_Dynamic_Array{
  115. data = s.data,
  116. len = 0,
  117. cap = s.len,
  118. allocator = mem.nil_allocator(),
  119. }
  120. return Builder{
  121. buf = transmute([dynamic]byte)d,
  122. }
  123. }
  124. // cast the builder byte buffer to a string and return it
  125. to_string :: proc(b: Builder) -> string {
  126. return string(b.buf[:])
  127. }
  128. // return the length of the builder byte buffer
  129. builder_len :: proc(b: Builder) -> int {
  130. return len(b.buf)
  131. }
  132. // return the cap of the builder byte buffer
  133. builder_cap :: proc(b: Builder) -> int {
  134. return cap(b.buf)
  135. }
  136. // returns the space left in the builder byte buffer to use up
  137. builder_space :: proc(b: Builder) -> int {
  138. return cap(b.buf) - len(b.buf)
  139. }
  140. /*
  141. appends a byte to the builder, returns the append diff
  142. builder := strings.make_builder()
  143. strings.write_byte(&builder, 'a') // 1
  144. strings.write_byte(&builder, 'b') // 1
  145. strings.write_byte(&builder, 'c') // 1
  146. fmt.println(strings.to_string(builder)) // -> abc
  147. */
  148. write_byte :: proc(b: ^Builder, x: byte) -> (n: int) {
  149. n0 := len(b.buf)
  150. append(&b.buf, x)
  151. n1 := len(b.buf)
  152. return n1-n0
  153. }
  154. /*
  155. appends a slice of bytes to the builder, returns the append diff
  156. builder := strings.make_builder()
  157. bytes := [?]byte { 'a', 'b', 'c' }
  158. strings.write_bytes(&builder, bytes[:]) // 3
  159. fmt.println(strings.to_string(builder)) // -> abc
  160. */
  161. write_bytes :: proc(b: ^Builder, x: []byte) -> (n: int) {
  162. n0 := len(b.buf)
  163. append(&b.buf, ..x)
  164. n1 := len(b.buf)
  165. return n1-n0
  166. }
  167. /*
  168. appends a single rune into the builder, returns written rune size and an `io.Error`
  169. builder := strings.make_builder()
  170. strings.write_rune_builder(&builder, 'ä') // 2 None
  171. strings.write_rune_builder(&builder, 'b') // 1 None
  172. strings.write_rune_builder(&builder, 'c') // 1 None
  173. fmt.println(strings.to_string(builder)) // -> äbc
  174. */
  175. write_rune_builder :: proc(b: ^Builder, r: rune) -> (int, io.Error) {
  176. return io.write_rune(to_writer(b), r)
  177. }
  178. /*
  179. appends a quoted rune into the builder, returns written size
  180. builder := strings.make_builder()
  181. strings.write_string(&builder, "abc") // 3
  182. strings.write_quoted_rune_builder(&builder, 'ä') // 4
  183. strings.write_string(&builder, "abc") // 3
  184. fmt.println(strings.to_string(builder)) // -> abc'ä'abc
  185. */
  186. write_quoted_rune_builder :: proc(b: ^Builder, r: rune) -> (n: int) {
  187. return write_quoted_rune(to_writer(b), r)
  188. }
  189. @(private)
  190. _write_byte :: proc(w: io.Writer, c: byte) -> int {
  191. err := io.write_byte(w, c)
  192. return 1 if err == nil else 0
  193. }
  194. // writer append a quoted rune into the byte buffer, return the written size
  195. write_quoted_rune :: proc(w: io.Writer, r: rune) -> (n: int) {
  196. quote := byte('\'')
  197. n += _write_byte(w, quote)
  198. buf, width := utf8.encode_rune(r)
  199. if width == 1 && r == utf8.RUNE_ERROR {
  200. n += _write_byte(w, '\\')
  201. n += _write_byte(w, 'x')
  202. n += _write_byte(w, DIGITS_LOWER[buf[0]>>4])
  203. n += _write_byte(w, DIGITS_LOWER[buf[0]&0xf])
  204. } else {
  205. i, _ := io.write_escaped_rune(w, r, quote)
  206. n += i
  207. }
  208. n += _write_byte(w, quote)
  209. return
  210. }
  211. // overload for `write_string_*` variants
  212. write_string :: proc{
  213. write_string_builder,
  214. write_string_writer,
  215. }
  216. /*
  217. appends a string to the builder, return the written byte size
  218. builder := strings.make_builder()
  219. strings.write_string(&builder, "a") // 1
  220. strings.write_string(&builder, "bc") // 2
  221. strings.write_string(&builder, "xyz") // 3
  222. fmt.println(strings.to_string(builder)) // -> abcxyz
  223. */
  224. write_string_builder :: proc(b: ^Builder, s: string) -> (n: int) {
  225. return write_string_writer(to_writer(b), s)
  226. }
  227. // appends a string to the writer
  228. write_string_writer :: proc(w: io.Writer, s: string) -> (n: int) {
  229. n, _ = io.write(w, transmute([]byte)s)
  230. return
  231. }
  232. // pops and returns the last byte in the builder
  233. // returns 0 when the builder is empty
  234. pop_byte :: proc(b: ^Builder) -> (r: byte) {
  235. if len(b.buf) == 0 {
  236. return 0
  237. }
  238. r = b.buf[len(b.buf)-1]
  239. d := cast(^mem.Raw_Dynamic_Array)&b.buf
  240. d.len = max(d.len-1, 0)
  241. return
  242. }
  243. // pops the last rune in the builder and returns the popped rune and its rune width
  244. // returns 0, 0 when the builder is empty
  245. pop_rune :: proc(b: ^Builder) -> (r: rune, width: int) {
  246. if len(b.buf) == 0 {
  247. return 0, 0
  248. }
  249. r, width = utf8.decode_last_rune(b.buf[:])
  250. d := cast(^mem.Raw_Dynamic_Array)&b.buf
  251. d.len = max(d.len-width, 0)
  252. return
  253. }
  254. @(private)
  255. DIGITS_LOWER := "0123456789abcdefx"
  256. // overload for `write_quoted_string_*` variants
  257. write_quoted_string :: proc{
  258. write_quoted_string_builder,
  259. write_quoted_string_writer,
  260. }
  261. /*
  262. append a quoted string into the builder, return the written byte size
  263. builder := strings.make_builder()
  264. strings.write_quoted_string(&builder, "a") // 3
  265. strings.write_quoted_string(&builder, "bc", '\'') // 4
  266. strings.write_quoted_string(&builder, "xyz") // 5
  267. fmt.println(strings.to_string(builder)) // -> "a"'bc'xyz"
  268. */
  269. write_quoted_string_builder :: proc(b: ^Builder, str: string, quote: byte = '"') -> (n: int) {
  270. n, _ = io.write_quoted_string(to_writer(b), str, quote)
  271. return
  272. }
  273. @(deprecated="prefer io.write_quoted_string")
  274. write_quoted_string_writer :: proc(w: io.Writer, str: string, quote: byte = '"') -> (n: int) {
  275. n, _ = io.write_quoted_string(w, str, quote)
  276. return
  277. }
  278. // overload for `write_encoded_rune_*`
  279. write_encoded_rune :: proc{
  280. write_encoded_rune_builder,
  281. write_encoded_rune_writer,
  282. }
  283. // appends a rune to the builder, optional `write_quote` boolean tag, returns the written rune size
  284. write_encoded_rune_builder :: proc(b: ^Builder, r: rune, write_quote := true) -> (n: int) {
  285. n, _ = io.write_encoded_rune(to_writer(b), r, write_quote)
  286. return
  287. }
  288. @(deprecated="prefer io.write_encoded_rune")
  289. write_encoded_rune_writer :: proc(w: io.Writer, r: rune, write_quote := true) -> (n: int) {
  290. n, _ = io.write_encoded_rune(w, r, write_quote)
  291. return
  292. }
  293. // overload for `write_escaped_rune_*`
  294. write_escaped_rune :: proc{
  295. write_escaped_rune_builder,
  296. write_escaped_rune_writer,
  297. }
  298. // appends a rune to the builder, fully written out in case of escaped runes e.g. '\a' will be written as such
  299. // when `r` and `quote` match and `quote` is `\\` - they will be written as two slashes
  300. // `html_safe` flag in case the runes '<', '>', '&' should be encoded as digits e.g. `\u0026`
  301. write_escaped_rune_builder :: proc(b: ^Builder, r: rune, quote: byte, html_safe := false) -> (n: int) {
  302. n, _ = io.write_escaped_rune(to_writer(b), r, quote, html_safe)
  303. return
  304. }
  305. @(deprecated="prefer io.write_escaped_rune")
  306. write_escaped_rune_writer :: proc(w: io.Writer, r: rune, quote: byte, html_safe := false) -> (n: int) {
  307. n, _ = io.write_escaped_rune(w, r, quote, html_safe)
  308. return
  309. }
  310. // writes a u64 value `i` in `base` = 10 into the builder, returns the written amount of characters
  311. write_u64 :: proc(b: ^Builder, i: u64, base: int = 10) -> (n: int) {
  312. buf: [32]byte
  313. s := strconv.append_bits(buf[:], i, base, false, 64, strconv.digits, nil)
  314. return write_string(b, s)
  315. }
  316. // writes a i64 value `i` in `base` = 10 into the builder, returns the written amount of characters
  317. write_i64 :: proc(b: ^Builder, i: i64, base: int = 10) -> (n: int) {
  318. buf: [32]byte
  319. s := strconv.append_bits(buf[:], u64(i), base, true, 64, strconv.digits, nil)
  320. return write_string(b, s)
  321. }
  322. // writes a uint value `i` in `base` = 10 into the builder, returns the written amount of characters
  323. write_uint :: proc(b: ^Builder, i: uint, base: int = 10) -> (n: int) {
  324. return write_u64(b, u64(i), base)
  325. }
  326. // writes a int value `i` in `base` = 10 into the builder, returns the written amount of characters
  327. write_int :: proc(b: ^Builder, i: int, base: int = 10) -> (n: int) {
  328. return write_i64(b, i64(i), base)
  329. }