Skip to content

Commit

Permalink
use constant
Browse files Browse the repository at this point in the history
  • Loading branch information
StunxFS committed Nov 14, 2023
1 parent c069568 commit b8ec697
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions lib/std/src/fs/Path.ri
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public struct Path {
/// Returns `true` if the given byte is a valid path separator
#[inline]
public func is_separator(byte: uint8) -> bool {
return byte == b'/' #if _WINDOWS_ or byte == b'\\' #endif;
return byte == SEPARATOR;
}

/// Returns `true` if `path` (file or directory) exists.
Expand Down Expand Up @@ -81,13 +81,13 @@ public struct Path {
/// If the path is a file in the current directory (no directory component)
/// then returns `.`.
/// If the path is the root directory, returns `/`.
public func dir_name(path: string) -> ?string {
public func dirname(path: string) -> ?string {
if path.is_empty() {
return none;
}
if pos := path.last_index_of_byte(SEPARATOR) {
if pos == 0 and SEPARATOR == b'/' {
return "/";
if pos == 0 and SEPARATOR == SEPARATOR {
return separatorStr;
}
return path[..pos];
}
Expand All @@ -99,21 +99,23 @@ public struct Path {
return "";
}
mut end_index: uint := path.len - 1;
while path[end_index] == b'/' : end_index -= 1 {
while path[end_index] == SEPARATOR : end_index -= 1 {
if end_index == 0 {
return "";
}
}
mut start_index: uint := end_index;
end_index += 1;
while path[start_index] != b'/' : start_index -= 1 {
while path[start_index] != SEPARATOR : start_index -= 1 {
if start_index == 0 {
return path[0..end_index];
}
}
return path[start_index + 1 .. end_index];
}
/// Return alls characters found after the last occurrence of `separatorStr`.
/// File extension is included.
public func file_name(path: string) -> string {
return path.all_after_of_last(separatorStr);
}
Expand All @@ -123,8 +125,6 @@ public struct Path {
/// return the text after the `.`.
/// Files that end with `.`, or that start with `.` and have no other `.` in their
/// name, are considered to have no extension.
/// The returned slice is guaranteed to have its pointer within the start and end
/// pointer address range of `path`, even if it is length zero.
public func extension(path: string) -> string {
filename := Self.file_name(path);
index := filename.last_index_of_byte(b'.') ?? return "";
Expand Down

0 comments on commit b8ec697

Please sign in to comment.