Skip to content

Latest commit

 

History

History
328 lines (231 loc) · 6.83 KB

File metadata and controls

328 lines (231 loc) · 6.83 KB

Standard Library Reference

just a picture

Error Handling

std::panic::assert

If condition is false, the program will panic with the error message.

assert(condition, "error message");

std::panic::panic

Panic with the error message.

panic("error message");

std::panic::unreachable

Panic with the error message "unreachable".

unreachable();

std::panic::unimplemented

Panic with the error message "unimplemented".

unimplemented();

Standard Containers

std::list::list<T>

Source: list<T>

Linked list that accepts both trivial and non-trivial types.

  • If T is non-trivial type, it accepts types with these methods:
    • func clone(self) -> T
    • func delete(self)
  • If T is trivial type, it could be used directly.

Example:

func main() -> i32 {
    var a = list<str>::instance();
    var b = list<i64>::instance();

    var s = str::from("hello world!");
    a.push_back(s.__ptr__()); // s will do copy here
    b.push_back(123);         // do not need pointer type

    a.delete();
    b.delete();
    s.delete();
    return 0;
}

std::queue::queue<T>

Source: queue<T>

Queue that accepts both trivial and non-trivial types.

  • If T is non-trivial type, it accepts types with these methods:
    • func clone(self) -> T
    • func delete(self)
  • If T is trivial type, it could be used directly.

Example:

func main() -> i32 {
    var a = queue<str>::instance();
    var b = queue<i32>::instance();
    var s = str::from("hello world!");
    a.push(s.__ptr__());
    a.pop();
    b.push(123);
    b.pop();

    a.delete();
    b.delete();
    s.delete();
    return 0;
}

std::vec::vec<T>

Source: vec<T>

Vector that accepts both trivial and non-trivial types.

  • If T is non-trivial type, it accepts types with these methods:
    • func clone(self) -> T
    • func delete(self)
  • If T is trivial type, it could be used directly.

Example:

func main() -> i32 {
    var a = vec<str>::instance();
    var s = str::from("hello world!");
    a.push(s.__ptr__());
    a.pop_back();

    var b = vec<i32>::instance();
    b.push(123);

    a.delete();
    s.delete();
    b.delete();
    return 0;
}

std::set::hashset<T>

Source: hashset<T>

Hashset, T accepts types with these methods:

  • func clone(self) -> T
  • func delete(self)
  • func hash(self) -> u64

Example:

func main() -> i32 {
    var a = hashset<str>::instance();
    var s = str::from("hello world!");
    a.insert(s.__ptr__());
    a.delete();
    s.delete();
    return 0;
}

std::pair::pair<K, V>

Source: pair<K, V>

Pair, K and V accept both trivial and non-tribial types.

Example:

func main() -> i32 {
    var a = pair<str, i32>::instance(s.__ptr__(), 123);
    var b = pair<i32, str>::instance(123, s.__ptr__());
    var c = pair<i32, i32>::instance(123, 456);
    var d = pair<str, str>::instance(s.__ptr__(), s.__ptr__());

    s.delete();
    a.delete();
    b.delete();
    c.delete();
    d.delete();
    return 0;
}

std::map::hashmap<K, V>

Source: hashmap<K, V>

Hashmap, K and V accepts types with these methods:

  • func clone(self) -> K
  • func delete(self)

And K must implement func hash(self) -> u64.

Example:

func main() -> i32 {
    var a = hashmap<str, basic<i32>>::instance();
    var s = str::from("hello world!");

    a.insert(s.__ptr__(), basic<i32>::wrap(1).__ptr__());

    a.delete();
    s.delete();
    return 0;
}

Time Utils

std::time::chrono

Source: chrono

  • chrono::timestamp() -> time_t:

return the current timestamp in 64 bit time_t.

  • chrono::localtime() -> str:

return string of local time, for example "2025-08-15 00:00:00".

  • chrono::high_resolution_time() -> timespec:

return current timespec (64 bit sec, 64 bit nsec).

  • chrono::duration_s(start: timespec, end: timespec) -> f64
  • chrono::duration_ms(start: timespec, end: timespec) -> f64
  • chrono::duration_us(start: timespec, end: timespec) -> f64
  • chrono::duration_ns(start: timespec, end: timespec) -> f64

return duration in seconds, milliseconds, microseconds and nanoseconds.

Standard Trivial Type Wrapper

std::basic::basic<T>

Source: basic<T>

Basic type, T accepts primitive types. This wrapper could make basic types able to be used in hashset and hashmap. Because hashset and hashmap now need the key type with the method func hash(self) -> u64.

Example:

func main() -> i32 {
    var a = hashset<basic<i32>>::instance();
    a.insert(basic<i32>::wrap(1));
    a.delete();
    return 0;
}

File Formats

std::json::json

Source: json

Not stable, methods may change.

use std::json::{ json };
use std::str::{ str };
use std::libc::{ free };
use std::io::{ io };

func main() -> i32 {
    var input = str::from("{ \"a\": 1, \"b\": 2 }");
    defer input.delete();

    var j = json::parse(input.__ptr__());
    defer {
        j->delete();
        free(j => i8*);
    }

    if (!j->is_invalid()) {
        var s = j->to_string();
        defer s.delete();

        io::stdout().out(s.c_str).endln();
    }

    return 0;
}

TCP/UDP Utils

std::tcp

See example for std::tcp in example/socket/tcp_example.colgm

std::udp

See example for std::udp in example/socket/udp_example.colgm

Cryptography

std::crypto::md5

See example for std::crypto::md5 in test/md5_test.colgm

std::crypto::base64

See example for std::crypto::base64 in test/base64_test.colgm