Utils

#load "std/utils.bl"

Set of various utility functions.

set_flag

set_flag :: fn (flags: *?T, other: T) T #inline

Sets other flag or flags in flags input. This function is valid for numeric and enum flags types (checked by assert). This function can be easily replaced by flags |= other expression, pick your preferred way to do it.

Note: Multiple flags can be set at once by using an or operator other = A | B.

File: utils.bl

clr_flag

clr_flag :: fn (flags: *?T, other: T) T #inline

Clear other flag or flags in flags input. This function is valid for numeric and enum flags types (checked by assert).

Note: Multiple flags can be cleared at once by using an or operator other = A | B.

File: utils.bl

is_flag

is_flag :: fn (flags: ?T, other: T) bool #inline

Checks whether other flag or flags is set in flags. This function is valid for numeric and enum flags types (checked by assert).

In case you provide a combination of multiple flags as other argument, all of them must be set in the flags otherwise the function returns false.

Internally this is wrapper around (flags & other) == other expression.

File: utils.bl

env_get

env_get :: fn (var: string_view) string

Reads environment variable specified by var name. Result is empty in case no such variable was found or has no content. It's caller responsibility to delete result string.

File: utils.bl

env_set

env_set :: fn (var: string_view, value: string_view)  #inline

Sets environment variable.

File: utils.bl

random_seed_time

random_seed_time :: fn ()  #inline

Sets seed for std.rand or utility function random_number based on current system tick time.

File: utils.bl

random_number

random_number :: fn (min :: 0, max :: 1) s32 #inline

Generates random number in specified range <min, max> using standard libc rand generator. Random number generator seed is supposed to be set by :ref:random_seed_time or by std.srand call.

File: utils.bl

sort

sort :: fn (list: []?T, cmp: *fn (a: *T, b: *T) bool) 

Slice sorting utility.

File: utils.bl

find_if

find_if :: fn (arr: []?T, func: *fn (: *T) bool) (value: *T, index: s64) #inline

Iterate over arr slice and return pointer to the value and it's index if func validator function returs true.

The func is called for every element in the arr slice and pointer to the current element is passed into this function.

In case no element was found, function returns null pointer and -1 index.

File: utils.bl

hash_combine

hash_combine :: fn (first: ?T, second: T, more: ...T) T #inline

Combine two or more hashes into one, T is expected to be an integer type (checked by static assert).

File: utils.bl

is_power_of_two

is_power_of_two :: fn (n: usize) bool #inline

Check whether the number n is power of 2.

File: utils.bl

next_pow_2

next_pow_2 :: fn (n: s64) s64 #inline

Finds next power of 2.

File: utils.bl

utf8_to_utf32

utf8_to_utf32 :: fn (utf8_str: string_view, out_utf32_str: *[..]u32) Error

Converts UTF8 encoded string to UTF32.

File: utils.bl

utf8_to_utf32_single_char

utf8_to_utf32_single_char :: fn (utf8: string_view) (utf32: u32, decoded_bytes: s32, state: Error) #inline

File: utils.bl

utf32_to_utf8

utf32_to_utf8 :: fn (utf32_str: []u32, out_utf8_str: *string) Error

Converts UTF32 encoded string to UTF8.

File: utils.bl

utf32_to_utf8_single_char

utf32_to_utf8_single_char :: fn (utf32_char: u32, out_utf8_str: *string) Error

Appends the out_utf8_str string with utf8 characters converted from utf32_str input.

File: utils.bl

reinterpret_read_any

reinterpret_read_any :: fn (T: type, value: Any) T #inline

File: utils.bl

reinterpret_write_any

reinterpret_write_any :: fn (dest: Any, value: ?T) usize #inline

File: utils.bl

ternary

ternary :: fn (expr: bool, a: ?T, b: T) T #inline

Can be used as replacement for C ternary operator expr ? a : b.

File: utils.bl