Change to {} generics in lib7. Update qoi and other encodings and multiple other small changes.

This commit is contained in:
Christoffer Lerno
2025-02-24 01:44:57 +01:00
parent 87725a3a9e
commit b6f5938eda
37 changed files with 135 additions and 193 deletions

View File

@@ -1,7 +1,7 @@
// Copyright (c) 2023 Eduardo José Gómez Hernández. All rights reserved.
// Use of this source code is governed by the MIT license
// a copy of which can be found in the LICENSE_STDLIB file.
module std::atomic::types(<Type>);
module std::atomic::types{Type};
struct Atomic
{

View File

@@ -70,7 +70,7 @@ fn void BitSet.set_bool(&self, usz i, bool value) @operator([]=) @inline
<*
@require Type.kindof == UNSIGNED_INT
*>
module std::collections::growablebitset(<Type>);
module std::collections::growablebitset{Type};
import std::collections::list;
const BITS = Type.sizeof * 8;

View File

@@ -462,7 +462,7 @@ fn Object* Object.get_or_create_obj(&self, String key)
return container;
}
def ObjectInternalMap = HashMap(<String, Object*>) @private;
def ObjectInternalList = List(<Object*>) @private;
def ObjectInternalMapEntry = Entry(<String, Object*>) @private;
def ObjectInternalMap = HashMap{String, Object*} @private;
def ObjectInternalList = List{Object*} @private;
def ObjectInternalMapEntry = Entry{String, Object*} @private;

View File

@@ -1,7 +1,7 @@
<*
@require Type.kindof == ARRAY : "Required an array type"
*>
module std::collections::ringbuffer(<Type>);
module std::collections::ringbuffer{Type};
import std::io;
def Element = $typeof((Type){}[0]);

View File

@@ -74,18 +74,10 @@ import std::io;
fn usz! write(String filename, char[] input, QOIDesc* desc) => @pool()
{
// encode data
char[] output = new_encode(input, desc, allocator: allocator::temp())!;
char[] output = encode(tmem(), input, desc)!;
// open file
File! f = file::open(filename, "wb");
if (catch f) return QOIError.FILE_OPEN_FAILED?;
// write data to file and close it
usz! written = f.write(output);
if (catch written) return QOIError.FILE_WRITE_FAILED?;
if (catch f.close()) return QOIError.FILE_WRITE_FAILED?;
return written;
file::save(filename, output)!;
return output.len;
}
@@ -110,29 +102,20 @@ fn usz! write(String filename, char[] input, QOIDesc* desc) => @pool()
@param [&out] desc `The descriptor to fill with the image's info`
@param channels `The channels to be used`
*>
fn char[]! new_read(String filename, QOIDesc* desc, QOIChannels channels = AUTO, Allocator allocator = allocator::heap()) => @pool(allocator)
fn char[]! read(Allocator allocator, String filename, QOIDesc* desc, QOIChannels channels = AUTO) => @pool(allocator)
{
// read file
char[] data = file::load_temp(filename) ?? QOIError.FILE_OPEN_FAILED?!;
// pass data to decode function
return new_decode(data, desc, channels, allocator);
return decode(allocator, data, desc, channels);
}
fn char[]! read(String filename, QOIDesc* desc, QOIChannels channels = AUTO, Allocator allocator = allocator::heap()) @deprecated("Use new_read")
{
return new_read(filename, desc, channels, allocator);
}
// Back to basic non-stdio mode
module std::compression::qoi;
import std::bits;
fn char[]! encode(char[] input, QOIDesc* desc, Allocator allocator = allocator::heap()) @deprecated("use encode_new")
{
return new_encode(input, desc, allocator);
}
<*
Encode raw RGB or RGBA pixels into a QOI image in memory.
@@ -146,7 +129,7 @@ fn char[]! encode(char[] input, QOIDesc* desc, Allocator allocator = allocator::
@param [in] input `The raw RGB or RGBA pixels to encode`
@param [&in] desc `The descriptor of the image`
*>
fn char[]! new_encode(char[] input, QOIDesc* desc, Allocator allocator = allocator::heap()) @nodiscard
fn char[]! encode(Allocator allocator, char[] input, QOIDesc* desc) @nodiscard
{
// check info in desc
if (desc.width == 0 || desc.height == 0) return QOIError.INVALID_PARAMETERS?;
@@ -278,10 +261,6 @@ fn char[]! new_encode(char[] input, QOIDesc* desc, Allocator allocator = allocat
}
fn char[]! decode(char[] data, QOIDesc* desc, QOIChannels channels = AUTO, Allocator allocator = allocator::heap())
{
return new_decode(data, desc, channels, allocator);
}
<*
Decode a QOI image from memory.
@@ -304,7 +283,7 @@ fn char[]! decode(char[] data, QOIDesc* desc, QOIChannels channels = AUTO, Alloc
@param [&out] desc `The descriptor to fill with the image's info`
@param channels `The channels to be used`
*>
fn char[]! new_decode(char[] data, QOIDesc* desc, QOIChannels channels = AUTO, Allocator allocator = allocator::heap()) @nodiscard
fn char[]! decode(Allocator allocator, char[] data, QOIDesc* desc, QOIChannels channels = AUTO) @nodiscard
{
// check input data
if (data.len < Header.sizeof + END_OF_STREAM.len) return QOIError.INVALID_DATA?;

View File

@@ -1,4 +1,4 @@
// Copyright (c) 2021-2024 Christoffer Lerno. All rights reserved.
// Copyright (c) 2021-2025 Christoffer Lerno. All rights reserved.
// Use of this source code is governed by the MIT license
// a copy of which can be found in the LICENSE_STDLIB file.

View File

@@ -13,7 +13,7 @@ struct Allocation
void*[MAX_BACKTRACE] backtrace;
}
def AllocMap = HashMap(<uptr, Allocation>);
def AllocMap = HashMap { uptr, Allocation };
// A simple tracking allocator.
// It tracks allocations using a hash map but

View File

@@ -26,7 +26,7 @@ macro slice2d(array_ptr, x = 0, xlen = 0, y = 0, ylen = 0)
if (xlen < 1) xlen = $typeof((*array_ptr)[0]).len + xlen;
if (ylen < 1) ylen = $typeof((*array_ptr)).len + ylen;
var $ElementType = $typeof((*array_ptr)[0][0]);
return Slice2d(<$ElementType>) { ($ElementType*)array_ptr, $typeof((*array_ptr)[0]).len, y, ylen, x, xlen };
return Slice2d{$ElementType} { ($ElementType*)array_ptr, $typeof((*array_ptr)[0]).len, y, ylen, x, xlen };
}
@@ -56,7 +56,7 @@ macro rindex_of(array, element)
@require @typeis(arr1[0], $typeof(arr2[0])) "Arrays must have the same type"
@ensure result.len == arr1.len + arr2.len
*>
macro concat(arr1, arr2, Allocator allocator) @nodiscard
macro concat(Allocator allocator, arr1, arr2) @nodiscard
{
var $Type = $typeof(arr1[0]);
$Type[] result = allocator::alloc_array(allocator, $Type, arr1.len + arr2.len);
@@ -70,22 +70,6 @@ macro concat(arr1, arr2, Allocator allocator) @nodiscard
}
return result;
}
<*
Concatenate two arrays or slices, returning a slice containing the concatenation of them.
@param [in] arr1
@param [in] arr2
@param [&inout] allocator "The allocator to use, default is the heap allocator"
@require @typekind(arr1) == SLICE || @typekind(arr1) == ARRAY
@require @typekind(arr2) == SLICE || @typekind(arr2) == ARRAY
@require @typeis(arr1[0], $typeof(arr2[0])) "Arrays must have the same type"
@ensure return.len == arr1.len + arr2.len
*>
macro concat_new(arr1, arr2, Allocator allocator = allocator::heap()) @nodiscard
{
return concat(arr1, arr2, allocator);
}
<*
Concatenate two arrays or slices, returning a slice containing the concatenation of them,
allocated using the temp allocator.
@@ -97,9 +81,9 @@ macro concat_new(arr1, arr2, Allocator allocator = allocator::heap()) @nodiscard
@require @typeis(arr1[0], $typeof(arr2[0])) "Arrays must have the same type"
@ensure return.len == arr1.len + arr2.len
*>
macro tconcat(arr1, arr2) @nodiscard => concat(arr1, arr2, allocator::temp());
macro tconcat(arr1, arr2) @nodiscard => concat(allocator::temp(), arr1, arr2);
module std::core::array::slice(<Type>);
module std::core::array::slice{Type};
struct Slice2d
{

View File

@@ -10,7 +10,7 @@ struct BenchmarkUnit
BenchmarkFn func;
}
fn BenchmarkUnit[] benchmark_collection_create(Allocator allocator = allocator::heap())
fn BenchmarkUnit[] benchmark_collection_create(Allocator allocator)
{
BenchmarkFn[] fns = $$BENCHMARK_FNS;
String[] names = $$BENCHMARK_NAMES;
@@ -170,5 +170,5 @@ fn bool run_benchmarks(BenchmarkUnit[] benchmarks) @if(!$$OLD_TEST)
fn bool default_benchmark_runner(String[] args) => @pool()
{
return run_benchmarks(benchmark_collection_create(allocator::temp()));
return run_benchmarks(benchmark_collection_create(tmem()));
}

View File

@@ -45,7 +45,7 @@ struct TestUnit
TestFn func;
}
fn TestUnit[] test_collection_create(Allocator allocator = allocator::heap())
fn TestUnit[] test_collection_create(Allocator allocator)
{
TestFn[] fns = $$TEST_FNS;
String[] names = $$TEST_NAMES;
@@ -317,6 +317,6 @@ fn bool run_tests(String[] args, TestUnit[] tests) @private
fn bool default_test_runner(String[] args) => @pool()
{
assert(test_context == null, "test suite is already running");
return run_tests(args, test_collection_create(allocator::temp()));
return run_tests(args, test_collection_create(tmem()));
}

View File

@@ -20,7 +20,7 @@ const char DEFAULT_PAD = '=';
@require padding < 0xFF "Invalid padding character"
@return "The encoded string."
*>
fn String! encode(char[] src, Allocator allocator, char padding = DEFAULT_PAD, Base32Alphabet* alphabet = &STANDARD)
fn String! encode(Allocator allocator, char[] src, char padding = DEFAULT_PAD, Base32Alphabet* alphabet = &STANDARD)
{
char[] dst = allocator::alloc_array(allocator, char, encode_len(src.len, padding));
return encode_buffer(src, dst, padding, alphabet);
@@ -34,16 +34,14 @@ fn String! encode(char[] src, Allocator allocator, char padding = DEFAULT_PAD, B
@require padding < 0xFF "Invalid padding character"
@return "The decoded data."
*>
fn char[]! decode(char[] src, Allocator allocator, char padding = DEFAULT_PAD, Base32Alphabet* alphabet = &STANDARD)
fn char[]! decode(Allocator allocator, char[] src, char padding = DEFAULT_PAD, Base32Alphabet* alphabet = &STANDARD)
{
char[] dst = allocator::alloc_array(allocator, char, decode_len(src.len, padding));
return decode_buffer(src, dst, padding, alphabet);
}
fn String! encode_new(char[] code, char padding = DEFAULT_PAD, Base32Alphabet* alphabet = &STANDARD) @inline => encode(code, allocator::heap(), padding, alphabet);
fn String! encode_temp(char[] code, char padding = DEFAULT_PAD, Base32Alphabet* alphabet = &STANDARD) @inline => encode(code, allocator::temp(), padding, alphabet);
fn char[]! decode_new(char[] code, char padding = DEFAULT_PAD, Base32Alphabet* alphabet = &STANDARD) @inline => decode(code, allocator::heap(), padding, alphabet);
fn char[]! decode_temp(char[] code, char padding = DEFAULT_PAD, Base32Alphabet* alphabet = &STANDARD) @inline => decode(code, allocator::temp(), padding, alphabet);
fn String! tencode(char[] code, char padding = DEFAULT_PAD, Base32Alphabet* alphabet = &STANDARD) @inline => encode(tmem(), code, padding, alphabet);
fn char[]! tdecode(char[] code, char padding = DEFAULT_PAD, Base32Alphabet* alphabet = &STANDARD) @inline => decode(tmem(), code, padding, alphabet);
<*
Calculate the length in bytes of the decoded data.

View File

@@ -43,22 +43,20 @@ const Base64Alphabet URL = {
const STD_ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
const URL_ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
fn String encode(char[] src, Allocator allocator, char padding = DEFAULT_PAD, Base64Alphabet* alphabet = &STANDARD)
fn String encode(Allocator allocator, char[] src, char padding = DEFAULT_PAD, Base64Alphabet* alphabet = &STANDARD)
{
char[] dst = allocator::alloc_array(allocator, char, encode_len(src.len, padding));
return encode_buffer(src, dst, padding, alphabet);
}
fn char[]! decode(char[] src, Allocator allocator, char padding = DEFAULT_PAD, Base64Alphabet* alphabet = &STANDARD)
fn char[]! decode(Allocator allocator, char[] src, char padding = DEFAULT_PAD, Base64Alphabet* alphabet = &STANDARD)
{
char[] dst = allocator::alloc_array(allocator, char, decode_len(src.len, padding))!;
return decode_buffer(src, dst, padding, alphabet);
}
fn String encode_new(char[] code, char padding = DEFAULT_PAD, Base64Alphabet* alphabet = &STANDARD) @inline => encode(code, allocator::heap(), padding, alphabet);
fn String encode_temp(char[] code, char padding = DEFAULT_PAD, Base64Alphabet* alphabet = &STANDARD) @inline => encode(code, allocator::temp(), padding, alphabet);
fn char[]! decode_new(char[] code, char padding = DEFAULT_PAD, Base64Alphabet* alphabet = &STANDARD) @inline => decode(code, allocator::heap(), padding, alphabet);
fn char[]! decode_temp(char[] code, char padding = DEFAULT_PAD, Base64Alphabet* alphabet = &STANDARD) @inline => decode(code, allocator::temp(), padding, alphabet);
fn String tencode(char[] code, char padding = DEFAULT_PAD, Base64Alphabet* alphabet = &STANDARD) @inline => encode(tmem(), code, padding, alphabet);
fn char[]! tdecode(char[] code, char padding = DEFAULT_PAD, Base64Alphabet* alphabet = &STANDARD) @inline => decode(tmem(), code, padding, alphabet);
<*

View File

@@ -1,4 +1,4 @@
module std::experimental::scheduler(<Event>);
module std::experimental::scheduler{Event};
import std::collections, std::thread, std::time;
struct DelayedSchedulerEvent @local
@@ -19,9 +19,9 @@ fn int DelayedSchedulerEvent.compare_to(self, DelayedSchedulerEvent other) @loca
struct FrameScheduler
{
PriorityQueue(<DelayedSchedulerEvent>) delayed_events;
List(<Event>) events;
List(<Event>) pending_events;
PriorityQueue{DelayedSchedulerEvent} delayed_events;
List{Event} events;
List{Event} pending_events;
bool pending;
Mutex mtx;
}

View File

@@ -1,4 +1,4 @@
module std::hash::hmac(<HashAlg, HASH_BYTES, BLOCK_BYTES>);
module std::hash::hmac{HashAlg, HASH_BYTES, BLOCK_BYTES};
import std::crypto;
struct Hmac

View File

@@ -13,9 +13,9 @@ struct Md5
uint[16] block;
}
def HmacMd5 = Hmac(<Md5, HASH_BYTES, BLOCK_BYTES>);
def hmac = hmac::hash(<Md5, HASH_BYTES, BLOCK_BYTES>);
def pbkdf2 = hmac::pbkdf2(<Md5, HASH_BYTES, BLOCK_BYTES>);
def HmacMd5 = Hmac{Md5, HASH_BYTES, BLOCK_BYTES};
def hmac = hmac::hash{Md5, HASH_BYTES, BLOCK_BYTES};
def pbkdf2 = hmac::pbkdf2{Md5, HASH_BYTES, BLOCK_BYTES};
fn char[HASH_BYTES] hash(char[] data)
{

View File

@@ -18,9 +18,9 @@ struct Sha1
char[BLOCK_BYTES] buffer;
}
def HmacSha1 = Hmac(<Sha1, HASH_BYTES, BLOCK_BYTES>);
def hmac = hmac::hash(<Sha1, HASH_BYTES, BLOCK_BYTES>);
def pbkdf2 = hmac::pbkdf2(<Sha1, HASH_BYTES, BLOCK_BYTES>);
def HmacSha1 = Hmac{Sha1, HASH_BYTES, BLOCK_BYTES};
def hmac = hmac::hash{Sha1, HASH_BYTES, BLOCK_BYTES};
def pbkdf2 = hmac::pbkdf2{Sha1, HASH_BYTES, BLOCK_BYTES};
fn char[HASH_BYTES] hash(char[] data)
{

View File

@@ -34,9 +34,9 @@ struct Sha256
char[BLOCK_SIZE] buffer;
}
def HmacSha256 = Hmac(<Sha256, HASH_SIZE, BLOCK_SIZE>);
def hmac = hmac::hash(<Sha256, HASH_SIZE, BLOCK_SIZE>);
def pbkdf2 = hmac::pbkdf2(<Sha256, HASH_SIZE, BLOCK_SIZE>);
def HmacSha256 = Hmac{Sha256, HASH_SIZE, BLOCK_SIZE};
def hmac = hmac::hash{Sha256, HASH_SIZE, BLOCK_SIZE};
def pbkdf2 = hmac::pbkdf2{Sha256, HASH_SIZE, BLOCK_SIZE};
fn char[HASH_SIZE] hash(char[] data)
{

View File

@@ -7,7 +7,7 @@ const char PREFERRED_SEPARATOR_WIN32 = '\\';
const char PREFERRED_SEPARATOR_POSIX = '/';
const char PREFERRED_SEPARATOR = env::WIN32 ? PREFERRED_SEPARATOR_WIN32 : PREFERRED_SEPARATOR_POSIX;
def PathList = List(<Path>);
def PathList = List { Path };
fault PathResult
{

View File

@@ -90,33 +90,33 @@ fault MatrixError
MATRIX_INVERSE_DOESNT_EXIST,
}
def Complexf = Complex(<float>);
def Complex = Complex(<double>);
def COMPLEX_IDENTITY = complex::IDENTITY(<double>) @builtin;
def COMPLEXF_IDENTITY = complex::IDENTITY(<float>) @builtin;
def Complexf = Complex{float};
def Complex = Complex{double};
def COMPLEX_IDENTITY = complex::IDENTITY{double} @builtin;
def COMPLEXF_IDENTITY = complex::IDENTITY{float} @builtin;
def Quaternionf = Quaternion(<float>);
def Quaternion = Quaternion(<double>);
def QUATERNION_IDENTITY = quaternion::IDENTITY(<double>) @builtin;
def QUATERNIONF_IDENTITY = quaternion::IDENTITY(<float>) @builtin;
def Quaternionf = Quaternion{float};
def Quaternion = Quaternion{double};
def QUATERNION_IDENTITY = quaternion::IDENTITY{double} @builtin;
def QUATERNIONF_IDENTITY = quaternion::IDENTITY{float} @builtin;
def Matrix2f = Matrix2x2(<float>);
def Matrix2 = Matrix2x2(<double>);
def Matrix3f = Matrix3x3(<float>);
def Matrix3 = Matrix3x3(<double>);
def Matrix4f = Matrix4x4(<float>);
def Matrix4 = Matrix4x4(<double>);
def matrix4_ortho = matrix::ortho(<double>) @builtin;
def matrix4_perspective = matrix::perspective(<double>) @builtin;
def matrix4f_ortho = matrix::ortho(<float>) @builtin;
def matrix4f_perspective = matrix::perspective(<float>) @builtin;
def Matrix2f = Matrix2x2{float};
def Matrix2 = Matrix2x2{double};
def Matrix3f = Matrix3x3{float};
def Matrix3 = Matrix3x3{double};
def Matrix4f = Matrix4x4{float};
def Matrix4 = Matrix4x4{double};
def matrix4_ortho = matrix::ortho{double} @builtin;
def matrix4_perspective = matrix::perspective{double} @builtin;
def matrix4f_ortho = matrix::ortho{float} @builtin;
def matrix4f_perspective = matrix::perspective{float} @builtin;
def MATRIX2_IDENTITY = matrix::IDENTITY2(<double>) @builtin;
def MATRIX2F_IDENTITY = matrix::IDENTITY2(<float>) @builtin;
def MATRIX3_IDENTITY = matrix::IDENTITY3(<double>) @builtin;
def MATRIX3F_IDENTITY = matrix::IDENTITY3(<float>) @builtin;
def MATRIX4_IDENTITY = matrix::IDENTITY4(<double>) @builtin;
def MATRIX4F_IDENTITY = matrix::IDENTITY4(<float>) @builtin;
def MATRIX2_IDENTITY = matrix::IDENTITY2{double} @builtin;
def MATRIX2F_IDENTITY = matrix::IDENTITY2{float} @builtin;
def MATRIX3_IDENTITY = matrix::IDENTITY3{double} @builtin;
def MATRIX3F_IDENTITY = matrix::IDENTITY3{float} @builtin;
def MATRIX4_IDENTITY = matrix::IDENTITY4{double} @builtin;
def MATRIX4F_IDENTITY = matrix::IDENTITY4{float} @builtin;
<*

View File

@@ -1,4 +1,4 @@
module std::math::complex(<Real>);
module std::math::complex{Real};
union Complex
{

View File

@@ -1,4 +1,4 @@
module std::math::matrix(<Real>);
module std::math::matrix{Real};
import std::math::vector;
struct Matrix2x2

View File

@@ -1,4 +1,4 @@
module std::math::quaternion(<Real>);
module std::math::quaternion{Real};
import std::math::vector;
union Quaternion
{

View File

@@ -66,8 +66,8 @@ fn Vec3 Vec3.refract(self, Vec3 n, double r) => refract3(self, n, r);
fn void ortho_normalize(Vec3f* v1, Vec3f* v2) => ortho_normalize3(v1, v2);
fn void ortho_normalized(Vec3* v1, Vec3* v2) => ortho_normalize3(v1, v2);
fn Matrix4f matrix4f_look_at(Vec3f eye, Vec3f target, Vec3f up) @deprecated => matrix::look_at(<float>)(eye, target, up);
fn Matrix4 matrix4_look_at(Vec3 eye, Vec3 target, Vec3 up) @deprecated => matrix::look_at(<double>)(eye, target, up);
fn Matrix4f matrix4f_look_at(Vec3f eye, Vec3f target, Vec3f up) @deprecated => matrix::look_at{float}(eye, target, up);
fn Matrix4 matrix4_look_at(Vec3 eye, Vec3 target, Vec3 up) @deprecated => matrix::look_at{double}(eye, target, up);
fn Vec3f Vec3f.rotate_quat(self, Quaternionf q) => rotate_by_quat3(self, q);
fn Vec3 Vec3.rotate_quat(self, Quaternion q) => rotate_by_quat3(self, q);

View File

@@ -56,19 +56,14 @@ fn usz! InetAddress.to_format(InetAddress* addr, Formatter* formatter) @dynamic
return formatter.printf("%d.%d.%d.%d", addr.ipv4.a, addr.ipv4.b, addr.ipv4.c, addr.ipv4.d)!;
}
fn String InetAddress.to_new_string(InetAddress* addr, Allocator allocator = allocator::heap()) @dynamic
fn String InetAddress.to_string(&self, Allocator allocator)
{
if (addr.is_ipv6)
{
char[8 * 5 + 1] buffer;
String res = (String)io::bprintf(&buffer, "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x",
addr.ipv6.a, addr.ipv6.b, addr.ipv6.c, addr.ipv6.d,
addr.ipv6.e, addr.ipv6.f, addr.ipv6.g, addr.ipv6.h)!!;
return res.copy(allocator);
}
char[3 * 4 + 3 + 1] buffer;
String res = (String)io::bprintf(&buffer, "%d.%d.%d.%d", addr.ipv4.a, addr.ipv4.b, addr.ipv4.c, addr.ipv4.d)!!;
return res.copy(allocator);
return string::format(allocator, "%s", *self);
}
fn String InetAddress.to_tstring(&self)
{
return string::format(tmem(), "%s", *self);
}
fn InetAddress! ipv6_from_str(String s)

View File

@@ -58,14 +58,9 @@ fn uint! ipv4toint(String s)
return out;
}
fn String! int_to_new_ipv4(uint val, Allocator allocator = allocator::heap())
fn String! int_to_ipv4(uint val, Allocator allocator)
{
char[3 * 4 + 3 + 1] buffer;
String res = (String)io::bprintf(&buffer, "%d.%d.%d.%d", val >> 24, (val >> 16) & 0xFF, (val >> 8) & 0xFF, val & 0xFF)!;
return res.copy(allocator);
}
fn String! int_to_temp_ipv4(uint val)
{
return int_to_new_ipv4(val, allocator::temp());
}

View File

@@ -91,7 +91,7 @@ fn void*[] capture_current(void*[] buffer)
$endswitch
}
def BacktraceList = List(<Backtrace>);
def BacktraceList = List{Backtrace};
def symbolize_backtrace = linux::symbolize_backtrace @if(env::LINUX);
def symbolize_backtrace = win32::symbolize_backtrace @if(env::WIN32);

View File

@@ -11,20 +11,20 @@ Sort list using the counting sort algorithm.
macro countingsort(list, key_fn = EMPTY_MACRO_SLOT) @builtin
{
usz len = sort::len_from_list(list);
cs::csort(<$typeof(list), $typeof(key_fn)>)(list, 0, len, key_fn, ~((uint)0));
cs::csort{$typeof(list), $typeof(key_fn)}(list, 0, len, key_fn, ~((uint)0));
}
macro insertionsort_indexed(list, start, end, cmp = EMPTY_MACRO_SLOT, context = EMPTY_MACRO_SLOT) @builtin
{
is::isort(<$typeof(list), $typeof(cmp), $typeof(context)>)(list, (usz)start, (usz)end, cmp, context);
is::isort{$typeof(list), $typeof(cmp), $typeof(context)}(list, (usz)start, (usz)end, cmp, context);
}
macro quicksort_indexed(list, start, end, cmp = EMPTY_MACRO_SLOT, context = EMPTY_MACRO_SLOT) @builtin
{
qs::qsort(<$typeof(list), $typeof(cmp), $typeof(context)>)(list, (isz)start, (isz)(end-1), cmp, context);
qs::qsort{$typeof(list), $typeof(cmp), $typeof(context)}(list, (isz)start, (isz)(end-1), cmp, context);
}
module std::sort::cs(<Type, KeyFn>);
module std::sort::cs{Type, KeyFn};
def Counts = usz[256] @private;
def Ranges = usz[257] @private;

View File

@@ -10,14 +10,14 @@ macro insertionsort(list, cmp = EMPTY_MACRO_SLOT, context = EMPTY_MACRO_SLOT) @b
{
$if @typekind(list) == POINTER &&& (@typekind(*list) == ARRAY || @typekind(*list) == VECTOR):
$typeof((*list)[0])[] list2 = list;
is::isort(<$typeof(list2), $typeof(cmp), $typeof(context)>)(list2, 0, list.len, cmp, context);
is::isort{$typeof(list2), $typeof(cmp), $typeof(context)}(list2, 0, list.len, cmp, context);
$else
usz len = sort::len_from_list(list);
is::isort(<$typeof(list), $typeof(cmp), $typeof(context)>)(list, 0, (isz)len, cmp, context);
is::isort{$typeof(list), $typeof(cmp), $typeof(context)}(list, 0, (isz)len, cmp, context);
$endif
}
module std::sort::is(<Type, CmpFn, Context>);
module std::sort::is{Type, CmpFn, Context};
def ElementType = $typeof(((Type){})[0]);

View File

@@ -11,10 +11,10 @@ macro quicksort(list, cmp = EMPTY_MACRO_SLOT, context = EMPTY_MACRO_SLOT) @built
{
$if @typekind(list) == POINTER &&& (@typekind(*list) == ARRAY || @typekind(*list) == VECTOR):
$typeof((*list)[0])[] list2 = list;
qs::qsort(<$typeof(list2), $typeof(cmp), $typeof(context)>)(list2, 0, (isz)list.len - 1, cmp, context);
qs::qsort{$typeof(list2), $typeof(cmp), $typeof(context)}(list2, 0, (isz)list.len - 1, cmp, context);
$else
usz len = sort::len_from_list(list);
qs::qsort(<$typeof(list), $typeof(cmp), $typeof(context)>)(list, 0, (isz)len - 1, cmp, context);
qs::qsort{$typeof(list), $typeof(cmp), $typeof(context)}(list, 0, (isz)len - 1, cmp, context);
$endif
}
@@ -30,10 +30,10 @@ list will be partially sorted.
macro quickselect(list, isz k, cmp = EMPTY_MACRO_SLOT, context = EMPTY_MACRO_SLOT) @builtin
{
usz len = sort::len_from_list(list);
return qs::qselect(<$typeof(list), $typeof(cmp), $typeof(context)>)(list, 0, (isz)len - 1, k, cmp, context);
return qs::qselect{$typeof(list), $typeof(cmp), $typeof(context)}(list, 0, (isz)len - 1, k, cmp, context);
}
module std::sort::qs(<Type, CmpFn, Context>);
module std::sort::qs{Type, CmpFn, Context};
def ElementType = $typeof(((Type){})[0]);

View File

@@ -1,4 +1,4 @@
module std::thread::channel(<Type>);
module std::thread::channel{Type};
distinct BufferedChannel = void*;
@@ -21,12 +21,7 @@ struct BufferedChannelImpl @private
Type[?] buf;
}
fn void! BufferedChannel.new_init(&self, usz size = 1)
{
return self.init(size, allocator::heap());
}
fn void! BufferedChannel.init(&self, usz size = 1, Allocator allocator)
fn void! BufferedChannel.init(&self, Allocator allocator, usz size = 1)
{
BufferedChannelImpl* channel = allocator::new_with_padding(allocator, BufferedChannelImpl, Type.sizeof * size)!;
defer catch allocator::free(allocator, channel);

View File

@@ -1,4 +1,4 @@
module std::thread::pool(<SIZE>);
module std::thread::pool{SIZE};
import std::thread;
struct ThreadPool

View File

@@ -1,4 +1,4 @@
module std::thread::channel(<Type>);
module std::thread::channel{Type};
distinct UnbufferedChannel = void*;
@@ -18,8 +18,6 @@ struct UnbufferedChannelImpl @private
ConditionVariable read_cond;
}
fn void! UnbufferedChannel.new_init(&self) => self.init(allocator::heap());
fn void! UnbufferedChannel.init(&self, Allocator allocator)
{
UnbufferedChannelImpl* channel = allocator::alloc(allocator, UnbufferedChannelImpl);

View File

@@ -15,20 +15,20 @@ fn void test_qoi_all()
QOIDesc test_desc;
// decode the test data
char[] decoded = qoi::new_decode(TEST_QOI_DATA[..], &test_desc)!!;
char[] decoded = qoi::decode(mem, TEST_QOI_DATA[..], &test_desc)!!;
defer free(decoded);
assert(test_desc.width == 340 && test_desc.height == 169, "Expected resolution of 340x169");
// encode the decoded data
char[] encoded = qoi::new_encode(decoded, &test_desc)!!;
char[] encoded = qoi::encode(mem, decoded, &test_desc)!!;
assert(encoded == TEST_QOI_DATA[..], "Encoder output should match the test data");
defer free(encoded);
// encode and write the decoded data to a file
usz written = qoi::write("unittest.qoi", decoded, &test_desc)!!;
// read and decode the written data
char[] read = qoi::new_read("unittest.qoi", &test_desc)!!;
char[] read = qoi::read(mem, "unittest.qoi", &test_desc)!!;
assert(read == decoded, "Read data should match the decoded data");
// cleanup

View File

@@ -21,12 +21,12 @@ fn void find_subarray()
fn void concat()
{
int[3] a = { 1, 2, 3 };
free(array::concat_new(a, a));
free(array::concat_new(a[..], a[..]));
free(array::concat_new(a[:0], a[:0]));
free(array::concat_new((int[2]) { 1, 2 }, a[:0]));
free(array::concat_new(a[:0], (int[2]) { 1, 2 }));
int[] c = array::concat_new(a[1..2], a);
free(array::concat(mem, a, a));
free(array::concat(mem, a[..], a[..]));
free(array::concat(mem, a[:0], a[:0]));
free(array::concat(mem, (int[2]) { 1, 2 }, a[:0]));
free(array::concat(mem, a[:0], (int[2]) { 1, 2 }));
int[] c = array::concat(mem, a[1..2], a);
defer free(c);
assert (c == (int[]){ 2, 3, 1, 2, 3 });
}

View File

@@ -98,10 +98,10 @@ fn void base32_api()
{
foreach (t : std_tests)
{
String got = base32::encode_temp(t.dec)!!;
String got = base32::tencode(t.dec)!!;
assert(got == t.enc, "got: %s, want: %s", got, t.enc);
char[] got_chars = base32::decode_temp(t.enc)!!;
char[] got_chars = base32::tdecode(t.enc)!!;
assert(got_chars == t.dec, "got: %s, want: %s", got_chars, t.dec);
}
};

View File

@@ -12,21 +12,21 @@ fn void test_ipv4()
fn void test_ipv4_to_string()
{
InetAddress a = net::ipv4_from_str("127.0.0.1")!!;
assert(a.to_new_string(allocator::temp()) == "127.0.0.1");
assert(a.to_tstring() == "127.0.0.1");
}
fn void test_ipv6_to_string()
{
InetAddress a = net::ipv6_from_str("2001:db8::2:1")!!;
free(a.to_new_string());
free(a.to_string(mem));
assert(a.to_new_string(allocator::temp()) == "2001:0db8:0000:0000:0000:0000:0002:0001");
assert(net::ipv6_from_str("2001:db8::1").to_new_string(allocator::temp())!! == "2001:0db8:0000:0000:0000:0000:0000:0001");
assert(net::ipv6_from_str("::1").to_new_string(allocator::temp())!! == "0000:0000:0000:0000:0000:0000:0000:0001");
assert(net::ipv6_from_str("2001::1").to_new_string(allocator::temp())!! == "2001:0000:0000:0000:0000:0000:0000:0001");
assert(net::ipv6_from_str("2001:db8:1234::").to_new_string(allocator::temp())!! == "2001:0db8:1234:0000:0000:0000:0000:0000");
assert(net::ipv6_from_str("2001::").to_new_string(allocator::temp())!! == "2001:0000:0000:0000:0000:0000:0000:0000");
assert(net::ipv6_from_str("::").to_new_string(allocator::temp())!! == "0000:0000:0000:0000:0000:0000:0000:0000");
assert(a.to_tstring() == "2001:0db8:0000:0000:0000:0000:0002:0001");
assert(net::ipv6_from_str("2001:db8::1").to_tstring()!! == "2001:0db8:0000:0000:0000:0000:0000:0001");
assert(net::ipv6_from_str("::1").to_tstring()!! == "0000:0000:0000:0000:0000:0000:0000:0001");
assert(net::ipv6_from_str("2001::1").to_tstring()!! == "2001:0000:0000:0000:0000:0000:0000:0001");
assert(net::ipv6_from_str("2001:db8:1234::").to_tstring()!! == "2001:0db8:1234:0000:0000:0000:0000:0000");
assert(net::ipv6_from_str("2001::").to_tstring()!! == "2001:0000:0000:0000:0000:0000:0000:0000");
assert(net::ipv6_from_str("::").to_tstring()!! == "0000:0000:0000:0000:0000:0000:0000:0000");
}
fn void test_ipv4_parse()

View File

@@ -10,7 +10,7 @@ fn void init_destroy_buffered() @test
for (usz i = 0; i < 20; i++)
{
BufferedChannel{int} c;
c.new_init(1)!!;
c.init(mem, 1)!!;
defer c.destroy()!!;
}
}
@@ -20,7 +20,7 @@ fn void init_destroy_unbuffered() @test
for (usz i = 0; i < 20; i++)
{
UnbufferedChannel{int} c;
c.new_init()!!;
c.init(mem)!!;
defer c.destroy()!!;
}
}
@@ -28,7 +28,7 @@ fn void init_destroy_unbuffered() @test
fn void push_to_buffered_channel_no_lock() @test
{
BufferedChannel{int} c;
c.new_init(1)!!;
c.init(mem, 1)!!;
defer c.destroy()!!;
c.push(1)!!;
@@ -37,7 +37,7 @@ fn void push_to_buffered_channel_no_lock() @test
fn void push_pop_buffered_no_locks() @test
{
BufferedChannel{int} c;
c.new_init(1)!!;
c.init(mem, 1)!!;
defer c.destroy()!!;
c.push(123)!!;
@@ -48,7 +48,7 @@ fn void push_pop_buffered_no_locks() @test
fn void push_pop_unbuffered_with_locks() @test
{
UnbufferedChannel{int} c;
c.new_init()!!;
c.init(mem)!!;
defer c.destroy()!!;
Thread thread;
@@ -71,7 +71,7 @@ fn void push_pop_unbuffered_with_locks() @test
fn void sending_to_closed_unbuffered_chan_is_forbidden() @test
{
UnbufferedChannel{int} c;
c.new_init()!!;
c.init(mem, )!!;
defer c.destroy()!!;
c.close()!!;
@@ -87,7 +87,7 @@ fn void sending_to_closed_unbuffered_chan_is_forbidden() @test
fn void sending_to_closed_buffered_chan_is_forbidden() @test
{
BufferedChannel{int} c;
c.new_init(1)!!;
c.init(mem, 1)!!;
defer c.destroy()!!;
c.close()!!;
@@ -103,7 +103,7 @@ fn void sending_to_closed_buffered_chan_is_forbidden() @test
fn void reading_from_empty_closed_unbuffered_chan_is_forbidden() @test
{
UnbufferedChannel{int} c;
c.new_init()!!;
c.init(mem, )!!;
defer c.destroy()!!;
c.close()!!;
@@ -119,7 +119,7 @@ fn void reading_from_empty_closed_unbuffered_chan_is_forbidden() @test
fn void reading_from_empty_closed_buffered_chan_is_forbidden() @test
{
BufferedChannel{int} c;
c.new_init(1)!!;
c.init(mem, 1)!!;
defer c.destroy()!!;
c.close()!!;
@@ -135,7 +135,7 @@ fn void reading_from_empty_closed_buffered_chan_is_forbidden() @test
fn void reading_from_non_empty_closed_buffered_chan_is_ok() @test
{
BufferedChannel{int} c;
c.new_init(3)!!;
c.init(mem, 3)!!;
defer c.destroy()!!;
c.push(1)!!;
@@ -164,7 +164,7 @@ fn void reading_from_non_empty_closed_buffered_chan_is_ok() @test
fn void reading_from_empty_buffered_chan_aborted_by_close() @test
{
BufferedChannel{int} c;
c.new_init(3)!!;
c.init(mem, 3)!!;
defer c.destroy()!!;
Thread thread;
@@ -190,7 +190,7 @@ fn void reading_from_empty_buffered_chan_aborted_by_close() @test
fn void reading_from_unbuffered_chan_aborted_by_close() @test
{
UnbufferedChannel{int} c;
c.new_init()!!;
c.init(mem, )!!;
defer c.destroy()!!;
Thread thread;
@@ -216,7 +216,7 @@ fn void reading_from_unbuffered_chan_aborted_by_close() @test
fn void sending_to_full_buffered_chan_aborted_by_close() @test
{
BufferedChannel{int} c;
c.new_init(1)!!;
c.init(mem, 1)!!;
defer c.destroy()!!;
c.push(1)!!;
@@ -244,7 +244,7 @@ fn void sending_to_full_buffered_chan_aborted_by_close() @test
fn void sending_to_unbuffered_chan_aborted_by_close() @test
{
UnbufferedChannel{int} c;
c.new_init()!!;
c.init(mem, )!!;
defer c.destroy()!!;
Thread thread;
@@ -270,7 +270,7 @@ fn void sending_to_unbuffered_chan_aborted_by_close() @test
fn void multiple_actions_unbuffered() @test
{
UnbufferedChannel{int} c;
c.new_init()!!;
c.init(mem, )!!;
defer c.destroy()!!;
Thread thread;
@@ -304,7 +304,7 @@ fn void multiple_actions_unbuffered() @test
fn void multiple_actions_buffered() @test
{
BufferedChannel{int} c;
c.new_init(10)!!;
c.init(mem, 10)!!;
defer c.destroy()!!;
Thread thread;