From 70029cc4b8034ff6293fc1e5a9d43c8bb49c1865 Mon Sep 17 00:00:00 2001 From: Christoffer Lerno Date: Sun, 23 Feb 2025 22:54:48 +0100 Subject: [PATCH] Updated stdlib to experimental allocator first. Updates to DString, String and anything using new_* syntax. --- lib7/std/collections/anylist.c3 | 13 -- lib7/std/collections/elastic_array.c3 | 10 -- lib7/std/core/builtin.c3 | 2 +- lib7/std/core/dstring.c3 | 64 ++++----- lib7/std/core/private/main_stub.c3 | 2 +- lib7/std/core/runtime_benchmark.c3 | 2 +- lib7/std/core/runtime_test.c3 | 4 +- lib7/std/core/string.c3 | 133 ++++++------------ lib7/std/encoding/csv.c3 | 14 +- lib7/std/encoding/hex.c3 | 11 +- lib7/std/encoding/json.c3 | 20 +-- lib7/std/io/file.c3 | 2 +- lib7/std/io/io.c3 | 6 +- lib7/std/io/os/chdir.c3 | 2 +- lib7/std/io/os/file_libc.c3 | 6 +- lib7/std/io/os/fileinfo.c3 | 4 +- lib7/std/io/os/getcwd.c3 | 2 +- lib7/std/io/os/ls.c3 | 8 +- lib7/std/io/os/mkdir.c3 | 2 +- lib7/std/io/os/rmdir.c3 | 2 +- lib7/std/io/os/rmtree.c3 | 10 +- lib7/std/io/os/temp_directory.c3 | 12 +- lib7/std/io/path.c3 | 85 ++++------- lib7/std/math/bigint.c3 | 2 +- lib7/std/math/random/math.seeder.c3 | 2 +- lib7/std/math/uuid.c3 | 4 +- lib7/std/net/inetaddr.c3 | 2 +- lib7/std/net/url.c3 | 40 +++--- lib7/std/net/url_encoding.c3 | 36 ++--- lib7/std/os/env.c3 | 37 +++-- lib7/std/os/macos/darwin.c3 | 2 +- lib7/std/os/subprocess.c3 | 8 +- lib7/std/time/format.c3 | 43 +++--- lib7/std/time/time.c3 | 2 +- test/test_suite7/expressions/opt_in_conv.c3 | 2 +- .../functions/missing_return_lambda.c3 | 2 +- test/test_suite7/macros/ref_macro_method.c3 | 2 +- .../slices/subscript_check_1519.c3t | 4 +- .../statements/dead_statements.c3t | 2 +- test/test_suite7/stdlib/map_linux.c3t | 12 +- test/test_suite7/stdlib/map_macos.c3t | 4 +- .../switch/switch_in_defer_macro.c3t | 6 +- test/unit/stdlib/collections/priorityqueue.c3 | 4 +- 43 files changed, 248 insertions(+), 384 deletions(-) diff --git a/lib7/std/collections/anylist.c3 b/lib7/std/collections/anylist.c3 index d8a855d6d..6f2b1c8fc 100644 --- a/lib7/std/collections/anylist.c3 +++ b/lib7/std/collections/anylist.c3 @@ -67,19 +67,6 @@ fn usz! AnyList.to_format(&self, Formatter* formatter) @dynamic } } -fn String AnyList.to_new_string(&self, Allocator allocator = null) @dynamic -{ - return string::format("%s", *self, allocator: allocator ?: allocator::heap()); -} - - -fn String AnyList.to_string(&self, Allocator allocator) @dynamic -{ - return string::format("%s", *self, allocator: allocator); -} - -fn String AnyList.to_tstring(&self) => string::tformat("%s", *self); - <* Push an element on the list by cloning it. *> diff --git a/lib7/std/collections/elastic_array.c3 b/lib7/std/collections/elastic_array.c3 index e2c89e6b9..292d48189 100644 --- a/lib7/std/collections/elastic_array.c3 +++ b/lib7/std/collections/elastic_array.c3 @@ -39,16 +39,6 @@ fn usz! ElasticArray.to_format(&self, Formatter* formatter) @dynamic } } -fn String ElasticArray.to_string(&self, Allocator allocator) @dynamic -{ - return string::format("%s", *self, allocator: allocator); -} - -fn String ElasticArray.to_new_string(&self, Allocator allocator = nul) @dynamic -{ - return string::format("%s", *self, allocator: allocator ?: allocator::heap()); -} - fn String ElasticArray.to_tstring(&self) { return string::tformat("%s", *self); diff --git a/lib7/std/core/builtin.c3 b/lib7/std/core/builtin.c3 index ca73f36b8..4e01a6b7e 100644 --- a/lib7/std/core/builtin.c3 +++ b/lib7/std/core/builtin.c3 @@ -171,7 +171,7 @@ fn void panicf(String fmt, String file, String function, uint line, args...) @stack_mem(512; Allocator allocator) { DString s; - s.new_init(allocator: allocator); + s.init(allocator); s.appendf(fmt, ...args); in_panic = false; panic(s.str_view(), file, function, line); diff --git a/lib7/std/core/dstring.c3 b/lib7/std/core/dstring.c3 index a9b584c07..bf4d57cf0 100644 --- a/lib7/std/core/dstring.c3 +++ b/lib7/std/core/dstring.c3 @@ -9,7 +9,7 @@ const usz MIN_CAPACITY @private = 16; <* @require !self.data() "String already initialized" *> -fn DString DString.new_init(&self, usz capacity = MIN_CAPACITY, Allocator allocator = allocator::heap()) +fn DString DString.init(&self, Allocator allocator, usz capacity = MIN_CAPACITY) { if (capacity < MIN_CAPACITY) capacity = MIN_CAPACITY; StringData* data = allocator::alloc_with_padding(allocator, StringData, capacity)!!; @@ -22,23 +22,22 @@ fn DString DString.new_init(&self, usz capacity = MIN_CAPACITY, Allocator alloca <* @require !self.data() "String already initialized" *> -fn DString DString.temp_init(&self, usz capacity = MIN_CAPACITY) +fn DString DString.tinit(&self, usz capacity = MIN_CAPACITY) { - self.new_init(capacity, allocator::temp()) @inline; - return *self; + return self.init(tmem(), capacity) @inline; } -fn DString new_with_capacity(usz capacity, Allocator allocator = allocator::heap()) +fn DString new_with_capacity(Allocator allocator, usz capacity) { - return (DString){}.new_init(capacity, allocator); + return (DString){}.init(allocator, capacity); } -fn DString temp_with_capacity(usz capacity) => new_with_capacity(capacity, allocator::temp()) @inline; +fn DString tnew_with_capacity(usz capacity) => new_with_capacity(tmem(), capacity) @inline; -fn DString new(String c = "", Allocator allocator = allocator::heap()) +fn DString new(Allocator allocator, String c = "") { usz len = c.len; - StringData* data = (StringData*)new_with_capacity(len, allocator); + StringData* data = (StringData*)new_with_capacity(allocator, len); if (len) { data.len = len; @@ -47,7 +46,7 @@ fn DString new(String c = "", Allocator allocator = allocator::heap()) return (DString)data; } -fn DString temp_new(String s = "") => new(s, allocator::temp()) @inline; +fn DString tnew(String s = "") => new(tmem(), s) @inline; fn void DString.replace_char(self, char ch, char replacement) @@ -99,16 +98,16 @@ fn void DString.replace(&self, String needle, String replacement) }; } -fn DString DString.new_concat(self, DString b, Allocator allocator = allocator::heap()) +fn DString DString.concat(self, Allocator allocator, DString b) { DString string; - string.new_init(self.len() + b.len(), allocator); + string.init(allocator, self.len() + b.len()); string.append(self); string.append(b); return string; } -fn DString DString.temp_concat(self, DString b) => self.new_concat(b, allocator::temp()); +fn DString DString.tconcat(self, DString b) => self.concat(tmem(), b); fn ZString DString.zstr_view(&self) { @@ -218,23 +217,18 @@ fn usz DString.append_char32(&self, Char32 c) return n; } -fn DString DString.tcopy(&self) => self.copy(allocator::temp()); +fn DString DString.tcopy(&self) => self.copy(tmem()); -fn DString DString.copy(self, Allocator allocator = null) +fn DString DString.copy(self, Allocator allocator) { - if (!self) - { - if (allocator) return new_with_capacity(0, allocator); - return (DString)null; - } + if (!self) return new(allocator); StringData* data = self.data(); - if (!allocator) allocator = allocator::heap(); - DString new_string = new_with_capacity(data.capacity, allocator); + DString new_string = new_with_capacity(allocator, data.capacity); mem::copy((char*)new_string.data(), (char*)data, StringData.sizeof + data.len); return new_string; } -fn ZString DString.copy_zstr(self, Allocator allocator = allocator::heap()) +fn ZString DString.copy_zstr(self, Allocator allocator) { usz str_len = self.len(); if (!str_len) @@ -248,12 +242,12 @@ fn ZString DString.copy_zstr(self, Allocator allocator = allocator::heap()) return (ZString)zstr; } -fn String DString.copy_str(self, Allocator allocator = allocator::heap()) +fn String DString.copy_str(self, Allocator allocator) { return (String)self.copy_zstr(allocator)[:self.len()]; } -fn String DString.tcopy_str(self) => self.copy_str(allocator::temp()) @inline; +fn String DString.tcopy_str(self) => self.copy_str(tmem()) @inline; fn bool DString.equals(self, DString other_string) { @@ -303,7 +297,7 @@ fn void DString.append_chars(&self, String str) if (!other_len) return; if (!*self) { - *self = new(str); + *self = tnew(str); return; } self.reserve(other_len); @@ -312,9 +306,9 @@ fn void DString.append_chars(&self, String str) data.len += other_len; } -fn Char32[] DString.copy_utf32(&self, Allocator allocator = allocator::heap()) +fn Char32[] DString.copy_utf32(&self, Allocator allocator) { - return self.str_view().to_utf32(allocator) @inline!!; + return self.str_view().to_utf32_copy(allocator) @inline!!; } fn void DString.append_string(&self, DString str) @@ -345,7 +339,7 @@ fn void DString.append_char(&self, char c) { if (!*self) { - *self = new_with_capacity(MIN_CAPACITY); + *self = tnew_with_capacity(MIN_CAPACITY); } self.reserve(1); StringData* data = self.data(); @@ -543,7 +537,7 @@ macro void DString.insert_at(&self, usz index, value) fn usz! DString.appendf(&self, String format, args...) @maydiscard { - if (!self.data()) self.new_init(format.len + 20); + if (!self.data()) self.tinit(format.len + 20); @pool(self.data().allocator) { Formatter formatter; @@ -554,7 +548,7 @@ fn usz! DString.appendf(&self, String format, args...) @maydiscard fn usz! DString.appendfn(&self, String format, args...) @maydiscard { - if (!self.data()) self.new_init(format.len + 20); + if (!self.data()) self.tinit(format.len + 20); @pool(self.data().allocator) { Formatter formatter; @@ -565,15 +559,15 @@ fn usz! DString.appendfn(&self, String format, args...) @maydiscard }; } -fn DString new_join(String[] s, String joiner, Allocator allocator = allocator::heap()) +fn DString join(Allocator allocator, String[] s, String joiner) { - if (!s.len) return (DString)null; + if (!s.len) return new(allocator); usz total_size = joiner.len * s.len; foreach (String* &str : s) { total_size += str.len; } - DString res = new_with_capacity(total_size, allocator); + DString res = new_with_capacity(allocator, total_size); res.append(s[0]); foreach (String* &str : s[1..]) { @@ -613,7 +607,7 @@ fn void DString.reserve(&self, usz addition) StringData* data = self.data(); if (!data) { - *self = dstring::new_with_capacity(addition); + *self = dstring::tnew_with_capacity(addition); return; } usz len = data.len + addition; diff --git a/lib7/std/core/private/main_stub.c3 b/lib7/std/core/private/main_stub.c3 index f64ded9e2..1cdf2d4f8 100644 --- a/lib7/std/core/private/main_stub.c3 +++ b/lib7/std/core/private/main_stub.c3 @@ -80,7 +80,7 @@ macro String[] wargs_strings(int argc, Char16** argv) @private { Char16* arg = argv[i]; Char16[] argstring = arg[:_strlen(arg)]; - list[i] = string::new_from_utf16(argstring) ?? "?".copy(); + list[i] = string::new_from_utf16(mem, argstring) ?? "?".copy(mem); } return list[:argc]; } diff --git a/lib7/std/core/runtime_benchmark.c3 b/lib7/std/core/runtime_benchmark.c3 index e9eb8d611..777597a9d 100644 --- a/lib7/std/core/runtime_benchmark.c3 +++ b/lib7/std/core/runtime_benchmark.c3 @@ -123,7 +123,7 @@ fn bool run_benchmarks(BenchmarkUnit[] benchmarks) @if(!$$OLD_TEST) usz len = max_name + 9; - DString name = dstring::temp_with_capacity(64); + DString name = dstring::tnew_with_capacity(64); name.append_repeat('-', len / 2); name.append(" BENCHMARKS "); name.append_repeat('-', len - len / 2); diff --git a/lib7/std/core/runtime_test.c3 b/lib7/std/core/runtime_test.c3 index 123fa4552..5fad98705 100644 --- a/lib7/std/core/runtime_test.c3 +++ b/lib7/std/core/runtime_test.c3 @@ -74,7 +74,7 @@ fn int cmp_test_unit(TestUnit a, TestUnit b) fn bool terminal_has_ansi_codes() @local => @pool() { - if (try v = env::get_var_temp("TERM")) + if (try v = env::tget_var("TERM")) { if (v.contains("xterm") || v.contains("vt100") || v.contains("screen")) return true; } @@ -228,7 +228,7 @@ fn bool run_tests(String[] args, TestUnit[] tests) @private int tests_passed = 0; int tests_skipped = 0; int test_count = tests.len; - DString name = dstring::temp_with_capacity(64); + DString name = dstring::tnew_with_capacity(64); usz len = max_name + 9; name.append_repeat('-', len / 2); name.append(" TESTS "); diff --git a/lib7/std/core/string.c3 b/lib7/std/core/string.c3 index 3bccacec6..2dccdcd9e 100644 --- a/lib7/std/core/string.c3 +++ b/lib7/std/core/string.c3 @@ -41,7 +41,7 @@ fault NumberConversion *> fn ZString tformat_zstr(String fmt, args...) { - DString str = dstring::temp_with_capacity(fmt.len + args.len * 8); + DString str = dstring::tnew_with_capacity(fmt.len + args.len * 8); str.appendf(fmt, ...args); return str.zstr_view(); } @@ -52,20 +52,13 @@ fn ZString tformat_zstr(String fmt, args...) @param [inout] allocator `The allocator to use` @param [in] fmt `The formatting string` *> -fn String format(String fmt, args..., Allocator allocator) => @pool(allocator) +fn String format(Allocator allocator, String fmt, args...) => @pool(allocator) { - DString str = dstring::temp_with_capacity(fmt.len + args.len * 8); + DString str = dstring::tnew_with_capacity(fmt.len + args.len * 8); str.appendf(fmt, ...args); return str.copy_str(allocator); } -<* - Return a heap allocated String created using the formatting function. - - @param [in] fmt `The formatting string` -*> -fn String new_format(String fmt, args..., Allocator allocator = null) => format(fmt, ...args, allocator: allocator ?: allocator::heap()); - <* Return a temporary String created using the formatting function. @@ -73,24 +66,11 @@ fn String new_format(String fmt, args..., Allocator allocator = null) => format( *> fn String tformat(String fmt, args...) { - DString str = dstring::temp_with_capacity(fmt.len + args.len * 8); + DString str = dstring::tnew_with_capacity(fmt.len + args.len * 8); str.appendf(fmt, ...args); return str.str_view(); } -<* - Return a new ZString created using the formatting function. - - @param [in] fmt `The formatting string` - @param [inout] allocator `The allocator to use` -*> -fn ZString new_format_zstr(String fmt, args..., Allocator allocator = allocator::heap()) => @pool(allocator) -{ - DString str = dstring::temp_with_capacity(fmt.len + args.len * 8); - str.appendf(fmt, ...args); - return str.copy_zstr(allocator); -} - <* Check if a character is in a set. @@ -105,7 +85,7 @@ macro bool char_in_set(char c, String set) return false; } -fn String join_new(String[] s, String joiner, Allocator allocator = allocator::heap()) +fn String join(Allocator allocator, String[] s, String joiner) { if (!s) { @@ -119,7 +99,7 @@ fn String join_new(String[] s, String joiner, Allocator allocator = allocator::h } @pool(allocator) { - DString res = dstring::temp_with_capacity(total_size); + DString res = dstring::tnew_with_capacity(total_size); res.append(s[0]); foreach (String* &str : s[1..]) { @@ -246,7 +226,7 @@ fn String String.strip_end(string, String needle) @require needle.len > 0 "The needle must be at least 1 character long" @ensure return.len > 0 *> -fn String[] String.split(s, String needle, usz max = 0, Allocator allocator = allocator::heap(), bool skip_empty = false) +fn String[] String.split(s, Allocator allocator, String needle, usz max = 0, bool skip_empty = false) { usz capacity = 16; usz i = 0; @@ -281,18 +261,6 @@ fn String[] String.split(s, String needle, usz max = 0, Allocator allocator = al return holder[:i]; } -<* - Split a string into parts, e.g "a|b|c" split with "|" yields { "a", "b", "c" }, using the heap allocator - to store the parts. - - @param [in] s - @param [in] needle - @param max "Max number of elements, 0 means no limit, defaults to 0" - @param skip_empty "True to skip empty elements" - @require needle.len > 0 "The needle must be at least 1 character long" - @ensure return.len > 0 -*> -fn String[] String.new_split(s, String needle, usz max = 0, bool skip_empty) => s.split(needle, max, allocator::heap(), skip_empty) @inline; <* This function is identical to String.split, but implicitly uses the @@ -303,7 +271,7 @@ fn String[] String.new_split(s, String needle, usz max = 0, bool skip_empty) => @param max "Max number of elements, 0 means no limit, defaults to 0" @param skip_empty "True to skip empty elements" *> -fn String[] String.tsplit(s, String needle, usz max = 0, bool skip_empty = false) => s.split(needle, max, allocator::temp(), skip_empty) @inline; +fn String[] String.tsplit(s, String needle, usz max = 0, bool skip_empty = false) => s.split(tmem(), needle, max, skip_empty) @inline; fault SplitResult { BUFFER_EXCEEDED } @@ -521,7 +489,7 @@ fn usz ZString.len(str) } -fn ZString String.zstr_copy(s, Allocator allocator = allocator::heap()) +fn ZString String.zstr_copy(s, Allocator allocator) { usz len = s.len; char* str = allocator::malloc(allocator, len + 1); @@ -530,7 +498,7 @@ fn ZString String.zstr_copy(s, Allocator allocator = allocator::heap()) return (ZString)str; } -fn String String.concat(s1, String s2, Allocator allocator = allocator::heap()) +fn String String.concat(s1, Allocator allocator, String s2) { usz full_len = s1.len + s2.len; char* str = allocator::malloc(allocator, full_len + 1); @@ -541,17 +509,17 @@ fn String String.concat(s1, String s2, Allocator allocator = allocator::heap()) return (String)str[:full_len]; } -fn String String.tconcat(s1, String s2) => s1.concat(s2, allocator::temp()); +fn String String.tconcat(s1, String s2) => s1.concat(tmem(), s2); -fn ZString String.zstr_tcopy(s) => s.zstr_copy(allocator::temp()) @inline; +fn ZString String.zstr_tcopy(s) => s.zstr_copy(tmem()) @inline; <* Copy this string, by duplicating the string, always adding a zero byte sentinel, so that it safely can be converted to a ZString by a cast. *> -fn String String.copy(s, Allocator allocator = allocator::heap()) +fn String String.copy(s, Allocator allocator) { usz len = s.len; char* str = allocator::malloc(allocator, len + 1); @@ -560,23 +528,23 @@ fn String String.copy(s, Allocator allocator = allocator::heap()) return (String)str[:len]; } -fn void String.free(&s, Allocator allocator = allocator::heap()) +fn void String.free(&s, Allocator allocator) { if (!s.ptr) return; allocator::free(allocator, s.ptr); *s = ""; } -fn String String.tcopy(s) => s.copy(allocator::temp()) @inline; +fn String String.tcopy(s) => s.copy(tmem()) @inline; -fn String ZString.copy(z, Allocator allocator = allocator::heap()) +fn String ZString.copy(z, Allocator allocator) { return z.str_view().copy(allocator) @inline; } fn String ZString.tcopy(z) { - return z.str_view().copy(allocator::temp()) @inline; + return z.str_view().copy(tmem()) @inline; } <* @@ -585,7 +553,7 @@ fn String ZString.tcopy(z) @return! UnicodeResult.INVALID_UTF8 "If the string contained an invalid UTF-8 sequence" @return! AllocationFailure "If allocation of the string fails" *> -fn Char16[]! String.to_new_utf16(s, Allocator allocator = allocator::heap()) +fn Char16[]! String.to_utf16_copy(s, Allocator allocator) { usz len16 = conv::utf16len_for_utf8(s); Char16* data = allocator::alloc_array_try(allocator, Char16, len16 + 1)!; @@ -594,26 +562,16 @@ fn Char16[]! String.to_new_utf16(s, Allocator allocator = allocator::heap()) return data[:len16]; } -<* - Convert an UTF-8 string to UTF-16 - @return "The UTF-16 string as a slice, allocated using the given allocator" - @return! UnicodeResult.INVALID_UTF8 "If the string contained an invalid UTF-8 sequence" - @return! AllocationFailure "If allocation of the string fails" -*> -fn Char16[]! String.to_temp_utf16(s) +fn Char16[]! String.to_utf16_tcopy(s) => s.to_utf16_copy(tmem()); + +fn WString! String.to_wstring_copy(s, Allocator allocator) { - return s.to_new_utf16(allocator::temp()); + return (WString)s.to_utf16_copy(allocator).ptr; } -fn WString! String.to_wstring(s, Allocator allocator) -{ - return (WString)s.to_new_utf16(allocator).ptr; -} +fn WString! String.to_wstring_tcopy(s) => s.to_wstring_copy(tmem()); -fn WString! String.to_temp_wstring(s) => s.to_wstring(allocator::temp()); -fn WString! String.to_new_wstring(s) => s.to_wstring(allocator::heap()); - -fn Char32[]! String.to_utf32(s, Allocator allocator) +fn Char32[]! String.to_utf32_copy(s, Allocator allocator) { usz codepoints = conv::utf8_codepoints(s); Char32* data = allocator::alloc_array_try(allocator, Char32, codepoints + 1)!; @@ -622,30 +580,27 @@ fn Char32[]! String.to_utf32(s, Allocator allocator) return data[:codepoints]; } -fn Char32[]! String.to_new_utf32(s) => s.to_utf32(allocator::heap()) @inline; -fn Char32[]! String.to_temp_utf32(s) => s.to_utf32(allocator::temp()) @inline; - <* - Convert a string to ASCII lower case. + Convert a string to ASCII lower case in place. @param [inout] s @pure *> -fn void String.convert_ascii_to_lower(s) +fn void String.convert_to_lower(s) { foreach (&c : s) if (c.is_upper() @pure) *c += 'a' - 'A'; } -fn String String.new_ascii_to_lower(s, Allocator allocator = allocator::heap()) +fn String String.to_lower_copy(s, Allocator allocator) { String copy = s.copy(allocator); - copy.convert_ascii_to_lower(); + copy.convert_to_lower(); return copy; } -fn String String.temp_ascii_to_lower(s) +fn String String.to_lower_tcopy(s) { - return s.new_ascii_to_lower(allocator::temp()); + return s.to_lower_copy(tmem()); } <* @@ -654,7 +609,7 @@ fn String String.temp_ascii_to_lower(s) @param [inout] s @pure *> -fn void String.convert_ascii_to_upper(s) +fn void String.convert_to_upper(s) { foreach (&c : s) if (c.is_lower() @pure) *c -= 'a' - 'A'; } @@ -667,10 +622,10 @@ fn void String.convert_ascii_to_upper(s) @return `a new String converted to ASCII upper case.` *> -fn String String.new_ascii_to_upper(s, Allocator allocator = allocator::heap()) +fn String String.to_upper_copy(s, Allocator allocator) { String copy = s.copy(allocator); - copy.convert_ascii_to_upper(); + copy.convert_to_upper(); return copy; } @@ -683,12 +638,12 @@ fn StringIterator String.iterator(s) @param [in] s @return `a temporary String converted to ASCII upper case.` *> -fn String String.temp_ascii_to_upper(s) +fn String String.to_upper_tcopy(s) { - return s.new_ascii_to_upper(allocator::temp()); + return s.to_upper_copy(tmem()); } -fn String! new_from_utf32(Char32[] utf32, Allocator allocator = allocator::heap()) +fn String! new_from_utf32(Allocator allocator, Char32[] utf32) { usz len = conv::utf8len_for_utf32(utf32); char* data = allocator::malloc_try(allocator, len + 1)!; @@ -698,7 +653,7 @@ fn String! new_from_utf32(Char32[] utf32, Allocator allocator = allocator::heap( return (String)data[:len]; } -fn String! new_from_utf16(Char16[] utf16, Allocator allocator = allocator::heap()) +fn String! new_from_utf16(Allocator allocator, Char16[] utf16) { usz len = conv::utf8len_for_utf16(utf16); char* data = allocator::malloc_try(allocator, len + 1)!; @@ -708,16 +663,16 @@ fn String! new_from_utf16(Char16[] utf16, Allocator allocator = allocator::heap( return (String)data[:len]; } -fn String! new_from_wstring(WString wstring, Allocator allocator = allocator::heap()) +fn String! new_from_wstring(Allocator allocator, WString wstring) { usz utf16_len; while (wstring[utf16_len] != 0) utf16_len++; Char16[] utf16 = wstring[:utf16_len]; - return new_from_utf16(utf16, allocator); + return new_from_utf16(allocator, utf16); } -fn String! temp_from_wstring(WString wstring) => new_from_wstring(wstring, allocator::temp()) @inline; -fn String! temp_from_utf16(Char16[] utf16) => new_from_utf16(utf16, allocator::temp()) @inline; +fn String! tnew_from_wstring(WString wstring) => new_from_wstring(tmem(), wstring) @inline; +fn String! tnew_from_utf16(Char16[] utf16) => new_from_utf16(tmem(), utf16) @inline; fn usz String.utf8_codepoints(s) { @@ -865,15 +820,15 @@ fn String! Splitter.next(&self) } } -macro String new_struct_to_str(x, Allocator allocator = allocator::heap()) +macro String new_from_struct(Allocator allocator, x) { DString s; @stack_mem(512; Allocator mem) { - s.new_init(allocator: mem); + s.init(allocator: mem); io::fprint(&s, x)!!; return s.copy_str(allocator); }; } -macro String temp_struct_to_str(x) => new_struct_to_str(x, allocator::temp()); +macro String tnew_from_struct(x) => new_from_struct(tmem(), x); diff --git a/lib7/std/encoding/csv.c3 b/lib7/std/encoding/csv.c3 index b0f9760d0..42e077a54 100644 --- a/lib7/std/encoding/csv.c3 +++ b/lib7/std/encoding/csv.c3 @@ -38,26 +38,20 @@ fn void CsvReader.init(&self, InStream stream, String separator = ",") self.stream = stream; self.separator = separator; } - -fn CsvRow! CsvReader.read_new_row(self) -{ - return self.read_row(allocator::heap()) @inline; -} - <* @param [&inout] allocator *> fn CsvRow! CsvReader.read_row(self, Allocator allocator) { - String row = io::readline(self.stream, allocator: allocator)!; + String row = io::readline(allocator, self.stream)!; defer catch allocator::free(allocator, row); - String[] list = row.split(self.separator, allocator: allocator); + String[] list = row.split(allocator, self.separator); return { list, row, allocator }; } -fn CsvRow! CsvReader.read_temp_row(self) +fn CsvRow! CsvReader.tread_row(self) { - return self.read_row(allocator::temp()) @inline; + return self.read_row(tmem()) @inline; } <* diff --git a/lib7/std/encoding/hex.c3 b/lib7/std/encoding/hex.c3 index d46653dbb..38ef23a81 100644 --- a/lib7/std/encoding/hex.c3 +++ b/lib7/std/encoding/hex.c3 @@ -13,22 +13,21 @@ fn char[]! decode_buffer(char[] code, char[] buffer) return buffer[:decode_bytes(code, buffer)!]; } -fn String encode(char[] code, Allocator allocator) +fn String encode(Allocator allocator, char[] code) { char[] data = allocator::alloc_array(allocator, char, encode_len(code.len)); return (String)data[:encode_bytes(code, data)]; } -fn char[]! decode(char[] code, Allocator allocator) +fn char[]! decode(Allocator allocator, char[] code) { char[] data = allocator::alloc_array(allocator, char, decode_len(code.len)); return data[:decode_bytes(code, data)!]; } -fn String encode_new(char[] code) @inline => encode(code, allocator::heap()); -fn String encode_temp(char[] code) @inline => encode(code, allocator::temp()); -fn char[]! decode_new(char[] code) @inline => decode(code, allocator::heap()); -fn char[]! decode_temp(char[] code) @inline => decode(code, allocator::temp()); +fn String tencode(char[] code) @inline => encode(tmem(), code); +fn char[]! tdecode(char[] code) @inline => decode(tmem(), code); + <* Calculate the size of the encoded data. diff --git a/lib7/std/encoding/json.c3 b/lib7/std/encoding/json.c3 index 4cea7cebf..a26bf87ec 100644 --- a/lib7/std/encoding/json.c3 +++ b/lib7/std/encoding/json.c3 @@ -15,21 +15,21 @@ fault JsonParsingError INVALID_NUMBER, } -fn Object*! parse_string(String s, Allocator allocator = allocator::heap()) +fn Object*! parse_string(Allocator allocator, String s) { - return parse((ByteReader){}.init(s), allocator); + return parse(allocator, (ByteReader){}.init(s)); } -fn Object*! temp_parse_string(String s) +fn Object*! tparse_string(String s) { - return parse((ByteReader){}.init(s), allocator::temp()); + return parse(tmem(), (ByteReader){}.init(s)); } -fn Object*! parse(InStream s, Allocator allocator = allocator::heap()) +fn Object*! parse(Allocator allocator, InStream s) { @stack_mem(512; Allocator mem) { - JsonContext context = { .last_string = dstring::new_with_capacity(64, mem), .stream = s, .allocator = allocator }; + JsonContext context = { .last_string = dstring::new_with_capacity(mem, 64), .stream = s, .allocator = allocator }; @pool(allocator) { return parse_any(&context); @@ -37,9 +37,9 @@ fn Object*! parse(InStream s, Allocator allocator = allocator::heap()) }; } -fn Object*! temp_parse(InStream s) +fn Object*! tparse(InStream s) { - return parse(s, allocator::temp()); + return parse(tmem(), s); } // -- Implementation follows -- @@ -106,7 +106,7 @@ fn JsonTokenType! lex_number(JsonContext *context, char c) @local { @stack_mem(256; Allocator mem) { - DString t = dstring::new_with_capacity(32, allocator: mem); + DString t = dstring::new_with_capacity(mem, 32); bool negate = c == '-'; if (negate) { @@ -159,7 +159,7 @@ fn Object*! parse_map(JsonContext* context) @local @stack_mem(256; Allocator mem) { - DString temp_key = dstring::new_with_capacity(32, mem); + DString temp_key = dstring::new_with_capacity(mem, 32); while (token != JsonTokenType.RBRACE) { if (token != JsonTokenType.STRING) return JsonParsingError.UNEXPECTED_CHARACTER?; diff --git a/lib7/std/io/file.c3 b/lib7/std/io/file.c3 index 0e27ca96a..9dd4100e1 100644 --- a/lib7/std/io/file.c3 +++ b/lib7/std/io/file.c3 @@ -21,7 +21,7 @@ fn File! open_path(Path path, String mode) fn bool exists(String file) => @pool() { - return path::exists(path::temp_new(file)) ?? false; + return path::exists(path::tnew(file)) ?? false; } fn File from_handle(CFile file) diff --git a/lib7/std/io/io.c3 b/lib7/std/io/io.c3 index b04ab12e3..fa31ff35f 100644 --- a/lib7/std/io/io.c3 +++ b/lib7/std/io/io.c3 @@ -55,7 +55,7 @@ fault IoError @param [inout] allocator `the allocator to use.` @return `The string containing the data read.` *> -macro String! readline(stream = io::stdin(), Allocator allocator = allocator::heap()) +macro String! readline(Allocator allocator, stream = io::stdin()) { bool $is_stream = @typeis(stream, InStream); $if $is_stream: @@ -68,7 +68,7 @@ macro String! readline(stream = io::stdin(), Allocator allocator = allocator::he if (val == '\n') return ""; @pool(allocator) { - DString str = dstring::temp_with_capacity(256); + DString str = dstring::tnew_with_capacity(256); if (val != '\r') str.append(val); while (1) { @@ -100,7 +100,7 @@ macro String! readline(stream = io::stdin(), Allocator allocator = allocator::he *> macro String! treadline(stream = io::stdin()) { - return readline(stream, allocator::temp()) @inline; + return readline(tmem(), stream) @inline; } <* diff --git a/lib7/std/io/os/chdir.c3 b/lib7/std/io/os/chdir.c3 index 1356edcb2..c2a57ac52 100644 --- a/lib7/std/io/os/chdir.c3 +++ b/lib7/std/io/os/chdir.c3 @@ -21,7 +21,7 @@ macro void! native_chdir(Path path) @pool() { // TODO improve with better error handling. - if (win32::setCurrentDirectoryW(path.str_view().to_temp_utf16()!!)) return; + if (win32::setCurrentDirectoryW(path.str_view().to_utf16_tcopy()!!)) return; }; return IoError.GENERAL_ERROR?; $default: diff --git a/lib7/std/io/os/file_libc.c3 b/lib7/std/io/os/file_libc.c3 index 72a428317..8aaa8d94f 100644 --- a/lib7/std/io/os/file_libc.c3 +++ b/lib7/std/io/os/file_libc.c3 @@ -8,7 +8,7 @@ import libc; fn void*! native_fopen(String filename, String mode) @inline => @pool() { $if env::WIN32: - void* file = libc::_wfopen(filename.to_temp_wstring(), mode.to_temp_wstring())!; + void* file = libc::_wfopen(filename.to_wstring_tcopy(), mode.to_wstring_tcopy())!; $else void* file = libc::fopen(filename.zstr_tcopy(), mode.zstr_tcopy()); $endif @@ -18,7 +18,7 @@ fn void*! native_fopen(String filename, String mode) @inline => @pool() fn void! native_remove(String filename) => @pool() { $if env::WIN32: - CInt result = libc::_wremove(filename.to_temp_wstring())!; + CInt result = libc::_wremove(filename.to_wstring_tcopy())!; $else CInt result = libc::remove(filename.zstr_tcopy()); $endif @@ -42,7 +42,7 @@ fn void! native_remove(String filename) => @pool() fn void*! native_freopen(void* file, String filename, String mode) @inline => @pool() { $if env::WIN32: - file = libc::_wfreopen(filename.to_temp_wstring(), mode.to_temp_wstring(), file)!; + file = libc::_wfreopen(filename.to_wstring_tcopy(), mode.to_wstring_tcopy(), file)!; $else file = libc::freopen(filename.zstr_tcopy(), mode.zstr_tcopy(), file); $endif diff --git a/lib7/std/io/os/fileinfo.c3 b/lib7/std/io/os/fileinfo.c3 index f24fafb4c..ab6d3a8e8 100644 --- a/lib7/std/io/os/fileinfo.c3 +++ b/lib7/std/io/os/fileinfo.c3 @@ -40,7 +40,7 @@ fn void! native_stat(Stat* stat, String path) @if(env::DARWIN || env::LINUX || e fn usz! native_file_size(String path) @if(env::WIN32) => @pool() { Win32_FILE_ATTRIBUTE_DATA data; - win32::getFileAttributesExW(path.to_temp_wstring()!, Win32_GET_FILEEX_INFO_LEVELS.STANDARD, &data); + win32::getFileAttributesExW(path.to_wstring_tcopy()!, Win32_GET_FILEEX_INFO_LEVELS.STANDARD, &data); Win32_LARGE_INTEGER size; size.lowPart = data.nFileSizeLow; size.highPart = data.nFileSizeHigh; @@ -74,7 +74,7 @@ fn bool native_file_or_dir_exists(String path) $case env::WIN32: @pool() { - return (bool)win32::pathFileExistsW(path.to_temp_utf16()) ?? false; + return (bool)win32::pathFileExistsW(path.to_utf16_tcopy()) ?? false; }; $case env::POSIX: @pool() diff --git a/lib7/std/io/os/getcwd.c3 b/lib7/std/io/os/getcwd.c3 index 9e58c7093..69f649612 100644 --- a/lib7/std/io/os/getcwd.c3 +++ b/lib7/std/io/os/getcwd.c3 @@ -17,7 +17,7 @@ macro String! getcwd(Allocator allocator = allocator::heap()) free = true; } Char16[] str16 = res[:win32::wcslen(res)]; - return string::new_from_utf16(str16, allocator); + return string::new_from_utf16(allocator, str16); $case env::POSIX: const usz DEFAULT_BUFFER = 256; diff --git a/lib7/std/io/os/ls.c3 b/lib7/std/io/os/ls.c3 index 7a51865e8..a3008bd0b 100644 --- a/lib7/std/io/os/ls.c3 +++ b/lib7/std/io/os/ls.c3 @@ -15,7 +15,7 @@ fn PathList! native_ls(Path dir, bool no_dirs, bool no_symlinks, String mask, Al if (!name || name == "." || name == "..") continue; if (entry.d_type == posix::DT_LNK && no_symlinks) continue; if (entry.d_type == posix::DT_DIR && no_dirs) continue; - Path path = path::new(name, allocator)!!; + Path path = path::new(allocator, name)!!; list.push(path); } return list; @@ -31,7 +31,7 @@ fn PathList! native_ls(Path dir, bool no_dirs, bool no_symlinks, String mask, Al @pool(allocator) { - WString result = dir.str_view().tconcat(`\*`).to_temp_wstring()!!; + WString result = dir.str_view().tconcat(`\*`).to_wstring_tcopy()!!; Win32_WIN32_FIND_DATAW find_data; Win32_HANDLE find = win32::findFirstFileW(result, &find_data); if (find == win32::INVALID_HANDLE_VALUE) return IoError.CANNOT_READ_DIR?; @@ -41,9 +41,9 @@ fn PathList! native_ls(Path dir, bool no_dirs, bool no_symlinks, String mask, Al if (no_dirs && (find_data.dwFileAttributes & win32::FILE_ATTRIBUTE_DIRECTORY)) continue; @pool(allocator) { - String filename = string::temp_from_wstring((WString)&find_data.cFileName)!; + String filename = string::tnew_from_wstring((WString)&find_data.cFileName)!; if (filename == ".." || filename == ".") continue; - list.push(path::new(filename, allocator)!); + list.push(path::new(allocator, filename)!); }; } while (win32::findNextFileW(find, &find_data)); return list; diff --git a/lib7/std/io/os/mkdir.c3 b/lib7/std/io/os/mkdir.c3 index 1a2b4b5f4..385c00338 100644 --- a/lib7/std/io/os/mkdir.c3 +++ b/lib7/std/io/os/mkdir.c3 @@ -29,7 +29,7 @@ macro bool! native_mkdir(Path path, MkdirPermissions permissions) @pool() { // TODO security attributes - if (win32::createDirectoryW(path.str_view().to_temp_utf16()!!, null)) return true; + if (win32::createDirectoryW(path.str_view().to_utf16_tcopy()!!, null)) return true; switch (win32::getLastError()) { case win32::ERROR_ACCESS_DENIED: diff --git a/lib7/std/io/os/rmdir.c3 b/lib7/std/io/os/rmdir.c3 index 80f8f5dc6..970b1d319 100644 --- a/lib7/std/io/os/rmdir.c3 +++ b/lib7/std/io/os/rmdir.c3 @@ -26,7 +26,7 @@ macro bool! native_rmdir(Path path) $case env::WIN32: @pool() { - if (win32::removeDirectoryW(path.str_view().to_temp_utf16()!!)) return true; + if (win32::removeDirectoryW(path.str_view().to_utf16_tcopy()!!)) return true; switch (win32::getLastError()) { case win32::ERROR_ACCESS_DENIED: diff --git a/lib7/std/io/os/rmtree.c3 b/lib7/std/io/os/rmtree.c3 index 500be1bb9..866e89e34 100644 --- a/lib7/std/io/os/rmtree.c3 +++ b/lib7/std/io/os/rmtree.c3 @@ -16,7 +16,7 @@ fn void! native_rmtree(Path dir) { String name = ((ZString)&entry.name).str_view(); if (!name || name == "." || name == "..") continue; - Path new_path = dir.temp_append(name)!; + Path new_path = dir.tappend(name)!; if (entry.d_type == posix::DT_DIR) { native_rmtree(new_path)!; @@ -39,7 +39,7 @@ fn void! native_rmtree(Path path) { Win32_WIN32_FIND_DATAW find_data; String s = path.str_view().tconcat("\\*"); - Win32_HANDLE find = win32::findFirstFileW(s.to_temp_utf16(), &find_data)!; + Win32_HANDLE find = win32::findFirstFileW(s.to_utf16_tcopy(), &find_data)!; if (find == win32::INVALID_HANDLE_VALUE) return IoError.CANNOT_READ_DIR?; defer win32::findClose(find); @@ -47,16 +47,16 @@ fn void! native_rmtree(Path path) { @pool() { - String filename = string::new_from_wstring((WString)&find_data.cFileName, allocator::temp())!; + String filename = string::new_from_wstring(tmem(), (WString)&find_data.cFileName)!; if (filename == "." || filename == "..") continue; - Path file_path = path.temp_append(filename)!; + Path file_path = path.tappend(filename)!; if (find_data.dwFileAttributes & win32::FILE_ATTRIBUTE_DIRECTORY) { native_rmtree(file_path)!; } else { - win32::deleteFileW(file_path.str_view().to_temp_wstring()!!); + win32::deleteFileW(file_path.str_view().to_wstring_tcopy()!!); } }; } while (win32::findNextFileW(find, &find_data) != 0); diff --git a/lib7/std/io/os/temp_directory.c3 b/lib7/std/io/os/temp_directory.c3 index 1a0659d99..0f53880e8 100644 --- a/lib7/std/io/os/temp_directory.c3 +++ b/lib7/std/io/os/temp_directory.c3 @@ -1,23 +1,23 @@ module std::io::os @if(env::LIBC); import std::io::path, std::os; -fn Path! native_temp_directory(Allocator allocator = allocator::heap()) @if(!env::WIN32) +fn Path! native_temp_directory(Allocator allocator) @if(!env::WIN32) { foreach (String env : { "TMPDIR", "TMP", "TEMP", "TEMPDIR" }) { - String tmpdir = env::get_var(env) ?? ""; - if (tmpdir) return path::new(tmpdir, allocator); + String tmpdir = env::tget_var(env) ?? ""; + if (tmpdir) return path::new(allocator, tmpdir); } - return path::new("/tmp", allocator); + return path::new(allocator, "/tmp"); } -fn Path! native_temp_directory(Allocator allocator = allocator::heap()) @if(env::WIN32) => @pool(allocator) +fn Path! native_temp_directory(Allocator allocator) @if(env::WIN32) => @pool(allocator) { Win32_DWORD len = win32::getTempPathW(0, null); if (!len) return IoError.GENERAL_ERROR?; Char16[] buff = mem::temp_alloc_array(Char16, len + (usz)1); if (!win32::getTempPathW(len, buff)) return IoError.GENERAL_ERROR?; - return path::new(string::temp_from_utf16(buff[:len]), allocator); + return path::new(allocator, string::tnew_from_utf16(buff[:len])); } module std::io::os @if(env::NO_LIBC); diff --git a/lib7/std/io/path.c3 b/lib7/std/io/path.c3 index 39f419866..4c40f80e5 100644 --- a/lib7/std/io/path.c3 +++ b/lib7/std/io/path.c3 @@ -29,16 +29,11 @@ enum PathEnv POSIX } -fn Path! new_cwd(Allocator allocator = allocator::heap()) => @pool(allocator) -{ - return new(os::getcwd(allocator::temp()), allocator); -} - -fn Path! getcwd(Allocator allocator = allocator::heap()) @deprecated("Use new_cwd()") +fn Path! cwd(Allocator allocator) { @pool(allocator) { - return new(os::getcwd(allocator::temp()), allocator); + return new(allocator, os::getcwd(tmem())); }; } @@ -46,10 +41,9 @@ fn bool is_dir(Path path) => os::native_is_dir(path.str_view()); fn bool is_file(Path path) => os::native_is_file(path.str_view()); fn usz! file_size(Path path) => os::native_file_size(path.str_view()); fn bool exists(Path path) => os::native_file_or_dir_exists(path.str_view()); -fn Path! temp_cwd() => new_cwd(allocator::temp()) @inline; -fn Path! tgetcwd() @deprecated("Use temp_cwd()") => new_cwd(allocator::temp()) @inline; +fn Path! tcwd() => cwd(tmem()) @inline; fn void! chdir(Path path) => os::native_chdir(path) @inline; -fn Path! temp_directory(Allocator allocator = allocator::heap()) => os::native_temp_directory(allocator); +fn Path! temp_directory(Allocator allocator) => os::native_temp_directory(allocator); fn void! delete(Path path) => os::native_remove(path.str_view()) @inline; macro bool is_separator(char c, PathEnv path_env = DEFAULT_PATH_ENV) @@ -67,17 +61,7 @@ macro bool is_win32_separator(char c) return c == '/' || c == '\\'; } -fn PathList! ls(Path dir, bool no_dirs = false, bool no_symlinks = false, String mask = "", Allocator allocator = allocator::heap()) @deprecated("use new_ls") -{ - return new_ls(dir, no_dirs, no_symlinks, mask, allocator); - -} -fn PathList! temp_ls(Path dir, bool no_dirs = false, bool no_symlinks = false, String mask = "") -{ - return new_ls(dir, no_dirs, no_symlinks, mask, allocator::temp()) @inline; -} - -fn PathList! new_ls(Path dir, bool no_dirs = false, bool no_symlinks = false, String mask = "", Allocator allocator = allocator::heap()) +fn PathList! ls(Allocator allocator, Path dir, bool no_dirs = false, bool no_symlinks = false, String mask = "") { $if $defined(os::native_ls): return os::native_ls(dir, no_dirs, no_symlinks, mask, allocator); @@ -146,7 +130,7 @@ fn void! rmtree(Path path) @return! PathResult.INVALID_PATH `if the path was invalid` *> -fn Path! new(String path, Allocator allocator = allocator::heap(), PathEnv path_env = DEFAULT_PATH_ENV) +fn Path! new(Allocator allocator, String path, PathEnv path_env = DEFAULT_PATH_ENV) { return { normalize(path.copy(allocator), path_env), path_env }; } @@ -156,24 +140,24 @@ fn Path! new(String path, Allocator allocator = allocator::heap(), PathEnv path_ @return! PathResult.INVALID_PATH `if the path was invalid` *> -fn Path! temp_new(String path, PathEnv path_env = DEFAULT_PATH_ENV) +fn Path! tnew(String path, PathEnv path_env = DEFAULT_PATH_ENV) { - return new(path, allocator::temp(), path_env); + return new(tmem(), path, path_env); } -fn Path! new_win32_wstring(WString path, Allocator allocator = allocator::heap()) => @pool(allocator) +fn Path! from_win32_wstring(Allocator allocator, WString path) => @pool(allocator) { - return path::new(string::temp_from_wstring(path)!, allocator: allocator); + return path::new(allocator, string::tnew_from_wstring(path)!); } -fn Path! new_windows(String path, Allocator allocator = allocator::heap()) +fn Path! for_windows(Allocator allocator, String path) { - return new(path, allocator, WIN32); + return new(allocator, path, WIN32); } -fn Path! new_posix(String path, Allocator allocator = allocator::heap()) +fn Path! for_posix(Allocator allocator, String path) { - return new(path, allocator, POSIX); + return new(allocator, path, POSIX); } fn bool Path.equals(self, Path p2) @@ -181,24 +165,19 @@ fn bool Path.equals(self, Path p2) return self.env == p2.env && self.path_string == p2.path_string; } -fn Path! Path.append(self, String filename, Allocator allocator = allocator::heap()) @deprecated("Use path.new_append(...)") -{ - return self.new_append(filename, allocator) @inline; -} - <* Append the string to the current path. @param [in] filename *> -fn Path! Path.new_append(self, String filename, Allocator allocator = allocator::heap()) +fn Path! Path.append(self, Allocator allocator, String filename) { - if (!self.path_string.len) return new(filename, allocator, self.env)!; + if (!self.path_string.len) return new(allocator, filename, self.env)!; assert(!is_separator(self.path_string[^1], self.env)); @pool(allocator) { - DString dstr = dstring::temp_with_capacity(self.path_string.len + 1 + filename.len); + DString dstr = dstring::tnew_with_capacity(self.path_string.len + 1 + filename.len); dstr.append(self.path_string); dstr.append(PREFERRED_SEPARATOR); dstr.append(filename); @@ -206,9 +185,7 @@ fn Path! Path.new_append(self, String filename, Allocator allocator = allocator: }; } -fn Path! Path.temp_append(self, String filename) => self.new_append(filename, allocator::temp()); - -fn Path! Path.tappend(self, String filename) @deprecated("Use path.temp_append(...)") => self.new_append(filename, allocator::temp()); +fn Path! Path.tappend(self, String filename) => self.append(tmem(), filename); fn usz Path.start_of_base_name(self) @local { @@ -242,25 +219,21 @@ fn bool! Path.is_absolute(self) return path_start < path_str.len && is_separator(path_str[path_start], self.env); } -fn Path! Path.absolute(self, Allocator allocator = allocator::heap()) @deprecated("Use path.new_absolute()") -{ - return self.new_absolute(allocator) @inline; -} <* @require self.env == DEFAULT_PATH_ENV : "This method is only available on native paths" *> -fn Path! Path.new_absolute(self, Allocator allocator = allocator::heap()) +fn Path! Path.absolute(self, Allocator allocator) { String path_str = self.str_view(); if (!path_str.len) return PathResult.INVALID_PATH?; - if (self.is_absolute()!) return new(path_str, allocator, self.env); + if (self.is_absolute()!) return new(allocator, path_str, self.env); if (path_str == ".") { @pool(allocator) { - String cwd = os::getcwd(allocator::temp())!; - return new(cwd, allocator, self.env); + String cwd = os::getcwd(tmem())!; + return new(allocator, cwd, self.env); }; } $if DEFAULT_PATH_ENV == WIN32: @@ -268,13 +241,13 @@ fn Path! Path.new_absolute(self, Allocator allocator = allocator::heap()) { const usz BUFFER_LEN = 4096; WString buffer = (WString)mem::temp_alloc_array(Char16, BUFFER_LEN); - buffer = win32::_wfullpath(buffer, path_str.to_temp_wstring()!, BUFFER_LEN); + buffer = win32::_wfullpath(buffer, path_str.to_wstring_tcopy()!, BUFFER_LEN); if (!buffer) return PathResult.INVALID_PATH?; - return { string::new_from_wstring(buffer, allocator), WIN32 }; + return { string::new_from_wstring(allocator, buffer), WIN32 }; }; $else - String cwd = os::getcwd(allocator::temp())!; - return (Path){ cwd, self.env }.new_append(path_str, allocator)!; + String cwd = os::getcwd(tmem())!; + return (Path){ cwd, self.env }.append(allocator, path_str)!; $endif } @@ -551,12 +524,12 @@ fn bool! Path.walk(self, PathWalker w, void* data) const PATH_MAX = 512; @stack_mem(PATH_MAX; Allocator allocator) { - Path abs = self.new_absolute(allocator)!; - PathList files = new_ls(abs, allocator: allocator)!; + Path abs = self.absolute(allocator)!; + PathList files = ls(allocator, abs)!; foreach (f : files) { if (f.str_view() == "." || f.str_view() == "..") continue; - f = abs.new_append(f.str_view(), allocator)!; + f = abs.append(allocator, f.str_view())!; bool is_directory = is_dir(f); if (w(f, is_directory, data)!) return true; if (is_directory && f.walk(w, data)!) return true; diff --git a/lib7/std/math/bigint.c3 b/lib7/std/math/bigint.c3 index 79462a27d..768a1f033 100644 --- a/lib7/std/math/bigint.c3 +++ b/lib7/std/math/bigint.c3 @@ -525,7 +525,7 @@ fn String BigInt.to_string_with_radix(&self, int radix, Allocator allocator) { BigInt a = *self; DString str; - str.new_init(4096, allocator: mem); + str.init(mem, 4096); bool negative = self.is_negative(); if (negative) { diff --git a/lib7/std/math/random/math.seeder.c3 b/lib7/std/math/random/math.seeder.c3 index 3c4449ddb..84de920bf 100644 --- a/lib7/std/math/random/math.seeder.c3 +++ b/lib7/std/math/random/math.seeder.c3 @@ -86,7 +86,7 @@ fn char[8 * 4] entropy() @if(!env::WASM_NOLIBC) hash(&entropy), random_int, hash(clock::now()), - hash(&DString.new_init), + hash(&DString.init), hash(allocator::heap()) }; return bitcast(entropy_data, char[8 * 4]); diff --git a/lib7/std/math/uuid.c3 b/lib7/std/math/uuid.c3 index 8d309c2c6..f5648bc27 100644 --- a/lib7/std/math/uuid.c3 +++ b/lib7/std/math/uuid.c3 @@ -35,7 +35,7 @@ fn usz! Uuid.to_format(&self, Formatter* formatter) @dynamic (*self)[10], (*self)[11], (*self)[12], (*self)[13], (*self)[14], (*self)[15]); } -fn String Uuid.to_string(&self, Allocator allocator) @dynamic +fn String Uuid.to_string(&self, Allocator allocator) { - return string::new_format("%s", *self, allocator: allocator); + return string::format(allocator, "%s", *self); } \ No newline at end of file diff --git a/lib7/std/net/inetaddr.c3 b/lib7/std/net/inetaddr.c3 index b146d14fa..26c8f4650 100644 --- a/lib7/std/net/inetaddr.c3 +++ b/lib7/std/net/inetaddr.c3 @@ -260,7 +260,7 @@ fn bool InetAddress.is_multicast_link_local(InetAddress* addr) fn AddrInfo*! addrinfo(String host, uint port, AIFamily ai_family, AISockType ai_socktype) @if(os::SUPPORTS_INET) => @pool() { ZString zhost = host.zstr_tcopy(); - DString str = dstring::temp_with_capacity(32); + DString str = dstring::tnew_with_capacity(32); str.appendf("%d", port); AddrInfo hints = { .ai_family = ai_family, .ai_socktype = ai_socktype }; AddrInfo* ai; diff --git a/lib7/std/net/url.c3 b/lib7/std/net/url.c3 index b41f1ab87..b242d54c8 100644 --- a/lib7/std/net/url.c3 +++ b/lib7/std/net/url.c3 @@ -49,7 +49,7 @@ struct Url(Printable) @require url_string.len > 0 "the url_string must be len 1 or more" @return "the parsed Url" *> -fn Url! temp_parse(String url_string) => new_parse(url_string, allocator::temp()); +fn Url! tparse(String url_string) => parse(tmem(), url_string); <* Parse a URL string into a Url struct. @@ -58,7 +58,7 @@ fn Url! temp_parse(String url_string) => new_parse(url_string, allocator::temp() @require url_string.len > 0 "the url_string must be len 1 or more" @return "the parsed Url" *> -fn Url! new_parse(String url_string, Allocator allocator = allocator::heap()) +fn Url! parse(Allocator allocator, String url_string) { url_string = url_string.trim(); if (!url_string) return UrlParsingResult.EMPTY?; @@ -76,7 +76,7 @@ fn Url! new_parse(String url_string, Allocator allocator = allocator::heap()) // Handle schemes without authority like 'mailto:' if (!pos) return UrlParsingResult.INVALID_SCHEME?; url.scheme = url_string[:pos].copy(allocator); - url.path = decode(url_string[pos + 1 ..], PATH, allocator) ?? UrlParsingResult.INVALID_PATH?!; + url.path = decode(allocator, url_string[pos + 1 ..], PATH) ?? UrlParsingResult.INVALID_PATH?!; return url; } @@ -98,8 +98,8 @@ fn Url! new_parse(String url_string, Allocator allocator = allocator::heap()) if (!username.len) return UrlParsingResult.INVALID_USER?; url.host = - url.username = decode(username, HOST, allocator) ?? UrlParsingResult.INVALID_USER?!; - if (userpass.len) url.password = decode(userpass[1], USERPASS, allocator) ?? UrlParsingResult.INVALID_PASSWORD?!; + url.username = decode(allocator, username, HOST) ?? UrlParsingResult.INVALID_USER?!; + if (userpass.len) url.password = decode(allocator, userpass[1], USERPASS) ?? UrlParsingResult.INVALID_PASSWORD?!; }; authority = authority[userinfo.len + 1 ..]; } @@ -131,7 +131,7 @@ fn Url! new_parse(String url_string, Allocator allocator = allocator::heap()) } }; } - url.host = decode(host, HOST, allocator) ?? UrlParsingResult.INVALID_HOST?!; + url.host = decode(allocator, host, HOST) ?? UrlParsingResult.INVALID_HOST?!; url_string = url_string[authority_end ..]; } @@ -142,12 +142,12 @@ fn Url! new_parse(String url_string, Allocator allocator = allocator::heap()) if (@ok(query_index) || @ok(fragment_index)) { usz path_end = min(query_index ?? url_string.len, fragment_index ?? url_string.len); - url.path = decode(url_string[:path_end], PATH, allocator) ?? UrlParsingResult.INVALID_PATH?!; + url.path = decode(allocator, url_string[:path_end], PATH) ?? UrlParsingResult.INVALID_PATH?!; url_string = url_string[path_end ..]; } else { - url.path = decode(url_string, PATH, allocator) ?? UrlParsingResult.INVALID_PATH?!; + url.path = decode(allocator, url_string, PATH) ?? UrlParsingResult.INVALID_PATH?!; url_string = ""; } @@ -165,7 +165,7 @@ fn Url! new_parse(String url_string, Allocator allocator = allocator::heap()) // Parse fragment if (url_string.starts_with("#")) { - url.fragment = decode(url_string[1..], FRAGMENT, allocator) ?? UrlParsingResult.INVALID_FRAGMENT?!; + url.fragment = decode(allocator, url_string[1..], FRAGMENT) ?? UrlParsingResult.INVALID_FRAGMENT?!; } return url; } @@ -179,7 +179,7 @@ fn Url! new_parse(String url_string, Allocator allocator = allocator::heap()) *> fn String Url.to_string(&self, Allocator allocator = allocator::heap()) @dynamic => @pool(allocator) { - DString builder = dstring::temp_new(); + DString builder = dstring::tnew(); // Add scheme if it exists if (self.scheme != "") @@ -192,21 +192,21 @@ fn String Url.to_string(&self, Allocator allocator = allocator::heap()) @dynamic // Add username and password if they exist if (self.username != "") { - String username = temp_encode(self.username, USERPASS); + String username = tencode(self.username, USERPASS); builder.append_chars(username); if (self.password != "") { builder.append_char(':'); - String password = temp_encode(self.password, USERPASS); + String password = tencode(self.password, USERPASS); builder.append_chars(password); } builder.append_char('@'); } // Add host - String host = temp_encode(self.host, HOST); + String host = tencode(self.host, HOST); builder.append_chars(host); // Add port @@ -217,7 +217,7 @@ fn String Url.to_string(&self, Allocator allocator = allocator::heap()) @dynamic } // Add path - String path = temp_encode(self.path, PATH); + String path = tencode(self.path, PATH); builder.append_chars(path); // Add query if it exists (note that `query` is expected to @@ -233,7 +233,7 @@ fn String Url.to_string(&self, Allocator allocator = allocator::heap()) @dynamic { builder.append_char('#'); - String fragment = temp_encode(self.fragment, FRAGMENT); + String fragment = tencode(self.fragment, FRAGMENT); builder.append_chars(fragment); } @@ -283,8 +283,8 @@ fn UrlQueryValues parse_query(String query, Allocator allocator) @pool(allocator) { String[] parts = rv.tsplit("=", 2); - String key = temp_decode(parts[0], QUERY) ?? parts[0]; - vals.add(key, parts.len == 1 ? key : (temp_decode(parts[1], QUERY) ?? parts[1])); + String key = tdecode(parts[0], QUERY) ?? parts[0]; + vals.add(key, parts.len == 1 ? key : (tdecode(parts[1], QUERY) ?? parts[1])); }; } return vals; @@ -326,12 +326,12 @@ fn UrlQueryValues* UrlQueryValues.add(&self, String key, String value) *> fn String UrlQueryValues.to_string(&self, Allocator allocator = allocator::heap()) @dynamic => @pool(allocator) { - DString builder = dstring::temp_new(); + DString builder = dstring::tnew(); usz i; foreach (key: self.key_order) { - String encoded_key = temp_encode(key, QUERY); + String encoded_key = tencode(key, QUERY); UrlQueryValueList! values = self.map.get(key); if (catch values) continue; @@ -343,7 +343,7 @@ fn String UrlQueryValues.to_string(&self, Allocator allocator = allocator::heap( builder.append_chars(encoded_key); builder.append_char('='); - String encoded_value = temp_encode(value, QUERY); + String encoded_value = tencode(value, QUERY); builder.append_chars(encoded_value); i++; } diff --git a/lib7/std/net/url_encoding.c3 b/lib7/std/net/url_encoding.c3 index 314a85b0e..e1baee050 100644 --- a/lib7/std/net/url_encoding.c3 +++ b/lib7/std/net/url_encoding.c3 @@ -67,10 +67,10 @@ fn usz encode_len(String s, UrlEncodingMode mode) @inline @param [inout] allocator @return "Percent-encoded String" *> -fn String encode(String s, UrlEncodingMode mode, Allocator allocator) => @pool(allocator) +fn String encode(Allocator allocator, String s, UrlEncodingMode mode) => @pool(allocator) { usz n = encode_len(s, mode); - DString builder = dstring::temp_with_capacity(n); + DString builder = dstring::tnew_with_capacity(n); foreach(i, c: s) { @@ -83,8 +83,8 @@ fn String encode(String s, UrlEncodingMode mode, Allocator allocator) => @pool(a // add encoded char case should_encode(c, mode): builder.append_char('%'); - String hex = hex::encode_temp(s[i:1]); - builder.append(hex.temp_ascii_to_upper()); + String hex = hex::tencode(s[i:1]); + builder.append(hex.to_upper_tcopy()); // use char, no encoding needed default: @@ -95,15 +95,6 @@ fn String encode(String s, UrlEncodingMode mode, Allocator allocator) => @pool(a return builder.copy_str(allocator); } -<* - Encode the string s for a given encoding mode. - Returned string must be freed. - - @param s "String to encode" - @param mode "Url encoding mode" - @return "Percent-encoded String" -*> -fn String new_encode(String s, UrlEncodingMode mode) => encode(s, mode, allocator::heap()); <* Encode string s for a given encoding mode, stored on the temp allocator. @@ -112,7 +103,7 @@ fn String new_encode(String s, UrlEncodingMode mode) => encode(s, mode, allocato @param mode "Url encoding mode" @return "Percent-encoded String" *> -fn String temp_encode(String s, UrlEncodingMode mode) => encode(s, mode, allocator::temp()); +fn String tencode(String s, UrlEncodingMode mode) => encode(tmem(), s, mode); <* Calculate the length of the percent-decoded string. @@ -143,10 +134,10 @@ fn usz! decode_len(String s, UrlEncodingMode mode) @inline @param [inout] allocator @return "Percent-decoded String" *> -fn String! decode(String s, UrlEncodingMode mode, Allocator allocator) => @pool(allocator) +fn String! decode(Allocator allocator, String s, UrlEncodingMode mode) => @pool(allocator) { usz n = decode_len(s, mode)!; - DString builder = dstring::temp_with_capacity(n); + DString builder = dstring::tnew_with_capacity(n); for (usz i = 0; i < s.len; i++) { @@ -154,7 +145,7 @@ fn String! decode(String s, UrlEncodingMode mode, Allocator allocator) => @pool { // decode encoded char case '%': - char[] hex = hex::decode_temp(s[i+1:2])!; + char[] hex = hex::tdecode(s[i+1:2])!; builder.append(hex); i += 2; @@ -171,15 +162,6 @@ fn String! decode(String s, UrlEncodingMode mode, Allocator allocator) => @pool return builder.copy_str(allocator); } -<* - Decode string s for a given encoding mode. - Returned string must be freed. - - @param s "String to decode" - @param mode "Url encoding mode" - @return "Percent-decoded String" -*> -fn String! new_decode(String s, UrlEncodingMode mode) => decode(s, mode, allocator::heap()); <* Decode string s for a given encoding mode, stored on the temp allocator. @@ -188,4 +170,4 @@ fn String! new_decode(String s, UrlEncodingMode mode) => decode(s, mode, alloca @param mode "Url encoding mode" @return "Percent-decoded String" *> -fn String! temp_decode(String s, UrlEncodingMode mode) => decode(s, mode, allocator::temp()); +fn String! tdecode(String s, UrlEncodingMode mode) => decode(tmem(), s, mode); diff --git a/lib7/std/os/env.c3 b/lib7/std/os/env.c3 index c4b23e88d..bbf62bafe 100644 --- a/lib7/std/os/env.c3 +++ b/lib7/std/os/env.c3 @@ -9,7 +9,7 @@ import std::io::path, libc, std::os; @require name.len > 0 @return! SearchResult.MISSING *> -fn String! get_var(String name, Allocator allocator = allocator::heap()) => @pool(allocator) +fn String! get_var(Allocator allocator, String name) => @pool(allocator) { $switch @@ -20,7 +20,7 @@ fn String! get_var(String name, Allocator allocator = allocator::heap()) => @poo // https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-getenvironmentvariable const usz BUFSIZE = 1024; WString buff = (WString)tcalloc(BUFSIZE * 2 + 2); - WString wstr = name.to_temp_wstring()!; + WString wstr = name.to_wstring_tcopy()!; usz len = win32::getEnvironmentVariableW(wstr, buff, BUFSIZE); if (len == 0) return SearchResult.MISSING?; if (len > BUFSIZE) @@ -28,15 +28,15 @@ fn String! get_var(String name, Allocator allocator = allocator::heap()) => @poo buff = (WString)tmalloc(len * 2 + 2); win32::getEnvironmentVariableW(wstr, buff, (Win32_DWORD)len); } - return string::new_from_wstring(buff, allocator); + return string::new_from_wstring(allocator, buff); $default: return ""; $endswitch } -fn String! get_var_temp(String name) +fn String! tget_var(String name) { - return get_var(name, allocator::temp()); + return get_var(tmem(), name); } <* @@ -48,14 +48,14 @@ fn bool set_var(String name, String value, bool overwrite = true) => @pool() { $switch $case env::WIN32: - WString wname = name.to_temp_wstring()!!; + WString wname = name.to_wstring_tcopy()!!; if (!overwrite) { Char16[8] buff; if (win32::getEnvironmentVariableW(wname, &buff, 8) > 0) return true; } // https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-setenvironmentvariable - return (win32::setEnvironmentVariableW(wname, value.to_temp_wstring()) ?? 1) == 0; + return (win32::setEnvironmentVariableW(wname, value.to_wstring_tcopy()) ?? 1) == 0; $case env::LIBC && !env::WIN32: return libc::setenv(name.zstr_tcopy(), value.zstr_tcopy(), (int)overwrite) == 0; $default: @@ -74,7 +74,7 @@ fn String! get_home_dir(Allocator using = allocator::heap()) $else home = "USERPROFILE"; $endif - return get_var(home, using); + return get_var(using, home); } fn Path! get_config_dir(Allocator allocator = allocator::heap()) @deprecated("use new_get_config_dir()") @@ -85,19 +85,19 @@ fn Path! get_config_dir(Allocator allocator = allocator::heap()) @deprecated("us <* Returns the current user's config directory. *> -fn Path! new_get_config_dir(Allocator allocator = allocator::heap()) => @pool(allocator) +fn Path! new_get_config_dir(Allocator allocator) => @pool(allocator) { $if env::WIN32: - return path::new(get_var_temp("AppData"), allocator); + return path::new(allocator, tget_var("AppData")); $else $if env::DARWIN: - String s = get_var_temp("HOME")!; + String s = tget_var("HOME")!; const DIR = "Library/Application Support"; $else - String s = get_var_temp("XDG_CONFIG_HOME") ?? get_var_temp("HOME")!; + String s = tget_var("XDG_CONFIG_HOME") ?? tget_var("HOME")!; const DIR = ".config"; $endif - return path::temp_new(s).new_append(DIR, allocator: allocator); + return path::tnew(s).append(allocator, DIR); $endif } @@ -110,7 +110,7 @@ fn bool clear_var(String name) => @pool() { $switch $case env::WIN32: - WString wname = name.to_temp_wstring()!!; + WString wname = name.to_wstring_tcopy()!!; return win32::setEnvironmentVariableW(wname, null) == 0; $case env::LIBC && !env::WIN32: return libc::unsetenv(name.zstr_tcopy()) == 0; @@ -119,16 +119,11 @@ fn bool clear_var(String name) => @pool() $endswitch } -fn String! executable_path(Allocator allocator = allocator::heap()) @deprecated("use new_executable_path()") -{ - return new_executable_path(allocator) @inline; -} - -fn String! new_executable_path(Allocator allocator = allocator::heap()) +fn String! executable_path(Allocator allocator) { $if env::DARWIN: return darwin::executable_path(allocator); $else return SearchResult.MISSING?; $endif -} \ No newline at end of file +} diff --git a/lib7/std/os/macos/darwin.c3 b/lib7/std/os/macos/darwin.c3 index ba0298973..f4805855f 100644 --- a/lib7/std/os/macos/darwin.c3 +++ b/lib7/std/os/macos/darwin.c3 @@ -80,7 +80,7 @@ fn uptr! load_address() @local { Darwin_segment_command_64* cmd = darwin::getsegbyname("__TEXT"); if (!cmd) return BacktraceFault.SEGMENT_NOT_FOUND?; - String path = env::new_executable_path(allocator::temp()) ?? BacktraceFault.EXECUTABLE_PATH_NOT_FOUND?!; + String path = env::executable_path(tmem()) ?? BacktraceFault.EXECUTABLE_PATH_NOT_FOUND?!; uint dyld_count = darwin::_dyld_image_count(); for (uint i = 0; i < dyld_count; i++) { diff --git a/lib7/std/os/subprocess.c3 b/lib7/std/os/subprocess.c3 index 9227bdac2..719f76781 100644 --- a/lib7/std/os/subprocess.c3 +++ b/lib7/std/os/subprocess.c3 @@ -74,7 +74,7 @@ fn void! create_named_pipe_helper(void** rd, void **wr) @local @if(env::WIN32) fn WString convert_command_line_win32(String[] command_line) @inline @if(env::WIN32) @local { - DString str = dstring::temp_with_capacity(512); + DString str = dstring::tnew_with_capacity(512); foreach LINE: (i, s : command_line) { if (i != 0) str.append(' '); @@ -109,7 +109,7 @@ fn WString convert_command_line_win32(String[] command_line) @inline @if(env::WI str.append('"'); } str.append('\0'); - return str.str_view().to_temp_wstring()!!; + return str.str_view().to_wstring_tcopy()!!; } <* @@ -138,7 +138,7 @@ fn SubProcess! create(String[] command_line, SubProcessOptions options = {}, Str WString used_environment = null; if (!options.inherit_environment) { - DString env = dstring::temp_with_capacity(64); + DString env = dstring::tnew_with_capacity(64); if (!environment.len) { env.append("\0"); @@ -149,7 +149,7 @@ fn SubProcess! create(String[] command_line, SubProcessOptions options = {}, Str env.append("\0"); } env.append("\0"); - used_environment = env.str_view().to_temp_wstring()!; + used_environment = env.str_view().to_wstring_tcopy()!; } int fd = win32::_open_osfhandle((iptr)wr, 0); if (fd != -1) diff --git a/lib7/std/time/format.c3 b/lib7/std/time/format.c3 index 1c43f44ba..41ec56b6f 100644 --- a/lib7/std/time/format.c3 +++ b/lib7/std/time/format.c3 @@ -20,59 +20,54 @@ enum DateTimeFormat TIMEONLY, // "15:04:05" } -fn String format(DateTimeFormat type, TzDateTime dt, Allocator allocator) +fn String format(Allocator allocator, DateTimeFormat type, TzDateTime dt) { switch (type) { case ANSIC: - return string::format("%s %s %2d %02d:%02d:%02d %04d", dt.weekday.abbrev, dt.month.abbrev, dt.day, dt.hour, dt.min, dt.sec, dt.year, allocator: allocator); + return string::format(allocator, "%s %s %2d %02d:%02d:%02d %04d", dt.weekday.abbrev, dt.month.abbrev, dt.day, dt.hour, dt.min, dt.sec, dt.year); case UNIXDATE: - return string::format("%s %s %2d %02d:%02d:%02d GMT %04d", dt.weekday.abbrev, dt.month.abbrev, dt.day, dt.hour, dt.min, dt.sec, dt.year, allocator: allocator); + return string::format(allocator, "%s %s %2d %02d:%02d:%02d GMT %04d", dt.weekday.abbrev, dt.month.abbrev, dt.day, dt.hour, dt.min, dt.sec, dt.year); case RUBYDATE: - return string::format("%s %s %2d %02d:%02d:%02d %s %04d", dt.weekday.abbrev, dt.month.abbrev, dt.day, dt.hour, dt.min, dt.sec, temp_numeric_tzsuffix(dt.gmt_offset), dt.year, allocator: allocator); + return string::format(allocator, "%s %s %2d %02d:%02d:%02d %s %04d", dt.weekday.abbrev, dt.month.abbrev, dt.day, dt.hour, dt.min, dt.sec, temp_numeric_tzsuffix(dt.gmt_offset), dt.year); case RFC822: dt = dt.to_gmt_offset(0); // For named representations of the timezone we always go for GMT, which is required by some RFCs - return string::format("%02d %s %02d %02d:%02d GMT", dt.day, dt.month.abbrev, dt.year % 100, dt.hour, dt.min, allocator: allocator); + return string::format(allocator, "%02d %s %02d %02d:%02d GMT", dt.day, dt.month.abbrev, dt.year % 100, dt.hour, dt.min); case RFC822Z: - return string::format("%02d %s %02d %02d:%02d %s", dt.day, dt.month.abbrev, dt.year % 100, dt.hour, dt.min, temp_numeric_tzsuffix(dt.gmt_offset), allocator: allocator); + return string::format(allocator, "%02d %s %02d %02d:%02d %s", dt.day, dt.month.abbrev, dt.year % 100, dt.hour, dt.min, temp_numeric_tzsuffix(dt.gmt_offset)); case RFC850: dt = dt.to_gmt_offset(0); // For named representations of the timezone we always go for GMT, which is required by some RFCs - return string::format("%s, %02d-%s-%02d %02d:%02d:%02d GMT", dt.weekday.name, dt.day, dt.month.abbrev, dt.year % 100, dt.hour, dt.min, dt.sec, allocator: allocator); + return string::format(allocator, "%s, %02d-%s-%02d %02d:%02d:%02d GMT", dt.weekday.name, dt.day, dt.month.abbrev, dt.year % 100, dt.hour, dt.min, dt.sec); case RFC1123: dt = dt.to_gmt_offset(0); // For named representations of the timezone we always go for GMT, which is required by some RFCs - return string::format("%s, %02d %s %d %02d:%02d:%02d GMT", dt.weekday.abbrev, dt.day, dt.month.abbrev, dt.year, dt.hour, dt.min, dt.sec, allocator: allocator); + return string::format(allocator, "%s, %02d %s %d %02d:%02d:%02d GMT", dt.weekday.abbrev, dt.day, dt.month.abbrev, dt.year, dt.hour, dt.min, dt.sec); case RFC1123Z: - return string::format("%s, %02d %s %d %02d:%02d:%02d %s", dt.weekday.abbrev, dt.day, dt.month.abbrev, dt.year, dt.hour, dt.min, dt.sec, temp_numeric_tzsuffix(dt.gmt_offset), allocator: allocator); + return string::format(allocator, "%s, %02d %s %d %02d:%02d:%02d %s", dt.weekday.abbrev, dt.day, dt.month.abbrev, dt.year, dt.hour, dt.min, dt.sec, temp_numeric_tzsuffix(dt.gmt_offset)); case RFC3339: dt = dt.to_gmt_offset(0); - return string::format("%04d-%02d-%02dT%02d:%02d:%02dZ", dt.year, dt.month + 1, dt.day, dt.hour, dt.min, dt.sec, allocator: allocator); + return string::format(allocator, "%04d-%02d-%02dT%02d:%02d:%02dZ", dt.year, dt.month + 1, dt.day, dt.hour, dt.min, dt.sec); case RFC3339Z: - return string::format("%04d-%02d-%02dT%02d:%02d:%02d%s", dt.year, dt.month + 1, dt.day, dt.hour, dt.min, dt.sec, temp_numeric_tzsuffix_colon(dt.gmt_offset), allocator: allocator); + return string::format(allocator, "%04d-%02d-%02dT%02d:%02d:%02d%s", dt.year, dt.month + 1, dt.day, dt.hour, dt.min, dt.sec, temp_numeric_tzsuffix_colon(dt.gmt_offset)); case RFC3339MS: dt = dt.to_gmt_offset(0); - return string::format("%04d-%02d-%02dT%02d:%02d:%02d.%dZ", dt.year, dt.month + 1, dt.day, dt.hour, dt.min, dt.sec, dt.usec, allocator: allocator); + return string::format(allocator, "%04d-%02d-%02dT%02d:%02d:%02d.%dZ", dt.year, dt.month + 1, dt.day, dt.hour, dt.min, dt.sec, dt.usec); case RFC3339ZMS: - return string::format("%04d-%02d-%02dT%02d:%02d:%02d.%d%s", dt.year, dt.month + 1, dt.day, dt.hour, dt.min, dt.sec, dt.usec, temp_numeric_tzsuffix_colon(dt.gmt_offset), allocator: allocator); + return string::format(allocator, "%04d-%02d-%02dT%02d:%02d:%02d.%d%s", dt.year, dt.month + 1, dt.day, dt.hour, dt.min, dt.sec, dt.usec, temp_numeric_tzsuffix_colon(dt.gmt_offset)); case DATETIME: - return string::format("%04d-%02d-%02d %02d:%02d:%02d", dt.year, dt.month + 1, dt.day, dt.hour, dt.min, dt.sec, allocator: allocator); + return string::format(allocator, "%04d-%02d-%02d %02d:%02d:%02d", dt.year, dt.month + 1, dt.day, dt.hour, dt.min, dt.sec); case DATEONLY: - return string::format("%04d-%02d-%02d", dt.year, dt.month + 1, dt.day, allocator: allocator); + return string::format(allocator, "%04d-%02d-%02d", dt.year, dt.month + 1, dt.day); case TIMEONLY: - return string::format("%02d:%02d:%02d", dt.hour, dt.min, dt.sec, allocator: allocator); + return string::format(allocator, "%02d:%02d:%02d", dt.hour, dt.min, dt.sec); } } -fn String new_format(DateTimeFormat dt_format, TzDateTime dt) => format(dt_format, dt, allocator::heap()); -fn String temp_format(DateTimeFormat dt_format, TzDateTime dt) => format(dt_format, dt, allocator::temp()); +fn String tformat(DateTimeFormat dt_format, TzDateTime dt) => format(tmem(), dt_format, dt); -fn String TzDateTime.format(self, DateTimeFormat dt_format, Allocator allocator) => format(dt_format, self, allocator); -fn String TzDateTime.new_format(self, DateTimeFormat dt_format) => format(dt_format, self, allocator::heap()); -fn String TzDateTime.temp_format(self, DateTimeFormat dt_format) => format(dt_format, self, allocator::temp()); +fn String TzDateTime.format(self, Allocator allocator, DateTimeFormat dt_format) => format(allocator, dt_format, self); // .with_gmt_offset(0) instead of .to_local() is used to avoid surprises when user is formatting to a representation without a timezone -fn String DateTime.format(self, DateTimeFormat dt_format, Allocator allocator) => format(dt_format, self.with_gmt_offset(0), allocator); -fn String DateTime.new_format(self, DateTimeFormat dt_format) => format(dt_format, self.with_gmt_offset(0), allocator::heap()); -fn String DateTime.temp_format(self, DateTimeFormat dt_format) => format(dt_format, self.with_gmt_offset(0), allocator::temp()); +fn String DateTime.format(self, Allocator allocator, DateTimeFormat dt_format) => format(allocator, dt_format, self.with_gmt_offset(0)); <* Returns the timezone offset in the format of "+HHMM" or "-HHMM" diff --git a/lib7/std/time/time.c3 b/lib7/std/time/time.c3 index a15218212..05e329990 100644 --- a/lib7/std/time/time.c3 +++ b/lib7/std/time/time.c3 @@ -120,7 +120,7 @@ fn usz! NanoDuration.to_format(&self, Formatter* formatter) @dynamic bool neg = nd < 0; if (neg) nd = -nd; - DString str = dstring::temp_with_capacity(64); + DString str = dstring::tnew_with_capacity(64); if (nd < 1_000_000_000) { // Less than 1s: print milliseconds, microseconds and nanoseconds. diff --git a/test/test_suite7/expressions/opt_in_conv.c3 b/test/test_suite7/expressions/opt_in_conv.c3 index 3989ac32b..f411901c7 100644 --- a/test/test_suite7/expressions/opt_in_conv.c3 +++ b/test/test_suite7/expressions/opt_in_conv.c3 @@ -3,7 +3,7 @@ import std; fn void main() { String s = "Hello"; - Char16* title = String.to_new_utf16(s).ptr; // #error: It is not possible to cast from + Char16* title = String.to_utf16_copy(s, mem).ptr; // #error: It is not possible to cast from int! a; float b = (float)a; // #error: It is not possible to cast from } \ No newline at end of file diff --git a/test/test_suite7/functions/missing_return_lambda.c3 b/test/test_suite7/functions/missing_return_lambda.c3 index c3f897e43..1189df58b 100644 --- a/test/test_suite7/functions/missing_return_lambda.c3 +++ b/test/test_suite7/functions/missing_return_lambda.c3 @@ -2,7 +2,7 @@ import std::io::path; fn void! process_dir(String dir_name) { - path::Path p = path::temp_new(dir_name)!; + path::Path p = path::tnew(dir_name)!; path::PathWalker fnwalk = fn bool!(Path p, bool is_dir, void*) { // #error: issing return statement at the end io::printfn("path is %s", p); }; diff --git a/test/test_suite7/macros/ref_macro_method.c3 b/test/test_suite7/macros/ref_macro_method.c3 index 7cd42e8f6..d34738a2d 100644 --- a/test/test_suite7/macros/ref_macro_method.c3 +++ b/test/test_suite7/macros/ref_macro_method.c3 @@ -17,7 +17,7 @@ fn void main() for (usz i; i < values_len; ++i) { - ms.dyn[i].temp_init(); + ms.dyn[i].tinit(); } ms.dyn[0].append_chars("sad"); diff --git a/test/test_suite7/slices/subscript_check_1519.c3t b/test/test_suite7/slices/subscript_check_1519.c3t index e64bdeedc..02a740370 100644 --- a/test/test_suite7/slices/subscript_check_1519.c3t +++ b/test/test_suite7/slices/subscript_check_1519.c3t @@ -13,7 +13,7 @@ macro @test_list(a) fn int main(String[] args) { - DString str = dstring::new("test"); + DString str = dstring::new(mem, "test"); return @test_list(str); } @@ -29,7 +29,7 @@ entry: store i64 %1, ptr %ptradd, align 8 %lo = load i64, ptr @std.core.mem.allocator.thread_allocator, align 8 %hi = load ptr, ptr getelementptr inbounds (i8, ptr @std.core.mem.allocator.thread_allocator, i64 8), align 8 - %2 = call ptr @std.core.dstring.new(ptr @.str, i64 4, i64 %lo, ptr %hi) + %2 = call ptr @std.core.dstring.new(i64 %lo, ptr %hi, ptr @.str, i64 4) store ptr %2, ptr %str, align 8 %3 = load ptr, ptr %str, align 8 store ptr %3, ptr %a, align 8 diff --git a/test/test_suite7/statements/dead_statements.c3t b/test/test_suite7/statements/dead_statements.c3t index 341c5cb4f..5abbaa7d7 100644 --- a/test/test_suite7/statements/dead_statements.c3t +++ b/test/test_suite7/statements/dead_statements.c3t @@ -7,7 +7,7 @@ import std::collections::list; fn void! load_corpus2(String code, String path) @local { for(;;) io::printfn("hi"); - path::Path p = path::temp_new(path)!; // #warning: This code will never execute + path::Path p = path::tnew(path)!; // #warning: This code will never execute if (!path::exists(p)) { diff --git a/test/test_suite7/stdlib/map_linux.c3t b/test/test_suite7/stdlib/map_linux.c3t index ad6757706..5c5c3b3d4 100644 --- a/test/test_suite7/stdlib/map_linux.c3t +++ b/test/test_suite7/stdlib/map_linux.c3t @@ -11,7 +11,7 @@ def IntDoubleMap = HashMap { int, double }; fn String Foo.to_new_string(Foo* foo, Allocator allocator = allocator::heap()) @dynamic { - DString s = dstring::new_with_capacity(128, allocator); + DString s = dstring::new_with_capacity(allocator, 128); s.appendf("{%s, %p}", foo.x, foo.bar); return s.str_view(); } @@ -68,7 +68,7 @@ entry: %lo = load i64, ptr %allocator, align 8 %ptradd1 = getelementptr inbounds i8, ptr %allocator, i64 8 %hi = load ptr, ptr %ptradd1, align 8 - %3 = call ptr @std.core.dstring.new_with_capacity(i64 128, i64 %lo, ptr %hi) + %3 = call ptr @std.core.dstring.new_with_capacity(i64 %lo, ptr %hi, i64 128) store ptr %3, ptr %s, align 8 %4 = insertvalue %any undef, ptr %0, 0 %5 = insertvalue %any %4, i64 ptrtoint (ptr @"$ct.int" to i64), 1 @@ -132,7 +132,7 @@ entry: call void @llvm.memset.p0.i64(ptr align 8 %map, i8 0, i64 48, i1 false) %lo = load i64, ptr @std.core.mem.allocator.thread_allocator, align 8 %hi = load ptr, ptr getelementptr inbounds (i8, ptr @std.core.mem.allocator.thread_allocator, i64 8), align 8 - %0 = call ptr @"std_collections_map$int$test.Foo$.HashMap.init"(ptr %map + %0 = call ptr @"std_collections_map$int$test.Foo$.HashMap.init"(ptr %map, i64 %lo, ptr %hi, i32 16, float 7.500000e-01) %ptradd = getelementptr inbounds i8, ptr %map, i64 32 %1 = insertvalue %any undef, ptr %ptradd, 0 %2 = insertvalue %any %1, i64 ptrtoint (ptr @"$ct.uint" to i64), 1 @@ -201,7 +201,7 @@ after_check18: ; preds = %entry, %after_check call void @llvm.memset.p0.i64(ptr align 8 %map2, i8 0, i64 48, i1 false) %lo38 = load i64, ptr @std.core.mem.allocator.thread_allocator, align 8 %hi39 = load ptr, ptr getelementptr inbounds (i8, ptr @std.core.mem.allocator.thread_allocator, i64 8), align 8 - %31 = call ptr @"std_collections_map$int$double$.HashMap.init"(ptr %map2 + %31 = call ptr @"std_collections_map$int$double$.HashMap.init"(ptr %map2, i64 %lo38, ptr %hi39, i32 16, float 7.500000e-01) %32 = call i8 @"std_collections_map$int$double$.HashMap.set"(ptr %map2, i32 4, double 1.300000e+00) %33 = call i8 @"std_collections_map$int$double$.HashMap.has_value"(ptr %map2, double 1.300000e+00) store i8 %33, ptr %taddr41, align 1 @@ -249,8 +249,8 @@ if.exit: ; preds = %if.then, %after_che store i64 %53, ptr %mark, align 8 call void @llvm.memset.p0.i64(ptr align 8 %map3, i8 0, i64 48, i1 false) %lo65 = load i64, ptr @std.core.mem.allocator.thread_allocator, align 8 - %hi66 = load ptr, ptr getelementptr inbounds - %54 = call ptr @"std_collections_map$int$double$.HashMap.init"(ptr %map3, + %hi66 = load ptr, ptr getelementptr inbounds (i8, ptr @std.core.mem.allocator.thread_allocator, i64 8), align 8 + %54 = call ptr @"std_collections_map$int$double$.HashMap.init"(ptr %map3, i64 %lo65, ptr %hi66, i32 16, float 7.500000e-01) %55 = call i8 @"std_collections_map$int$double$.HashMap.set"(ptr %map3, i32 5, double 3.200000e+00) %56 = call i8 @"std_collections_map$int$double$.HashMap.set"(ptr %map3, i32 7, double 5.200000e+00) %lo68 = load i64, ptr @std.core.mem.allocator.thread_allocator, align 8 diff --git a/test/test_suite7/stdlib/map_macos.c3t b/test/test_suite7/stdlib/map_macos.c3t index 3b2622bb5..33cba557c 100644 --- a/test/test_suite7/stdlib/map_macos.c3t +++ b/test/test_suite7/stdlib/map_macos.c3t @@ -11,7 +11,7 @@ def IntDoubleMap = HashMap { int, double }; fn String Foo.to_new_string(Foo* foo, Allocator allocator = allocator::heap()) @dynamic { - DString s = dstring::new_with_capacity(128, allocator); + DString s = dstring::new_with_capacity(allocator, 128); s.appendf("{%s, %p}", foo.x, foo.bar); return s.str_view(); } @@ -68,7 +68,7 @@ entry: %lo = load i64, ptr %allocator, align 8 %ptradd1 = getelementptr inbounds i8, ptr %allocator, i64 8 %hi = load ptr, ptr %ptradd1, align 8 - %3 = call ptr @std.core.dstring.new_with_capacity(i64 128, i64 %lo, ptr %hi) + %3 = call ptr @std.core.dstring.new_with_capacity(i64 %lo, ptr %hi, i64 128) store ptr %3, ptr %s, align 8 %4 = insertvalue %any undef, ptr %0, 0 %5 = insertvalue %any %4, i64 ptrtoint (ptr @"$ct.int" to i64), 1 diff --git a/test/test_suite7/switch/switch_in_defer_macro.c3t b/test/test_suite7/switch/switch_in_defer_macro.c3t index 0ed5f27bf..684ff5ebb 100644 --- a/test/test_suite7/switch/switch_in_defer_macro.c3t +++ b/test/test_suite7/switch/switch_in_defer_macro.c3t @@ -283,7 +283,7 @@ fn String! Lexer.parse_string(&self, char quote) @private char c = self.read_char_for_string()!; if (c == quote) return ""; DString str; - str.new_init(8, self.allocator); + str.init(self.allocator, 8); char prev; while (true) { @@ -353,7 +353,7 @@ fn void! Lexer.parse_comment(&self, String end) @private { // Find the end token and accumulate the data in between. DString acc; - acc.new_init(8, self.allocator); + acc.init(self.allocator, 8); char[] buf = self.buf[:end.len]; while (true) { @@ -394,7 +394,7 @@ macro Lexer.unread(self, n) @private fn String! Lexer.parse_ident(&self) @private { DString str; - str.new_init(8, self.allocator); + str.init(self.allocator, 8); while (true) { char! c = self.reader.read_byte(); diff --git a/test/unit/stdlib/collections/priorityqueue.c3 b/test/unit/stdlib/collections/priorityqueue.c3 index 671c93a2d..44bd0c7bc 100644 --- a/test/unit/stdlib/collections/priorityqueue.c3 +++ b/test/unit/stdlib/collections/priorityqueue.c3 @@ -2,7 +2,7 @@ module priorityqueue_test @test; import std::collections; import std::collections::priorityqueue; -def Queue = PriorityQueue{int}; +def Queue = PriorityQueue(); fn void priorityqueue() { @@ -32,7 +32,7 @@ fn void priorityqueue() assert(x == 3, "got %d; want %d", x, 3); } -def QueueMax = PriorityQueueMax{int}; +def QueueMax = PriorityQueueMax(); fn void priorityqueue_max() {