os: minor clean ups on filepath.v (#14506)

master
Ben 2022-05-24 10:29:32 +02:00 committed by GitHub
parent f35f7fe997
commit f431020764
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 14 additions and 14 deletions

View File

@ -13,7 +13,7 @@ const (
dot = `.` dot = `.`
qmark = `?` qmark = `?`
dot_dot = '..' dot_dot = '..'
empty = '' empty_str = ''
dot_str = '.' dot_str = '.'
) )
@ -59,14 +59,14 @@ pub fn abs_path(path string) string {
[direct_array_access] [direct_array_access]
pub fn norm_path(path string) string { pub fn norm_path(path string) string {
if path.len == 0 { if path.len == 0 {
return '.' return os.dot_str
} }
rooted := is_abs_path(path) rooted := is_abs_path(path)
volume := get_volume(path) volume := get_volume(path)
volume_len := volume.len volume_len := volume.len
cpath := clean_path(path[volume_len..]) cpath := clean_path(path[volume_len..])
if cpath.len == 0 && volume_len == 0 { if cpath.len == 0 && volume_len == 0 {
return '.' return os.dot_str
} }
spath := cpath.split(path_separator) spath := cpath.split(path_separator)
if os.dot_dot !in spath { if os.dot_dot !in spath {
@ -82,7 +82,7 @@ pub fn norm_path(path string) string {
mut backlink_count := 0 mut backlink_count := 0
for i := spath_len - 1; i >= 0; i-- { for i := spath_len - 1; i >= 0; i-- {
part := spath[i] part := spath[i]
if part == os.empty { if part == os.empty_str {
continue continue
} }
if part == os.dot_dot { if part == os.dot_dot {
@ -113,7 +113,7 @@ pub fn norm_path(path string) string {
return volume return volume
} }
if !rooted { if !rooted {
return '.' return os.dot_str
} }
return path_separator return path_separator
} }
@ -131,7 +131,7 @@ pub fn norm_path(path string) string {
// - the last path separator // - the last path separator
fn clean_path(path string) string { fn clean_path(path string) string {
if path.len == 0 { if path.len == 0 {
return '' return os.empty_str
} }
mut sb := strings.new_builder(path.len) mut sb := strings.new_builder(path.len)
mut sc := textscanner.new(path) mut sc := textscanner.new(path)
@ -201,11 +201,11 @@ fn win_volume_len(path string) int {
fn get_volume(path string) string { fn get_volume(path string) string {
$if !windows { $if !windows {
return '' return os.empty_str
} }
volume := path[..win_volume_len(path)] volume := path[..win_volume_len(path)]
if volume.len == 0 { if volume.len == 0 {
return '' return os.empty_str
} }
if volume[0] == os.fslash { if volume[0] == os.fslash {
return volume.replace('/', '\\') return volume.replace('/', '\\')