mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
js: implement more functions for JS backend (#12167)
This commit is contained in:
@@ -2,8 +2,11 @@ module builtin
|
||||
|
||||
// used to generate JS throw statements.
|
||||
|
||||
[noreturn]
|
||||
pub fn js_throw(s any) {
|
||||
#throw s
|
||||
|
||||
for {}
|
||||
}
|
||||
|
||||
#let globalPrint;
|
||||
@@ -46,6 +49,7 @@ pub fn eprint(s string) {
|
||||
// Exits the process in node, and halts execution in the browser
|
||||
// because `process.exit` is undefined. Workaround for not having
|
||||
// a 'real' way to exit in the browser.
|
||||
[noreturn]
|
||||
pub fn exit(c int) {
|
||||
JS.process.exit(c)
|
||||
js_throw('exit($c)')
|
||||
|
@@ -68,3 +68,29 @@ pub fn (c byte) is_letter() bool {
|
||||
pub fn (c byte) is_alnum() bool {
|
||||
return c.is_letter() || c.is_digit()
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
|
@@ -900,3 +900,13 @@ pub fn (s []string) join(sep string) string {
|
||||
|
||||
// There's no better way to find length of JS String in bytes.
|
||||
#Object.defineProperty(string.prototype,"len", { get: function() {return new int(new TextEncoder().encode(this.str).length);}, set: function(l) {/* ignore */ } });
|
||||
// index returns the position of the first character of the input string.
|
||||
// It will return `none` if the input string can't be found.
|
||||
pub fn (s string) index(search string) ?int {
|
||||
res := 0
|
||||
#res.val = s.str.indexOf(search)
|
||||
if res == -1 {
|
||||
return none
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
Reference in New Issue
Block a user