| 1 | module builtin |
| 2 | |
| 3 | pub struct string { |
| 4 | pub: |
| 5 | str &u8 |
| 6 | len int |
| 7 | } |
| 8 | |
| 9 | pub fn strlen(s &u8) int { |
| 10 | mut i := 0 |
| 11 | for ; s[i] != 0; i++ {} |
| 12 | return i |
| 13 | } |
| 14 | |
| 15 | pub fn tos(s &u8, len int) string { |
| 16 | if s == 0 { |
| 17 | panic('tos(): nil string') |
| 18 | } |
| 19 | return string{ |
| 20 | str: s |
| 21 | len: len |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | fn (s string) add(a string) string { |
| 26 | new_len := a.len + s.len |
| 27 | mut res := string{ |
| 28 | len: new_len |
| 29 | str: malloc(new_len + 1) |
| 30 | } |
| 31 | for j in 0 .. s.len { |
| 32 | res[j] = s[j] |
| 33 | } |
| 34 | for j in 0 .. a.len { |
| 35 | res[s.len + j] = a[j] |
| 36 | } |
| 37 | res[new_len] = 0 // V strings are not null terminated, but just in case |
| 38 | return res |
| 39 | } |
| 40 | |
| 41 | /* |
| 42 | pub fn tos_clone(s byteptr) string { |
| 43 | if s == 0 { |
| 44 | panic('tos: nil string') |
| 45 | } |
| 46 | return tos2(s).clone() |
| 47 | } |
| 48 | */ |
| 49 | |
| 50 | // Same as `tos`, but calculates the length. Called by `string(bytes)` casts. |
| 51 | // Used only internally. |
| 52 | pub fn tos2(s &u8) string { |
| 53 | if s == 0 { |
| 54 | panic('tos2: nil string') |
| 55 | } |
| 56 | return string{ |
| 57 | str: s |
| 58 | len: strlen(s) |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | pub fn tos3(s &char) string { |
| 63 | if s == 0 { |
| 64 | panic('tos3: nil string') |
| 65 | } |
| 66 | return string{ |
| 67 | str: &u8(s) |
| 68 | len: strlen(&u8(s)) |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | pub fn string_eq(s1 string, s2 string) bool { |
| 73 | if s1.len != s2.len { |
| 74 | return false |
| 75 | } |
| 76 | for i in 0 .. s1.len { |
| 77 | if s1[i] != s2[i] { |
| 78 | return false |
| 79 | } |
| 80 | } |
| 81 | return true |
| 82 | } |
| 83 | |
| 84 | pub fn string_ne(s1 string, s2 string) bool { |
| 85 | return !string_eq(s1, s2) |
| 86 | } |
| 87 | |
| 88 | pub fn i64_tos(buf &u8, len int, n0 i64, base int) string { |
| 89 | if base < 2 { |
| 90 | panic('base must be >= 2') |
| 91 | } |
| 92 | if base > 36 { |
| 93 | panic('base must be <= 36') |
| 94 | } |
| 95 | |
| 96 | mut b := tos(buf, len) |
| 97 | mut i := len - 1 |
| 98 | |
| 99 | mut n := n0 |
| 100 | neg := n < 0 |
| 101 | if neg { |
| 102 | n = -n |
| 103 | } |
| 104 | |
| 105 | b[i] = 0 |
| 106 | i-- |
| 107 | |
| 108 | for { |
| 109 | c := (n % base) + 48 |
| 110 | b[i] = if c > 57 { c + 7 } else { c } |
| 111 | i-- |
| 112 | if i < 0 { |
| 113 | panic('buffer to small') |
| 114 | } |
| 115 | n /= base |
| 116 | if n < 1 { |
| 117 | break |
| 118 | } |
| 119 | } |
| 120 | if neg { |
| 121 | if i < 0 { |
| 122 | panic('buffer to small') |
| 123 | } |
| 124 | b[i] = 45 |
| 125 | i-- |
| 126 | } |
| 127 | offset := i + 1 |
| 128 | b.str = b.str + offset |
| 129 | b.len -= (offset + 1) |
| 130 | return b |
| 131 | } |
| 132 | |
| 133 | pub fn i64_str(n0 i64, base int) string { |
| 134 | buf := malloc(80) |
| 135 | return i64_tos(buf, 79, n0, base) |
| 136 | } |
| 137 | |
| 138 | pub fn ptr_str(ptr voidptr) string { |
| 139 | buf := [16]u8{} |
| 140 | hex := i64_tos(buf, 15, i64(ptr), 16) |
| 141 | res := '0x' + hex |
| 142 | return res |
| 143 | } |
| 144 | |
| 145 | pub fn (a string) clone() string { |
| 146 | mut b := string{ |
| 147 | len: a.len |
| 148 | str: malloc(a.len + 1) |
| 149 | } |
| 150 | mem_copy(b.str, a.str, a.len) |
| 151 | b[a.len] = 0 |
| 152 | return b |
| 153 | } |
| 154 | |