mirror of
https://github.com/c3lang/c3c.git
synced 2026-02-27 12:01:16 +00:00
Change zero terminated wide strings to use WString over Char16
This commit is contained in:
committed by
Christoffer Lerno
parent
68af987c60
commit
e2676a5c7f
@@ -2,6 +2,7 @@ module std::core::string;
|
||||
import std::ascii;
|
||||
|
||||
def ZString = distinct inline char*;
|
||||
def WString = distinct inline Char16*;
|
||||
def Char32 = uint;
|
||||
def Char16 = ushort;
|
||||
|
||||
@@ -352,6 +353,8 @@ fn Char16[]! String.to_utf16(String s, Allocator* using = mem::heap())
|
||||
return data[:len16];
|
||||
}
|
||||
|
||||
fn WString! String.to_wstring(String s, Allocator* using = mem::heap()) => (WString)s.to_utf16(using).ptr;
|
||||
|
||||
fn Char32[]! String.to_utf32(String s, Allocator* using = mem::heap())
|
||||
{
|
||||
usz codepoints = conv::utf8_codepoints(s);
|
||||
@@ -405,11 +408,11 @@ fn String! from_utf16(Char16[] utf16, Allocator* using = mem::heap())
|
||||
return (String)data[:len];
|
||||
}
|
||||
|
||||
fn String! from_zutf16(Char16* utf16_pointer, Allocator* using = mem::heap())
|
||||
fn String! from_wstring(WString wstring, Allocator* using = mem::heap())
|
||||
{
|
||||
usz utf16_len;
|
||||
while (utf16_pointer[utf16_len] != 0) utf16_len++;
|
||||
Char16[] utf16 = utf16_pointer[:utf16_len];
|
||||
while (wstring[utf16_len] != 0) utf16_len++;
|
||||
Char16[] utf16 = wstring[:utf16_len];
|
||||
return from_utf16(utf16, using);
|
||||
}
|
||||
|
||||
@@ -497,6 +500,7 @@ macro String.to_integer(string, $Type)
|
||||
|
||||
|
||||
fn Char16[]! String.to_temp_utf16(s) => s.to_utf16(mem::temp());
|
||||
fn WString! String.to_temp_wstring(s) => s.to_wstring(mem::temp());
|
||||
|
||||
fn int128! String.to_int128(s) => s.to_integer(int128);
|
||||
fn long! String.to_long(s) => s.to_integer(long);
|
||||
|
||||
@@ -10,7 +10,7 @@ fn void*! native_fopen(String filename, String mode) @inline
|
||||
@pool()
|
||||
{
|
||||
$if env::WIN32:
|
||||
void* file = libc::_wfopen(filename.to_temp_utf16(), filename.to_temp_utf16())!;
|
||||
void* file = libc::_wfopen(filename.to_temp_wstring(), filename.to_temp_wstring())!;
|
||||
$else
|
||||
void* file = libc::fopen(filename.zstr_tcopy(), mode.zstr_tcopy());
|
||||
$endif
|
||||
@@ -23,7 +23,7 @@ fn void! native_remove(String filename)
|
||||
@pool()
|
||||
{
|
||||
$if env::WIN32:
|
||||
CInt result = libc::_wremove(filename.to_temp_utf16())!;
|
||||
CInt result = libc::_wremove(filename.to_temp_wstring())!;
|
||||
$else
|
||||
CInt result = libc::remove(filename.zstr_tcopy());
|
||||
$endif
|
||||
@@ -50,7 +50,7 @@ fn void*! native_freopen(void* file, String filename, String mode) @inline
|
||||
@pool()
|
||||
{
|
||||
$if env::WIN32:
|
||||
file = libc::_wfreopen(filename.to_temp_utf16(), mode.to_temp_utf16(), file)!;
|
||||
file = libc::_wfreopen(filename.to_temp_wstring(), mode.to_temp_wstring(), file)!;
|
||||
$else
|
||||
file = libc::freopen(filename.zstr_tcopy(), mode.zstr_tcopy(), file);
|
||||
$endif
|
||||
|
||||
@@ -44,9 +44,8 @@ fn usz! native_file_size(String path) @if(env::WIN32)
|
||||
{
|
||||
@pool()
|
||||
{
|
||||
Char16[] path16 = path.to_temp_utf16()!;
|
||||
Win32_FILE_ATTRIBUTE_DATA data;
|
||||
win32::getFileAttributesExW(path16, Win32_GET_FILEEX_INFO_LEVELS.STANDARD, &data);
|
||||
win32::getFileAttributesExW(path.to_temp_wstring()!, Win32_GET_FILEEX_INFO_LEVELS.STANDARD, &data);
|
||||
Win32_LARGE_INTEGER size;
|
||||
size.lowPart = data.nFileSizeLow;
|
||||
size.highPart = data.nFileSizeHigh;
|
||||
|
||||
@@ -7,7 +7,7 @@ macro String! getcwd(Allocator* using = mem::heap())
|
||||
$case env::WIN32:
|
||||
const DEFAULT_BUFFER = 256;
|
||||
Char16[DEFAULT_BUFFER] buffer;
|
||||
Char16 *res = win32::_wgetcwd(&buffer, DEFAULT_BUFFER);
|
||||
WString res = win32::_wgetcwd(&buffer, DEFAULT_BUFFER);
|
||||
bool free = false;
|
||||
defer if (free) libc::free(res);
|
||||
if (!res)
|
||||
|
||||
@@ -28,7 +28,7 @@ fn PathList! native_ls(Path dir, bool no_dirs, bool no_symlinks, String mask, Al
|
||||
list.init(.using = using);
|
||||
@stack_mem(1024; Allocator* mem)
|
||||
{
|
||||
Char16* result = dir.as_str().concat(`\*`).to_utf16(.using = mem)!!;
|
||||
WString result = dir.as_str().concat(`\*`).to_wstring(.using = mem)!!;
|
||||
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?;
|
||||
@@ -38,7 +38,7 @@ 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;
|
||||
@stack_mem(480; Allocator* mem2)
|
||||
{
|
||||
String filename = string::from_zutf16(&find_data.cFileName, mem2)!;
|
||||
String filename = string::from_wstring((WString)&find_data.cFileName, mem2)!;
|
||||
if (filename == ".." || filename == ".") continue;
|
||||
list.append(path::new(filename, using));
|
||||
};
|
||||
|
||||
@@ -44,17 +44,20 @@ fn void! native_rmtree(Path path)
|
||||
defer win32::findClose(find);
|
||||
do
|
||||
{
|
||||
String filename = string::from_zutf16(&find_data.cFileName, mem::temp())!;
|
||||
if (filename == "." || filename == "..") continue;
|
||||
Path file_path = path.tappend(filename)!;
|
||||
if (find_data.dwFileAttributes & win32::FILE_ATTRIBUTE_DIRECTORY)
|
||||
@pool()
|
||||
{
|
||||
native_rmtree(file_path)!;
|
||||
}
|
||||
else
|
||||
{
|
||||
win32::deleteFileW(file_path.as_str().to_utf16(mem::temp()));
|
||||
}
|
||||
String filename = string::from_wstring((WString)&find_data.cFileName, mem::temp())!;
|
||||
if (filename == "." || filename == "..") continue;
|
||||
Path file_path = path.tappend(filename)!;
|
||||
if (find_data.dwFileAttributes & win32::FILE_ATTRIBUTE_DIRECTORY)
|
||||
{
|
||||
native_rmtree(file_path)!;
|
||||
}
|
||||
else
|
||||
{
|
||||
win32::deleteFileW(file_path.as_str().to_temp_wstring()!!);
|
||||
}
|
||||
};
|
||||
} while (win32::findNextFileW(find, &find_data) != 0);
|
||||
os::native_rmdir(path)!;
|
||||
}
|
||||
|
||||
@@ -111,11 +111,11 @@ fn Path! new(String path, Allocator* using = mem::heap(), PathEnv path_env = DEF
|
||||
return { normalize(path.copy(using), path_env), path_env };
|
||||
}
|
||||
|
||||
fn Path! new_win32_zutf16(Char16* path, Allocator* using = mem::heap())
|
||||
fn Path! new_win32_wstring(WString path, Allocator* using = mem::heap())
|
||||
{
|
||||
@stack_mem(480; Allocator* mem)
|
||||
{
|
||||
return path::new(string::from_zutf16(path, .using = mem)!, .using = using);
|
||||
return path::new(string::from_wstring(path, .using = mem)!, .using = using);
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -16,10 +16,10 @@ extern fn Time_t _mktime64(Tm *timeptr); def mktime = _mktime64;
|
||||
extern fn usz _msize(void* ptr);
|
||||
extern fn CInt _read(Fd fd, void* buffer, CUInt buffer_size);
|
||||
extern fn CInt _setjmp(void* frameptr, JmpBuf* buffer) @if(env::WIN32);
|
||||
extern fn CFile _wfopen(Char16*, Char16*);
|
||||
extern fn CFile _wfreopen(Char16*, Char16*, CFile);
|
||||
extern fn CFile _wfopen(WString, WString);
|
||||
extern fn CFile _wfreopen(WString, WString, CFile);
|
||||
extern fn CInt _write(Fd fd, void* buffer, CUInt count);
|
||||
extern fn CInt _wremove(Char16*);
|
||||
extern fn CInt _wremove(WString);
|
||||
|
||||
// Aliases to simplify libc use
|
||||
macro Tm* localtime_r(Time_t* timer, Tm* buf) => _localtime64_s(buf, timer);
|
||||
|
||||
@@ -73,7 +73,7 @@ fn void! create_named_pipe_helper(void** rd, void **wr) @local @if(env::WIN32)
|
||||
};
|
||||
}
|
||||
|
||||
fn Char16* convert_command_line_win32(String[] command_line) @inline @if(env::WIN32) @local
|
||||
fn WString convert_command_line_win32(String[] command_line) @inline @if(env::WIN32) @local
|
||||
{
|
||||
DString str;
|
||||
str.tinit(512);
|
||||
@@ -113,7 +113,7 @@ fn Char16* convert_command_line_win32(String[] command_line) @inline @if(env::WI
|
||||
str.append('"');
|
||||
}
|
||||
str.append('\0');
|
||||
return str.as_str().to_utf16(.using = mem::temp())!!;
|
||||
return str.as_str().to_temp_wstring()!!;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -139,7 +139,7 @@ fn SubProcess! create(String[] command_line, SubProcessOptions options = {}, Str
|
||||
CFile stderr;
|
||||
@pool()
|
||||
{
|
||||
Char16* used_environment = null;
|
||||
WString used_environment = null;
|
||||
if (!options.inherit_environment)
|
||||
{
|
||||
DString env;
|
||||
@@ -154,7 +154,7 @@ fn SubProcess! create(String[] command_line, SubProcessOptions options = {}, Str
|
||||
env.append("\0");
|
||||
}
|
||||
env.append("\0");
|
||||
used_environment = env.as_str().to_utf16(.using = mem::temp()).ptr!;
|
||||
used_environment = env.as_str().to_temp_wstring()!;
|
||||
}
|
||||
int fd = win32::_open_osfhandle((iptr)wr, 0);
|
||||
if (fd != -1)
|
||||
|
||||
@@ -90,18 +90,18 @@ extern fn Win32_BOOL readFile(Win32_HANDLE hFile, Win32_LPVOID lpBuffer, Win32_D
|
||||
Win32_LPDWORD lpNumberOfBytesRead, Win32_LPOVERLAPPED lpOverlapped
|
||||
) @extern("ReadFile");
|
||||
|
||||
extern fn Char16* _wgetcwd(Char16* buffer, int maxlen);
|
||||
extern fn usz wcslen(Char16* str);
|
||||
extern fn WString _wgetcwd(Char16* buffer, int maxlen);
|
||||
extern fn usz wcslen(WString str);
|
||||
|
||||
extern fn int _open_osfhandle(iptr osfhandle, int flags);
|
||||
extern fn iptr _get_osfhandle(int fd);
|
||||
extern fn CFile _fdopen(int fd, ZString mode);
|
||||
extern fn CInt _access(ZString path, CInt mode);
|
||||
extern fn CInt _waccess(Char16* path, CInt mode);
|
||||
extern fn CInt _waccess(WString path, CInt mode);
|
||||
|
||||
/*
|
||||
extern ulong _win32_GetCurrentDirectoryW(ulong, Char16* buffer) @extern("GetCurrentDirectoryW");
|
||||
extern bool _win32_CreateSymbolicLinkW(Char16* symlink_file, Char16* target_file, ulong flags) @extern("CreateSymbolicLinkW");
|
||||
extern bool _win32_CopyFileW(Char16* from_file, Char16* to_file, bool no_overwrite) @extern("CopyFileW");
|
||||
extern ulong _win32_GetFullPathNameW(Char16* file_name, ulong buffer_len, Char16* buffer, Char16** file_part) @extern("GetFullPathNameW");
|
||||
extern bool _win32_CreateSymbolicLinkW(WString symlink_file, WString target_file, ulong flags) @extern("CreateSymbolicLinkW");
|
||||
extern bool _win32_CopyFileW(WString from_file, WString to_file, bool no_overwrite) @extern("CopyFileW");
|
||||
extern ulong _win32_GetFullPathNameW(WString file_name, ulong buffer_len, Char16* buffer, WString* file_part) @extern("GetFullPathNameW");
|
||||
*/
|
||||
|
||||
@@ -88,7 +88,7 @@ def Win32_PCHAR = Win32_CHAR*;
|
||||
def Win32_PCSTR = Win32_CHAR*;
|
||||
def Win32_PCTSTR = Win32_LPCWSTR;
|
||||
def Win32_PCUNICODE_STRING = Win32_UNICODE_STRING*;
|
||||
def Win32_PCWSTR = Char16*;
|
||||
def Win32_PCWSTR = WString;
|
||||
def Win32_PDWORD = Win32_DWORD*;
|
||||
def Win32_PDWORDLONG = Win32_DWORDLONG*;
|
||||
def Win32_PDWORDPTR = Win32_DWORD_PTR*;
|
||||
|
||||
Reference in New Issue
Block a user