Skip to content

Commit 391fe3f

Browse files
committed
---
yaml --- r: 81606 b: refs/heads/snap-stage3 c: 1dfe508 h: refs/heads/master v: v3
1 parent c64a884 commit 391fe3f

File tree

4 files changed

+74
-1
lines changed

4 files changed

+74
-1
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
refs/heads/master: 4c6bf4872012c010f84dc7fa2cdfe87522533f89
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
4-
refs/heads/snap-stage3: c16d7a439441ecfb8ac53deee070bb616fdc759e
4+
refs/heads/snap-stage3: 1dfe5088d85df13a172810d0df92c25dbcc37e98
55
refs/heads/try: 70152ff55722878cde684ee6462c14c65f2c4729
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b

branches/snap-stage3/src/libstd/path2/mod.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,17 @@ pub trait GenericPath: Clone + GenericPathUnsafe {
8181
}
8282
}
8383

84+
/// Creates a new Path from a byte vector, if possible.
85+
/// The resulting Path will always be normalized.
86+
#[inline]
87+
fn from_vec_opt(path: &[u8]) -> Option<Self> {
88+
if contains_nul(path) {
89+
None
90+
} else {
91+
Some(unsafe { GenericPathUnsafe::from_vec_unchecked(path) })
92+
}
93+
}
94+
8495
/// Creates a new Path from a string.
8596
/// The resulting Path will always be normalized.
8697
///
@@ -97,6 +108,18 @@ pub trait GenericPath: Clone + GenericPathUnsafe {
97108
}
98109
}
99110

111+
/// Creates a new Path from a string, if possible.
112+
/// The resulting Path will always be normalized.
113+
#[inline]
114+
fn from_str_opt(path: &str) -> Option<Self> {
115+
let v = path.as_bytes();
116+
if contains_nul(v) {
117+
None
118+
} else {
119+
Some(unsafe { GenericPathUnsafe::from_str_unchecked(path) })
120+
}
121+
}
122+
100123
/// Creates a new Path from a CString.
101124
/// The resulting Path will always be normalized.
102125
///

branches/snap-stage3/src/libstd/path2/posix.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,12 @@ impl Path {
287287
GenericPath::from_vec(v)
288288
}
289289

290+
/// Returns a new Path from a byte vector, if possible
291+
#[inline]
292+
pub fn from_vec_opt(v: &[u8]) -> Option<Path> {
293+
GenericPath::from_vec_opt(v)
294+
}
295+
290296
/// Returns a new Path from a string
291297
///
292298
/// # Failure
@@ -297,6 +303,12 @@ impl Path {
297303
GenericPath::from_str(s)
298304
}
299305

306+
/// Returns a new Path from a string, if possible
307+
#[inline]
308+
pub fn from_str_opt(s: &str) -> Option<Path> {
309+
GenericPath::from_str_opt(s)
310+
}
311+
300312
/// Converts the Path into an owned byte vector
301313
pub fn into_vec(self) -> ~[u8] {
302314
self.repr
@@ -475,6 +487,14 @@ mod tests {
475487
assert_eq!(Path::from_vec(b!("foo", 0xff, "/bar")).into_str(), None);
476488
}
477489
490+
#[test]
491+
fn test_opt_paths() {
492+
assert_eq!(Path::from_vec_opt(b!("foo/bar", 0)), None);
493+
t!(v: Path::from_vec_opt(b!("foo/bar")).unwrap(), b!("foo/bar"));
494+
assert_eq!(Path::from_str_opt("foo/bar\0"), None);
495+
t!(s: Path::from_str_opt("foo/bar").unwrap(), "foo/bar");
496+
}
497+
478498
#[test]
479499
fn test_null_byte() {
480500
use path2::null_byte::cond;

branches/snap-stage3/src/libstd/path2/windows.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,15 @@ impl GenericPathUnsafe for Path {
328328
}
329329

330330
impl GenericPath for Path {
331+
#[inline]
332+
fn from_vec_opt(v: &[u8]) -> Option<Path> {
333+
if contains_nul(v) || !str::is_utf8(v) {
334+
None
335+
} else {
336+
Some(unsafe { GenericPathUnsafe::from_vec_unchecked(v) })
337+
}
338+
}
339+
331340
/// See `GenericPath::as_str` for info.
332341
/// Always returns a `Some` value.
333342
#[inline]
@@ -583,6 +592,12 @@ impl Path {
583592
GenericPath::from_vec(v)
584593
}
585594

595+
/// Returns a new Path from a byte vector, if possible
596+
#[inline]
597+
pub fn from_vec_opt(v: &[u8]) -> Option<Path> {
598+
GenericPath::from_vec_opt(v)
599+
}
600+
586601
/// Returns a new Path from a string
587602
///
588603
/// # Failure
@@ -593,6 +608,12 @@ impl Path {
593608
GenericPath::from_str(s)
594609
}
595610

611+
/// Returns a new Path from a string, if possible
612+
#[inline]
613+
pub fn from_str_opt(s: &str) -> Option<Path> {
614+
GenericPath::from_str_opt(s)
615+
}
616+
596617
/// Converts the Path into an owned byte vector
597618
pub fn into_vec(self) -> ~[u8] {
598619
self.repr.into_bytes()
@@ -1201,6 +1222,15 @@ mod tests {
12011222
t!(s: Path::from_str("\\\\.\\foo\\bar"), "\\\\.\\foo\\bar");
12021223
}
12031224

1225+
#[test]
1226+
fn test_opt_paths() {
1227+
assert_eq!(Path::from_vec_opt(b!("foo\\bar", 0)), None);
1228+
assert_eq!(Path::from_vec_opt(b!("foo\\bar", 0x80)), None);
1229+
t!(v: Path::from_vec_opt(b!("foo\\bar")).unwrap(), b!("foo\\bar"));
1230+
assert_eq!(Path::from_str_opt("foo\\bar\0"), None);
1231+
t!(s: Path::from_str_opt("foo\\bar").unwrap(), "foo\\bar");
1232+
}
1233+
12041234
#[test]
12051235
fn test_null_byte() {
12061236
use path2::null_byte::cond;

0 commit comments

Comments
 (0)