Fix bug in defer from macros. Ensure debug location on panic functions. Add getcwd.

This commit is contained in:
Christoffer Lerno
2022-12-12 17:13:33 +01:00
committed by Christoffer Lerno
parent 8b0d409695
commit a9ed514fe5
8 changed files with 100 additions and 30 deletions

View File

@@ -162,7 +162,7 @@ fn char[][] split(char[] s, char[] needle, Allocator* allocator = mem::current_a
if (try index)
{
res = s[:index];
s = s[index + 1..];
s = s[index + needle.len..];
}
else
{
@@ -192,7 +192,7 @@ fn usz! index_of(char[] s, char[] needle)
{
if (!match) index_start = i;
match++;
if (match == needed) return i;
if (match == needed) return index_start;
search = needle[match];
continue;
}
@@ -214,6 +214,15 @@ fn ZString copy_zstring(char[] s, Allocator* allocator = mem::current_allocator(
return (ZString)str;
}
fn char[] copyz(char[] s, Allocator* allocator = mem::current_allocator())
{
usz len = s.len;
char* str = allocator.alloc(len + 1)!!;
mem::copy(str, s.ptr, len);
str[len] = 0;
return str[:len];
}
fn ZString tcopy_zstring(char[] s)
{
return copy_zstring(s, mem::temp_allocator());
@@ -317,10 +326,15 @@ fn char[] concat(char[] s1, char[] s2)
return str[..full_len];
}
fn usz ZString.len(ZString *str)
fn char[] ZString.as_str(ZString str)
{
return ((char*)str)[:str.len()];
}
fn usz ZString.len(ZString str)
{
usz len = 0;
char* ptr = (char*)*str;
char* ptr = (char*)str;
while (char c = ptr++[0])
{
if (c & 0xC0 != 0x80) len++;

View File

@@ -1,5 +1,5 @@
module std::io::dir;
import std::io::os;
// In progress.
define Path = distinct char[];
@@ -11,6 +11,16 @@ fault PathResult
INVALID_PATH
}
fn char[]! getcwd(Allocator* allocator = mem::default_allocator())
{
return os::getcwd(allocator);
}
fn char[]! tgetcwd()
{
return getcwd(mem::temp_allocator()) @inline;
}
macro bool is_separator(char c)
{
$if (USE_WIN32_FILESYSTEM):

47
lib/std/io/os/getcwd.c3 Normal file
View File

@@ -0,0 +1,47 @@
module std::io::os;
import libc;
$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())
{
const DEFAULT_BUFFER = 256;
Char16[DEFAULT_BUFFER] buffer;
Char16 *res = _wgetcwd(&buffer, DEFAULT_BUFFER);
bool free = false;
defer if (free) libc::free(res);
if (!res)
{
if (libc::errno() != errno::ERANGE) return IoError.GENERAL_ERROR!;
res = _wgetcwd(null, 0);
free = true;
}
Char16[] str16 = res[:wcslen(res)];
return str::utf16to8(str16, allocator);
}
$else:
extern fn ZString _getcwd(char* pwd, usz len) @extname("getcwd");
macro char[]! getcwd(Allocator* allocator = mem::default_allocator())
{
const usz DEFAULT_BUFFER = 256;
char[DEFAULT_BUFFER] buffer;
ZString res = _getcwd(&buffer, DEFAULT_BUFFER);
bool free = false;
if (!res)
{
// Improve error
if (libc::errno() != errno::ERANGE) return IoError.GENERAL_ERROR!;
res = _getcwd(null, 0);
free = true;
}
defer if (free) libc::free((void*)res);
char[] copy = str::copyz(res.as_str(), allocator);
return copy;
}
$endif;