1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

os: add os.to_slash and os.from_slash functions (#16055)

This commit is contained in:
zztkm 2022-10-14 16:22:36 +09:00 committed by GitHub
parent 09e23e3ed6
commit 3e4cfc7343
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 0 deletions

View File

@ -220,6 +220,24 @@ fn clean_path(path string) string {
return res
}
// to_slash returns the result of replacing each separator character
// in path with a slash (`/`).
pub fn to_slash(path string) string {
if path_separator == '/' {
return path
}
return path.replace(path_separator, '/')
}
// from_slash returns the result of replacing each slash (`/`) character
// is path with a separator character.
pub fn from_slash(path string) string {
if path_separator == '/' {
return path
}
return path.replace('/', path_separator)
}
// win_volume_len returns the length of the
// Windows volume/drive from the given `path`.
fn win_volume_len(path string) int {

View File

@ -53,6 +53,22 @@ fn test_clean_path() {
assert clean_path('//////////') == '/'
}
fn test_to_slash() {
sep := path_separator
assert to_slash('') == ''
assert to_slash(sep) == ('/')
assert to_slash([sep, 'a', sep, 'b'].join('')) == '/a/b'
assert to_slash(['a', sep, sep, 'b'].join('')) == 'a//b'
}
fn test_from_slash() {
sep := path_separator
assert from_slash('') == ''
assert from_slash('/') == sep
assert from_slash('/a/b') == [sep, 'a', sep, 'b'].join('')
assert from_slash('a//b') == ['a', sep, sep, 'b'].join('')
}
fn test_norm_path() {
$if windows {
assert norm_path(r'C:/path/to//file.v\\') == r'C:\path\to\file.v'