Files
herolib/vdocs/builtin.md
2025-02-07 12:07:32 +03:00

40 KiB

module builtin

Contents

Constants

[Return to contents]

[Return to contents]

[Return to contents]

[Return to contents]

[Return to contents]

[Return to contents]

[Return to contents]

[Return to contents]

[Return to contents]

[Return to contents]

[Return to contents]

[Return to contents]

[Return to contents]

[Return to contents]

[Return to contents]

[Return to contents]

[Return to contents]

[Return to contents]

[Return to contents]

[Return to contents]

[Return to contents]

C.android_print

[Return to contents]

arguments

[Return to contents]

at_exit

[Return to contents]

c_error_number_str

[Return to contents]

compare_strings

[Return to contents]

copy

[Return to contents]

cstring_to_vstring

[Return to contents]

eprint

[Return to contents]

eprintln

[Return to contents]

error

Example


if ouch { return error('an error occurred') }

[Return to contents]

error_with_code

Example


if ouch { return error_with_code('an error occurred', 1) }

[Return to contents]

exit

[Return to contents]

f32_abs

Example


assert f32_abs(-2.0) == 2.0

[Return to contents]

f32_max

Example


assert f32_max(2.0,3.0) == 3.0

[Return to contents]

f32_min

Example


assert f32_min(2.0,3.0) == 2.0

[Return to contents]

f64_abs

Example


assert f64_abs(-2.0) == f64(2.0)

[Return to contents]

f64_max

Example


assert f64_max(2.0,3.0) == 3.0

[Return to contents]

f64_min

Example


assert f64_min(2.0,3.0) == 2.0

[Return to contents]

flush_stderr

[Return to contents]

flush_stdout

[Return to contents]

free

[Return to contents]

gc_check_leaks

[Return to contents]

gc_collect

[Return to contents]

gc_disable

[Return to contents]

gc_enable

[Return to contents]

gc_get_warn_proc

[Return to contents]

gc_heap_usage

[Return to contents]

gc_is_enabled

[Return to contents]

gc_memory_use

[Return to contents]

gc_set_warn_proc

[Return to contents]

get_str_intp_u32_format

[Return to contents]

get_str_intp_u64_format

[Return to contents]

input_character

[Return to contents]

int_max

Example


assert int_max(2,3) == 3

[Return to contents]

int_min

Example


assert int_min(2,3) == 2

[Return to contents]

isnil

[Return to contents]

malloc

[Return to contents]

malloc_noscan

[Return to contents]

malloc_uncollectable

[Return to contents]

memdup

[Return to contents]

memdup_noscan

[Return to contents]

memdup_uncollectable

[Return to contents]

panic

[Return to contents]

panic_error_number

[Return to contents]

panic_lasterr

[Return to contents]

panic_n

[Return to contents]

panic_n2

[Return to contents]

panic_option_not_set

[Return to contents]

panic_result_not_set

[Return to contents]

print

[Return to contents]

print_backtrace

[Return to contents]

print_backtrace_skipping_top_frames

[Return to contents]

print_character

[Return to contents]

println

[Return to contents]

proc_pidpath

[Return to contents]

ptr_str

[Return to contents]

realloc_data

[Return to contents]

str_intp

[Return to contents]

str_intp_g32

[Return to contents]

str_intp_g64

[Return to contents]

str_intp_rune

[Return to contents]

str_intp_sq

[Return to contents]

str_intp_sub

[Return to contents]

string_from_wide

[Return to contents]

string_from_wide2

[Return to contents]

string_to_ansi_not_null_terminated

[Return to contents]

tos

[Return to contents]

tos2

[Return to contents]

tos3

[Return to contents]

tos4

[Return to contents]

tos5

[Return to contents]

tos_clone

[Return to contents]

unbuffer_stdout

[Return to contents]

utf32_decode_to_buffer

[Return to contents]

utf32_to_str

[Return to contents]

utf32_to_str_no_malloc

[Return to contents]

utf8_char_len

[Return to contents]

utf8_str_visible_length

[Return to contents]

v_realloc

[Return to contents]

vcalloc

[Return to contents]

vcalloc_noscan

[Return to contents]

vmemcmp

[Return to contents]

vmemcpy

[Return to contents]

vmemmove

[Return to contents]

vmemset

[Return to contents]

vstrlen

[Return to contents]

vstrlen_char

[Return to contents]

wide_to_ansi

[Return to contents]

IError

[Return to contents]

free

[Return to contents]

str

[Return to contents]

C.intptr_t

[Return to contents]

FnExitCb

[Return to contents]

FnGC_WarnCB

[Return to contents]

MessageError

str

[Return to contents]

msg

[Return to contents]

code

[Return to contents]

free

[Return to contents]

[]rune

string

[Return to contents]

[]string

free

[Return to contents]

join

Example


assert ['Hello','V'].join(' ') == 'Hello V'

[Return to contents]

join_lines

[Return to contents]

sort_by_len

[Return to contents]

sort_ignore_case

[Return to contents]

str

Example


['a', 'b', 'c'].str() // => "['a', 'b', 'c']".

[Return to contents]

[]u8

byterune

[Return to contents]

bytestr

[Return to contents]

hex

[Return to contents]

utf8_to_utf32

[Return to contents]

bool

str

Example


assert (2 > 1).str() == 'true'

[Return to contents]

byte

[Return to contents]

byteptr

str

[Return to contents]

vbytes

[Return to contents]

vstring

[Return to contents]

vstring_literal

[Return to contents]

vstring_literal_with_len

[Return to contents]

vstring_with_len

[Return to contents]

chan

close

[Return to contents]

try_pop

[Return to contents]

try_push

[Return to contents]

char

str

[Return to contents]

vstring

[Return to contents]

vstring_literal

[Return to contents]

vstring_literal_with_len

[Return to contents]

vstring_with_len

[Return to contents]

charptr

str

[Return to contents]

vstring

[Return to contents]

vstring_literal

[Return to contents]

vstring_literal_with_len

[Return to contents]

vstring_with_len

[Return to contents]

f32

str

[Return to contents]

strg

[Return to contents]

strsci

Example


assert f32(1.234).strsci(3) == '1.234e+00'

[Return to contents]

strlong

[Return to contents]

eq_epsilon

Example


assert f32(2.0).eq_epsilon(2.0)

[Return to contents]

f64

str

[Return to contents]

strg

[Return to contents]

strsci

Example


assert f64(1.234).strsci(3) == '1.234e+00'

[Return to contents]

strlong

Example


assert f64(1.23456).strlong() == '1.23456'

[Return to contents]

eq_epsilon

Example


assert f64(2.0).eq_epsilon(2.0)

[Return to contents]

float literal

str

[Return to contents]

i16

str

Example


assert i16(-20).str() == '-20'

[Return to contents]

hex

Examples


assert i16(2).hex() == '2'

assert i16(200).hex() == 'c8'

[Return to contents]

hex_full

[Return to contents]

i32

str

[Return to contents]

i64

str

Example


assert i64(-200000).str() == '-200000'

[Return to contents]

hex

Examples


assert i64(2).hex() == '2'

assert i64(-200).hex() == 'ffffffffffffff38'

assert i64(2021).hex() == '7e5'

[Return to contents]

hex_full

[Return to contents]

i8

str

Example


assert i8(-2).str() == '-2'

[Return to contents]

hex

Examples


assert i8(8).hex() == '08'

assert i8(10).hex() == '0a'

assert i8(15).hex() == '0f'

[Return to contents]

hex_full

[Return to contents]

int

hex_full

[Return to contents]

str

Example


assert int(-2020).str() == '-2020'

[Return to contents]

hex

Examples


assert int(2).hex() == '2'

assert int(200).hex() == 'c8'

[Return to contents]

hex2

Examples


assert int(8).hex2() == '0x8'

assert int(15).hex2() == '0xf'

assert int(18).hex2() == '0x12'

[Return to contents]

int literal

str

[Return to contents]

hex

[Return to contents]

hex_full

[Return to contents]

isize

str

[Return to contents]

none

str

[Return to contents]

rune

str

[Return to contents]

repeat

[Return to contents]

bytes

[Return to contents]

length_in_bytes

[Return to contents]

to_upper

[Return to contents]

to_lower

[Return to contents]

to_title

[Return to contents]

u16

str

Example


assert u16(20).str() == '20'

[Return to contents]

hex

Examples


assert u16(2).hex() == '2'

assert u16(200).hex() == 'c8'

[Return to contents]

hex_full

[Return to contents]

u32

str

Example


assert u32(20000).str() == '20000'

[Return to contents]

hex

Examples


assert u32(2).hex() == '2'

assert u32(200).hex() == 'c8'

[Return to contents]

hex_full

[Return to contents]

u64

str

Example


assert u64(2000000).str() == '2000000'

[Return to contents]

hex

Examples


assert u64(2).hex() == '2'

assert u64(2000).hex() == '7d0'

[Return to contents]

hex_full

Examples


assert u64(2).hex_full() == '0000000000000002'

assert u64(255).hex_full() == '00000000000000ff'

[Return to contents]

u8

ascii_str

Example


assert u8(97).ascii_str() == 'a'

[Return to contents]

free

[Return to contents]

hex

Examples


assert u8(2).hex() == '02'

assert u8(15).hex() == '0f'

assert u8(255).hex() == 'ff'

[Return to contents]

hex_full

[Return to contents]

is_alnum

Example


assert u8(`V`).is_alnum() == true

[Return to contents]

is_bin_digit

Example


assert u8(`0`).is_bin_digit() == true

[Return to contents]

is_capital

Examples


assert `H`.is_capital() == true

assert `h`.is_capital() == false

[Return to contents]

is_digit

Example


assert u8(`9`).is_digit() == true

[Return to contents]

is_hex_digit

Example


assert u8(`F`).is_hex_digit() == true

[Return to contents]

is_letter

Example


assert u8(`V`).is_letter() == true

[Return to contents]

is_oct_digit

Example


assert u8(`7`).is_oct_digit() == true

[Return to contents]

is_space

Example


assert u8(` `).is_space() == true

[Return to contents]

repeat

[Return to contents]

str

Example


assert u8(111).str() == '111'

[Return to contents]

str_escaped

Example


assert u8(0).str_escaped() == r'`\0`'

[Return to contents]

vbytes

[Return to contents]

vstring

[Return to contents]

vstring_literal

[Return to contents]

vstring_literal_with_len

[Return to contents]

vstring_with_len

[Return to contents]

usize

str

[Return to contents]

voidptr

hex_full

[Return to contents]

str

[Return to contents]

vbytes

[Return to contents]

ArrayFlags

[Return to contents]

AttributeKind

[Return to contents]

ChanState

[Return to contents]

StrIntpType

[Return to contents]

str

[Return to contents]

C.DIR

[Return to contents]

C.FILE

[Return to contents]

C.GC_stack_base

[Return to contents]

C.IError

[Return to contents]

EnumData

[Return to contents]

Error

[Return to contents]

msg

[Return to contents]

code

[Return to contents]

FieldData

[Return to contents]

FunctionData

[Return to contents]

GCHeapUsage

[Return to contents]

MethodParam

[Return to contents]

SortedMap

[Return to contents]

delete

[Return to contents]

keys

[Return to contents]

free

[Return to contents]

print

[Return to contents]

StrIntpCgenData

[Return to contents]

StrIntpData

[Return to contents]

StrIntpMem

[Return to contents]

VAssertMetaInfo

[Return to contents]

free

[Return to contents]

VAttribute

[Return to contents]

VContext

[Return to contents]

VariantData

[Return to contents]

WrapConfig

[Return to contents]

array

[Return to contents]

repeat

[Return to contents]

repeat_to_depth

[Return to contents]

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]]

[Return to contents]

prepend

[Return to contents]

delete

Example


mut a := ['0', '1', '2', '3', '4', '5']
a.delete(1) // a is now ['0', '2', '3', '4', '5']

[Return to contents]

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

[Return to contents]

clear

Example


a.clear() // `a.len` is now 0

[Return to contents]

reset

[Return to contents]

trim

Example


a.trim(3) // `a.len` is now <= 3

[Return to contents]

drop

Example


mut a := [1,2]
a << 3
a.drop(2)
assert a == [3]
assert a.cap > a.len

[Return to contents]

first

[Return to contents]

last

[Return to contents]

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]

[Return to contents]

delete_last

[Return to contents]

clone

[Return to contents]

clone_to_depth

[Return to contents]

push_many

[Return to contents]

reverse_in_place

[Return to contents]

reverse

[Return to contents]

free

[Return to contents]

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'

[Return to contents]

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'`

[Return to contents]

count

Example


array.count(it % 2 == 1) // will return how many elements are odd

[Return to contents]

all

Example


array.all(it % 2 == 1) // will return true if every element is odd

[Return to contents]

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']

[Return to contents]

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

[Return to contents]

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]

[Return to contents]

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']
}

[Return to contents]

sorted_with_compare

[Return to contents]

contains

Example


[1, 2, 3].contains(4) == false

[Return to contents]

index

[Return to contents]

grow_cap

[Return to contents]

grow_len

[Return to contents]

pointers

[Return to contents]

map

[Return to contents]

move

[Return to contents]

clear

Example


a.clear() // `a.len` and `a.key_values.len` is now 0

[Return to contents]

reserve

[Return to contents]

delete

[Return to contents]

keys

[Return to contents]

values

[Return to contents]

clone

[Return to contents]

free

[Return to contents]

string

[Return to contents]

after

Examples


assert '23:34:45.234'.after(':') == '45.234'

assert 'abcd'.after('z') == 'abcd'

[Return to contents]

after_char

Examples


assert '23:34:45.234'.after_char(`:`) == '34:45.234'

assert 'abcd'.after_char(`:`) == 'abcd'

[Return to contents]

all_after

Examples


assert '23:34:45.234'.all_after('.') == '234'

assert 'abcd'.all_after('z') == 'abcd'

[Return to contents]

all_after_first

Examples


assert '23:34:45.234'.all_after_first(':') == '34:45.234'

assert 'abcd'.all_after_first('z') == 'abcd'

[Return to contents]

all_after_last

Examples


assert '23:34:45.234'.all_after_last(':') == '45.234'

assert 'abcd'.all_after_last('z') == 'abcd'

[Return to contents]

all_before

Examples


assert '23:34:45.234'.all_before('.') == '23:34:45'

assert 'abcd'.all_before('.') == 'abcd'

[Return to contents]

all_before_last

Examples


assert '23:34:45.234'.all_before_last(':') == '23:34'

assert 'abcd'.all_before_last('.') == 'abcd'

[Return to contents]

before

Examples


assert '23:34:45.234'.before('.') == '23:34:45'

assert 'abcd'.before('.') == 'abcd'

[Return to contents]

bool

[Return to contents]

bytes

[Return to contents]

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'

[Return to contents]

capitalize

Example


assert 'hello'.capitalize() == 'Hello'

[Return to contents]

clone

[Return to contents]

compare

[Return to contents]

contains

[Return to contents]

contains_any

[Return to contents]

contains_any_substr

[Return to contents]

contains_only

[Return to contents]

contains_u8

[Return to contents]

count

[Return to contents]

ends_with

[Return to contents]

f32

[Return to contents]

f64

[Return to contents]

fields

Examples


assert '\t\tv = v'.fields() == ['v', '=', 'v']

assert '  sss   ssss'.fields() == ['sss', 'ssss']

[Return to contents]

find_between

Example


assert 'hey [man] how you doin'.find_between('[', ']') == 'man'

[Return to contents]

free

[Return to contents]

hash

[Return to contents]

hex

[Return to contents]

i16

[Return to contents]

i32

[Return to contents]

i64

[Return to contents]

i8

[Return to contents]

indent_width

Examples


assert '  v'.indent_width() == 2

assert '\t\tv'.indent_width() == 2

[Return to contents]

index

[Return to contents]

index_after

[Return to contents]

index_any

[Return to contents]

index_u8

[Return to contents]

int

[Return to contents]

is_ascii

[Return to contents]

is_bin

[Return to contents]

is_blank

Examples


assert ' '.is_blank()

assert '\t'.is_blank()

assert 'v'.is_blank() == false

[Return to contents]

is_capital

Examples


assert 'Hello'.is_capital() == true

assert 'HelloWorld'.is_capital() == false

[Return to contents]

is_hex

[Return to contents]

is_int

[Return to contents]

is_lower

Example


assert 'hello developer'.is_lower() == true

[Return to contents]

is_oct

[Return to contents]

is_pure_ascii

[Return to contents]

is_title

Example


assert 'Hello V Developer'.is_title() == true

[Return to contents]

is_upper

Example


assert 'HELLO V'.is_upper() == true

[Return to contents]

last_index

[Return to contents]

last_index_u8

[Return to contents]

len_utf8

[Return to contents]

limit

[Return to contents]

match_glob

Examples


assert 'ABCD'.match_glob('AB*')

assert 'ABCD'.match_glob('*D')

assert 'ABCD'.match_glob('*B*')

assert !'ABCD'.match_glob('AB')

[Return to contents]

normalize_tabs

Example


assert '\t\tpop rax\t; pop rax'.normalize_tabs(2) == '    pop rax  ; pop rax'

[Return to contents]

parse_int

[Return to contents]

parse_uint

[Return to contents]

repeat

[Return to contents]

replace

[Return to contents]

replace_char

Example


assert '\tHello!'.replace_char(`\t`,` `,8) == '        Hello!'

[Return to contents]

replace_each

Example


assert 'ABCD'.replace_each(['B','C/','C','D','D','C']) == 'AC/DC'

[Return to contents]

replace_once

[Return to contents]

reverse

Example


assert 'Hello V'.reverse() == 'V olleH'

[Return to contents]

rsplit

Examples


assert 'A B C'.rsplit(' ') == ['C','B','A']

assert 'DEF'.rsplit('') == ['F','E','D']

[Return to contents]

rsplit_any

Example


"first row\nsecond row".rsplit_any(" \n") == ['row', 'second', 'row', 'first']

[Return to contents]

rsplit_nth

[Return to contents]

rsplit_once

Example


path, ext := 'file.ts.dts'.rsplit_once('.')?
assert path == 'file.ts'
assert ext == 'dts'

[Return to contents]

runes

[Return to contents]

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'

[Return to contents]

split

Examples


assert 'A B C'.split(' ') == ['A','B','C']

assert 'DEF'.split('') == ['D','E','F']

[Return to contents]

split_any

Example


"first row\nsecond row".split_any(" \n") == ['first', 'row', 'second', 'row']

[Return to contents]

split_by_space

[Return to contents]

split_into_lines

[Return to contents]

split_n

[Return to contents]

split_nth

[Return to contents]

split_once

Example


path, ext := 'file.ts.dts'.split_once('.')?
assert path == 'file'
assert ext == 'ts.dts'

[Return to contents]

starts_with

[Return to contents]

starts_with_capital

Examples


assert 'Hello'.starts_with_capital() == true

assert 'Hello. World.'.starts_with_capital() == true

[Return to contents]

str

[Return to contents]

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'

[Return to contents]

strip_margin_custom

[Return to contents]

substr

Example


assert 'ABCD'.substr(1,3) == 'BC'

[Return to contents]

substr_ni

[Return to contents]

substr_unsafe

[Return to contents]

substr_with_check

[Return to contents]

title

Example


assert 'hello v developer'.title() == 'Hello V Developer'

[Return to contents]

to_lower

Example


assert 'Hello V'.to_lower() == 'hello v'

[Return to contents]

to_lower_ascii

[Return to contents]

to_upper

Example


assert 'Hello V'.to_upper() == 'HELLO V'

[Return to contents]

to_upper_ascii

[Return to contents]

to_wide

[Return to contents]

trim

Example


assert ' ffHello V ffff'.trim(' f') == 'Hello V'

[Return to contents]

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'

[Return to contents]

trim_indexes

Example


left, right := '-hi-'.trim_indexes('-')

[Return to contents]

trim_left

Example


assert 'd Hello V developer'.trim_left(' d') == 'Hello V developer'

[Return to contents]

trim_right

Example


assert ' Hello V d'.trim_right(' d') == ' Hello V'

[Return to contents]

trim_space

Example


assert ' Hello V '.trim_space() == 'Hello V'

[Return to contents]

trim_space_left

Example


assert ' Hello V '.trim_space_left() == 'Hello V '

[Return to contents]

trim_space_right

Example


assert ' Hello V '.trim_space_right() == ' Hello V'

[Return to contents]

trim_string_left

Example


assert 'WorldHello V'.trim_string_left('World') == 'Hello V'

[Return to contents]

trim_string_right

Example


assert 'Hello VWorld'.trim_string_right('World') == 'Hello V'

[Return to contents]

u16

[Return to contents]

u32

[Return to contents]

u64

[Return to contents]

u8

[Return to contents]

u8_array

[Return to contents]

uncapitalize

Example


assert 'Hello, Bob!'.uncapitalize() == 'hello, Bob!'

[Return to contents]

utf32_code

[Return to contents]

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'`

[Return to contents]

Powered by vdoc. Generated on: 7 Feb 2025 12:06:55