Skip to content

Commit

Permalink
as_str() replaced by str_view()
Browse files Browse the repository at this point in the history
  • Loading branch information
lerno committed Sep 24, 2023
1 parent 3675254 commit a1ecf22
Show file tree
Hide file tree
Showing 31 changed files with 228 additions and 227 deletions.
2 changes: 1 addition & 1 deletion lib/std/core/builtin.c3
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ fn void panicf(String fmt, String file, String function, uint line, args...)
DString s;
s.init(.using = mem);
s.printf(fmt, ...args);
panic(s.as_str(), file, function, line);
panic(s.str_view(), file, function, line);
};
}

Expand Down
6 changes: 3 additions & 3 deletions lib/std/core/dstring.c3
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ fn void DString.chop(self, usz new_size)
self.data().len = new_size;
}

fn String DString.as_str(self)
fn String DString.str_view(self)
{
StringData* data = (StringData*)self;
if (!data) return "";
Expand Down Expand Up @@ -261,14 +261,14 @@ fn void DString.append_chars(&self, String str)

fn Char32[] DString.copy_utf32(&self, Allocator* using = mem::heap())
{
return self.as_str().to_utf32(using) @inline!!;
return self.str_view().to_utf32(using) @inline!!;
}

fn void DString.append_string(&self, DString str)
{
StringData* other = (StringData*)str;
if (!other) return;
self.append(str.as_str());
self.append(str.str_view());
}

fn void DString.clear(self)
Expand Down
4 changes: 2 additions & 2 deletions lib/std/core/runtime.c3
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ fn bool run_benchmarks(BenchmarkUnit[] benchmarks)
defer name.clear();
name.printf("Benchmarking %s ", unit.name);
name.append_repeat('.', max_name - unit.name.len + 2);
io::printf("%s ", name.as_str());
io::printf("%s ", name.str_view());

for (uint i = 0; i < benchmark_warmup_iterations; i++)
{
Expand Down Expand Up @@ -225,7 +225,7 @@ fn bool run_tests(TestUnit[] tests)
defer name.clear();
name.printf("Testing %s ", unit.name);
name.append_repeat('.', max_name - unit.name.len + 2);
io::printf("%s ", name.as_str());
io::printf("%s ", name.str_view());
CallstackElement* stack = $$stacktrace();
if (stack) stack.prev = null;
if (libc::setjmp(&context.buf) == 0)
Expand Down
8 changes: 4 additions & 4 deletions lib/std/core/string.c3
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ macro String tprintf(String fmt, ...)
DString str;
str.tinit();
str.printf(fmt, $vasplat());
return str.as_str();
return str.str_view();
}

macro bool char_in_set(char c, String set)
Expand Down Expand Up @@ -288,7 +288,7 @@ fn usz! String.rindex_of(s, String needle)
return SearchResult.MISSING?;
}

fn String ZString.as_str(str)
fn String ZString.str_view(str)
{
return (String)(str[:str.len()]);
}
Expand Down Expand Up @@ -355,8 +355,8 @@ fn void String.free(&s, Allocator* using = mem::heap())

fn String String.tcopy(s) => s.copy(mem::temp()) @inline;

fn String ZString.copy(z, Allocator* using = mem::heap()) => z.as_str().copy(using) @inline;
fn String ZString.tcopy(z) => z.as_str().copy(mem::temp()) @inline;
fn String ZString.copy(z, Allocator* using = mem::heap()) => z.str_view().copy(using) @inline;
fn String ZString.tcopy(z) => z.str_view().copy(mem::temp()) @inline;

/**
* Convert an UTF-8 string to UTF-16
Expand Down
8 changes: 4 additions & 4 deletions lib/std/encoding/json.c3
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ fn Object*! parse_from_token(JsonContext* context, JsonTokenType token) @local
case RBRACE:
case RBRACKET:
case COLON: return JsonParsingError.UNEXPECTED_CHARACTER?;
case STRING: return object::new_string(context.last_string.as_str(), context.allocator);
case STRING: return object::new_string(context.last_string.str_view(), context.allocator);
case NUMBER: return object::new_float(context.last_number, context.allocator);
case TRUE: return object::new_bool(true);
case FALSE: return object::new_bool(false);
Expand Down Expand Up @@ -125,7 +125,7 @@ fn JsonTokenType! lex_number(JsonContext *context, char c) @local
}
}
pushback(context, c);
double! d = t.as_str().to_double() ?? JsonParsingError.INVALID_NUMBER?;
double! d = t.str_view().to_double() ?? JsonParsingError.INVALID_NUMBER?;
context.last_number = d!;
return NUMBER;
};
Expand All @@ -143,14 +143,14 @@ fn Object*! parse_map(JsonContext* context) @local
{
if (token != JsonTokenType.STRING) return JsonParsingError.UNEXPECTED_CHARACTER?;
DString string = context.last_string;
if (map.has_key(string.as_str())) return JsonParsingError.DUPLICATE_MEMBERS?;
if (map.has_key(string.str_view())) return JsonParsingError.DUPLICATE_MEMBERS?;
// Copy the key to our temp holder. We do this to work around the issue
// if the temp allocator should be used as the default allocator.
temp_key.clear();
temp_key.append(string);
parse_expected(context, COLON)!;
Object* element = parse_any(context)!;
map.set(temp_key.as_str(), element);
map.set(temp_key.str_view(), element);
token = advance(context)!;
if (token == JsonTokenType.COMMA)
{
Expand Down
2 changes: 1 addition & 1 deletion lib/std/io/file.c3
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ fn File! open(String filename, String mode)

fn File! open_path(Path path, String mode)
{
return from_handle(os::native_fopen(path.as_str(), mode));
return from_handle(os::native_fopen(path.str_view(), mode));
}

fn File from_handle(CFile file)
Expand Down
4 changes: 2 additions & 2 deletions lib/std/io/formatter.c3
Original file line number Diff line number Diff line change
Expand Up @@ -173,11 +173,11 @@ fn usz! Formatter.out_str(&self, any arg) @private
case DISTINCT:
if (arg.type == ZString.typeid)
{
return self.out_substr(((ZString*)arg).as_str());
return self.out_substr(((ZString*)arg).str_view());
}
if (arg.type == DString.typeid)
{
return self.out_substr(((DString*)arg).as_str());
return self.out_substr(((DString*)arg).str_view());
}
return self.out_str(any { arg.ptr, arg.type.inner });
case POINTER:
Expand Down
2 changes: 1 addition & 1 deletion lib/std/io/os/chdir.c3
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ macro void! native_chdir(Path path)
@pool()
{
// TODO improve with better error handling.
if (win32::setCurrentDirectoryW(path.as_str().to_temp_utf16()!!)) return;
if (win32::setCurrentDirectoryW(path.str_view().to_temp_utf16()!!)) return;
};
return IoError.GENERAL_ERROR?;
$default:
Expand Down
6 changes: 3 additions & 3 deletions lib/std/io/os/ls.c3
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ fn PathList! native_ls(Path dir, bool no_dirs, bool no_symlinks, String mask, Al
{
PathList list;
list.init(.using = using);
DIRPtr directory = posix::opendir(dir.as_str() ? dir.as_zstr() : (ZString)".");
DIRPtr directory = posix::opendir(dir.str_view() ? dir.as_zstr() : (ZString)".");
defer if (directory) posix::closedir(directory);
if (!directory) return (path::is_dir(dir) ? IoError.CANNOT_READ_DIR : IoError.FILE_NOT_DIR)?;
Posix_dirent* entry;
while ((entry = posix::readdir(directory)))
{
String name = ((ZString)&entry.name).as_str();
String name = ((ZString)&entry.name).str_view();
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;
Expand All @@ -29,7 +29,7 @@ fn PathList! native_ls(Path dir, bool no_dirs, bool no_symlinks, String mask, Al

@pool(using)
{
WString result = dir.as_str().tconcat(`\*`).to_temp_wstring()!!;
WString result = dir.str_view().tconcat(`\*`).to_temp_wstring()!!;
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?;
Expand Down
2 changes: 1 addition & 1 deletion lib/std/io/os/mkdir.c3
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ macro bool! native_mkdir(Path path, MkdirPermissions permissions)
@pool()
{
// TODO security attributes
if (win32::createDirectoryW(path.as_str().to_temp_utf16()!!, null)) return true;
if (win32::createDirectoryW(path.str_view().to_temp_utf16()!!, null)) return true;
switch (win32::getLastError())
{
case win32::ERROR_ACCESS_DENIED:
Expand Down
2 changes: 1 addition & 1 deletion lib/std/io/os/rmdir.c3
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ macro bool! native_rmdir(Path path)
$case env::WIN32:
@pool()
{
if (win32::removeDirectoryW(path.as_str().to_temp_utf16()!!)) return true;
if (win32::removeDirectoryW(path.str_view().to_temp_utf16()!!)) return true;
switch (win32::getLastError())
{
case win32::ERROR_ACCESS_DENIED:
Expand Down
8 changes: 4 additions & 4 deletions lib/std/io/os/rmtree.c3
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module std::io::file::os @if(env::POSIX);
import libc;

/**
* @require dir.as_str()
* @require dir.str_view()
**/
fn void! native_rmtree(Path dir)
{
Expand All @@ -14,7 +14,7 @@ fn void! native_rmtree(Path dir)
{
@pool()
{
String name = ((ZString)&entry.name).as_str();
String name = ((ZString)&entry.name).str_view();
if (!name || name == "." || name == "..") continue;
Path new_path = dir.tappend(name)!;
if (entry.d_type == posix::DT_DIR)
Expand All @@ -37,7 +37,7 @@ module std::io::os @if(env::WIN32);
fn void! native_rmtree(Path path)
{
Win32_WIN32_FIND_DATAW find_data;
String s = path.as_str().tconcat("\\*");
String s = path.str_view().tconcat("\\*");
Win32_HANDLE find = win32::findFirstFileW(s.to_utf16(mem::temp()), &find_data)!;

if (find == win32::INVALID_HANDLE_VALUE) return IoError.CANNOT_READ_DIR?;
Expand All @@ -55,7 +55,7 @@ fn void! native_rmtree(Path path)
}
else
{
win32::deleteFileW(file_path.as_str().to_temp_wstring()!!);
win32::deleteFileW(file_path.str_view().to_temp_wstring()!!);
}
};
} while (win32::findNextFileW(find, &find_data) != 0);
Expand Down
26 changes: 13 additions & 13 deletions lib/std/io/path.c3
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ fn Path! getcwd(Allocator* using = mem::heap())
};
}

fn bool is_dir(Path path) => os::native_is_dir(path.as_str());
fn bool is_file(Path path) => os::native_is_file(path.as_str());
fn usz! file_size(Path path) => os::native_file_size(path.as_str());
fn bool exists(Path path) => os::native_file_or_dir_exists(path.as_str());
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! tgetcwd() => getcwd(mem::temp()) @inline;
fn void! chdir(Path path) => os::native_chdir(path) @inline;
fn Path! temp_directory(Allocator* using = mem::heap()) => os::native_temp_directory(using);
fn void! delete(Path path) => os::native_remove(path.as_str()) @inline;
fn void! delete(Path path) => os::native_remove(path.str_view()) @inline;

macro bool is_separator(char c, PathEnv path_env = DEFAULT_PATH_ENV)
{
Expand Down Expand Up @@ -168,15 +168,15 @@ fn usz Path.start_of_base_name(self) @local

fn bool! Path.is_absolute(self)
{
String path_str = self.as_str();
String path_str = self.str_view();
if (!path_str.len) return false;
usz path_start = volume_name_len(path_str, self.env)!;
return path_start < path_str.len && is_separator(path_str[path_start], self.env);
}

fn Path! Path.absolute(self, Allocator* using = mem::heap())
{
String path_str = self.as_str();
String path_str = self.str_view();
if (!path_str.len) path_str = ".";
if (path_str == ".")
{
Expand Down Expand Up @@ -225,7 +225,7 @@ fn String! Path.extension(self)

fn String Path.volume_name(self)
{
usz len = volume_name_len(self.as_str(), self.env)!!;
usz len = volume_name_len(self.str_view(), self.env)!!;
if (!len) return "";
return self.path_string[:len];
}
Expand Down Expand Up @@ -387,7 +387,7 @@ fn ZString Path.as_zstr(self) => (ZString)self.path_string.ptr;

fn String Path.root_directory(self)
{
String path_str = self.as_str();
String path_str = self.str_view();
usz len = path_str.len;
if (!len) return "";
if (self.env == PathEnv.WIN32)
Expand Down Expand Up @@ -422,8 +422,8 @@ fn bool! Path.walk(self, PathWalker w, void* data)
PathList files = ls(abs, .using = using)!;
foreach (f : files)
{
if (f.as_str() == "." || f.as_str() == "..") continue;
f = abs.append(f.as_str(), using)!;
if (f.str_view() == "." || f.str_view() == "..") continue;
f = abs.append(f.str_view(), using)!;
bool is_directory = is_dir(f);
if (w(f, is_directory, data)!) return true;
if (is_directory && f.walk(w, data)!) return true;
Expand All @@ -432,15 +432,15 @@ fn bool! Path.walk(self, PathWalker w, void* data)
return false;
}

fn String Path.as_str(self) @inline
fn String Path.str_view(self) @inline
{
return self.path_string;
}


fn bool Path.has_suffix(self, String str)
{
return self.as_str().ends_with(str);
return self.str_view().ends_with(str);
}

fn void Path.free(self)
Expand Down
4 changes: 2 additions & 2 deletions lib/std/io/stream.c3
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,9 @@ macro usz! print_gen(self, x)
$case String:
return self.write(x);
$case ZString:
return self.write(x.as_str());
return self.write(x.str_view());
$case DString:
return self.write(x.as_str());
return self.write(x.str_view());
$default:
$if @convertible(x, String):
return self.write((String)x);
Expand Down
4 changes: 2 additions & 2 deletions lib/std/io/stream/buffer.c3
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const StreamInterface READBUFFER_INTERFACE = {
.read_byte_fn = (ReadByteStreamFn)&ReadBuffer.read_byte,
};

fn String ReadBuffer.as_str(&self) @inline
fn String ReadBuffer.str_view(&self) @inline
{
return (String)self.bytes[self.read_idx:self.write_idx - self.read_idx];
}
Expand Down Expand Up @@ -90,7 +90,7 @@ const StreamInterface WRITEBUFFER_INTERFACE = {
.write_byte_fn = (WriteByteStreamFn)&WriteBuffer.write_byte,
};

fn String WriteBuffer.as_str(&self) @inline
fn String WriteBuffer.str_view(&self) @inline
{
return (String)self.bytes[:self.index];
}
Expand Down
2 changes: 1 addition & 1 deletion lib/std/io/stream/bytewriter.c3
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ fn void ByteWriter.destroy(&self)
*self = { };
}

fn String ByteWriter.as_str(&self) @inline
fn String ByteWriter.str_view(&self) @inline
{
return (String)self.bytes[:self.index];
}
Expand Down
2 changes: 1 addition & 1 deletion lib/std/os/env.c3
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ fn String! get_var(String name)
@pool()
{
ZString val = libc::getenv(name.zstr_tcopy());
return val ? val.as_str() : SearchResult.MISSING?;
return val ? val.str_view() : SearchResult.MISSING?;
};
$else
return "";
Expand Down
2 changes: 1 addition & 1 deletion lib/std/os/macos/darwin.c3
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ fn uptr! load_address() @local
{
ZString image_name = darwin::_dyld_get_image_name(i);
if (!image_name) continue;
if (image_name.as_str() != path) continue;
if (image_name.str_view() != path) continue;
return cmd.vmaddr + darwin::_dyld_get_image_vmaddr_slide(i);
}
return BacktraceFault.IMAGE_NOT_FOUND?;
Expand Down
4 changes: 2 additions & 2 deletions lib/std/os/subprocess.c3
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ fn WString convert_command_line_win32(String[] command_line) @inline @if(env::WI
str.append('"');
}
str.append('\0');
return str.as_str().to_temp_wstring()!!;
return str.str_view().to_temp_wstring()!!;
}

/**
Expand Down Expand Up @@ -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_temp_wstring()!;
used_environment = env.str_view().to_temp_wstring()!;
}
int fd = win32::_open_osfhandle((iptr)wr, 0);
if (fd != -1)
Expand Down
2 changes: 1 addition & 1 deletion lib/std/time/time.c3
Original file line number Diff line number Diff line change
Expand Up @@ -160,5 +160,5 @@ fn usz! NanoDuration.to_format(&self, Formatter* formatter) @dynamic
str.printf("%ds", sec);
}
}
return formatter.printf(str.as_str())!;
return formatter.printf(str.str_view())!;
}
Loading

0 comments on commit a1ecf22

Please sign in to comment.