Skip to content

Commit a0704c4

Browse files
authored
Merge pull request #8 from picobyte/patch-1
add from_backslash() and _lossy() functions
2 parents 23bcfbf + 78a5a32 commit a0704c4

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

Diff for: README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Rust library to convert a file path from/to slash path
77
[`path-slash`][crates-io] is a tiny library to convert a file path (e.g. `foo/bar`, `foo\bar` or
88
`C:\foo\bar`) from/to slash path (e.g. `foo/bar`, `C:/foo/bar`).
99

10-
On Unix-like OS, path separator is slash `/` by default. So any conversion is not necessary. But on
10+
On Unix-like OS, path separator is slash `/` by default. One may want to convert a Windows path. But on
1111
Windows, file path separator `\` needs to be replaced with slash `/` (and of course `\`s for escaping
1212
characters should not be replaced).
1313

@@ -25,6 +25,8 @@ and `std::path::PathBuf` gains some methods and associated functions
2525
- `PathBufExt`
2626
- `PathBuf::from_slash<S: AsRef<str>>(s: S) -> PathBuf`
2727
- `PathBuf::from_slash_lossy<S: AsRef<OsStr>>(s: S) -> PathBuf`
28+
- `PathBuf::from_backslash<S: AsRef<str>>(s: S) -> PathBuf`
29+
- `PathBuf::from_backslash_lossy<S: AsRef<OsStr>>(s: S) -> PathBuf`
2830
- `PathBuf::to_slash(&self) -> Option<String>`
2931
- `PathBuf::to_slash_lossy(&self) -> String`
3032

Diff for: src/lib.rs

+39
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,8 @@ impl PathExt for Path {
248248
pub trait PathBufExt {
249249
fn from_slash<S: AsRef<str>>(s: S) -> Self;
250250
fn from_slash_lossy<S: AsRef<OsStr>>(s: S) -> Self;
251+
fn from_backslash<S: AsRef<str>>(s: S) -> Self;
252+
fn from_backslash_lossy<S: AsRef<OsStr>>(s: S) -> Self;
251253
fn to_slash(&self) -> Option<String>;
252254
fn to_slash_lossy(&self) -> String;
253255
}
@@ -313,6 +315,43 @@ impl PathBufExt for PathBuf {
313315
PathBuf::from(s)
314316
}
315317

318+
/// Convert the backslash path (path separated with '\') to [`std::path::PathBuf`].
319+
///
320+
/// Any '\' in the slash path is replaced with the file path separator.
321+
/// The replacements only happen on non-Windows.
322+
#[cfg(not(target_os = "windows"))]
323+
fn from_backslash<S: AsRef<str>>(s: S) -> Self {
324+
use std::path;
325+
326+
let s = s
327+
.as_ref()
328+
.chars()
329+
.map(|c| match c {
330+
'\\' => path::MAIN_SEPARATOR,
331+
c => c,
332+
})
333+
.collect::<String>();
334+
PathBuf::from(s)
335+
}
336+
337+
#[cfg(target_os = "windows")]
338+
fn from_backslash<S: AsRef<str>>(s: S) -> Self {
339+
PathBuf::from(s.as_ref())
340+
}
341+
342+
/// Convert the backslash path (path separated with '\') to [`std::path::PathBuf`].
343+
///
344+
/// Any '\' in the slash path is replaced with the file path separator.
345+
#[cfg(not(target_os = "windows"))]
346+
fn from_backslash_lossy<S: AsRef<OsStr>>(s: S) -> Self {
347+
PathBuf::from(s.as_ref().to_string_lossy().replace(r"\", "/").chars().as_str())
348+
}
349+
350+
#[cfg(target_os = "windows")]
351+
fn from_backslash_lossy<S: AsRef<OsStr>>(s: S) -> Self {
352+
PathBuf::from(s.as_ref())
353+
}
354+
316355
/// Convert the slash path (path separated with '/') to [`std::path::PathBuf`].
317356
///
318357
/// Any '/' in the slash path is replaced with the file path separator.

0 commit comments

Comments
 (0)