| 1 | // Copyright (c) 2019-2024 Alexander Medvednikov. All rights reserved. |
| 2 | // Use of this source code is governed by an MIT license |
| 3 | // that can be found in the LICENSE file. |
| 4 | module strings |
| 5 | |
| 6 | /* |
| 7 | pub struct Builder { |
| 8 | mut: |
| 9 | buf []u8 |
| 10 | pub mut: |
| 11 | len int |
| 12 | initial_size int = 1 |
| 13 | }*/ |
| 14 | |
| 15 | pub type Builder = []u8 |
| 16 | |
| 17 | pub fn new_builder(initial_size int) Builder { |
| 18 | return []u8{cap: initial_size} |
| 19 | } |
| 20 | |
| 21 | pub fn (mut b Builder) write_byte(data u8) { |
| 22 | b << data |
| 23 | } |
| 24 | |
| 25 | pub fn (mut b Builder) write_u8(data u8) { |
| 26 | b << data |
| 27 | } |
| 28 | |
| 29 | pub fn (mut b Builder) write(data []u8) ?int { |
| 30 | if data.len == 0 { |
| 31 | return 0 |
| 32 | } |
| 33 | b << data |
| 34 | return data.len |
| 35 | } |
| 36 | |
| 37 | pub fn (b &Builder) byte_at(n int) u8 { |
| 38 | unsafe { |
| 39 | return b[n] |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | pub fn (mut b Builder) write_string(s string) { |
| 44 | if s == '' { |
| 45 | return |
| 46 | } |
| 47 | |
| 48 | for c in s { |
| 49 | b << c |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | pub fn (mut b Builder) writeln(s string) { |
| 54 | if s != '' { |
| 55 | b.write_string(s) |
| 56 | } |
| 57 | |
| 58 | b << 10 |
| 59 | } |
| 60 | |
| 61 | pub fn (mut b Builder) str() string { |
| 62 | s := '' |
| 63 | |
| 64 | #for (const c of b.val.arr.arr) |
| 65 | #s.str += String.fromCharCode(+c) |
| 66 | b.clear() |
| 67 | return s |
| 68 | } |
| 69 | |
| 70 | pub fn (mut b Builder) cut_last(n int) string { |
| 71 | cut_pos := b.len - n |
| 72 | x := unsafe { b[cut_pos..] } |
| 73 | res := x.bytestr() |
| 74 | b.trim(cut_pos) |
| 75 | return res |
| 76 | } |
| 77 | |
| 78 | pub fn (mut b Builder) go_back_to(pos int) { |
| 79 | b.trim(pos) |
| 80 | } |
| 81 | |
| 82 | // go_back discards the last `n` bytes from the buffer. |
| 83 | pub fn (mut b Builder) go_back(n int) { |
| 84 | b.trim(b.len - n) |
| 85 | } |
| 86 | |
| 87 | // cut_to cuts the string after `pos` and returns it. |
| 88 | // if `pos` is superior to builder length, returns an empty string |
| 89 | // and cancel further operations |
| 90 | pub fn (mut b Builder) cut_to(pos int) string { |
| 91 | if pos > b.len { |
| 92 | return '' |
| 93 | } |
| 94 | return b.cut_last(b.len - pos) |
| 95 | } |
| 96 | |
| 97 | pub fn (mut b Builder) write_runes(runes []rune) { |
| 98 | for r in runes { |
| 99 | res := r.str() |
| 100 | #res.str = String.fromCharCode(r.val) |
| 101 | b << res.bytes() |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | // after(6) returns 'world' |
| 106 | // buf == 'hello world' |
| 107 | pub fn (mut b Builder) after(n int) string { |
| 108 | if n >= b.len { |
| 109 | return '' |
| 110 | } |
| 111 | |
| 112 | x := unsafe { b[n..b.len] } |
| 113 | return x.bytestr() |
| 114 | } |
| 115 | |
| 116 | // last_n(5) returns 'world' |
| 117 | // buf == 'hello world' |
| 118 | pub fn (mut b Builder) last_n(n int) string { |
| 119 | if n >= b.len { |
| 120 | return '' |
| 121 | } |
| 122 | |
| 123 | x := unsafe { b[b.len - n..b.len] } |
| 124 | return x.bytestr() |
| 125 | } |
| 126 | |