40 KiB
module builtin
Contents
- Constants
- C.android_print
- arguments
- at_exit
- c_error_number_str
- compare_strings
- copy
- cstring_to_vstring
- eprint
- eprintln
- error
- error_with_code
- exit
- f32_abs
- f32_max
- f32_min
- f64_abs
- f64_max
- f64_min
- flush_stderr
- flush_stdout
- free
- gc_check_leaks
- gc_collect
- gc_disable
- gc_enable
- gc_get_warn_proc
- gc_heap_usage
- gc_is_enabled
- gc_memory_use
- gc_set_warn_proc
- get_str_intp_u32_format
- get_str_intp_u64_format
- input_character
- int_max
- int_min
- isnil
- malloc
- malloc_noscan
- malloc_uncollectable
- memdup
- memdup_noscan
- memdup_uncollectable
- panic
- panic_error_number
- panic_lasterr
- panic_n
- panic_n2
- panic_option_not_set
- panic_result_not_set
- print_backtrace
- print_backtrace_skipping_top_frames
- print_character
- println
- proc_pidpath
- ptr_str
- realloc_data
- str_intp
- str_intp_g32
- str_intp_g64
- str_intp_rune
- str_intp_sq
- str_intp_sub
- string_from_wide
- string_from_wide2
- string_to_ansi_not_null_terminated
- tos
- tos2
- tos3
- tos4
- tos5
- tos_clone
- unbuffer_stdout
- utf32_decode_to_buffer
- utf32_to_str
- utf32_to_str_no_malloc
- utf8_char_len
- utf8_str_visible_length
- v_realloc
- vcalloc
- vcalloc_noscan
- vmemcmp
- vmemcpy
- vmemmove
- vmemset
- vstrlen
- vstrlen_char
- wide_to_ansi
- IError
- C.intptr_t
- FnExitCb
- FnGC_WarnCB
- MessageError
- []rune
- []string
- []u8
- bool
- byte
- byteptr
- chan
- char
- charptr
- f32
- f64
- [float-literal](#float literal)
- i16
- i32
- i64
- i8
- int
- [int-literal](#int literal)
- isize
- none
- rune
- u16
- u32
- u64
- u8
- usize
- voidptr
- ArrayFlags
- AttributeKind
- ChanState
- StrIntpType
- C.DIR
- C.FILE
- C.GC_stack_base
- C.IError
- EnumData
- Error
- FieldData
- FunctionData
- GCHeapUsage
- MethodParam
- SortedMap
- StrIntpCgenData
- StrIntpData
- StrIntpMem
- VAssertMetaInfo
- VAttribute
- VContext
- VariantData
- WrapConfig
- array
- map
- string
- after
- after_char
- all_after
- all_after_first
- all_after_last
- all_before
- all_before_last
- before
- bool
- bytes
- camel_to_snake
- capitalize
- clone
- compare
- contains
- contains_any
- contains_any_substr
- contains_only
- contains_u8
- count
- ends_with
- f32
- f64
- fields
- find_between
- free
- hash
- hex
- i16
- i32
- i64
- i8
- indent_width
- index
- index_after
- index_any
- index_u8
- int
- is_ascii
- is_bin
- is_blank
- is_capital
- is_hex
- is_int
- is_lower
- is_oct
- is_pure_ascii
- is_title
- is_upper
- last_index
- last_index_u8
- len_utf8
- limit
- match_glob
- normalize_tabs
- parse_int
- parse_uint
- repeat
- replace
- replace_char
- replace_each
- replace_once
- reverse
- rsplit
- rsplit_any
- rsplit_nth
- rsplit_once
- runes
- snake_to_camel
- split
- split_any
- split_by_space
- split_into_lines
- split_n
- split_nth
- split_once
- starts_with
- starts_with_capital
- str
- strip_margin
- strip_margin_custom
- substr
- substr_ni
- substr_unsafe
- substr_with_check
- title
- to_lower
- to_lower_ascii
- to_upper
- to_upper_ascii
- to_wide
- trim
- trim_indent
- trim_indexes
- trim_left
- trim_right
- trim_space
- trim_space_left
- trim_space_right
- trim_string_left
- trim_string_right
- u16
- u32
- u64
- u8
- u8_array
- uncapitalize
- utf32_code
- wrap
Constants
C.android_print
arguments
at_exit
c_error_number_str
compare_strings
copy
cstring_to_vstring
eprint
eprintln
error
Example
if ouch { return error('an error occurred') }
error_with_code
Example
if ouch { return error_with_code('an error occurred', 1) }
exit
f32_abs
Example
assert f32_abs(-2.0) == 2.0
f32_max
Example
assert f32_max(2.0,3.0) == 3.0
f32_min
Example
assert f32_min(2.0,3.0) == 2.0
f64_abs
Example
assert f64_abs(-2.0) == f64(2.0)
f64_max
Example
assert f64_max(2.0,3.0) == 3.0
f64_min
Example
assert f64_min(2.0,3.0) == 2.0
flush_stderr
flush_stdout
free
gc_check_leaks
gc_collect
gc_disable
gc_enable
gc_get_warn_proc
gc_heap_usage
gc_is_enabled
gc_memory_use
gc_set_warn_proc
get_str_intp_u32_format
get_str_intp_u64_format
input_character
int_max
Example
assert int_max(2,3) == 3
int_min
Example
assert int_min(2,3) == 2
isnil
malloc
malloc_noscan
malloc_uncollectable
memdup
memdup_noscan
memdup_uncollectable
panic
panic_error_number
panic_lasterr
panic_n
panic_n2
panic_option_not_set
panic_result_not_set
print_backtrace
print_backtrace_skipping_top_frames
print_character
println
proc_pidpath
ptr_str
realloc_data
str_intp
str_intp_g32
str_intp_g64
str_intp_rune
str_intp_sq
str_intp_sub
string_from_wide
string_from_wide2
string_to_ansi_not_null_terminated
tos
tos2
tos3
tos4
tos5
tos_clone
unbuffer_stdout
utf32_decode_to_buffer
utf32_to_str
utf32_to_str_no_malloc
utf8_char_len
utf8_str_visible_length
v_realloc
vcalloc
vcalloc_noscan
vmemcmp
vmemcpy
vmemmove
vmemset
vstrlen
vstrlen_char
wide_to_ansi
IError
free
str
C.intptr_t
FnExitCb
FnGC_WarnCB
MessageError
str
msg
code
free
[]rune
string
[]string
free
join
Example
assert ['Hello','V'].join(' ') == 'Hello V'
join_lines
sort_by_len
sort_ignore_case
str
Example
['a', 'b', 'c'].str() // => "['a', 'b', 'c']".
[]u8
byterune
bytestr
hex
utf8_to_utf32
bool
str
Example
assert (2 > 1).str() == 'true'
byte
byteptr
str
vbytes
vstring
vstring_literal
vstring_literal_with_len
vstring_with_len
chan
close
try_pop
try_push
char
str
vstring
vstring_literal
vstring_literal_with_len
vstring_with_len
charptr
str
vstring
vstring_literal
vstring_literal_with_len
vstring_with_len
f32
str
strg
strsci
Example
assert f32(1.234).strsci(3) == '1.234e+00'
strlong
eq_epsilon
Example
assert f32(2.0).eq_epsilon(2.0)
f64
str
strg
strsci
Example
assert f64(1.234).strsci(3) == '1.234e+00'
strlong
Example
assert f64(1.23456).strlong() == '1.23456'
eq_epsilon
Example
assert f64(2.0).eq_epsilon(2.0)
float literal
str
i16
str
Example
assert i16(-20).str() == '-20'
hex
Examples
assert i16(2).hex() == '2'
assert i16(200).hex() == 'c8'
hex_full
i32
str
i64
str
Example
assert i64(-200000).str() == '-200000'
hex
Examples
assert i64(2).hex() == '2'
assert i64(-200).hex() == 'ffffffffffffff38'
assert i64(2021).hex() == '7e5'
hex_full
i8
str
Example
assert i8(-2).str() == '-2'
hex
Examples
assert i8(8).hex() == '08'
assert i8(10).hex() == '0a'
assert i8(15).hex() == '0f'
hex_full
int
hex_full
str
Example
assert int(-2020).str() == '-2020'
hex
Examples
assert int(2).hex() == '2'
assert int(200).hex() == 'c8'
hex2
Examples
assert int(8).hex2() == '0x8'
assert int(15).hex2() == '0xf'
assert int(18).hex2() == '0x12'
int literal
str
hex
hex_full
isize
str
none
str
rune
str
repeat
bytes
length_in_bytes
to_upper
to_lower
to_title
u16
str
Example
assert u16(20).str() == '20'
hex
Examples
assert u16(2).hex() == '2'
assert u16(200).hex() == 'c8'
hex_full
u32
str
Example
assert u32(20000).str() == '20000'
hex
Examples
assert u32(2).hex() == '2'
assert u32(200).hex() == 'c8'
hex_full
u64
str
Example
assert u64(2000000).str() == '2000000'
hex
Examples
assert u64(2).hex() == '2'
assert u64(2000).hex() == '7d0'
hex_full
Examples
assert u64(2).hex_full() == '0000000000000002'
assert u64(255).hex_full() == '00000000000000ff'
u8
ascii_str
Example
assert u8(97).ascii_str() == 'a'
free
hex
Examples
assert u8(2).hex() == '02'
assert u8(15).hex() == '0f'
assert u8(255).hex() == 'ff'
hex_full
is_alnum
Example
assert u8(`V`).is_alnum() == true
is_bin_digit
Example
assert u8(`0`).is_bin_digit() == true
is_capital
Examples
assert `H`.is_capital() == true
assert `h`.is_capital() == false
is_digit
Example
assert u8(`9`).is_digit() == true
is_hex_digit
Example
assert u8(`F`).is_hex_digit() == true
is_letter
Example
assert u8(`V`).is_letter() == true
is_oct_digit
Example
assert u8(`7`).is_oct_digit() == true
is_space
Example
assert u8(` `).is_space() == true
repeat
str
Example
assert u8(111).str() == '111'
str_escaped
Example
assert u8(0).str_escaped() == r'`\0`'
vbytes
vstring
vstring_literal
vstring_literal_with_len
vstring_with_len
usize
str
voidptr
hex_full
str
vbytes
ArrayFlags
AttributeKind
ChanState
StrIntpType
str
C.DIR
C.FILE
C.GC_stack_base
C.IError
EnumData
Error
msg
code
FieldData
FunctionData
GCHeapUsage
MethodParam
SortedMap
delete
keys
free
StrIntpCgenData
StrIntpData
StrIntpMem
VAssertMetaInfo
free
VAttribute
VContext
VariantData
WrapConfig
array
repeat
repeat_to_depth
insert
Example
mut a := [1, 2, 4]
a.insert(2, 3) // a now is [1, 2, 3, 4]
mut b := [3, 4]
b.insert(0, [1, 2]) // b now is [1, 2, 3, 4]
mut c := [[3, 4]]
c.insert(0, [1, 2]) // c now is [[1, 2], [3, 4]]
prepend
delete
Example
mut a := ['0', '1', '2', '3', '4', '5']
a.delete(1) // a is now ['0', '2', '3', '4', '5']
delete_many
Example
mut a := [1, 2, 3, 4, 5, 6, 7, 8, 9]
b := a[..9] // creates a `slice` of `a`, not a clone
a.delete_many(4, 3) // replaces `a` with a modified clone
dump(a) // a: [1, 2, 3, 4, 8, 9] // `a` is now different
dump(b) // b: [1, 2, 3, 4, 5, 6, 7, 8, 9] // `b` is still the same
clear
Example
a.clear() // `a.len` is now 0
reset
trim
Example
a.trim(3) // `a.len` is now <= 3
drop
Example
mut a := [1,2]
a << 3
a.drop(2)
assert a == [3]
assert a.cap > a.len
first
last
pop
Example
mut a := [1, 2, 3, 4, 5, 6, 7, 8, 9]
b := a[..9] // creates a "view" into the same memory
c := a.pop() // c == 9
a[1] = 5
dump(a) // a: [1, 5, 3, 4, 5, 6, 7, 8]
dump(b) // b: [1, 5, 3, 4, 5, 6, 7, 8, 9]
delete_last
clone
clone_to_depth
push_many
reverse_in_place
reverse
free
filter
Examples
array.filter(it < 5) // create an array of elements less than 5
array.filter(it % 2 == 1) // create an array of only odd elements
array.filter(it.name[0] == `A`) // create an array of elements whose `name` field starts with 'A'
any
Examples
array.any(it % 2 == 1) // will return true if any element is odd
array.any(it.name == 'Bob') // will yield `true` if any element has `.name == 'Bob'`
count
Example
array.count(it % 2 == 1) // will return how many elements are odd
all
Example
array.all(it % 2 == 1) // will return true if every element is odd
map
Example
words := ['hello', 'world']
r1 := words.map(it.to_upper())
assert r1 == ['HELLO', 'WORLD']
// map can also accept anonymous functions
r2 := words.map(fn (w string) string {
return w.to_upper()
})
assert r2 == ['HELLO', 'WORLD']
sort
Examples
array.sort() // will sort the array in ascending order
array.sort(b < a) // will sort the array in descending order
array.sort(b.name < a.name) // will sort descending by the .name field
sorted
Examples
assert [9,1,6,3,9].sorted() == [1,3,6,9,9]
assert [9,1,6,3,9].sorted(b < a) == [9,9,6,3,1]
sort_with_compare
Example
fn main() {
mut a := ['hi', '1', '5', '3']
a.sort_with_compare(fn (a &string, b &string) int {
if a < b {
return -1
}
if a > b {
return 1
}
return 0
})
assert a == ['1', '3', '5', 'hi']
}
sorted_with_compare
contains
Example
[1, 2, 3].contains(4) == false
index
grow_cap
grow_len
pointers
map
move
clear
Example
a.clear() // `a.len` and `a.key_values.len` is now 0
reserve
delete
keys
values
clone
free
string
after
Examples
assert '23:34:45.234'.after(':') == '45.234'
assert 'abcd'.after('z') == 'abcd'
after_char
Examples
assert '23:34:45.234'.after_char(`:`) == '34:45.234'
assert 'abcd'.after_char(`:`) == 'abcd'
all_after
Examples
assert '23:34:45.234'.all_after('.') == '234'
assert 'abcd'.all_after('z') == 'abcd'
all_after_first
Examples
assert '23:34:45.234'.all_after_first(':') == '34:45.234'
assert 'abcd'.all_after_first('z') == 'abcd'
all_after_last
Examples
assert '23:34:45.234'.all_after_last(':') == '45.234'
assert 'abcd'.all_after_last('z') == 'abcd'
all_before
Examples
assert '23:34:45.234'.all_before('.') == '23:34:45'
assert 'abcd'.all_before('.') == 'abcd'
all_before_last
Examples
assert '23:34:45.234'.all_before_last(':') == '23:34'
assert 'abcd'.all_before_last('.') == 'abcd'
before
Examples
assert '23:34:45.234'.before('.') == '23:34:45'
assert 'abcd'.before('.') == 'abcd'
bool
bytes
camel_to_snake
Examples
assert 'Abcd'.camel_to_snake() == 'abcd'
assert 'aaBB'.camel_to_snake() == 'aa_bb'
assert 'BBaa'.camel_to_snake() == 'b_baa'
assert 'aa_BB'.camel_to_snake() == 'aa_bb'
capitalize
Example
assert 'hello'.capitalize() == 'Hello'
clone
compare
contains
contains_any
contains_any_substr
contains_only
contains_u8
count
ends_with
f32
f64
fields
Examples
assert '\t\tv = v'.fields() == ['v', '=', 'v']
assert ' sss ssss'.fields() == ['sss', 'ssss']
find_between
Example
assert 'hey [man] how you doin'.find_between('[', ']') == 'man'
free
hash
hex
i16
i32
i64
i8
indent_width
Examples
assert ' v'.indent_width() == 2
assert '\t\tv'.indent_width() == 2
index
index_after
index_any
index_u8
int
is_ascii
is_bin
is_blank
Examples
assert ' '.is_blank()
assert '\t'.is_blank()
assert 'v'.is_blank() == false
is_capital
Examples
assert 'Hello'.is_capital() == true
assert 'HelloWorld'.is_capital() == false
is_hex
is_int
is_lower
Example
assert 'hello developer'.is_lower() == true
is_oct
is_pure_ascii
is_title
Example
assert 'Hello V Developer'.is_title() == true
is_upper
Example
assert 'HELLO V'.is_upper() == true
last_index
last_index_u8
len_utf8
limit
match_glob
Examples
assert 'ABCD'.match_glob('AB*')
assert 'ABCD'.match_glob('*D')
assert 'ABCD'.match_glob('*B*')
assert !'ABCD'.match_glob('AB')
normalize_tabs
Example
assert '\t\tpop rax\t; pop rax'.normalize_tabs(2) == ' pop rax ; pop rax'
parse_int
parse_uint
repeat
replace
replace_char
Example
assert '\tHello!'.replace_char(`\t`,` `,8) == ' Hello!'
replace_each
Example
assert 'ABCD'.replace_each(['B','C/','C','D','D','C']) == 'AC/DC'
replace_once
reverse
Example
assert 'Hello V'.reverse() == 'V olleH'
rsplit
Examples
assert 'A B C'.rsplit(' ') == ['C','B','A']
assert 'DEF'.rsplit('') == ['F','E','D']
rsplit_any
Example
"first row\nsecond row".rsplit_any(" \n") == ['row', 'second', 'row', 'first']
rsplit_nth
rsplit_once
Example
path, ext := 'file.ts.dts'.rsplit_once('.')?
assert path == 'file.ts'
assert ext == 'dts'
runes
snake_to_camel
Examples
assert 'abcd'.snake_to_camel() == 'Abcd'
assert 'ab_cd'.snake_to_camel() == 'AbCd'
assert '_abcd'.snake_to_camel() == 'Abcd'
assert '_abcd_'.snake_to_camel() == 'Abcd'
split
Examples
assert 'A B C'.split(' ') == ['A','B','C']
assert 'DEF'.split('') == ['D','E','F']
split_any
Example
"first row\nsecond row".split_any(" \n") == ['first', 'row', 'second', 'row']
split_by_space
split_into_lines
split_n
split_nth
split_once
Example
path, ext := 'file.ts.dts'.split_once('.')?
assert path == 'file'
assert ext == 'ts.dts'
starts_with
starts_with_capital
Examples
assert 'Hello'.starts_with_capital() == true
assert 'Hello. World.'.starts_with_capital() == true
str
strip_margin
Example
st := 'Hello there,
| this is a string,
| Everything before the first | is removed'.strip_margin()
assert st == 'Hello there,
this is a string,
Everything before the first | is removed'
strip_margin_custom
substr
Example
assert 'ABCD'.substr(1,3) == 'BC'
substr_ni
substr_unsafe
substr_with_check
title
Example
assert 'hello v developer'.title() == 'Hello V Developer'
to_lower
Example
assert 'Hello V'.to_lower() == 'hello v'
to_lower_ascii
to_upper
Example
assert 'Hello V'.to_upper() == 'HELLO V'
to_upper_ascii
to_wide
trim
Example
assert ' ffHello V ffff'.trim(' f') == 'Hello V'
trim_indent
Example
st := '
Hello there,
this is a string,
all the leading indents are removed
and also the first and the last lines if they are blank
'.trim_indent()
assert st == 'Hello there,
this is a string,
all the leading indents are removed
and also the first and the last lines if they are blank'
trim_indexes
Example
left, right := '-hi-'.trim_indexes('-')
trim_left
Example
assert 'd Hello V developer'.trim_left(' d') == 'Hello V developer'
trim_right
Example
assert ' Hello V d'.trim_right(' d') == ' Hello V'
trim_space
Example
assert ' Hello V '.trim_space() == 'Hello V'
trim_space_left
Example
assert ' Hello V '.trim_space_left() == 'Hello V '
trim_space_right
Example
assert ' Hello V '.trim_space_right() == ' Hello V'
trim_string_left
Example
assert 'WorldHello V'.trim_string_left('World') == 'Hello V'
trim_string_right
Example
assert 'Hello VWorld'.trim_string_right('World') == 'Hello V'
u16
u32
u64
u8
u8_array
uncapitalize
Example
assert 'Hello, Bob!'.uncapitalize() == 'hello, Bob!'
utf32_code
wrap
Example
`assert 'Hello, my name is Carl and I am a delivery'.wrap(width: 20) == 'Hello, my name is\nCarl and I am a\ndelivery'`