| 1 | module builtin |
| 2 | |
| 3 | pub type byte = u8 |
| 4 | |
| 5 | pub const min_i8 = i8(-128) |
| 6 | pub const max_i8 = i8(127) |
| 7 | |
| 8 | pub const min_i16 = i16(-32768) |
| 9 | pub const max_i16 = i16(32767) |
| 10 | |
| 11 | pub const min_i32 = i32(-2147483648) |
| 12 | pub const max_i32 = i32(2147483647) |
| 13 | |
| 14 | // -9223372036854775808 is wrong, because C compilers parse literal values |
| 15 | // without sign first, and 9223372036854775808 overflows i64, hence the |
| 16 | // consecutive subtraction by 1 |
| 17 | pub const min_i64 = i64(-9223372036854775807 - 1) |
| 18 | pub const max_i64 = i64(9223372036854775807) |
| 19 | |
| 20 | pub const min_int = $if new_int ? && x64 { int(min_i64) } $else { int(min_i32) } |
| 21 | pub const max_int = $if new_int ? && x64 { int(max_i64) } $else { int(max_i32) } |
| 22 | |
| 23 | pub const min_u8 = u8(0) |
| 24 | pub const max_u8 = u8(255) |
| 25 | |
| 26 | pub const min_u16 = u16(0) |
| 27 | pub const max_u16 = u16(65535) |
| 28 | |
| 29 | pub const min_u32 = u32(0) |
| 30 | pub const max_u32 = u32(4294967295) |
| 31 | |
| 32 | pub const min_u64 = u64(0) |
| 33 | pub const max_u64 = u64(18446744073709551615) |
| 34 | |
| 35 | pub fn (i i8) str() string { |
| 36 | mut res := '' |
| 37 | #res.str = i.val.toString() |
| 38 | |
| 39 | return res |
| 40 | } |
| 41 | |
| 42 | pub fn (i i16) str() string { |
| 43 | mut res := '' |
| 44 | #res.str = i.val.toString() |
| 45 | |
| 46 | return res |
| 47 | } |
| 48 | |
| 49 | pub fn (i u16) str() string { |
| 50 | mut res := '' |
| 51 | #res.str = i.val.toString() |
| 52 | |
| 53 | return res |
| 54 | } |
| 55 | |
| 56 | pub fn (i int) str() string { |
| 57 | mut res := '' |
| 58 | #res = new string( i+'' ) |
| 59 | |
| 60 | return res |
| 61 | } |
| 62 | |
| 63 | pub fn (i i64) str() string { |
| 64 | mut res := '' |
| 65 | #res = new string( i + '') |
| 66 | |
| 67 | return res |
| 68 | } |
| 69 | |
| 70 | pub fn (i u32) str() string { |
| 71 | mut res := '' |
| 72 | #res = new string( i + '') |
| 73 | |
| 74 | return res |
| 75 | } |
| 76 | |
| 77 | pub fn (i u64) str() string { |
| 78 | mut res := '' |
| 79 | #res = new string( i + '') |
| 80 | |
| 81 | return res |
| 82 | } |
| 83 | |
| 84 | pub fn (i bool) str() string { |
| 85 | mut res := '' |
| 86 | #res = new string( i + '') |
| 87 | |
| 88 | return res |
| 89 | } |
| 90 | |
| 91 | pub fn (i any) str() string { |
| 92 | mut res := '' |
| 93 | #res = new string( i.toString() ) |
| 94 | |
| 95 | return res |
| 96 | } |
| 97 | |
| 98 | pub fn (i int_literal) str() string { |
| 99 | res := '' |
| 100 | #res.str = i.val.toString() |
| 101 | |
| 102 | return res |
| 103 | } |
| 104 | |
| 105 | pub fn (x u64) hex() string { |
| 106 | res := '' |
| 107 | #res.str = x.val.toString(16) |
| 108 | |
| 109 | return res |
| 110 | } |
| 111 | |
| 112 | pub fn (x u64) hex_full() string { |
| 113 | res := '' |
| 114 | #res.str = x.val.toString(16) |
| 115 | |
| 116 | return res |
| 117 | } |
| 118 | |
| 119 | pub fn (x i64) hex() string { |
| 120 | res := '' |
| 121 | #res.str = x.val.toString(16) |
| 122 | |
| 123 | return res |
| 124 | } |
| 125 | |
| 126 | pub fn (x u32) hex() string { |
| 127 | res := '' |
| 128 | #res.str = x.val.toString(16) |
| 129 | |
| 130 | return res |
| 131 | } |
| 132 | |
| 133 | pub fn (x u16) hex() string { |
| 134 | res := '' |
| 135 | #res.str = x.val.toString(16) |
| 136 | |
| 137 | return res |
| 138 | } |
| 139 | |
| 140 | pub fn (x i8) hex() string { |
| 141 | mut res := '' |
| 142 | #res.str = x.val.toString(16) |
| 143 | if res.len < 2 { |
| 144 | res = '0' + res |
| 145 | } |
| 146 | return res |
| 147 | } |
| 148 | |
| 149 | pub fn (x i16) hex() string { |
| 150 | res := '' |
| 151 | #res.str = x.val.toString(16) |
| 152 | |
| 153 | return res |
| 154 | } |
| 155 | |
| 156 | pub fn (x int) hex() string { |
| 157 | res := '' |
| 158 | #res.str = x.val.toString(16) |
| 159 | |
| 160 | return res |
| 161 | } |
| 162 | |
| 163 | pub fn (x int_literal) hex() string { |
| 164 | res := '' |
| 165 | #res.str = x.val.toString(16) |
| 166 | |
| 167 | return res |
| 168 | } |
| 169 | |
| 170 | pub fn (x u8) hex() string { |
| 171 | mut res := '' |
| 172 | #res.str = x.val.toString(16) |
| 173 | if res.len < 2 { |
| 174 | res = '0' + res |
| 175 | } |
| 176 | return res |
| 177 | } |
| 178 | |
| 179 | // hex returns a string with the hexadecimal representation of the byte elements of the array. |
| 180 | pub fn (b []u8) hex() string { |
| 181 | mut hex := '' |
| 182 | for i in b { |
| 183 | mut z := i |
| 184 | z = z |
| 185 | #let n0 = i.val >> 4 |
| 186 | #hex.str += n0 < 10 ? String.fromCharCode(n0) : String.fromCharCode(n0 + 87) |
| 187 | |
| 188 | #let n1 = i.val & 0xF |
| 189 | #hex.str += n1 < 10 ? String.fromCharCode(n1) : String.fromCharCode(n1 + 87) |
| 190 | } |
| 191 | return hex |
| 192 | } |
| 193 | |
| 194 | pub fn (i int) hex2() string { |
| 195 | return '0x' + i.hex() |
| 196 | } |
| 197 | |
| 198 | pub fn (i i8) hex2() string { |
| 199 | return '0x' + i.hex() |
| 200 | } |
| 201 | |
| 202 | pub fn (i i16) hex2() string { |
| 203 | return '0x' + i.hex() |
| 204 | } |
| 205 | |
| 206 | pub fn (i i64) hex2() string { |
| 207 | return '0x' + i.hex() |
| 208 | } |
| 209 | |
| 210 | pub fn (i u8) hex2() string { |
| 211 | return '0x' + i.hex() |
| 212 | } |
| 213 | |
| 214 | pub fn (i u16) hex2() string { |
| 215 | return '0x' + i.hex() |
| 216 | } |
| 217 | |
| 218 | pub fn (i u32) hex2() string { |
| 219 | return '0x' + i.hex() |
| 220 | } |
| 221 | |
| 222 | pub fn (i u64) hex2() string { |
| 223 | return '0x' + i.hex() |
| 224 | } |
| 225 | |