rustc: Remove &str
indexing from the language.
Being able to index into the bytes of a string encourages poor UTF-8 hygiene. To get a view of `&[u8]` from either a `String` or `&str` slice, use the `as_bytes()` method. Closes #12710. [breaking-change]
This commit is contained in:
parent
44ec28cfac
commit
d21336ee0a
26 changed files with 101 additions and 87 deletions
|
@ -1569,8 +1569,8 @@ mod tests {
|
||||||
let n2: uint = v.len();
|
let n2: uint = v.len();
|
||||||
assert_eq!(n1, n2);
|
assert_eq!(n1, n2);
|
||||||
while i < n1 {
|
while i < n1 {
|
||||||
let a: u8 = s1.as_slice()[i];
|
let a: u8 = s1.as_bytes()[i];
|
||||||
let b: u8 = s2.as_slice()[i];
|
let b: u8 = s2.as_bytes()[i];
|
||||||
debug!("{}", a);
|
debug!("{}", a);
|
||||||
debug!("{}", b);
|
debug!("{}", b);
|
||||||
assert_eq!(a, b);
|
assert_eq!(a, b);
|
||||||
|
|
|
@ -222,7 +222,7 @@ impl String {
|
||||||
return None
|
return None
|
||||||
}
|
}
|
||||||
|
|
||||||
let byte = self.as_slice()[len - 1];
|
let byte = self.as_bytes()[len - 1];
|
||||||
self.vec.set_len(len - 1);
|
self.vec.set_len(len - 1);
|
||||||
Some(byte)
|
Some(byte)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1743,7 +1743,7 @@ impl<'a> StrSlice<'a> for &'a str {
|
||||||
fn lines_any(&self) -> AnyLines<'a> {
|
fn lines_any(&self) -> AnyLines<'a> {
|
||||||
self.lines().map(|line| {
|
self.lines().map(|line| {
|
||||||
let l = line.len();
|
let l = line.len();
|
||||||
if l > 0 && line[l - 1] == '\r' as u8 { line.slice(0, l - 1) }
|
if l > 0 && line.as_bytes()[l - 1] == '\r' as u8 { line.slice(0, l - 1) }
|
||||||
else { line }
|
else { line }
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -1867,26 +1867,26 @@ impl<'a> StrSlice<'a> for &'a str {
|
||||||
fn is_char_boundary(&self, index: uint) -> bool {
|
fn is_char_boundary(&self, index: uint) -> bool {
|
||||||
if index == self.len() { return true; }
|
if index == self.len() { return true; }
|
||||||
if index > self.len() { return false; }
|
if index > self.len() { return false; }
|
||||||
let b = self[index];
|
let b = self.as_bytes()[index];
|
||||||
return b < 128u8 || b >= 192u8;
|
return b < 128u8 || b >= 192u8;
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn char_range_at(&self, i: uint) -> CharRange {
|
fn char_range_at(&self, i: uint) -> CharRange {
|
||||||
if self[i] < 128u8 {
|
if self.as_bytes()[i] < 128u8 {
|
||||||
return CharRange {ch: self[i] as char, next: i + 1 };
|
return CharRange {ch: self.as_bytes()[i] as char, next: i + 1 };
|
||||||
}
|
}
|
||||||
|
|
||||||
// Multibyte case is a fn to allow char_range_at to inline cleanly
|
// Multibyte case is a fn to allow char_range_at to inline cleanly
|
||||||
fn multibyte_char_range_at(s: &str, i: uint) -> CharRange {
|
fn multibyte_char_range_at(s: &str, i: uint) -> CharRange {
|
||||||
let mut val = s[i] as u32;
|
let mut val = s.as_bytes()[i] as u32;
|
||||||
let w = UTF8_CHAR_WIDTH[val as uint] as uint;
|
let w = UTF8_CHAR_WIDTH[val as uint] as uint;
|
||||||
assert!((w != 0));
|
assert!((w != 0));
|
||||||
|
|
||||||
val = utf8_first_byte!(val, w);
|
val = utf8_first_byte!(val, w);
|
||||||
val = utf8_acc_cont_byte!(val, s[i + 1]);
|
val = utf8_acc_cont_byte!(val, s.as_bytes()[i + 1]);
|
||||||
if w > 2 { val = utf8_acc_cont_byte!(val, s[i + 2]); }
|
if w > 2 { val = utf8_acc_cont_byte!(val, s.as_bytes()[i + 2]); }
|
||||||
if w > 3 { val = utf8_acc_cont_byte!(val, s[i + 3]); }
|
if w > 3 { val = utf8_acc_cont_byte!(val, s.as_bytes()[i + 3]); }
|
||||||
|
|
||||||
return CharRange {ch: unsafe { mem::transmute(val) }, next: i + w};
|
return CharRange {ch: unsafe { mem::transmute(val) }, next: i + w};
|
||||||
}
|
}
|
||||||
|
@ -1899,23 +1899,25 @@ impl<'a> StrSlice<'a> for &'a str {
|
||||||
let mut prev = start;
|
let mut prev = start;
|
||||||
|
|
||||||
prev = prev.saturating_sub(1);
|
prev = prev.saturating_sub(1);
|
||||||
if self[prev] < 128 { return CharRange{ch: self[prev] as char, next: prev} }
|
if self.as_bytes()[prev] < 128 {
|
||||||
|
return CharRange{ch: self.as_bytes()[prev] as char, next: prev}
|
||||||
|
}
|
||||||
|
|
||||||
// Multibyte case is a fn to allow char_range_at_reverse to inline cleanly
|
// Multibyte case is a fn to allow char_range_at_reverse to inline cleanly
|
||||||
fn multibyte_char_range_at_reverse(s: &str, mut i: uint) -> CharRange {
|
fn multibyte_char_range_at_reverse(s: &str, mut i: uint) -> CharRange {
|
||||||
// while there is a previous byte == 10......
|
// while there is a previous byte == 10......
|
||||||
while i > 0 && s[i] & 192u8 == TAG_CONT_U8 {
|
while i > 0 && s.as_bytes()[i] & 192u8 == TAG_CONT_U8 {
|
||||||
i -= 1u;
|
i -= 1u;
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut val = s[i] as u32;
|
let mut val = s.as_bytes()[i] as u32;
|
||||||
let w = UTF8_CHAR_WIDTH[val as uint] as uint;
|
let w = UTF8_CHAR_WIDTH[val as uint] as uint;
|
||||||
assert!((w != 0));
|
assert!((w != 0));
|
||||||
|
|
||||||
val = utf8_first_byte!(val, w);
|
val = utf8_first_byte!(val, w);
|
||||||
val = utf8_acc_cont_byte!(val, s[i + 1]);
|
val = utf8_acc_cont_byte!(val, s.as_bytes()[i + 1]);
|
||||||
if w > 2 { val = utf8_acc_cont_byte!(val, s[i + 2]); }
|
if w > 2 { val = utf8_acc_cont_byte!(val, s.as_bytes()[i + 2]); }
|
||||||
if w > 3 { val = utf8_acc_cont_byte!(val, s[i + 3]); }
|
if w > 3 { val = utf8_acc_cont_byte!(val, s.as_bytes()[i + 3]); }
|
||||||
|
|
||||||
return CharRange {ch: unsafe { mem::transmute(val) }, next: i};
|
return CharRange {ch: unsafe { mem::transmute(val) }, next: i};
|
||||||
}
|
}
|
||||||
|
|
|
@ -370,7 +370,7 @@ impl Matches {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn is_arg(arg: &str) -> bool {
|
fn is_arg(arg: &str) -> bool {
|
||||||
arg.len() > 1 && arg[0] == '-' as u8
|
arg.len() > 1 && arg.as_bytes()[0] == '-' as u8
|
||||||
}
|
}
|
||||||
|
|
||||||
fn find_opt(opts: &[Opt], nm: Name) -> Option<uint> {
|
fn find_opt(opts: &[Opt], nm: Name) -> Option<uint> {
|
||||||
|
@ -553,7 +553,7 @@ pub fn getopts(args: &[String], optgrps: &[OptGroup]) -> Result {
|
||||||
} else {
|
} else {
|
||||||
let mut names;
|
let mut names;
|
||||||
let mut i_arg = None;
|
let mut i_arg = None;
|
||||||
if cur.as_slice()[1] == '-' as u8 {
|
if cur.as_bytes()[1] == '-' as u8 {
|
||||||
let tail = cur.as_slice().slice(2, curlen);
|
let tail = cur.as_slice().slice(2, curlen);
|
||||||
let tail_eq: Vec<&str> = tail.split('=').collect();
|
let tail_eq: Vec<&str> = tail.split('=').collect();
|
||||||
if tail_eq.len() <= 1 {
|
if tail_eq.len() <= 1 {
|
||||||
|
|
|
@ -657,8 +657,8 @@ pub fn sanitize(s: &str) -> String {
|
||||||
|
|
||||||
// Underscore-qualify anything that didn't start as an ident.
|
// Underscore-qualify anything that didn't start as an ident.
|
||||||
if result.len() > 0u &&
|
if result.len() > 0u &&
|
||||||
result.as_slice()[0] != '_' as u8 &&
|
result.as_bytes()[0] != '_' as u8 &&
|
||||||
! char::is_XID_start(result.as_slice()[0] as char) {
|
! char::is_XID_start(result.as_bytes()[0] as char) {
|
||||||
return format!("_{}", result.as_slice());
|
return format!("_{}", result.as_slice());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -737,9 +737,9 @@ pub fn mangle_exported_name(ccx: &CrateContext, path: PathElems,
|
||||||
let extra2 = id % EXTRA_CHARS.len();
|
let extra2 = id % EXTRA_CHARS.len();
|
||||||
let id = id / EXTRA_CHARS.len();
|
let id = id / EXTRA_CHARS.len();
|
||||||
let extra3 = id % EXTRA_CHARS.len();
|
let extra3 = id % EXTRA_CHARS.len();
|
||||||
hash.push_char(EXTRA_CHARS[extra1] as char);
|
hash.push_char(EXTRA_CHARS.as_bytes()[extra1] as char);
|
||||||
hash.push_char(EXTRA_CHARS[extra2] as char);
|
hash.push_char(EXTRA_CHARS.as_bytes()[extra2] as char);
|
||||||
hash.push_char(EXTRA_CHARS[extra3] as char);
|
hash.push_char(EXTRA_CHARS.as_bytes()[extra3] as char);
|
||||||
|
|
||||||
exported_name(path,
|
exported_name(path,
|
||||||
hash.as_slice(),
|
hash.as_slice(),
|
||||||
|
|
|
@ -181,7 +181,7 @@ fn item_sized(item: ebml::Doc) -> ast::Sized {
|
||||||
fn item_method_sort(item: ebml::Doc) -> char {
|
fn item_method_sort(item: ebml::Doc) -> char {
|
||||||
let mut ret = 'r';
|
let mut ret = 'r';
|
||||||
reader::tagged_docs(item, tag_item_trait_method_sort, |doc| {
|
reader::tagged_docs(item, tag_item_trait_method_sort, |doc| {
|
||||||
ret = doc.as_str_slice()[0] as char;
|
ret = doc.as_str_slice().as_bytes()[0] as char;
|
||||||
false
|
false
|
||||||
});
|
});
|
||||||
ret
|
ret
|
||||||
|
@ -757,13 +757,13 @@ fn get_explicit_self(item: ebml::Doc) -> ast::ExplicitSelf_ {
|
||||||
let explicit_self_doc = reader::get_doc(item, tag_item_trait_method_explicit_self);
|
let explicit_self_doc = reader::get_doc(item, tag_item_trait_method_explicit_self);
|
||||||
let string = explicit_self_doc.as_str_slice();
|
let string = explicit_self_doc.as_str_slice();
|
||||||
|
|
||||||
let explicit_self_kind = string[0];
|
let explicit_self_kind = string.as_bytes()[0];
|
||||||
match explicit_self_kind as char {
|
match explicit_self_kind as char {
|
||||||
's' => ast::SelfStatic,
|
's' => ast::SelfStatic,
|
||||||
'v' => ast::SelfValue,
|
'v' => ast::SelfValue,
|
||||||
'~' => ast::SelfUniq,
|
'~' => ast::SelfUniq,
|
||||||
// FIXME(#4846) expl. region
|
// FIXME(#4846) expl. region
|
||||||
'&' => ast::SelfRegion(None, get_mutability(string[1])),
|
'&' => ast::SelfRegion(None, get_mutability(string.as_bytes()[1])),
|
||||||
_ => fail!("unknown self type code: `{}`", explicit_self_kind as char)
|
_ => fail!("unknown self type code: `{}`", explicit_self_kind as char)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -399,7 +399,7 @@ struct DeadVisitor<'a> {
|
||||||
impl<'a> DeadVisitor<'a> {
|
impl<'a> DeadVisitor<'a> {
|
||||||
fn should_warn_about_field(&mut self, node: &ast::StructField_) -> bool {
|
fn should_warn_about_field(&mut self, node: &ast::StructField_) -> bool {
|
||||||
let (is_named, has_leading_underscore) = match node.ident() {
|
let (is_named, has_leading_underscore) = match node.ident() {
|
||||||
Some(ref ident) => (true, token::get_ident(*ident).get()[0] == ('_' as u8)),
|
Some(ref ident) => (true, token::get_ident(*ident).get().as_bytes()[0] == ('_' as u8)),
|
||||||
_ => (false, false)
|
_ => (false, false)
|
||||||
};
|
};
|
||||||
let field_type = ty::node_id_to_type(self.tcx, node.id);
|
let field_type = ty::node_id_to_type(self.tcx, node.id);
|
||||||
|
|
|
@ -1511,7 +1511,7 @@ impl<'a> Liveness<'a> {
|
||||||
|
|
||||||
fn should_warn(&self, var: Variable) -> Option<String> {
|
fn should_warn(&self, var: Variable) -> Option<String> {
|
||||||
let name = self.ir.variable_name(var);
|
let name = self.ir.variable_name(var);
|
||||||
if name.len() == 0 || name.as_slice()[0] == ('_' as u8) {
|
if name.len() == 0 || name.as_bytes()[0] == ('_' as u8) {
|
||||||
None
|
None
|
||||||
} else {
|
} else {
|
||||||
Some(name)
|
Some(name)
|
||||||
|
|
|
@ -126,7 +126,6 @@ pub enum FieldName {
|
||||||
#[deriving(Clone, PartialEq, Eq, Hash)]
|
#[deriving(Clone, PartialEq, Eq, Hash)]
|
||||||
pub enum ElementKind {
|
pub enum ElementKind {
|
||||||
VecElement,
|
VecElement,
|
||||||
StrElement,
|
|
||||||
OtherElement,
|
OtherElement,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -794,7 +793,7 @@ impl<'t,TYPER:Typer> MemCategorizationContext<'t,TYPER> {
|
||||||
//! - `derefs`: the deref number to be used for
|
//! - `derefs`: the deref number to be used for
|
||||||
//! the implicit index deref, if any (see above)
|
//! the implicit index deref, if any (see above)
|
||||||
|
|
||||||
let element_ty = match ty::index(base_cmt.ty) {
|
let element_ty = match ty::array_element_ty(base_cmt.ty) {
|
||||||
Some(ref mt) => mt.ty,
|
Some(ref mt) => mt.ty,
|
||||||
None => {
|
None => {
|
||||||
self.tcx().sess.span_bug(
|
self.tcx().sess.span_bug(
|
||||||
|
@ -1137,9 +1136,6 @@ impl<'t,TYPER:Typer> MemCategorizationContext<'t,TYPER> {
|
||||||
cat_interior(_, InteriorElement(VecElement)) => {
|
cat_interior(_, InteriorElement(VecElement)) => {
|
||||||
"vec content".to_string()
|
"vec content".to_string()
|
||||||
}
|
}
|
||||||
cat_interior(_, InteriorElement(StrElement)) => {
|
|
||||||
"str content".to_string()
|
|
||||||
}
|
|
||||||
cat_interior(_, InteriorElement(OtherElement)) => {
|
cat_interior(_, InteriorElement(OtherElement)) => {
|
||||||
"indexed content".to_string()
|
"indexed content".to_string()
|
||||||
}
|
}
|
||||||
|
@ -1320,7 +1316,6 @@ fn element_kind(t: ty::t) -> ElementKind {
|
||||||
ty::ty_rptr(_, ty::mt{ty:ty, ..}) |
|
ty::ty_rptr(_, ty::mt{ty:ty, ..}) |
|
||||||
ty::ty_uniq(ty) => match ty::get(ty).sty {
|
ty::ty_uniq(ty) => match ty::get(ty).sty {
|
||||||
ty::ty_vec(_, None) => VecElement,
|
ty::ty_vec(_, None) => VecElement,
|
||||||
ty::ty_str => StrElement,
|
|
||||||
_ => OtherElement
|
_ => OtherElement
|
||||||
},
|
},
|
||||||
ty::ty_vec(..) => VecElement,
|
ty::ty_vec(..) => VecElement,
|
||||||
|
|
|
@ -2551,6 +2551,21 @@ pub fn deref(t: t, explicit: bool) -> Option<mt> {
|
||||||
|
|
||||||
// Returns the type of t[i]
|
// Returns the type of t[i]
|
||||||
pub fn index(t: t) -> Option<mt> {
|
pub fn index(t: t) -> Option<mt> {
|
||||||
|
match get(t).sty {
|
||||||
|
ty_vec(mt, Some(_)) => Some(mt),
|
||||||
|
ty_ptr(mt{ty: t, ..}) | ty_rptr(_, mt{ty: t, ..}) |
|
||||||
|
ty_box(t) | ty_uniq(t) => match get(t).sty {
|
||||||
|
ty_vec(mt, None) => Some(mt),
|
||||||
|
_ => None,
|
||||||
|
},
|
||||||
|
_ => None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Returns the type of elements contained within an 'array-like' type.
|
||||||
|
// This is exactly the same as the above, except it supports strings,
|
||||||
|
// which can't actually be indexed.
|
||||||
|
pub fn array_element_ty(t: t) -> Option<mt> {
|
||||||
match get(t).sty {
|
match get(t).sty {
|
||||||
ty_vec(mt, Some(_)) => Some(mt),
|
ty_vec(mt, Some(_)) => Some(mt),
|
||||||
ty_ptr(mt{ty: t, ..}) | ty_rptr(_, mt{ty: t, ..}) |
|
ty_ptr(mt{ty: t, ..}) | ty_rptr(_, mt{ty: t, ..}) |
|
||||||
|
|
|
@ -1206,8 +1206,8 @@ mod test {
|
||||||
let mut cur = [0u8, .. 2];
|
let mut cur = [0u8, .. 2];
|
||||||
for f in files {
|
for f in files {
|
||||||
let stem = f.filestem_str().unwrap();
|
let stem = f.filestem_str().unwrap();
|
||||||
let root = stem[0] - ('0' as u8);
|
let root = stem.as_bytes()[0] - ('0' as u8);
|
||||||
let name = stem[1] - ('0' as u8);
|
let name = stem.as_bytes()[1] - ('0' as u8);
|
||||||
assert!(cur[root as uint] < name);
|
assert!(cur[root as uint] < name);
|
||||||
cur[root as uint] = name;
|
cur[root as uint] = name;
|
||||||
}
|
}
|
||||||
|
|
|
@ -242,14 +242,18 @@ impl GenericPathUnsafe for Path {
|
||||||
fn is_vol_abs(path: &str, prefix: Option<PathPrefix>) -> bool {
|
fn is_vol_abs(path: &str, prefix: Option<PathPrefix>) -> bool {
|
||||||
// assume prefix is Some(DiskPrefix)
|
// assume prefix is Some(DiskPrefix)
|
||||||
let rest = path.slice_from(prefix_len(prefix));
|
let rest = path.slice_from(prefix_len(prefix));
|
||||||
!rest.is_empty() && rest[0].is_ascii() && is_sep(rest[0] as char)
|
!rest.is_empty() && rest.as_bytes()[0].is_ascii() && is_sep(rest.as_bytes()[0] as char)
|
||||||
}
|
}
|
||||||
fn shares_volume(me: &Path, path: &str) -> bool {
|
fn shares_volume(me: &Path, path: &str) -> bool {
|
||||||
// path is assumed to have a prefix of Some(DiskPrefix)
|
// path is assumed to have a prefix of Some(DiskPrefix)
|
||||||
let repr = me.repr.as_slice();
|
let repr = me.repr.as_slice();
|
||||||
match me.prefix {
|
match me.prefix {
|
||||||
Some(DiskPrefix) => repr[0] == path[0].to_ascii().to_upper().to_byte(),
|
Some(DiskPrefix) => {
|
||||||
Some(VerbatimDiskPrefix) => repr[4] == path[0].to_ascii().to_upper().to_byte(),
|
repr.as_bytes()[0] == path.as_bytes()[0].to_ascii().to_upper().to_byte()
|
||||||
|
}
|
||||||
|
Some(VerbatimDiskPrefix) => {
|
||||||
|
repr.as_bytes()[4] == path.as_bytes()[0].to_ascii().to_upper().to_byte()
|
||||||
|
}
|
||||||
_ => false
|
_ => false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -279,7 +283,7 @@ impl GenericPathUnsafe for Path {
|
||||||
// if me is "C:" we don't want to add a path separator
|
// if me is "C:" we don't want to add a path separator
|
||||||
match me.prefix {
|
match me.prefix {
|
||||||
Some(DiskPrefix) if me.repr.len() == plen => (),
|
Some(DiskPrefix) if me.repr.len() == plen => (),
|
||||||
_ if !(me.repr.len() > plen && me.repr.as_slice()[me.repr.len()-1] == SEP_BYTE) => {
|
_ if !(me.repr.len() > plen && me.repr.as_bytes()[me.repr.len()-1] == SEP_BYTE) => {
|
||||||
s.push_char(SEP);
|
s.push_char(SEP);
|
||||||
}
|
}
|
||||||
_ => ()
|
_ => ()
|
||||||
|
@ -302,7 +306,7 @@ impl GenericPathUnsafe for Path {
|
||||||
// absolute path, or cwd-relative and self is not same volume
|
// absolute path, or cwd-relative and self is not same volume
|
||||||
replace_path(self, path, prefix);
|
replace_path(self, path, prefix);
|
||||||
}
|
}
|
||||||
None if !path.is_empty() && is_sep_(self.prefix, path[0]) => {
|
None if !path.is_empty() && is_sep_(self.prefix, path.as_bytes()[0]) => {
|
||||||
// volume-relative path
|
// volume-relative path
|
||||||
if self.prefix.is_some() {
|
if self.prefix.is_some() {
|
||||||
// truncate self down to the prefix, then append
|
// truncate self down to the prefix, then append
|
||||||
|
@ -478,7 +482,7 @@ impl GenericPath for Path {
|
||||||
match self.prefix {
|
match self.prefix {
|
||||||
Some(DiskPrefix) => {
|
Some(DiskPrefix) => {
|
||||||
let rest = self.repr.as_slice().slice_from(self.prefix_len());
|
let rest = self.repr.as_slice().slice_from(self.prefix_len());
|
||||||
rest.len() > 0 && rest[0] == SEP_BYTE
|
rest.len() > 0 && rest.as_bytes()[0] == SEP_BYTE
|
||||||
}
|
}
|
||||||
Some(_) => true,
|
Some(_) => true,
|
||||||
None => false
|
None => false
|
||||||
|
@ -638,11 +642,11 @@ impl Path {
|
||||||
let s = match self.prefix {
|
let s = match self.prefix {
|
||||||
Some(_) => {
|
Some(_) => {
|
||||||
let plen = self.prefix_len();
|
let plen = self.prefix_len();
|
||||||
if repr.len() > plen && repr[plen] == SEP_BYTE {
|
if repr.len() > plen && repr.as_bytes()[plen] == SEP_BYTE {
|
||||||
repr.slice_from(plen+1)
|
repr.slice_from(plen+1)
|
||||||
} else { repr.slice_from(plen) }
|
} else { repr.slice_from(plen) }
|
||||||
}
|
}
|
||||||
None if repr[0] == SEP_BYTE => repr.slice_from(1),
|
None if repr.as_bytes()[0] == SEP_BYTE => repr.slice_from(1),
|
||||||
None => repr
|
None => repr
|
||||||
};
|
};
|
||||||
let ret = s.split_terminator(SEP).map(Some);
|
let ret = s.split_terminator(SEP).map(Some);
|
||||||
|
@ -665,14 +669,14 @@ impl Path {
|
||||||
match (self.prefix, other.prefix) {
|
match (self.prefix, other.prefix) {
|
||||||
(Some(DiskPrefix), Some(VerbatimDiskPrefix)) => {
|
(Some(DiskPrefix), Some(VerbatimDiskPrefix)) => {
|
||||||
self.is_absolute() &&
|
self.is_absolute() &&
|
||||||
s_repr[0].to_ascii().eq_ignore_case(o_repr[4].to_ascii())
|
s_repr.as_bytes()[0].to_ascii().eq_ignore_case(o_repr.as_bytes()[4].to_ascii())
|
||||||
}
|
}
|
||||||
(Some(VerbatimDiskPrefix), Some(DiskPrefix)) => {
|
(Some(VerbatimDiskPrefix), Some(DiskPrefix)) => {
|
||||||
other.is_absolute() &&
|
other.is_absolute() &&
|
||||||
s_repr[4].to_ascii().eq_ignore_case(o_repr[0].to_ascii())
|
s_repr.as_bytes()[4].to_ascii().eq_ignore_case(o_repr.as_bytes()[0].to_ascii())
|
||||||
}
|
}
|
||||||
(Some(VerbatimDiskPrefix), Some(VerbatimDiskPrefix)) => {
|
(Some(VerbatimDiskPrefix), Some(VerbatimDiskPrefix)) => {
|
||||||
s_repr[4].to_ascii().eq_ignore_case(o_repr[4].to_ascii())
|
s_repr.as_bytes()[4].to_ascii().eq_ignore_case(o_repr.as_bytes()[4].to_ascii())
|
||||||
}
|
}
|
||||||
(Some(UNCPrefix(_,_)), Some(VerbatimUNCPrefix(_,_))) => {
|
(Some(UNCPrefix(_,_)), Some(VerbatimUNCPrefix(_,_))) => {
|
||||||
s_repr.slice(2, self.prefix_len()) == o_repr.slice(8, other.prefix_len())
|
s_repr.slice(2, self.prefix_len()) == o_repr.slice(8, other.prefix_len())
|
||||||
|
@ -718,12 +722,12 @@ impl Path {
|
||||||
let mut comps = comps;
|
let mut comps = comps;
|
||||||
match (comps.is_some(),prefix) {
|
match (comps.is_some(),prefix) {
|
||||||
(false, Some(DiskPrefix)) => {
|
(false, Some(DiskPrefix)) => {
|
||||||
if s[0] >= 'a' as u8 && s[0] <= 'z' as u8 {
|
if s.as_bytes()[0] >= 'a' as u8 && s.as_bytes()[0] <= 'z' as u8 {
|
||||||
comps = Some(vec![]);
|
comps = Some(vec![]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
(false, Some(VerbatimDiskPrefix)) => {
|
(false, Some(VerbatimDiskPrefix)) => {
|
||||||
if s[4] >= 'a' as u8 && s[0] <= 'z' as u8 {
|
if s.as_bytes()[4] >= 'a' as u8 && s.as_bytes()[0] <= 'z' as u8 {
|
||||||
comps = Some(vec![]);
|
comps = Some(vec![]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -778,12 +782,12 @@ impl Path {
|
||||||
let mut s = String::with_capacity(n);
|
let mut s = String::with_capacity(n);
|
||||||
match prefix {
|
match prefix {
|
||||||
Some(DiskPrefix) => {
|
Some(DiskPrefix) => {
|
||||||
s.push_char(prefix_[0].to_ascii().to_upper().to_char());
|
s.push_char(prefix_.as_bytes()[0].to_ascii().to_upper().to_char());
|
||||||
s.push_char(':');
|
s.push_char(':');
|
||||||
}
|
}
|
||||||
Some(VerbatimDiskPrefix) => {
|
Some(VerbatimDiskPrefix) => {
|
||||||
s.push_str(prefix_.slice_to(4));
|
s.push_str(prefix_.slice_to(4));
|
||||||
s.push_char(prefix_[4].to_ascii().to_upper().to_char());
|
s.push_char(prefix_.as_bytes()[4].to_ascii().to_upper().to_char());
|
||||||
s.push_str(prefix_.slice_from(5));
|
s.push_str(prefix_.slice_from(5));
|
||||||
}
|
}
|
||||||
Some(UNCPrefix(a,b)) => {
|
Some(UNCPrefix(a,b)) => {
|
||||||
|
@ -845,7 +849,7 @@ impl Path {
|
||||||
|
|
||||||
fn has_nonsemantic_trailing_slash(&self) -> bool {
|
fn has_nonsemantic_trailing_slash(&self) -> bool {
|
||||||
is_verbatim(self) && self.repr.len() > self.prefix_len()+1 &&
|
is_verbatim(self) && self.repr.len() > self.prefix_len()+1 &&
|
||||||
self.repr.as_slice()[self.repr.len()-1] == SEP_BYTE
|
self.repr.as_bytes()[self.repr.len()-1] == SEP_BYTE
|
||||||
}
|
}
|
||||||
|
|
||||||
fn update_normalized<S: Str>(&mut self, s: S) {
|
fn update_normalized<S: Str>(&mut self, s: S) {
|
||||||
|
@ -861,7 +865,7 @@ impl Path {
|
||||||
/// but absolute within that volume.
|
/// but absolute within that volume.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn is_vol_relative(path: &Path) -> bool {
|
pub fn is_vol_relative(path: &Path) -> bool {
|
||||||
path.prefix.is_none() && is_sep_byte(&path.repr.as_slice()[0])
|
path.prefix.is_none() && is_sep_byte(&path.repr.as_bytes()[0])
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns whether the path is considered "cwd-relative", which means a path
|
/// Returns whether the path is considered "cwd-relative", which means a path
|
||||||
|
@ -991,8 +995,8 @@ fn parse_prefix<'a>(mut path: &'a str) -> Option<PathPrefix> {
|
||||||
} else {
|
} else {
|
||||||
// \\?\path
|
// \\?\path
|
||||||
let idx = path.find('\\');
|
let idx = path.find('\\');
|
||||||
if idx == Some(2) && path[1] == ':' as u8 {
|
if idx == Some(2) && path.as_bytes()[1] == ':' as u8 {
|
||||||
let c = path[0];
|
let c = path.as_bytes()[0];
|
||||||
if c.is_ascii() && ::char::is_alphabetic(c as char) {
|
if c.is_ascii() && ::char::is_alphabetic(c as char) {
|
||||||
// \\?\C:\ path
|
// \\?\C:\ path
|
||||||
return Some(VerbatimDiskPrefix);
|
return Some(VerbatimDiskPrefix);
|
||||||
|
@ -1014,9 +1018,9 @@ fn parse_prefix<'a>(mut path: &'a str) -> Option<PathPrefix> {
|
||||||
}
|
}
|
||||||
_ => ()
|
_ => ()
|
||||||
}
|
}
|
||||||
} else if path.len() > 1 && path[1] == ':' as u8 {
|
} else if path.len() > 1 && path.as_bytes()[1] == ':' as u8 {
|
||||||
// C:
|
// C:
|
||||||
let c = path[0];
|
let c = path.as_bytes()[0];
|
||||||
if c.is_ascii() && ::char::is_alphabetic(c as char) {
|
if c.is_ascii() && ::char::is_alphabetic(c as char) {
|
||||||
return Some(DiskPrefix);
|
return Some(DiskPrefix);
|
||||||
}
|
}
|
||||||
|
|
|
@ -415,7 +415,7 @@ fn highlight_lines(err: &mut EmitterWriter,
|
||||||
}
|
}
|
||||||
let orig = fm.get_line(*lines.lines.get(0) as int);
|
let orig = fm.get_line(*lines.lines.get(0) as int);
|
||||||
for pos in range(0u, left-skip) {
|
for pos in range(0u, left-skip) {
|
||||||
let cur_char = orig.as_slice()[pos] as char;
|
let cur_char = orig.as_bytes()[pos] as char;
|
||||||
// Whenever a tab occurs on the previous line, we insert one on
|
// Whenever a tab occurs on the previous line, we insert one on
|
||||||
// the error-point-squiggly-line as well (instead of a space).
|
// the error-point-squiggly-line as well (instead of a space).
|
||||||
// That way the squiggly line will usually appear in the correct
|
// That way the squiggly line will usually appear in the correct
|
||||||
|
|
|
@ -1203,8 +1203,8 @@ impl<'a> StringReader<'a> {
|
||||||
|
|
||||||
fn read_one_line_comment(&mut self) -> String {
|
fn read_one_line_comment(&mut self) -> String {
|
||||||
let val = self.read_to_eol();
|
let val = self.read_to_eol();
|
||||||
assert!((val.as_slice()[0] == '/' as u8 && val.as_slice()[1] == '/' as u8)
|
assert!((val.as_bytes()[0] == '/' as u8 && val.as_bytes()[1] == '/' as u8)
|
||||||
|| (val.as_slice()[0] == '#' as u8 && val.as_slice()[1] == '!' as u8));
|
|| (val.as_bytes()[0] == '#' as u8 && val.as_bytes()[1] == '!' as u8));
|
||||||
return val;
|
return val;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -375,7 +375,7 @@ pub fn strptime(s: &str, format: &str) -> Result<Tm, String> {
|
||||||
fn match_str(s: &str, pos: uint, needle: &str) -> bool {
|
fn match_str(s: &str, pos: uint, needle: &str) -> bool {
|
||||||
let mut i = pos;
|
let mut i = pos;
|
||||||
for ch in needle.bytes() {
|
for ch in needle.bytes() {
|
||||||
if s[i] != ch {
|
if s.as_bytes()[i] != ch {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
i += 1u;
|
i += 1u;
|
||||||
|
|
|
@ -324,8 +324,8 @@ impl Uuid {
|
||||||
let mut s: Vec<u8> = Vec::from_elem(32, 0u8);
|
let mut s: Vec<u8> = Vec::from_elem(32, 0u8);
|
||||||
for i in range(0u, 16u) {
|
for i in range(0u, 16u) {
|
||||||
let digit = format!("{:02x}", self.bytes[i] as uint);
|
let digit = format!("{:02x}", self.bytes[i] as uint);
|
||||||
*s.get_mut(i*2+0) = digit.as_slice()[0];
|
*s.get_mut(i*2+0) = digit.as_bytes()[0];
|
||||||
*s.get_mut(i*2+1) = digit.as_slice()[1];
|
*s.get_mut(i*2+1) = digit.as_bytes()[1];
|
||||||
}
|
}
|
||||||
str::from_utf8(s.as_slice()).unwrap().to_string()
|
str::from_utf8(s.as_slice()).unwrap().to_string()
|
||||||
}
|
}
|
||||||
|
|
|
@ -183,7 +183,7 @@ fn main() {
|
||||||
|
|
||||||
if line.len() == 0u { continue; }
|
if line.len() == 0u { continue; }
|
||||||
|
|
||||||
match (line.as_slice()[0] as char, proc_mode) {
|
match (line.as_bytes()[0] as char, proc_mode) {
|
||||||
|
|
||||||
// start processing if this is the one
|
// start processing if this is the one
|
||||||
('>', false) => {
|
('>', false) => {
|
||||||
|
|
|
@ -17,10 +17,10 @@ pub fn main() {
|
||||||
assert_eq!(v.as_slice()[3u32], 3); //~ ERROR: mismatched types
|
assert_eq!(v.as_slice()[3u32], 3); //~ ERROR: mismatched types
|
||||||
assert_eq!(v.as_slice()[3i32], 3); //~ ERROR: mismatched types
|
assert_eq!(v.as_slice()[3i32], 3); //~ ERROR: mismatched types
|
||||||
println!("{}", v.as_slice()[3u8]); //~ ERROR: mismatched types
|
println!("{}", v.as_slice()[3u8]); //~ ERROR: mismatched types
|
||||||
assert_eq!(s.as_slice()[3u], 'd' as u8);
|
assert_eq!(s.as_bytes()[3u], 'd' as u8);
|
||||||
assert_eq!(s.as_slice()[3u8], 'd' as u8); //~ ERROR: mismatched types
|
assert_eq!(s.as_bytes()[3u8], 'd' as u8); //~ ERROR: mismatched types
|
||||||
assert_eq!(s.as_slice()[3i8], 'd' as u8); //~ ERROR: mismatched types
|
assert_eq!(s.as_bytes()[3i8], 'd' as u8); //~ ERROR: mismatched types
|
||||||
assert_eq!(s.as_slice()[3u32], 'd' as u8); //~ ERROR: mismatched types
|
assert_eq!(s.as_bytes()[3u32], 'd' as u8); //~ ERROR: mismatched types
|
||||||
assert_eq!(s.as_slice()[3i32], 'd' as u8); //~ ERROR: mismatched types
|
assert_eq!(s.as_bytes()[3i32], 'd' as u8); //~ ERROR: mismatched types
|
||||||
println!("{}", s.as_slice()[3u8]); //~ ERROR: mismatched types
|
println!("{}", s.as_bytes()[3u8]); //~ ERROR: mismatched types
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,8 +11,6 @@
|
||||||
extern crate debug;
|
extern crate debug;
|
||||||
|
|
||||||
pub fn main() {
|
pub fn main() {
|
||||||
let s = "hello".to_string();
|
let s: &str = "hello";
|
||||||
let c: u8 = s.as_slice()[4];
|
let c: u8 = s[4]; //~ ERROR cannot index a value of type `&str`
|
||||||
println!("{:?}", c);
|
|
||||||
assert_eq!(c, 0x6f as u8);
|
|
||||||
}
|
}
|
|
@ -14,5 +14,5 @@ fn main() {
|
||||||
let s: String = "hello".to_string();
|
let s: String = "hello".to_string();
|
||||||
|
|
||||||
// Bounds-check failure.
|
// Bounds-check failure.
|
||||||
assert_eq!(s.as_slice()[5], 0x0 as u8);
|
assert_eq!(s.as_bytes()[5], 0x0 as u8);
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,8 +17,8 @@ pub fn main() {
|
||||||
println!("{}", x);
|
println!("{}", x);
|
||||||
println!("{}", y);
|
println!("{}", y);
|
||||||
|
|
||||||
assert_eq!(x[0], 'h' as u8);
|
assert_eq!(x.as_bytes()[0], 'h' as u8);
|
||||||
assert_eq!(x[4], 'o' as u8);
|
assert_eq!(x.as_bytes()[4], 'o' as u8);
|
||||||
|
|
||||||
let z : &str = "thing";
|
let z : &str = "thing";
|
||||||
assert_eq!(v, x);
|
assert_eq!(v, x);
|
||||||
|
|
|
@ -15,6 +15,6 @@ pub fn main() {
|
||||||
let _y : String = "there".to_string();
|
let _y : String = "there".to_string();
|
||||||
let mut z = "thing".to_string();
|
let mut z = "thing".to_string();
|
||||||
z = x;
|
z = x;
|
||||||
assert_eq!(z.as_slice()[0], ('h' as u8));
|
assert_eq!(z.as_bytes()[0], ('h' as u8));
|
||||||
assert_eq!(z.as_slice()[4], ('o' as u8));
|
assert_eq!(z.as_bytes()[4], ('o' as u8));
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,5 +16,5 @@ pub fn main() {
|
||||||
let b: String = "world".to_string();
|
let b: String = "world".to_string();
|
||||||
let s: String = format!("{}{}", a, b);
|
let s: String = format!("{}{}", a, b);
|
||||||
println!("{}", s.clone());
|
println!("{}", s.clone());
|
||||||
assert_eq!(s.as_slice()[9], 'd' as u8);
|
assert_eq!(s.as_bytes()[9], 'd' as u8);
|
||||||
}
|
}
|
||||||
|
|
|
@ -39,10 +39,10 @@ fn test_str() {
|
||||||
let s0 = "test".to_string();
|
let s0 = "test".to_string();
|
||||||
tx.send(s0);
|
tx.send(s0);
|
||||||
let s1 = rx.recv();
|
let s1 = rx.recv();
|
||||||
assert_eq!(s1.as_slice()[0], 't' as u8);
|
assert_eq!(s1.as_bytes()[0], 't' as u8);
|
||||||
assert_eq!(s1.as_slice()[1], 'e' as u8);
|
assert_eq!(s1.as_bytes()[1], 'e' as u8);
|
||||||
assert_eq!(s1.as_slice()[2], 's' as u8);
|
assert_eq!(s1.as_bytes()[2], 's' as u8);
|
||||||
assert_eq!(s1.as_slice()[3], 't' as u8);
|
assert_eq!(s1.as_bytes()[3], 't' as u8);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[deriving(Show)]
|
#[deriving(Show)]
|
||||||
|
|
|
@ -99,7 +99,7 @@ fn check_legs(arc: Arc<Vec<Box<Pet+Share+Send>>>) {
|
||||||
fn check_names(arc: Arc<Vec<Box<Pet+Share+Send>>>) {
|
fn check_names(arc: Arc<Vec<Box<Pet+Share+Send>>>) {
|
||||||
for pet in arc.iter() {
|
for pet in arc.iter() {
|
||||||
pet.name(|name| {
|
pet.name(|name| {
|
||||||
assert!(name[0] == 'a' as u8 && name[1] == 'l' as u8);
|
assert!(name.as_bytes()[0] == 'a' as u8 && name.as_bytes()[1] == 'l' as u8);
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -46,7 +46,7 @@ pub fn main() {
|
||||||
for ab in a.as_slice().bytes() {
|
for ab in a.as_slice().bytes() {
|
||||||
println!("{}", i);
|
println!("{}", i);
|
||||||
println!("{}", ab);
|
println!("{}", ab);
|
||||||
let bb: u8 = b.as_slice()[i as uint];
|
let bb: u8 = b.as_bytes()[i as uint];
|
||||||
println!("{}", bb);
|
println!("{}", bb);
|
||||||
assert_eq!(ab, bb);
|
assert_eq!(ab, bb);
|
||||||
i += 1;
|
i += 1;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue