2021-07-18 08:00:20 +02:00
|
|
|
module builtin
|
|
|
|
|
2022-04-15 13:45:52 +02:00
|
|
|
pub fn (b u8) is_space() bool {
|
2021-07-18 08:00:20 +02:00
|
|
|
mut result := false
|
2021-08-25 13:40:53 +02:00
|
|
|
#result.val = /^\s*$/.test(String.fromCharCode(b))
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2022-04-15 13:45:52 +02:00
|
|
|
pub fn (c u8) str() string {
|
2021-09-08 19:30:46 +02:00
|
|
|
res := ''
|
|
|
|
#res.str = c.val.toString()
|
|
|
|
|
|
|
|
return res
|
|
|
|
}
|
2021-10-03 09:08:21 +02:00
|
|
|
|
2022-04-15 13:45:52 +02:00
|
|
|
pub fn (c u8) ascii_str() string {
|
2021-10-03 09:08:21 +02:00
|
|
|
res := ''
|
|
|
|
#res.str = String.fromCharCode(c.val)
|
|
|
|
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2022-04-15 13:45:52 +02:00
|
|
|
pub fn (c u8) repeat(count int) string {
|
2021-10-03 09:08:21 +02:00
|
|
|
mut res := ''
|
|
|
|
for _ in 0 .. count {
|
|
|
|
res += c.ascii_str()
|
|
|
|
}
|
|
|
|
|
|
|
|
return res
|
|
|
|
}
|
2021-10-07 14:55:47 +02:00
|
|
|
|
2021-11-07 12:30:40 +01:00
|
|
|
[inline]
|
2022-04-15 13:45:52 +02:00
|
|
|
pub fn (c u8) is_digit() bool {
|
2021-10-07 14:55:47 +02:00
|
|
|
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]
|
2022-04-15 13:45:52 +02:00
|
|
|
pub fn (c u8) is_hex_digit() bool {
|
2021-11-07 12:30:40 +01:00
|
|
|
return (c >= `0` && c <= `9`) || (c >= `a` && c <= `f`) || (c >= `A` && c <= `F`)
|
2021-10-07 14:55:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// is_oct_digit returns `true` if the byte is in range 0-7 and `false` otherwise.
|
|
|
|
// Example: assert byte(`7`) == true
|
|
|
|
[inline]
|
2022-04-15 13:45:52 +02:00
|
|
|
pub fn (c u8) is_oct_digit() bool {
|
2021-10-07 14:55:47 +02:00
|
|
|
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]
|
2022-04-15 13:45:52 +02:00
|
|
|
pub fn (c u8) is_bin_digit() bool {
|
2021-10-07 14:55:47 +02:00
|
|
|
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]
|
2022-04-15 13:45:52 +02:00
|
|
|
pub fn (c u8) is_letter() bool {
|
2021-10-07 14:55:47 +02:00
|
|
|
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]
|
2022-04-15 13:45:52 +02:00
|
|
|
pub fn (c u8) is_alnum() bool {
|
2021-11-07 12:30:40 +01:00
|
|
|
return (c >= `a` && c <= `z`) || (c >= `A` && c <= `Z`) || (c >= `0` && c <= `9`)
|
2021-10-07 14:55:47 +02:00
|
|
|
}
|
2021-10-13 08:40:14 +02:00
|
|
|
|
2021-11-07 12:30:40 +01:00
|
|
|
// is_capital returns `true`, if the byte is a Latin capital letter.
|
|
|
|
// Example: assert `H`.is_capital() == true
|
2022-03-31 18:32:32 +02:00
|
|
|
// Example: assert `h`.is_capital() == false
|
2021-11-07 12:30:40 +01:00
|
|
|
[inline]
|
2022-04-15 13:45:52 +02:00
|
|
|
pub fn (c u8) is_capital() bool {
|
2021-10-13 08:40:14 +02:00
|
|
|
return c >= `A` && c <= `Z`
|
|
|
|
}
|
|
|
|
|
|
|
|
// str_escaped returns the contents of `byte` as an escaped `string`.
|
|
|
|
// Example: assert byte(0).str_escaped() == r'`\0`'
|
|
|
|
|
2022-04-15 13:45:52 +02:00
|
|
|
pub fn (b u8) str_escaped() string {
|
2021-10-13 08:40:14 +02:00
|
|
|
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
|
|
|
|
}
|