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:
parent
09e23e3ed6
commit
3e4cfc7343
@ -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 {
|
||||
|
@ -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'
|
||||
|
Loading…
x
Reference in New Issue
Block a user