Use "String" consistently for "char[]" (#694)

Use "String" consistently for "char[]". Fix win32 return value.
This commit is contained in:
Christoffer Lerno
2023-01-07 22:50:33 +01:00
committed by GitHub
parent 5b2b4e900f
commit 43dc2d650c
62 changed files with 273 additions and 246 deletions

View File

@@ -56,12 +56,12 @@ macro varcast(variant v, $Type) @builtin
struct CallstackElement
{
CallstackElement* prev;
char[] function;
char[] file;
String function;
String file;
uint line;
}
fn void default_panic(char[] message, char[] file, char[] function, uint line)
fn void default_panic(String message, String file, String function, uint line)
{
CallstackElement* stack = $$stacktrace();
$if ($defined(libc::stderr) && $defined(libc::fprintf)):
@@ -88,7 +88,7 @@ fn void default_panic(char[] message, char[] file, char[] function, uint line)
$$trap();
}
define PanicFn = fn void(char[] message, char[] file, char[] function, uint line);
define PanicFn = fn void(String message, String file, String function, uint line);
PanicFn panic = &default_panic;
@@ -110,7 +110,7 @@ macro bitcast(expr, $Type) @builtin
/**
* @require $Type.kindof == TypeKind.ENUM `Only enums may be used`
**/
macro enum_by_name($Type, char[] enum_name) @builtin
macro enum_by_name($Type, String enum_name) @builtin
{
typeid x = $Type.typeid;
foreach (i, name : x.names)
@@ -164,4 +164,4 @@ macro uint long.hash(long i) = (uint)((i >> 32) ^ i);
macro uint ulong.hash(ulong i) = (uint)((i >> 32) ^ i);
macro uint bool.hash(bool b) = (uint)b;
macro uint typeid.hash(typeid t) = (uint)(((uptr)t >> 32) ^ (uptr)t);
macro uint char[].hash(char[] c) = (uint)fnv32a::encode(c);
macro uint String.hash(String c) = (uint)fnv32a::encode(c);

View File

@@ -185,7 +185,7 @@ fn Char32! utf8_to_char32(char* ptr, usz* size)
* @param utf8 `An UTF-8 encoded slice of bytes`
* @return `the number of encoded code points`
**/
fn usz utf8_codepoints(char[] utf8)
fn usz utf8_codepoints(String utf8)
{
usz len = 0;
foreach (char c : utf8)
@@ -257,7 +257,7 @@ fn usz utf8len_for_utf16(Char16[] utf16)
* @param utf8 `the utf8 data to calculate from`
* @return `the length of the resulting UTF16 array`
**/
fn usz utf16len_for_utf8(char[] utf8)
fn usz utf16len_for_utf8(String utf8)
{
usz len = utf8.len;
usz len16 = 0;
@@ -297,7 +297,7 @@ fn usz utf16len_for_utf32(Char32[] utf32)
* @param [out] utf8_buffer
* @return `the number of bytes written.`
**/
fn usz! utf32to8(Char32[] utf32, char[] utf8_buffer)
fn usz! utf32to8(Char32[] utf32, String utf8_buffer)
{
usz len = utf8_buffer.len;
char* ptr = utf8_buffer.ptr;
@@ -317,7 +317,7 @@ fn usz! utf32to8(Char32[] utf32, char[] utf8_buffer)
* @param [out] utf32_buffer
* @return `the number of Char32s written.`
**/
fn usz! utf8to32(char[] utf8, Char32[] utf32_buffer)
fn usz! utf8to32(String utf8, Char32[] utf32_buffer)
{
usz len = utf8.len;
Char32* ptr = utf32_buffer.ptr;
@@ -361,7 +361,7 @@ fn void! utf16to8_unsafe(Char16[] utf16, char* utf8_buffer)
* @param [in] utf8 `The UTF8 buffer containing the data to convert.`
* @param [out] utf32_buffer `the (sufficiently large) buffer to hold the UTF8 data.`
**/
fn void! utf8to32_unsafe(char[] utf8, Char32* utf32_buffer)
fn void! utf8to32_unsafe(String utf8, Char32* utf32_buffer)
{
usz len = utf8.len;
for (usz i = 0; i < len;)
@@ -381,7 +381,7 @@ fn void! utf8to32_unsafe(char[] utf8, Char32* utf32_buffer)
* @param [in] utf8 `The UTF8 buffer containing the data to convert.`
* @param [out] utf16_buffer `the (sufficiently large) buffer to hold the UTF8 data.`
**/
fn void! utf8to16_unsafe(char[] utf8, Char16* utf16_buffer)
fn void! utf8to16_unsafe(String utf8, Char16* utf16_buffer)
{
usz len = utf8.len;
for (usz i = 0; i < len;)

View File

@@ -1,5 +1,7 @@
module std::core::str;
define ZString = distinct char*;
define String = char[];
define Char32 = uint;
define Char16 = ushort;
@@ -18,17 +20,17 @@ fault NumberConversion
MALFORMED_INTEGER,
INTEGER_OVERFLOW,
}
fn String join(char[][] s, char[] joiner)
fn VarString join(String[] s, String joiner)
{
if (!s.len) return (String)null;
if (!s.len) return (VarString)null;
usz total_size = joiner.len * s.len;
foreach (char[]* &str : s)
foreach (String* &str : s)
{
total_size += str.len;
}
String res = string::new_with_capacity(total_size);
VarString res = string::new_with_capacity(total_size);
res.append(s[0]);
foreach (char[]* &str : s[1..])
foreach (String* &str : s[1..])
{
res.append(joiner);
res.append(*str);
@@ -36,7 +38,7 @@ fn String join(char[][] s, char[] joiner)
return res;
}
macro bool char_in_set(char c, char[] set)
macro bool char_in_set(char c, String set)
{
foreach (ch : set)
{
@@ -50,7 +52,7 @@ private macro char_is_space_tab(char c)
return c == ' ' || c == '\t';
}
private macro to_integer($Type, char[] string)
private macro to_integer($Type, String string)
{
usz len = string.len;
usz index = 0;
@@ -121,19 +123,19 @@ private macro to_integer($Type, char[] string)
return value;
}
fn int128! to_int128(char[] string) = to_integer(int128, string);
fn long! to_long(char[] string) = to_integer(long, string);
fn int! to_int(char[] string) = to_integer(int, string);
fn short! to_short(char[] string) = to_integer(short, string);
fn ichar! to_ichar(char[] string) = to_integer(ichar, string);
fn int128! to_int128(String string) = to_integer(int128, string);
fn long! to_long(String string) = to_integer(long, string);
fn int! to_int(String string) = to_integer(int, string);
fn short! to_short(String string) = to_integer(short, string);
fn ichar! to_ichar(String string) = to_integer(ichar, string);
fn uint128! to_uint128(char[] str) = to_integer(uint128, str);
fn ulong! to_ulong(char[] str) = to_integer(ulong, str);
fn uint! to_uint(char[] str) = to_integer(uint, str);
fn ushort! to_ushort(char[] str) = to_integer(ushort, str);
fn char! to_uchar(char[] str) = to_integer(char, str);
fn uint128! to_uint128(String str) = to_integer(uint128, str);
fn ulong! to_ulong(String str) = to_integer(ulong, str);
fn uint! to_uint(String str) = to_integer(uint, str);
fn ushort! to_ushort(String str) = to_integer(ushort, str);
fn char! to_uchar(String str) = to_integer(char, str);
fn char[] trim(char[] string, char[] to_trim = "\t\n\r ")
fn String trim(String string, String to_trim = "\t\n\r ")
{
usz start = 0;
usz len = string.len;
@@ -144,7 +146,7 @@ fn char[] trim(char[] string, char[] to_trim = "\t\n\r ")
return string[start..end];
}
fn bool starts_with(char[] s, char[] needle)
fn bool starts_with(String s, String needle)
{
if (needle.len > s.len) return false;
foreach (i, c : needle)
@@ -154,17 +156,17 @@ fn bool starts_with(char[] s, char[] needle)
return true;
}
fn char[][] tsplit(char[] s, char[] needle) = split(s, needle, mem::temp_allocator()) @inline;
fn String[] tsplit(String s, String needle) = split(s, needle, mem::temp_allocator()) @inline;
fn char[][] split(char[] s, char[] needle, Allocator* allocator = mem::current_allocator())
fn String[] split(String s, String needle, Allocator* allocator = mem::current_allocator())
{
usz capacity = 16;
usz i = 0;
char[]* holder = allocator.alloc(char[].sizeof * capacity)!!;
String* holder = allocator.alloc(String.sizeof * capacity)!!;
while (s.len)
{
usz! index = index_of(s, needle);
char[] res = void;
String res = void;
if (try index)
{
res = s[:index];
@@ -178,14 +180,14 @@ fn char[][] split(char[] s, char[] needle, Allocator* allocator = mem::current_a
if (i == capacity)
{
capacity *= 2;
holder = allocator.realloc(holder, char[].sizeof * capacity)!!;
holder = allocator.realloc(holder, String.sizeof * capacity)!!;
}
holder[i++] = res;
}
return holder[:i];
}
fn usz! rindex_of(char[] s, char[] needle)
fn usz! rindex_of(String s, String needle)
{
usz match = 0;
usz needed = needle.len;
@@ -211,7 +213,7 @@ fn usz! rindex_of(char[] s, char[] needle)
return SearchResult.MISSING!;
}
fn usz! index_of(char[] s, char[] needle)
fn usz! index_of(String s, String needle)
{
usz match = 0;
usz needed = needle.len;
@@ -237,7 +239,7 @@ fn usz! index_of(char[] s, char[] needle)
return SearchResult.MISSING!;
}
fn ZString copy_zstring(char[] s, Allocator* allocator = mem::current_allocator())
fn ZString copy_zstring(String s, Allocator* allocator = mem::current_allocator())
{
usz len = s.len;
char* str = allocator.alloc(len + 1)!!;
@@ -246,7 +248,7 @@ fn ZString copy_zstring(char[] s, Allocator* allocator = mem::current_allocator(
return (ZString)str;
}
fn char[] copyz(char[] s, Allocator* allocator = mem::current_allocator())
fn String copyz(String s, Allocator* allocator = mem::current_allocator())
{
usz len = s.len;
char* str = allocator.alloc(len + 1)!!;
@@ -255,12 +257,12 @@ fn char[] copyz(char[] s, Allocator* allocator = mem::current_allocator())
return str[:len];
}
fn ZString tcopy_zstring(char[] s)
fn ZString tcopy_zstring(String s)
{
return copy_zstring(s, mem::temp_allocator());
}
fn bool compare(char[] a, char[] b)
fn bool compare(String a, String b)
{
if (a.len != b.len) return false;
foreach (i, c : a)
@@ -276,7 +278,7 @@ fault UnicodeResult
CONVERSION_FAILED,
}
fn usz utf8_codepoints(char[] utf8)
fn usz utf8_codepoints(String utf8)
{
usz len = 0;
foreach (char c : utf8)
@@ -286,7 +288,7 @@ fn usz utf8_codepoints(char[] utf8)
return len;
}
fn Char32[]! utf8to32(char[] utf8, Allocator* allocator = mem::current_allocator)
fn Char32[]! utf8to32(String utf8, Allocator* allocator = mem::current_allocator)
{
usz codepoints = conv::utf8_codepoints(utf8);
Char32* data = allocator.alloc(Char32.sizeof * (codepoints + 1))?;
@@ -295,7 +297,7 @@ fn Char32[]! utf8to32(char[] utf8, Allocator* allocator = mem::current_allocator
return data[:codepoints];
}
fn char[] utf32to8(Char32[] utf32, Allocator* allocator = mem::current_allocator)
fn String utf32to8(Char32[] utf32, Allocator* allocator = mem::current_allocator)
{
usz len = conv::utf8len_for_utf32(utf32);
char* data = allocator.alloc(len + 1)!!;
@@ -304,7 +306,7 @@ fn char[] utf32to8(Char32[] utf32, Allocator* allocator = mem::current_allocator
return data[:len];
}
fn Char16[]! utf8to16(char[] utf8, Allocator* allocator = mem::current_allocator)
fn Char16[]! utf8to16(String utf8, Allocator* allocator = mem::current_allocator)
{
usz len16 = conv::utf16len_for_utf8(utf8);
Char16* data = allocator.alloc((len16 + 1) * Char16.sizeof)?;
@@ -314,7 +316,7 @@ fn Char16[]! utf8to16(char[] utf8, Allocator* allocator = mem::current_allocator
}
fn char[]! utf16to8(Char16[] utf16, Allocator* allocator = mem::current_allocator())
fn String! utf16to8(Char16[] utf16, Allocator* allocator = mem::current_allocator())
{
usz len = conv::utf8len_for_utf16(utf16);
char* data = allocator.alloc(len + 1)?;
@@ -322,21 +324,21 @@ fn char[]! utf16to8(Char16[] utf16, Allocator* allocator = mem::current_allocato
return data[:len];
}
fn char[] copy(char[] s, Allocator* allocator = mem::current_allocator())
fn String copy(String s, Allocator* allocator = mem::current_allocator())
{
usz len = s.len;
ZString str_copy = copy_zstring(s, allocator) @inline;
return str_copy[:len];
}
fn char[] tcopy(char[] s)
fn String tcopy(String s)
{
usz len = s.len;
ZString str_copy = tcopy_zstring(s) @inline;
return str_copy[:len];
}
fn char[] tconcat(char[] s1, char[] s2)
fn String tconcat(String s1, String s2)
{
usz full_len = s1.len + s2.len;
char* str = tmalloc(full_len + 1);
@@ -347,7 +349,7 @@ fn char[] tconcat(char[] s1, char[] s2)
return str[:full_len];
}
fn char[] concat(char[] s1, char[] s2)
fn String concat(String s1, String s2)
{
usz full_len = s1.len + s2.len;
char* str = malloc(full_len + 1);
@@ -358,7 +360,7 @@ fn char[] concat(char[] s1, char[] s2)
return str[:full_len];
}
fn char[] ZString.as_str(ZString str)
fn String ZString.as_str(ZString str)
{
return ((char*)str)[:str.len()];
}

View File

@@ -1,7 +1,7 @@
module std::core::string;
import libc;
define String = distinct void*;
define VarString = distinct void*;
private struct StringData
{
@@ -13,30 +13,30 @@ private struct StringData
const usz MIN_CAPACITY = 16;
fn String new_with_capacity(usz capacity, Allocator* allocator = mem::current_allocator())
fn VarString new_with_capacity(usz capacity, Allocator* allocator = mem::current_allocator())
{
if (capacity < MIN_CAPACITY) capacity = MIN_CAPACITY;
StringData* data = allocator.alloc(StringData.sizeof + capacity)!!;
data.allocator = allocator;
data.len = 0;
data.capacity = capacity;
return (String)data;
return (VarString)data;
}
fn String new(char[] c)
fn VarString new(String c)
{
usz len = c.len;
String str = new_with_capacity(len);
VarString str = new_with_capacity(len);
StringData* data = str.data();
if (len)
{
data.len = len;
mem::copy(&data.chars, c.ptr, len);
}
return (String)data;
return (VarString)data;
}
fn ZString String.zstr(String str)
fn ZString VarString.zstr(VarString str)
{
StringData* data = str.data();
if (!data) return (ZString)"";
@@ -52,7 +52,7 @@ fn ZString String.zstr(String str)
return (ZString)&data.chars[0];
}
fn usz String.len(String this)
fn usz VarString.len(VarString this)
{
if (!this) return 0;
return this.data().len;
@@ -61,20 +61,20 @@ fn usz String.len(String this)
/**
* @require new_size <= this.len()
*/
fn void String.chop(String this, usz new_size)
fn void VarString.chop(VarString this, usz new_size)
{
if (!this) return;
this.data().len = new_size;
}
fn char[] String.str(String str)
fn String VarString.str(VarString str)
{
StringData* data = (StringData*)str;
if (!data) return char[] {};
return data.chars[:data.len];
if (!data) return String {};
return (String)data.chars[:data.len];
}
fn void String.append_utf32(String* str, Char32[] chars)
fn void VarString.append_utf32(VarString* str, Char32[] chars)
{
str.reserve(chars.len);
foreach (Char32 c : chars)
@@ -86,12 +86,12 @@ fn void String.append_utf32(String* str, Char32[] chars)
/**
* @require index < str.len()
**/
fn void String.set(String str, usz index, char c)
fn void VarString.set(VarString str, usz index, char c)
{
str.data().chars[index] = c;
}
fn void String.append_repeat(String* str, char c, usz times)
fn void VarString.append_repeat(VarString* str, char c, usz times)
{
if (times == 0) return;
str.reserve(times);
@@ -105,7 +105,7 @@ fn void String.append_repeat(String* str, char c, usz times)
/**
* @require c < 0x10ffff
*/
fn void String.append_char32(String* str, Char32 c)
fn void VarString.append_char32(VarString* str, Char32 c)
{
if (c < 0x7f)
{
@@ -139,23 +139,23 @@ fn void String.append_char32(String* str, Char32 c)
data.chars[data.len++] = (char)(0x80 | (c & 0x3F));
}
fn String String.tcopy(String* str) = str.copy(mem::temp_allocator());
fn VarString VarString.tcopy(VarString* str) = str.copy(mem::temp_allocator());
fn String String.copy(String* str, Allocator* allocator = null)
fn VarString VarString.copy(VarString* str, Allocator* allocator = null)
{
if (!str)
{
if (allocator) return new_with_capacity(0, allocator);
return (String)null;
return (VarString)null;
}
if (!allocator) allocator = mem::current_allocator();
StringData* data = str.data();
String new_string = new_with_capacity(data.capacity, allocator);
VarString new_string = new_with_capacity(data.capacity, allocator);
mem::copy((char*)new_string.data(), (char*)data, StringData.sizeof + data.len);
return new_string;
}
fn ZString String.copy_zstr(String* str, Allocator* allocator = mem::current_allocator())
fn ZString VarString.copy_zstr(VarString* str, Allocator* allocator = mem::current_allocator())
{
usz str_len = str.len();
if (!str_len)
@@ -169,14 +169,14 @@ fn ZString String.copy_zstr(String* str, Allocator* allocator = mem::current_all
return (ZString)zstr;
}
fn char[] String.copy_str(String* str, Allocator* allocator = mem::current_allocator())
fn String VarString.copy_str(VarString* str, Allocator* allocator = mem::current_allocator())
{
return str.copy_zstr(allocator)[:str.len()];
return (String)str.copy_zstr(allocator)[:str.len()];
}
fn char[] String.tcopy_str(String* str) = str.copy_str(mem::temp_allocator()) @inline;
fn String VarString.tcopy_str(VarString* str) = str.copy_str(mem::temp_allocator()) @inline;
fn bool String.equals(String str, String other_string)
fn bool VarString.equals(VarString str, VarString other_string)
{
StringData *str1 = str.data();
StringData *str2 = other_string.data();
@@ -192,16 +192,16 @@ fn bool String.equals(String str, String other_string)
return true;
}
fn void String.destroy(String* str)
fn void VarString.destroy(VarString* str)
{
if (!*str) return;
StringData* data = str.data();
if (!data) return;
data.allocator.free(data)!!;
*str = (String)null;
*str = (VarString)null;
}
fn bool String.less(String str, String other_string)
fn bool VarString.less(VarString str, VarString other_string)
{
StringData* str1 = str.data();
StringData* str2 = other_string.data();
@@ -218,7 +218,7 @@ fn bool String.less(String str, String other_string)
return true;
}
fn void String.append_chars(String* this, char[] str)
fn void VarString.append_chars(VarString* this, String str)
{
usz other_len = str.len;
if (!other_len) return;
@@ -233,19 +233,19 @@ fn void String.append_chars(String* this, char[] str)
data.len += other_len;
}
fn Char32[] String.copy_utf32(String* this, Allocator* allocator = mem::current_allocator())
fn Char32[] VarString.copy_utf32(VarString* this, Allocator* allocator = mem::current_allocator())
{
return str::utf8to32(this.str(), allocator) @inline!!;
}
fn void String.append_string(String* this, String str)
fn void VarString.append_string(VarString* this, VarString str)
{
StringData* other = (StringData*)str;
if (!other) return;
this.append(str.str());
}
fn void String.append_char(String* str, char c)
fn void VarString.append_char(VarString* str, char c)
{
if (!*str)
{
@@ -257,23 +257,23 @@ fn void String.append_char(String* str, char c)
}
macro void String.append(String* str, value)
macro void VarString.append(VarString* str, value)
{
var $Type = $typeof(value);
$switch ($Type):
$case char:
$case ichar:
str.append_char(value);
$case String:
$case VarString:
str.append_string(value);
$case char[]:
$case String:
str.append_chars(value);
$case Char32:
str.append_char32(value);
$default:
$if (@convertible($Type, Char32)):
str.append_char32(value);
$elif (@convertible($Type, char[])):
$elif (@convertible($Type, String)):
str.append_chars(value);
$else:
$assert("Unsupported type for appending");
@@ -282,12 +282,12 @@ macro void String.append(String* str, value)
}
private fn StringData* String.data(String str) @inline
private fn StringData* VarString.data(VarString str) @inline
{
return (StringData*)str;
}
private fn void String.reserve(String* str, usz addition)
private fn void VarString.reserve(VarString* str, usz addition)
{
StringData* data = str.data();
if (!data)
@@ -299,12 +299,12 @@ private fn void String.reserve(String* str, usz addition)
if (data.capacity >= len) return;
usz new_capacity = data.capacity *= 2;
if (new_capacity < MIN_CAPACITY) new_capacity = MIN_CAPACITY;
*str = (String)data.allocator.realloc(data, StringData.sizeof + new_capacity)!!;
*str = (VarString)data.allocator.realloc(data, StringData.sizeof + new_capacity)!!;
}
fn String String.new_concat(String a, String b, Allocator* allocator = mem::current_allocator())
fn VarString VarString.new_concat(VarString a, VarString b, Allocator* allocator = mem::current_allocator())
{
String string = new_with_capacity(a.len() + b.len(), allocator);
VarString string = new_with_capacity(a.len() + b.len(), allocator);
string.append(a);
string.append(b);
return string;

View File

@@ -2,7 +2,7 @@ module std::core::string::iterator;
struct StringIterator
{
char[] utf8;
String utf8;
usz current;
}

View File

@@ -1,7 +1,7 @@
module std::io::dir;
import std::io::os;
// In progress.
define Path = distinct char[];
define Path = distinct String;
const PREFERRED_SEPARATOR = USE_WIN32_FILESYSTEM ? '\\' : '/';
private const USE_WIN32_FILESYSTEM = env::OS_TYPE != OsType.WIN32;
@@ -11,12 +11,12 @@ fault PathResult
INVALID_PATH
}
fn char[]! getcwd(Allocator* allocator = mem::default_allocator())
fn String! getcwd(Allocator* allocator = mem::default_allocator())
{
return os::getcwd(allocator);
}
fn char[]! tgetcwd()
fn String! tgetcwd()
{
return getcwd(mem::temp_allocator()) @inline;
}
@@ -56,7 +56,7 @@ $else:
$endif;
}
private fn usz! root_name_len(char[] path)
private fn usz! root_name_len(String path)
{
usz len = path.len;
if (!len) return 0;
@@ -83,9 +83,9 @@ private fn usz! root_name_len(char[] path)
return 0;
}
private fn void! normalize_path(char[]* path_ref)
private fn void! normalize_path(String* path_ref)
{
char[] path = *path_ref;
String path = *path_ref;
if (!path.len) return;
usz path_start = root_name_len(path)?;
usz len = path_start;
@@ -137,16 +137,16 @@ private fn void! normalize_path(char[]* path_ref)
*path_ref = path[:len];
}
fn Path new_path(char[] path)
fn Path new_path(String path)
{
char[] copy = str::copy(path);
String copy = str::copy(path);
normalize_path(&copy)!!;
return (Path)copy;
}
fn Path Path.root_name(Path path)
{
char[] path_str = (char[])path;
String path_str = (String)path;
usz len = root_name_len(path_str)!!;
if (!len) return (Path)"";
return (Path)path_str[0:len];
@@ -154,7 +154,7 @@ fn Path Path.root_name(Path path)
fn Path Path.root_directory(Path path)
{
char[] path_str = (char[])path;
String path_str = (String)path;
usz len = path_str.len;
if (!len) return (Path)"";
$if (USE_WIN32_FILESYSTEM):

View File

@@ -1,7 +1,7 @@
module std::io;
import libc;
fn void! File.open(File* file, char[] filename, char[] mode)
fn void! File.open(File* file, String filename, String mode)
{
@pool()
{
@@ -102,7 +102,7 @@ fn usz File.write(File* file, void* buffer, usz items, usz element_size = 1)
* @param [&in] file
* @require file.file `File must be initialized`
*/
fn usz! File.println(File* file, char[] string)
fn usz! File.println(File* file, String string)
{
usz len = string.len;
if (len != libc::fwrite(string.ptr, 1, len, file.file)) return IoError.UNKNOWN_ERROR!;
@@ -114,9 +114,9 @@ fn usz! File.println(File* file, char[] string)
* @param [&in] file
* @require file.file `File must be initialized`
*/
fn String File.getline(File* file, Allocator* allocator = mem::current_allocator())
fn VarString File.getline(File* file, Allocator* allocator = mem::current_allocator())
{
String s = string::new_with_capacity(120, allocator);
VarString s = string::new_with_capacity(120, allocator);
while (!file.eof())
{
int c = libc::fgetc(file.file);
@@ -130,12 +130,12 @@ fn String File.getline(File* file, Allocator* allocator = mem::current_allocator
/**
* @param [&in] file
* @require file.file `File must be initialized`
* @return "a zero terminated char[] (the pointer may be safely cast into a ZString)"
* @return "a zero terminated String (the pointer may be safely cast into a ZString)"
*/
fn char[] File.tgetline(File* file)
fn String File.tgetline(File* file)
{
String s = file.getline(mem::temp_allocator());
VarString s = file.getline(mem::temp_allocator());
ZString z = s.zstr();
return z[:s.len()];
}

View File

@@ -43,7 +43,7 @@ fn void! FileInfo.read(FileInfo* info, Path path)
@pool()
{
Darwin64Stat stat;
int res = _stat(str::tcopy_zstring((char[])path), &stat);
int res = _stat(str::tcopy_zstring((String)path), &stat);
if (res != 0)
{
switch (libc::errno())

View File

@@ -131,7 +131,7 @@ private fn uint simple_atoi(char* buf, usz maxlen, usz* len_ptr) @inline
}
private fn void! Formatter.out_substr(Formatter *this, char[] str)
private fn void! Formatter.out_substr(Formatter *this, String str)
{
usz l = conv::utf8_codepoints(str);
uint prec = this.prec;
@@ -415,7 +415,7 @@ private fn void! Formatter.ntoa(Formatter* this, uint128 value, bool negative, u
return this.ntoa_format(buf[:PRINTF_NTOA_BUFFER_SIZE], len, negative, base);
}
private fn void! Formatter.ntoa_format(Formatter* this, char[] buf, usz len, bool negative, uint base)
private fn void! Formatter.ntoa_format(Formatter* this, String buf, usz len, bool negative, uint base)
{
// pad leading zeros
if (!this.flags.left)
@@ -511,7 +511,7 @@ private fn void! Formatter.out_char(Formatter* this, variant arg)
}
private fn void! Formatter.out_reverse(Formatter* this, char[] buf)
private fn void! Formatter.out_reverse(Formatter* this, String buf)
{
usz buffer_start_idx = this.idx;
usz len = buf.len;

View File

@@ -17,7 +17,7 @@ fault PrintFault
define OutputFn = fn void!(char c, void* buffer);
define ToStringFunction = fn char[](void* value, Allocator *allocator);
define ToStringFunction = fn String(void* value, Allocator *allocator);
define ToFormatFunction = fn void!(void* value, Formatter* formatter);
define FloatType = double;
@@ -163,9 +163,9 @@ private fn void! Formatter.out_str(Formatter* this, variant arg)
unreachable();
case DISTINCT:
if (this.print_with_function(arg)?) return;
if (arg.type == String.typeid)
if (arg.type == VarString.typeid)
{
return this.out_substr(((String*)arg).str());
return this.out_substr(((VarString*)arg).str());
}
return this.out_str(variant { arg.ptr, arg.type.inner });
case POINTER:
@@ -188,7 +188,7 @@ private fn void! Formatter.out_str(Formatter* this, variant arg)
typeid inner = arg.type.inner;
usz size = inner.sizeof;
usz len = arg.type.len;
// Pretend this is a char[]
// Pretend this is a String
void* ptr = (void*)arg.ptr;
this.out('[')?;
for (usz i = 0; i < len; i++)
@@ -204,7 +204,7 @@ private fn void! Formatter.out_str(Formatter* this, variant arg)
typeid inner = arg.type.inner;
usz size = inner.sizeof;
usz len = arg.type.len;
// Pretend this is a char[]
// Pretend this is a String
void* ptr = (void*)arg.ptr;
this.out_substr("[<")?;
for (usz i = 0; i < len; i++)
@@ -220,11 +220,11 @@ private fn void! Formatter.out_str(Formatter* this, variant arg)
typeid inner = arg.type.inner;
if (inner == char.typeid)
{
return this.out_substr(*(char[]*)arg);
return this.out_substr(*(String*)arg);
}
usz size = inner.sizeof;
// Pretend this is a char[]
char[]* temp = (void*)arg.ptr;
// Pretend this is a String
String* temp = (void*)arg.ptr;
void* ptr = (void*)temp.ptr;
usz len = temp.len;
this.out('[')?;
@@ -283,18 +283,18 @@ private fn void! out_fputchar_fn(char c, void* data)
private fn void! out_string_append_fn(char c, void* data)
{
String* s = data;
VarString* s = data;
s.append_char(c);
}
fn usz! printf(char[] format, args...) @maydiscard
fn usz! printf(String format, args...) @maydiscard
{
Formatter formatter;
formatter.init(&out_putchar_fn);
return formatter.vprintf(format, args);
}
fn usz! printfln(char[] format, args...) @maydiscard
fn usz! printfln(String format, args...) @maydiscard
{
Formatter formatter;
formatter.init(&out_putchar_fn);
@@ -303,14 +303,14 @@ fn usz! printfln(char[] format, args...) @maydiscard
return len + 1;
}
fn usz! String.printf(String* str, char[] format, args...) @maydiscard
fn usz! VarString.printf(VarString* str, String format, args...) @maydiscard
{
Formatter formatter;
formatter.init(&out_string_append_fn, str);
return formatter.vprintf(format, args);
}
fn usz! String.printfln(String* str, char[] format, args...) @maydiscard
fn usz! VarString.printfln(VarString* str, String format, args...) @maydiscard
{
Formatter formatter;
formatter.init(&out_string_append_fn, str);
@@ -325,7 +325,7 @@ private struct BufferData
usz written;
}
fn char[]! bprintf(char[] buffer, char[] format, args...) @maydiscard
fn char[]! bprintf(char[] buffer, String format, args...) @maydiscard
{
Formatter formatter;
BufferData data = { .buffer = buffer };
@@ -334,14 +334,14 @@ fn char[]! bprintf(char[] buffer, char[] format, args...) @maydiscard
return buffer[:size];
}
fn usz! File.printf(File file, char[] format, args...) @maydiscard
fn usz! File.printf(File file, String format, args...) @maydiscard
{
Formatter formatter;
formatter.init(&out_putchar_fn, &file);
return formatter.vprintf(format, args)?;
}
fn usz! File.printfln(File file, char[] format, args...) @maydiscard
fn usz! File.printfln(File file, String format, args...) @maydiscard
{
Formatter formatter;
formatter.init(&out_putchar_fn, &file);
@@ -351,12 +351,12 @@ fn usz! File.printfln(File file, char[] format, args...) @maydiscard
return len + 1;
}
fn usz! Formatter.printf(Formatter* this, char[] format, args...)
fn usz! Formatter.printf(Formatter* this, String format, args...)
{
return this.vprintf(format, args) @inline;
}
fn usz! Formatter.vprintf(Formatter* this, char[] format, variant[] variants)
fn usz! Formatter.vprintf(Formatter* this, String format, variant[] variants)
{
if (!this.out_fn)
{

View File

@@ -6,7 +6,7 @@ $if (env::OS_TYPE == OsType.WIN32):
extern fn Char16* _wgetcwd(Char16* buffer, int maxlen);
extern fn usz wcslen(Char16* str);
macro char[]! getcwd(Allocator* allocator = mem::default_allocator())
macro String! getcwd(Allocator* allocator = mem::default_allocator())
{
const DEFAULT_BUFFER = 256;
Char16[DEFAULT_BUFFER] buffer;
@@ -26,7 +26,7 @@ macro char[]! getcwd(Allocator* allocator = mem::default_allocator())
$else:
extern fn ZString _getcwd(char* pwd, usz len) @extname("getcwd");
macro char[]! getcwd(Allocator* allocator = mem::default_allocator())
macro String! getcwd(Allocator* allocator = mem::default_allocator())
{
const usz DEFAULT_BUFFER = 256;
char[DEFAULT_BUFFER] buffer;
@@ -40,7 +40,7 @@ macro char[]! getcwd(Allocator* allocator = mem::default_allocator())
free = true;
}
defer if (free) libc::free((void*)res);
char[] copy = str::copyz(res.as_str(), allocator);
String copy = str::copyz(res.as_str(), allocator);
return copy;
}

View File

@@ -32,7 +32,7 @@ define TestFn = fn void!();
struct TestRunner
{
char[][] test_names;
String[] test_names;
TestFn[] test_fns;
JmpBuf buf;
}
@@ -48,7 +48,7 @@ fn TestRunner test_runner_create()
import libc;
private TestRunner* current_runner;
fn void test_panic(char[] message, char[] file, char[] function, uint line)
fn void test_panic(String message, String file, String function, uint line)
{
io::println("[error]");
io::printfln("\n Error: %s", message);
@@ -65,7 +65,7 @@ fn bool TestRunner.run(TestRunner* runner)
int tests_passed = 0;
int tests = runner.test_names.len;
io::println("----- TESTS -----");
foreach(i, char[] name : runner.test_names)
foreach(i, String name : runner.test_names)
{
io::printf("Testing %s ... ", name);
if (libc::setjmp(&runner.buf) == 0)