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;
}