| 1 | module builtin |
| 2 | |
| 3 | pub type byte = u8 |
| 4 | |
| 5 | // type i32 = int |
| 6 | |
| 7 | // digit pairs in reverse order |
| 8 | const digit_pairs = '00102030405060708090011121314151617181910212223242526272829203132333435363738393041424344454647484940515253545556575859506162636465666768696071727374757677787970818283848586878889809192939495969798999' |
| 9 | |
| 10 | pub const min_i8 = i8(-128) |
| 11 | pub const max_i8 = i8(127) |
| 12 | |
| 13 | pub const min_i16 = i16(-32768) |
| 14 | pub const max_i16 = i16(32767) |
| 15 | |
| 16 | pub const min_i32 = i32(-2147483648) |
| 17 | pub const max_i32 = i32(2147483647) |
| 18 | |
| 19 | // -9223372036854775808 is wrong, because C compilers parse literal values |
| 20 | // without sign first, and 9223372036854775808 overflows i64, hence the |
| 21 | // consecutive subtraction by 1 |
| 22 | pub const min_i64 = i64(-9223372036854775807 - 1) |
| 23 | pub const max_i64 = i64(9223372036854775807) |
| 24 | |
| 25 | pub const min_int = int(min_i32) |
| 26 | pub const max_int = int(max_i32) |
| 27 | |
| 28 | pub const min_u8 = u8(0) |
| 29 | pub const max_u8 = u8(255) |
| 30 | |
| 31 | pub const min_u16 = u16(0) |
| 32 | pub const max_u16 = u16(65535) |
| 33 | |
| 34 | pub const min_u32 = u32(0) |
| 35 | pub const max_u32 = u32(4294967295) |
| 36 | |
| 37 | pub const min_u64 = u64(0) |
| 38 | pub const max_u64 = u64(18446744073709551615) |
| 39 | |
| 40 | // This implementation is the quickest with gcc -O2 |
| 41 | // str_l returns the string representation of the integer nn with max chars. |
| 42 | @[direct_array_access; inline] |
| 43 | fn (nn int) str_l(max int) string { |
| 44 | unsafe { |
| 45 | mut n := i64(nn) |
| 46 | mut d := 0 |
| 47 | if n == 0 { |
| 48 | return '0' |
| 49 | } |
| 50 | |
| 51 | mut is_neg := false |
| 52 | if n < 0 { |
| 53 | n = -n |
| 54 | is_neg = true |
| 55 | } |
| 56 | mut index := max |
| 57 | mut buf := malloc(max + 1) |
| 58 | buf[index] = 0 |
| 59 | index-- |
| 60 | |
| 61 | for n > 0 { |
| 62 | n1 := int(n / 100) |
| 63 | // calculate the digit_pairs start index |
| 64 | d = int(u32(int(n) - (n1 * 100)) << 1) |
| 65 | n = n1 |
| 66 | buf[index] = digit_pairs.str[d] |
| 67 | index-- |
| 68 | d++ |
| 69 | buf[index] = digit_pairs.str[d] |
| 70 | index-- |
| 71 | } |
| 72 | index++ |
| 73 | // remove head zero |
| 74 | if d < 20 { |
| 75 | index++ |
| 76 | } |
| 77 | // Prepend - if it's negative |
| 78 | if is_neg { |
| 79 | index-- |
| 80 | buf[index] = `-` |
| 81 | } |
| 82 | diff := max - index |
| 83 | vmemmove(buf, voidptr(buf + index), diff + 1) |
| 84 | return tos(buf, diff) |
| 85 | |
| 86 | // return tos(memdup(&buf[0] + index, (max - index)), (max - index)) |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | // str returns the value of the `u8` as a `string`. |
| 91 | // Example: assert u8(2).str() == '2' |
| 92 | pub fn (n u8) str() string { |
| 93 | return int(n).str_l(5) |
| 94 | } |
| 95 | |
| 96 | // str returns the value of the `i8` as a `string`. |
| 97 | // Example: assert i8(-2).str() == '-2' |
| 98 | pub fn (n i8) str() string { |
| 99 | return int(n).str_l(5) |
| 100 | } |
| 101 | |
| 102 | // str returns the value of the `i16` as a `string`. |
| 103 | // Example: assert i16(-20).str() == '-20' |
| 104 | pub fn (n i16) str() string { |
| 105 | return int(n).str_l(7) |
| 106 | } |
| 107 | |
| 108 | // str returns the value of the `u16` as a `string`. |
| 109 | // Example: assert u16(20).str() == '20' |
| 110 | pub fn (n u16) str() string { |
| 111 | return int(n).str_l(7) |
| 112 | } |
| 113 | |
| 114 | // str returns the value of the `int` as a `string`. |
| 115 | // Example: assert int(-2020).str() == '-2020' |
| 116 | pub fn (n int) str() string { |
| 117 | return n.str_l(12) |
| 118 | } |
| 119 | |
| 120 | // str returns the value of the `u32` as a `string`. |
| 121 | // Example: assert u32(20000).str() == '20000' |
| 122 | @[direct_array_access; inline] |
| 123 | pub fn (nn u32) str() string { |
| 124 | unsafe { |
| 125 | mut n := nn |
| 126 | mut d := u32(0) |
| 127 | if n == 0 { |
| 128 | return '0' |
| 129 | } |
| 130 | max := 12 |
| 131 | mut buf := malloc(max + 1) |
| 132 | mut index := max |
| 133 | buf[index] = 0 |
| 134 | index-- |
| 135 | for n > 0 { |
| 136 | n1 := n / u32(100) |
| 137 | d = ((n - (n1 * u32(100))) << u32(1)) |
| 138 | n = n1 |
| 139 | buf[index] = digit_pairs[int(d)] |
| 140 | index-- |
| 141 | d++ |
| 142 | buf[index] = digit_pairs[int(d)] |
| 143 | index-- |
| 144 | } |
| 145 | index++ |
| 146 | // remove head zero |
| 147 | if d < u32(20) { |
| 148 | index++ |
| 149 | } |
| 150 | diff := max - index |
| 151 | vmemmove(buf, voidptr(buf + index), diff + 1) |
| 152 | return tos(buf, diff) |
| 153 | |
| 154 | // return tos(memdup(&buf[0] + index, (max - index)), (max - index)) |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | // str returns the value of the `int_literal` as a `string`. |
| 159 | @[inline] |
| 160 | pub fn (n int_literal) str() string { |
| 161 | return i64(n).str() |
| 162 | } |
| 163 | |
| 164 | // str returns the value of the `i64` as a `string`. |
| 165 | // Example: assert i64(-200000).str() == '-200000' |
| 166 | @[direct_array_access; inline] |
| 167 | pub fn (nn i64) str() string { |
| 168 | unsafe { |
| 169 | mut n := nn |
| 170 | mut d := i64(0) |
| 171 | if n == 0 { |
| 172 | return '0' |
| 173 | } else if n == min_i64 { |
| 174 | // math.min_i64 |
| 175 | return '-9223372036854775808' |
| 176 | } |
| 177 | max := 20 |
| 178 | mut buf := malloc(max + 1) |
| 179 | mut is_neg := false |
| 180 | if n < 0 { |
| 181 | n = -n |
| 182 | is_neg = true |
| 183 | } |
| 184 | mut index := max |
| 185 | buf[index] = 0 |
| 186 | index-- |
| 187 | for n > 0 { |
| 188 | n1 := n / i64(100) |
| 189 | d = (u32(n - (n1 * i64(100))) << i64(1)) |
| 190 | n = n1 |
| 191 | buf[index] = digit_pairs[int(d)] |
| 192 | index-- |
| 193 | d++ |
| 194 | buf[index] = digit_pairs[int(d)] |
| 195 | index-- |
| 196 | } |
| 197 | index++ |
| 198 | // remove head zero |
| 199 | if d < i64(20) { |
| 200 | index++ |
| 201 | } |
| 202 | // Prepend - if it's negative |
| 203 | if is_neg { |
| 204 | index-- |
| 205 | buf[index] = `-` |
| 206 | } |
| 207 | diff := max - index |
| 208 | vmemmove(buf, voidptr(buf + index), diff + 1) |
| 209 | return tos(buf, diff) |
| 210 | // return tos(memdup(&buf[0] + index, (max - index)), (max - index)) |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | // str returns the value of the `u64` as a `string`. |
| 215 | // Example: assert u64(2000000).str() == '2000000' |
| 216 | @[direct_array_access; inline] |
| 217 | pub fn (nn u64) str() string { |
| 218 | unsafe { |
| 219 | mut n := nn |
| 220 | mut d := u64(0) |
| 221 | if n == 0 { |
| 222 | return '0' |
| 223 | } |
| 224 | max := 20 |
| 225 | mut buf := malloc(max + 1) |
| 226 | mut index := max |
| 227 | buf[index] = 0 |
| 228 | index-- |
| 229 | for n > 0 { |
| 230 | n1 := n / 100 |
| 231 | d = ((n - (n1 * 100)) << 1) |
| 232 | n = n1 |
| 233 | buf[index] = digit_pairs[int(d)] |
| 234 | index-- |
| 235 | d++ |
| 236 | buf[index] = digit_pairs[int(d)] |
| 237 | index-- |
| 238 | } |
| 239 | index++ |
| 240 | // remove head zero |
| 241 | if d < 20 { |
| 242 | index++ |
| 243 | } |
| 244 | diff := max - index |
| 245 | vmemmove(buf, voidptr(buf + index), diff + 1) |
| 246 | return tos(buf, diff) |
| 247 | // return tos(memdup(&buf[0] + index, (max - index)), (max - index)) |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | // str returns the value of the `bool` as a `string`. |
| 252 | // Example: assert (2 > 1).str() == 'true' |
| 253 | pub fn (b bool) str() string { |
| 254 | if b { |
| 255 | return 'true' |
| 256 | } |
| 257 | return 'false' |
| 258 | } |
| 259 | |