2021-07-18 09:00:20 +03:00
|
|
|
module builtin
|
|
|
|
|
|
|
|
pub fn (b byte) is_space() bool {
|
|
|
|
mut result := false
|
2021-08-25 14:40:53 +03:00
|
|
|
#result.val = /^\s*$/.test(String.fromCharCode(b))
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2021-09-08 20:30:46 +03:00
|
|
|
pub fn (c byte) str() string {
|
|
|
|
res := ''
|
|
|
|
#res.str = c.val.toString()
|
|
|
|
|
|
|
|
return res
|
|
|
|
}
|
2021-10-03 10:08:21 +03:00
|
|
|
|
|
|
|
pub fn (c byte) ascii_str() string {
|
|
|
|
res := ''
|
|
|
|
#res.str = String.fromCharCode(c.val)
|
|
|
|
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (c byte) repeat(count int) string {
|
|
|
|
mut res := ''
|
|
|
|
for _ in 0 .. count {
|
|
|
|
res += c.ascii_str()
|
|
|
|
}
|
|
|
|
|
|
|
|
return res
|
|
|
|
}
|
2021-10-07 15:55:47 +03:00
|
|
|
|
|
|
|
pub fn (c byte) is_digit() bool {
|
|
|
|
return c >= `0` && c <= `9`
|
|
|
|
}
|
|
|
|
|
|
|
|
// is_hex_digit returns `true` if the byte is either in range 0-9, a-f or A-F and `false` otherwise.
|
|
|
|
// Example: assert byte(`F`) == true
|
|
|
|
[inline]
|
|
|
|
pub fn (c byte) is_hex_digit() bool {
|
|
|
|
return c.is_digit() || (c >= `a` && c <= `f`) || (c >= `A` && c <= `F`)
|
|
|
|
}
|
|
|
|
|
|
|
|
// is_oct_digit returns `true` if the byte is in range 0-7 and `false` otherwise.
|
|
|
|
// Example: assert byte(`7`) == true
|
|
|
|
[inline]
|
|
|
|
pub fn (c byte) is_oct_digit() bool {
|
|
|
|
return c >= `0` && c <= `7`
|
|
|
|
}
|
|
|
|
|
|
|
|
// is_bin_digit returns `true` if the byte is a binary digit (0 or 1) and `false` otherwise.
|
|
|
|
// Example: assert byte(`0`) == true
|
|
|
|
[inline]
|
|
|
|
pub fn (c byte) is_bin_digit() bool {
|
|
|
|
return c == `0` || c == `1`
|
|
|
|
}
|
|
|
|
|
|
|
|
// is_letter returns `true` if the byte is in range a-z or A-Z and `false` otherwise.
|
|
|
|
// Example: assert byte(`V`) == true
|
|
|
|
[inline]
|
|
|
|
pub fn (c byte) is_letter() bool {
|
|
|
|
return (c >= `a` && c <= `z`) || (c >= `A` && c <= `Z`)
|
|
|
|
}
|
|
|
|
|
|
|
|
// is_alnum returns `true` if the byte is in range a-z, A-Z, 0-9 and `false` otherwise.
|
|
|
|
// Example: assert byte(`V`) == true
|
|
|
|
[inline]
|
|
|
|
pub fn (c byte) is_alnum() bool {
|
|
|
|
return c.is_letter() || c.is_digit()
|
|
|
|
}
|
2021-10-13 09:40:14 +03:00
|
|
|
|
|
|
|
// Define this on byte as well, so that we can do `s[0].is_capital()`
|
|
|
|
pub fn (c byte) is_capital() bool {
|
|
|
|
return c >= `A` && c <= `Z`
|
|
|
|
}
|
|
|
|
|
|
|
|
// str_escaped returns the contents of `byte` as an escaped `string`.
|
|
|
|
// Example: assert byte(0).str_escaped() == r'`\0`'
|
|
|
|
|
|
|
|
pub fn (b byte) str_escaped() string {
|
|
|
|
mut str := ''
|
|
|
|
match b {
|
|
|
|
0 { str = r'`\0`' }
|
|
|
|
7 { str = r'`\a`' }
|
|
|
|
8 { str = r'`\b`' }
|
|
|
|
9 { str = r'`\t`' }
|
|
|
|
10 { str = r'`\n`' }
|
|
|
|
11 { str = r'`\v`' }
|
|
|
|
12 { str = r'`\f`' }
|
|
|
|
13 { str = r'`\r`' }
|
|
|
|
27 { str = r'`\e`' }
|
|
|
|
32...126 { str = b.ascii_str() }
|
|
|
|
else { str = '0x' + b.hex() }
|
|
|
|
}
|
|
|
|
return str
|
|
|
|
}
|