Skip to content

Commit f13086f

Browse files
committed
Expose platform independent path separators
1 parent bf07c80 commit f13086f

File tree

3 files changed

+62
-41
lines changed

3 files changed

+62
-41
lines changed

src/libstd/path/mod.rs

+14
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,20 @@ pub use StrComponents = self::windows::StrComponents;
117117
#[cfg(windows)]
118118
pub use RevStrComponents = self::windows::RevStrComponents;
119119

120+
/// Alias for the platform-native separator character.
121+
#[cfg(unix)]
122+
pub use SEP = self::posix::SEP;
123+
/// Alias for the platform-native separator byte.
124+
#[cfg(windows)]
125+
pub use SEP = self::windows::SEP;
126+
127+
/// Alias for the platform-native separator character.
128+
#[cfg(unix)]
129+
pub use SEP_BYTE = self::posix::SEP_BYTE;
130+
/// Alias for the platform-native separator byte.
131+
#[cfg(windows)]
132+
pub use SEP_BYTE = self::windows::SEP_BYTE;
133+
120134
/// Typedef for the platform-native separator char func
121135
#[cfg(unix)]
122136
pub use is_sep = self::posix::is_sep;

src/libstd/path/posix.rs

+21-19
Original file line numberDiff line numberDiff line change
@@ -45,19 +45,21 @@ pub struct Path {
4545
}
4646

4747
/// The standard path separator character
48-
pub static sep: char = '/';
49-
static sep_byte: u8 = sep as u8;
48+
pub static SEP: char = '/';
49+
50+
/// The standard path separator byte
51+
pub static SEP_BYTE: u8 = SEP as u8;
5052

5153
/// Returns whether the given byte is a path separator
5254
#[inline]
5355
pub fn is_sep_byte(u: &u8) -> bool {
54-
*u as char == sep
56+
*u as char == SEP
5557
}
5658

5759
/// Returns whether the given char is a path separator
5860
#[inline]
5961
pub fn is_sep(c: char) -> bool {
60-
c == sep
62+
c == SEP
6163
}
6264

6365
impl Eq for Path {
@@ -115,7 +117,7 @@ impl GenericPathUnsafe for Path {
115117
unsafe fn new_unchecked<T: BytesContainer>(path: T) -> Path {
116118
let path = Path::normalize(path.container_as_bytes());
117119
assert!(!path.is_empty());
118-
let idx = path.rposition_elem(&sep_byte);
120+
let idx = path.rposition_elem(&SEP_BYTE);
119121
Path{ repr: path, sepidx: idx }
120122
}
121123

@@ -125,7 +127,7 @@ impl GenericPathUnsafe for Path {
125127
None if bytes!("..") == self.repr => {
126128
let mut v = vec::with_capacity(3 + filename.len());
127129
v.push_all(dot_dot_static);
128-
v.push(sep_byte);
130+
v.push(SEP_BYTE);
129131
v.push_all(filename);
130132
self.repr = Path::normalize(v);
131133
}
@@ -135,7 +137,7 @@ impl GenericPathUnsafe for Path {
135137
Some(idx) if self.repr.slice_from(idx+1) == bytes!("..") => {
136138
let mut v = vec::with_capacity(self.repr.len() + 1 + filename.len());
137139
v.push_all(self.repr);
138-
v.push(sep_byte);
140+
v.push(SEP_BYTE);
139141
v.push_all(filename);
140142
self.repr = Path::normalize(v);
141143
}
@@ -146,22 +148,22 @@ impl GenericPathUnsafe for Path {
146148
self.repr = Path::normalize(v);
147149
}
148150
}
149-
self.sepidx = self.repr.rposition_elem(&sep_byte);
151+
self.sepidx = self.repr.rposition_elem(&SEP_BYTE);
150152
}
151153

152154
unsafe fn push_unchecked<T: BytesContainer>(&mut self, path: T) {
153155
let path = path.container_as_bytes();
154156
if !path.is_empty() {
155-
if path[0] == sep_byte {
157+
if path[0] == SEP_BYTE {
156158
self.repr = Path::normalize(path);
157159
} else {
158160
let mut v = vec::with_capacity(self.repr.len() + path.len() + 1);
159161
v.push_all(self.repr);
160-
v.push(sep_byte);
162+
v.push(SEP_BYTE);
161163
v.push_all(path);
162164
self.repr = Path::normalize(v);
163165
}
164-
self.sepidx = self.repr.rposition_elem(&sep_byte);
166+
self.sepidx = self.repr.rposition_elem(&SEP_BYTE);
165167
}
166168
}
167169
}
@@ -211,7 +213,7 @@ impl GenericPath for Path {
211213
} else {
212214
self.repr.truncate(idx);
213215
}
214-
self.sepidx = self.repr.rposition_elem(&sep_byte);
216+
self.sepidx = self.repr.rposition_elem(&SEP_BYTE);
215217
true
216218
}
217219
}
@@ -227,7 +229,7 @@ impl GenericPath for Path {
227229

228230
#[inline]
229231
fn is_absolute(&self) -> bool {
230-
self.repr[0] == sep_byte
232+
self.repr[0] == SEP_BYTE
231233
}
232234

233235
fn is_ancestor_of(&self, other: &Path) -> bool {
@@ -291,7 +293,7 @@ impl GenericPath for Path {
291293
}
292294
}
293295
}
294-
Some(Path::new(comps.connect_vec(&sep_byte)))
296+
Some(Path::new(comps.connect_vec(&SEP_BYTE)))
295297
}
296298
}
297299

@@ -333,14 +335,14 @@ impl Path {
333335
fn normalize<V: Vector<u8>+CopyableVector<u8>>(v: V) -> ~[u8] {
334336
// borrowck is being very picky
335337
let val = {
336-
let is_abs = !v.as_slice().is_empty() && v.as_slice()[0] == sep_byte;
338+
let is_abs = !v.as_slice().is_empty() && v.as_slice()[0] == SEP_BYTE;
337339
let v_ = if is_abs { v.as_slice().slice_from(1) } else { v.as_slice() };
338340
let comps = normalize_helper(v_, is_abs);
339341
match comps {
340342
None => None,
341343
Some(comps) => {
342344
if is_abs && comps.is_empty() {
343-
Some(~[sep_byte])
345+
Some(~[SEP_BYTE])
344346
} else {
345347
let n = if is_abs { comps.len() } else { comps.len() - 1} +
346348
comps.iter().map(|v| v.len()).sum();
@@ -353,7 +355,7 @@ impl Path {
353355
}
354356
}
355357
for comp in it {
356-
v.push(sep_byte);
358+
v.push(SEP_BYTE);
357359
v.push_all(comp);
358360
}
359361
Some(v)
@@ -372,7 +374,7 @@ impl Path {
372374
/// /a/b/c and a/b/c yield the same set of components.
373375
/// A path of "/" yields no components. A path of "." yields one component.
374376
pub fn components<'a>(&'a self) -> Components<'a> {
375-
let v = if self.repr[0] == sep_byte {
377+
let v = if self.repr[0] == SEP_BYTE {
376378
self.repr.slice_from(1)
377379
} else { self.repr.as_slice() };
378380
let mut ret = v.split(is_sep_byte);
@@ -386,7 +388,7 @@ impl Path {
386388
/// Returns an iterator that yields each component of the path in reverse.
387389
/// See components() for details.
388390
pub fn rev_components<'a>(&'a self) -> RevComponents<'a> {
389-
let v = if self.repr[0] == sep_byte {
391+
let v = if self.repr[0] == SEP_BYTE {
390392
self.repr.slice_from(1)
391393
} else { self.repr.as_slice() };
392394
let mut ret = v.rsplit(is_sep_byte);

src/libstd/path/windows.rs

+27-22
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ impl GenericPathUnsafe for Path {
183183
None if ".." == self.repr => {
184184
let mut s = str::with_capacity(3 + filename.len());
185185
s.push_str("..");
186-
s.push_char(sep);
186+
s.push_char(SEP);
187187
s.push_str(filename);
188188
self.update_normalized(s);
189189
}
@@ -193,7 +193,7 @@ impl GenericPathUnsafe for Path {
193193
Some((_,idxa,end)) if self.repr.slice(idxa,end) == ".." => {
194194
let mut s = str::with_capacity(end + 1 + filename.len());
195195
s.push_str(self.repr.slice_to(end));
196-
s.push_char(sep);
196+
s.push_char(SEP);
197197
s.push_str(filename);
198198
self.update_normalized(s);
199199
}
@@ -206,7 +206,7 @@ impl GenericPathUnsafe for Path {
206206
Some((idxb,_,_)) => {
207207
let mut s = str::with_capacity(idxb + 1 + filename.len());
208208
s.push_str(self.repr.slice_to(idxb));
209-
s.push_char(sep);
209+
s.push_char(SEP);
210210
s.push_str(filename);
211211
self.update_normalized(s);
212212
}
@@ -264,8 +264,8 @@ impl GenericPathUnsafe for Path {
264264
// if me is "C:" we don't want to add a path separator
265265
match me.prefix {
266266
Some(DiskPrefix) if me.repr.len() == plen => (),
267-
_ if !(me.repr.len() > plen && me.repr[me.repr.len()-1] == sep as u8) => {
268-
s.push_char(sep);
267+
_ if !(me.repr.len() > plen && me.repr[me.repr.len()-1] == SEP_BYTE) => {
268+
s.push_char(SEP);
269269
}
270270
_ => ()
271271
}
@@ -460,7 +460,7 @@ impl GenericPath for Path {
460460
match self.prefix {
461461
Some(DiskPrefix) => {
462462
let rest = self.repr.slice_from(self.prefix_len());
463-
rest.len() > 0 && rest[0] == sep as u8
463+
rest.len() > 0 && rest[0] == SEP_BYTE
464464
}
465465
Some(_) => true,
466466
None => false
@@ -501,7 +501,7 @@ impl GenericPath for Path {
501501

502502
fn path_relative_from(&self, base: &Path) -> Option<Path> {
503503
fn comp_requires_verbatim(s: &str) -> bool {
504-
s == "." || s == ".." || s.contains_char(sep2)
504+
s == "." || s == ".." || s.contains_char(SEP2)
505505
}
506506

507507
if !self.equiv_prefix(base) {
@@ -619,14 +619,14 @@ impl Path {
619619
let s = match self.prefix {
620620
Some(_) => {
621621
let plen = self.prefix_len();
622-
if self.repr.len() > plen && self.repr[plen] == sep as u8 {
622+
if self.repr.len() > plen && self.repr[plen] == SEP_BYTE {
623623
self.repr.slice_from(plen+1)
624624
} else { self.repr.slice_from(plen) }
625625
}
626-
None if self.repr[0] == sep as u8 => self.repr.slice_from(1),
626+
None if self.repr[0] == SEP_BYTE => self.repr.slice_from(1),
627627
None => self.repr.as_slice()
628628
};
629-
let ret = s.split_terminator(sep).map(Some);
629+
let ret = s.split_terminator(SEP).map(Some);
630630
ret
631631
}
632632

@@ -703,7 +703,7 @@ impl Path {
703703
Some(VerbatimUNCPrefix(x, 0)) if s.len() == 8 + x => {
704704
// the server component has no trailing '\'
705705
let mut s = s.into_owned();
706-
s.push_char(sep);
706+
s.push_char(SEP);
707707
Some(s)
708708
}
709709
_ => None
@@ -739,7 +739,7 @@ impl Path {
739739
if is_abs {
740740
// normalize C:/ to C:\
741741
unsafe {
742-
str::raw::as_owned_vec(&mut s)[2] = sep as u8;
742+
str::raw::as_owned_vec(&mut s)[2] = SEP_BYTE;
743743
}
744744
}
745745
Some(s)
@@ -761,7 +761,7 @@ impl Path {
761761
}
762762
}
763763
} else if is_abs && comps.is_empty() {
764-
Some(str::from_char(sep))
764+
Some(str::from_char(SEP))
765765
} else {
766766
let prefix_ = s.slice_to(prefix_len(prefix));
767767
let n = prefix_.len() +
@@ -781,7 +781,7 @@ impl Path {
781781
Some(UNCPrefix(a,b)) => {
782782
s.push_str("\\\\");
783783
s.push_str(prefix_.slice(2, a+2));
784-
s.push_char(sep);
784+
s.push_char(SEP);
785785
s.push_str(prefix_.slice(3+a, 3+a+b));
786786
}
787787
Some(_) => s.push_str(prefix_),
@@ -795,7 +795,7 @@ impl Path {
795795
}
796796
}
797797
for comp in it {
798-
s.push_char(sep);
798+
s.push_char(SEP);
799799
s.push_str(comp);
800800
}
801801
Some(s)
@@ -837,7 +837,7 @@ impl Path {
837837

838838
fn has_nonsemantic_trailing_slash(&self) -> bool {
839839
is_verbatim(self) && self.repr.len() > self.prefix_len()+1 &&
840-
self.repr[self.repr.len()-1] == sep as u8
840+
self.repr[self.repr.len()-1] == SEP_BYTE
841841
}
842842

843843
fn update_normalized<S: Str>(&mut self, s: S) {
@@ -877,36 +877,41 @@ pub fn is_verbatim(path: &Path) -> bool {
877877
}
878878

879879
/// The standard path separator character
880-
pub static sep: char = '\\';
880+
pub static SEP: char = '\\';
881+
/// The standard path separator byte
882+
pub static SEP_BYTE: u8 = SEP as u8;
883+
884+
/// The alternative path separator character
885+
pub static SEP2: char = '/';
881886
/// The alternative path separator character
882-
pub static sep2: char = '/';
887+
pub static SEP2_BYTE: u8 = SEP2 as u8;
883888

884889
/// Returns whether the given char is a path separator.
885890
/// Allows both the primary separator '\' and the alternative separator '/'.
886891
#[inline]
887892
pub fn is_sep(c: char) -> bool {
888-
c == sep || c == sep2
893+
c == SEP || c == SEP2
889894
}
890895

891896
/// Returns whether the given char is a path separator.
892897
/// Only allows the primary separator '\'; use is_sep to allow '/'.
893898
#[inline]
894899
pub fn is_sep_verbatim(c: char) -> bool {
895-
c == sep
900+
c == SEP
896901
}
897902

898903
/// Returns whether the given byte is a path separator.
899904
/// Allows both the primary separator '\' and the alternative separator '/'.
900905
#[inline]
901906
pub fn is_sep_byte(u: &u8) -> bool {
902-
*u as char == sep || *u as char == sep2
907+
*u == SEP_BYTE || *u == SEP2_BYTE
903908
}
904909

905910
/// Returns whether the given byte is a path separator.
906911
/// Only allows the primary separator '\'; use is_sep_byte to allow '/'.
907912
#[inline]
908913
pub fn is_sep_byte_verbatim(u: &u8) -> bool {
909-
*u as char == sep
914+
*u == SEP_BYTE
910915
}
911916

912917
/// Prefix types for Path

0 commit comments

Comments
 (0)