| 1 | module builtin |
| 2 | |
| 3 | pub fn (b u8) is_space() bool { |
| 4 | mut result := false |
| 5 | #result.val = /^\s*$/.test(String.fromCharCode(b)) |
| 6 | |
| 7 | return result |
| 8 | } |
| 9 | |
| 10 | pub fn (c u8) str() string { |
| 11 | res := '' |
| 12 | #res.str = c.val.toString() |
| 13 | |
| 14 | return res |
| 15 | } |
| 16 | |
| 17 | pub fn (c u8) ascii_str() string { |
| 18 | res := '' |
| 19 | #res.str = String.fromCharCode(c.val) |
| 20 | |
| 21 | return res |
| 22 | } |
| 23 | |
| 24 | pub fn (c u8) repeat(count int) string { |
| 25 | mut res := '' |
| 26 | for _ in 0 .. count { |
| 27 | res += c.ascii_str() |
| 28 | } |
| 29 | |
| 30 | return res |
| 31 | } |
| 32 | |
| 33 | @[inline] |
| 34 | pub fn (c u8) is_digit() bool { |
| 35 | return c >= `0` && c <= `9` |
| 36 | } |
| 37 | |
| 38 | // is_hex_digit returns `true` if the byte is either in range 0-9, a-f or A-F and `false` otherwise. |
| 39 | // Example: assert u8(`F`) == true |
| 40 | @[inline] |
| 41 | pub fn (c u8) is_hex_digit() bool { |
| 42 | return c.is_digit() || (c >= `a` && c <= `f`) || (c >= `A` && c <= `F`) |
| 43 | } |
| 44 | |
| 45 | // is_oct_digit returns `true` if the byte is in range 0-7 and `false` otherwise. |
| 46 | // Example: assert u8(`7`) == true |
| 47 | @[inline] |
| 48 | pub fn (c u8) is_oct_digit() bool { |
| 49 | return c >= `0` && c <= `7` |
| 50 | } |
| 51 | |
| 52 | // is_bin_digit returns `true` if the byte is a binary digit (0 or 1) and `false` otherwise. |
| 53 | // Example: assert u8(`0`) == true |
| 54 | @[inline] |
| 55 | pub fn (c u8) is_bin_digit() bool { |
| 56 | return c == `0` || c == `1` |
| 57 | } |
| 58 | |
| 59 | // is_letter returns `true` if the byte is in range a-z or A-Z and `false` otherwise. |
| 60 | // Example: assert u8(`V`) == true |
| 61 | @[inline] |
| 62 | pub fn (c u8) is_letter() bool { |
| 63 | return (c >= `a` && c <= `z`) || (c >= `A` && c <= `Z`) |
| 64 | } |
| 65 | |
| 66 | // is_alnum returns `true` if the byte is in range a-z, A-Z, 0-9 and `false` otherwise. |
| 67 | // Example: assert u8(`V`) == true |
| 68 | @[inline] |
| 69 | pub fn (c u8) is_alnum() bool { |
| 70 | return (c >= `a` && c <= `z`) || (c >= `A` && c <= `Z`) || (c >= `0` && c <= `9`) |
| 71 | } |
| 72 | |
| 73 | // is_capital returns `true`, if the byte is a Latin capital letter. |
| 74 | // Example: assert `H`.is_capital() == true |
| 75 | // Example: assert `h`.is_capital() == false |
| 76 | @[inline] |
| 77 | pub fn (c u8) is_capital() bool { |
| 78 | return c >= `A` && c <= `Z` |
| 79 | } |
| 80 | |
| 81 | // str_escaped returns the contents of `byte` as an escaped `string`. |
| 82 | // Example: assert u8(0).str_escaped() == r'`\0`' |
| 83 | |
| 84 | pub fn (b u8) str_escaped() string { |
| 85 | mut str := '' |
| 86 | match b { |
| 87 | 0 { str = r'`\0`' } |
| 88 | 7 { str = r'`\a`' } |
| 89 | 8 { str = r'`\b`' } |
| 90 | 9 { str = r'`\t`' } |
| 91 | 10 { str = r'`\n`' } |
| 92 | 11 { str = r'`\v`' } |
| 93 | 12 { str = r'`\f`' } |
| 94 | 13 { str = r'`\r`' } |
| 95 | 27 { str = r'`\e`' } |
| 96 | 32...126 { str = b.ascii_str() } |
| 97 | else { str = '0x' + b.hex() } |
| 98 | } |
| 99 | |
| 100 | return str |
| 101 | } |
| 102 | |