buffer.odin 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. package bytes
  2. import "core:io"
  3. import "core:unicode/utf8"
  4. MIN_READ :: 512
  5. @(private)
  6. SMALL_BUFFER_SIZE :: 64
  7. // A Buffer is a variable-sized buffer of bytes with a io.Stream interface
  8. // The zero value for Buffer is an empty buffer ready to use.
  9. Buffer :: struct {
  10. buf: [dynamic]byte,
  11. off: int,
  12. last_read: Read_Op,
  13. }
  14. @(private)
  15. Read_Op :: enum i8 {
  16. Read = -1,
  17. Invalid = 0,
  18. Read_Rune1 = 1,
  19. Read_Rune2 = 2,
  20. Read_Rune3 = 3,
  21. Read_Rune4 = 4,
  22. }
  23. buffer_init :: proc(b: ^Buffer, buf: []byte, loc := #caller_location) {
  24. resize(&b.buf, len(buf), loc=loc)
  25. copy(b.buf[:], buf)
  26. }
  27. buffer_init_string :: proc(b: ^Buffer, s: string, loc := #caller_location) {
  28. resize(&b.buf, len(s), loc=loc)
  29. copy(b.buf[:], s)
  30. }
  31. buffer_init_allocator :: proc(b: ^Buffer, len, cap: int, allocator := context.allocator, loc := #caller_location) {
  32. if b.buf == nil {
  33. b.buf = make([dynamic]byte, len, cap, allocator, loc)
  34. return
  35. }
  36. b.buf.allocator = allocator
  37. reserve(&b.buf, cap)
  38. resize(&b.buf, len)
  39. }
  40. buffer_destroy :: proc(b: ^Buffer) {
  41. delete(b.buf)
  42. buffer_reset(b)
  43. }
  44. buffer_to_bytes :: proc(b: ^Buffer) -> []byte {
  45. return b.buf[b.off:]
  46. }
  47. buffer_to_string :: proc(b: ^Buffer) -> string {
  48. if b == nil {
  49. return "<nil>"
  50. }
  51. return string(b.buf[b.off:])
  52. }
  53. buffer_is_empty :: proc(b: ^Buffer) -> bool {
  54. return len(b.buf) <= b.off
  55. }
  56. buffer_length :: proc(b: ^Buffer) -> int {
  57. return len(b.buf) - b.off
  58. }
  59. buffer_capacity :: proc(b: ^Buffer) -> int {
  60. return cap(b.buf)
  61. }
  62. buffer_reset :: proc(b: ^Buffer) {
  63. clear(&b.buf)
  64. b.off = 0
  65. b.last_read = .Invalid
  66. }
  67. buffer_truncate :: proc(b: ^Buffer, n: int) {
  68. if n == 0 {
  69. buffer_reset(b)
  70. return
  71. }
  72. b.last_read = .Invalid
  73. if n < 0 || n > buffer_length(b) {
  74. panic("bytes.truncate: truncation out of range")
  75. }
  76. resize(&b.buf, b.off+n)
  77. }
  78. @(private)
  79. _buffer_try_grow :: proc(b: ^Buffer, n: int, loc := #caller_location) -> (int, bool) {
  80. if l := len(b.buf); n <= cap(b.buf)-l {
  81. resize(&b.buf, l+n, loc=loc)
  82. return l, true
  83. }
  84. return 0, false
  85. }
  86. @(private)
  87. _buffer_grow :: proc(b: ^Buffer, n: int, loc := #caller_location) -> int {
  88. m := buffer_length(b)
  89. if m == 0 && b.off != 0 {
  90. buffer_reset(b)
  91. }
  92. if i, ok := _buffer_try_grow(b, n, loc=loc); ok {
  93. return i
  94. }
  95. if b.buf == nil && n <= SMALL_BUFFER_SIZE {
  96. // Fixes #2756 by preserving allocator if already set on Buffer via init_buffer_allocator
  97. reserve(&b.buf, SMALL_BUFFER_SIZE, loc=loc)
  98. resize(&b.buf, n, loc=loc)
  99. return 0
  100. }
  101. c := cap(b.buf)
  102. if n <= c/2 - m {
  103. copy(b.buf[:], b.buf[b.off:])
  104. } else if c > max(int) - c - n {
  105. panic("bytes.Buffer: too large")
  106. } else {
  107. resize(&b.buf, 2*c + n, loc=loc)
  108. copy(b.buf[:], b.buf[b.off:])
  109. }
  110. b.off = 0
  111. resize(&b.buf, m+n, loc=loc)
  112. return m
  113. }
  114. buffer_grow :: proc(b: ^Buffer, n: int, loc := #caller_location) {
  115. if n < 0 {
  116. panic("bytes.buffer_grow: negative count")
  117. }
  118. m := _buffer_grow(b, n, loc=loc)
  119. resize(&b.buf, m, loc=loc)
  120. }
  121. buffer_write_at :: proc(b: ^Buffer, p: []byte, offset: int, loc := #caller_location) -> (n: int, err: io.Error) {
  122. if len(p) == 0 {
  123. return 0, nil
  124. }
  125. b.last_read = .Invalid
  126. if offset < 0 {
  127. err = .Invalid_Offset
  128. return
  129. }
  130. _, ok := _buffer_try_grow(b, offset+len(p), loc=loc)
  131. if !ok {
  132. _ = _buffer_grow(b, offset+len(p), loc=loc)
  133. }
  134. if len(b.buf) <= offset {
  135. return 0, .Short_Write
  136. }
  137. return copy(b.buf[offset:], p), nil
  138. }
  139. buffer_write :: proc(b: ^Buffer, p: []byte, loc := #caller_location) -> (n: int, err: io.Error) {
  140. b.last_read = .Invalid
  141. m, ok := _buffer_try_grow(b, len(p), loc=loc)
  142. if !ok {
  143. m = _buffer_grow(b, len(p), loc=loc)
  144. }
  145. return copy(b.buf[m:], p), nil
  146. }
  147. buffer_write_ptr :: proc(b: ^Buffer, ptr: rawptr, size: int, loc := #caller_location) -> (n: int, err: io.Error) {
  148. return buffer_write(b, ([^]byte)(ptr)[:size], loc=loc)
  149. }
  150. buffer_write_string :: proc(b: ^Buffer, s: string, loc := #caller_location) -> (n: int, err: io.Error) {
  151. b.last_read = .Invalid
  152. m, ok := _buffer_try_grow(b, len(s), loc=loc)
  153. if !ok {
  154. m = _buffer_grow(b, len(s), loc=loc)
  155. }
  156. return copy(b.buf[m:], s), nil
  157. }
  158. buffer_write_byte :: proc(b: ^Buffer, c: byte, loc := #caller_location) -> io.Error {
  159. b.last_read = .Invalid
  160. m, ok := _buffer_try_grow(b, 1, loc=loc)
  161. if !ok {
  162. m = _buffer_grow(b, 1, loc=loc)
  163. }
  164. b.buf[m] = c
  165. return nil
  166. }
  167. buffer_write_rune :: proc(b: ^Buffer, r: rune, loc := #caller_location) -> (n: int, err: io.Error) {
  168. if r < utf8.RUNE_SELF {
  169. buffer_write_byte(b, byte(r), loc=loc)
  170. return 1, nil
  171. }
  172. b.last_read = .Invalid
  173. m, ok := _buffer_try_grow(b, utf8.UTF_MAX, loc=loc)
  174. if !ok {
  175. m = _buffer_grow(b, utf8.UTF_MAX, loc=loc)
  176. }
  177. res: [4]byte
  178. res, n = utf8.encode_rune(r)
  179. copy(b.buf[m:][:utf8.UTF_MAX], res[:n])
  180. resize(&b.buf, m+n)
  181. return
  182. }
  183. buffer_next :: proc(b: ^Buffer, n: int) -> []byte {
  184. n := n
  185. b.last_read = .Invalid
  186. m := buffer_length(b)
  187. if n > m {
  188. n = m
  189. }
  190. data := b.buf[b.off : b.off + n]
  191. b.off += n
  192. if n > 0 {
  193. b.last_read = .Read
  194. }
  195. return data
  196. }
  197. buffer_read :: proc(b: ^Buffer, p: []byte) -> (n: int, err: io.Error) {
  198. b.last_read = .Invalid
  199. if buffer_is_empty(b) {
  200. buffer_reset(b)
  201. if len(p) == 0 {
  202. return 0, nil
  203. }
  204. return 0, .EOF
  205. }
  206. n = copy(p, b.buf[b.off:])
  207. b.off += n
  208. if n > 0 {
  209. b.last_read = .Read
  210. }
  211. return
  212. }
  213. buffer_read_ptr :: proc(b: ^Buffer, ptr: rawptr, size: int) -> (n: int, err: io.Error) {
  214. return buffer_read(b, ([^]byte)(ptr)[:size])
  215. }
  216. buffer_read_at :: proc(b: ^Buffer, p: []byte, offset: int) -> (n: int, err: io.Error) {
  217. if len(p) == 0 {
  218. return 0, nil
  219. }
  220. b.last_read = .Invalid
  221. if uint(offset) >= len(b.buf) {
  222. err = .EOF
  223. return
  224. }
  225. n = copy(p, b.buf[offset:])
  226. if n > 0 {
  227. b.last_read = .Read
  228. }
  229. return
  230. }
  231. buffer_read_byte :: proc(b: ^Buffer) -> (byte, io.Error) {
  232. if buffer_is_empty(b) {
  233. buffer_reset(b)
  234. return 0, .EOF
  235. }
  236. c := b.buf[b.off]
  237. b.off += 1
  238. b.last_read = .Read
  239. return c, nil
  240. }
  241. buffer_read_rune :: proc(b: ^Buffer) -> (r: rune, size: int, err: io.Error) {
  242. if buffer_is_empty(b) {
  243. buffer_reset(b)
  244. return 0, 0, .EOF
  245. }
  246. c := b.buf[b.off]
  247. if c < utf8.RUNE_SELF {
  248. b.off += 1
  249. b.last_read = .Read_Rune1
  250. return rune(c), 1, nil
  251. }
  252. r, size = utf8.decode_rune(b.buf[b.off:])
  253. b.off += size
  254. b.last_read = Read_Op(i8(size))
  255. return
  256. }
  257. buffer_unread_byte :: proc(b: ^Buffer) -> io.Error {
  258. if b.last_read == .Invalid {
  259. return .Invalid_Unread
  260. }
  261. b.last_read = .Invalid
  262. if b.off > 0 {
  263. b.off -= 1
  264. }
  265. return nil
  266. }
  267. buffer_unread_rune :: proc(b: ^Buffer) -> io.Error {
  268. if b.last_read <= .Invalid {
  269. return .Invalid_Unread
  270. }
  271. if b.off >= int(b.last_read) {
  272. b.off -= int(i8(b.last_read))
  273. }
  274. b.last_read = .Invalid
  275. return nil
  276. }
  277. buffer_seek :: proc(b: ^Buffer, offset: i64, whence: io.Seek_From) -> (i64, io.Error) {
  278. abs: i64
  279. switch whence {
  280. case .Start:
  281. abs = offset
  282. case .Current:
  283. abs = i64(b.off) + offset
  284. case .End:
  285. abs = i64(len(b.buf)) + offset
  286. case:
  287. return 0, .Invalid_Whence
  288. }
  289. abs_int := int(abs)
  290. if abs_int < 0 {
  291. return 0, .Invalid_Offset
  292. }
  293. b.last_read = .Invalid
  294. b.off = abs_int
  295. return abs, nil
  296. }
  297. buffer_read_bytes :: proc(b: ^Buffer, delim: byte) -> (line: []byte, err: io.Error) {
  298. i := index_byte(b.buf[b.off:], delim)
  299. end := b.off + i + 1
  300. if i < 0 {
  301. end = len(b.buf)
  302. err = .EOF
  303. }
  304. line = b.buf[b.off:end]
  305. b.off = end
  306. b.last_read = .Read
  307. return
  308. }
  309. buffer_read_string :: proc(b: ^Buffer, delim: byte) -> (line: string, err: io.Error) {
  310. slice: []byte
  311. slice, err = buffer_read_bytes(b, delim)
  312. return string(slice), err
  313. }
  314. buffer_write_to :: proc(b: ^Buffer, w: io.Writer) -> (n: i64, err: io.Error) {
  315. b.last_read = .Invalid
  316. if byte_count := buffer_length(b); byte_count > 0 {
  317. m, e := io.write(w, b.buf[b.off:])
  318. if m > byte_count {
  319. panic("bytes.buffer_write_to: invalid io.write count")
  320. }
  321. b.off += m
  322. n = i64(m)
  323. if e != nil {
  324. err = e
  325. return
  326. }
  327. if m != byte_count {
  328. err = .Short_Write
  329. return
  330. }
  331. }
  332. buffer_reset(b)
  333. return
  334. }
  335. buffer_read_from :: proc(b: ^Buffer, r: io.Reader) -> (n: i64, err: io.Error) #no_bounds_check {
  336. b.last_read = .Invalid
  337. for {
  338. i := _buffer_grow(b, MIN_READ)
  339. resize(&b.buf, i)
  340. m, e := io.read(r, b.buf[i:cap(b.buf)])
  341. if m < 0 {
  342. err = e if e != nil else .Negative_Read
  343. return
  344. }
  345. resize(&b.buf, i+m)
  346. n += i64(m)
  347. if e == .EOF {
  348. return
  349. }
  350. if e != nil {
  351. err = e
  352. return
  353. }
  354. }
  355. return
  356. }
  357. buffer_to_stream :: proc(b: ^Buffer) -> (s: io.Stream) {
  358. s.data = b
  359. s.procedure = _buffer_proc
  360. return
  361. }
  362. @(private)
  363. _buffer_proc :: proc(stream_data: rawptr, mode: io.Stream_Mode, p: []byte, offset: i64, whence: io.Seek_From) -> (n: i64, err: io.Error) {
  364. b := (^Buffer)(stream_data)
  365. #partial switch mode {
  366. case .Read:
  367. return io._i64_err(buffer_read(b, p))
  368. case .Read_At:
  369. return io._i64_err(buffer_read_at(b, p, int(offset)))
  370. case .Write:
  371. return io._i64_err(buffer_write(b, p))
  372. case .Write_At:
  373. return io._i64_err(buffer_write_at(b, p, int(offset)))
  374. case .Seek:
  375. n, err = buffer_seek(b, offset, whence)
  376. return
  377. case .Size:
  378. n = i64(buffer_length(b))
  379. return
  380. case .Destroy:
  381. buffer_destroy(b)
  382. return
  383. case .Query:
  384. return io.query_utility({.Read, .Read_At, .Write, .Write_At, .Seek, .Size, .Destroy, .Query})
  385. }
  386. return 0, .Empty
  387. }