core: Convert some records to structs
This commit is contained in:
parent
4a2a375fbf
commit
5a282ec26f
@ -339,22 +339,22 @@ fn scan_char(haystack: ~str, needle: char, idx: &mut uint) -> bool {
|
||||
if *idx >= haystack.len() {
|
||||
return false;
|
||||
}
|
||||
let {ch, next} = str::char_range_at(haystack, *idx);
|
||||
if ch != needle {
|
||||
let range = str::char_range_at(haystack, *idx);
|
||||
if range.ch != needle {
|
||||
return false;
|
||||
}
|
||||
*idx = next;
|
||||
*idx = range.next;
|
||||
return true;
|
||||
}
|
||||
|
||||
fn scan_integer(haystack: ~str, idx: &mut uint) -> bool {
|
||||
let mut i = *idx;
|
||||
while i < haystack.len() {
|
||||
let {ch, next} = str::char_range_at(haystack, i);
|
||||
if ch < '0' || '9' < ch {
|
||||
let range = str::char_range_at(haystack, i);
|
||||
if range.ch < '0' || '9' < range.ch {
|
||||
break;
|
||||
}
|
||||
i = next;
|
||||
i = range.next;
|
||||
}
|
||||
if i == *idx {
|
||||
return false;
|
||||
@ -370,9 +370,9 @@ fn scan_string(haystack: ~str, needle: ~str, idx: &mut uint) -> bool {
|
||||
if haystack_i >= haystack.len() {
|
||||
return false;
|
||||
}
|
||||
let {ch, next} = str::char_range_at(haystack, haystack_i);
|
||||
haystack_i = next;
|
||||
if !scan_char(needle, ch, &mut needle_i) {
|
||||
let range = str::char_range_at(haystack, haystack_i);
|
||||
haystack_i = range.next;
|
||||
if !scan_char(needle, range.ch, &mut needle_i) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
@ -47,32 +47,28 @@ use ptr::null;
|
||||
* pointers achieved about 103 million pushes/second. Using an option
|
||||
* type could only produce 47 million pushes/second.
|
||||
*/
|
||||
type DVec_<A> = {
|
||||
pub struct DVec<A> {
|
||||
mut data: ~[A]
|
||||
};
|
||||
|
||||
pub enum DVec<A> {
|
||||
DVec_(DVec_<A>)
|
||||
}
|
||||
|
||||
/// Creates a new, empty dvec
|
||||
pub pure fn DVec<A>() -> DVec<A> {
|
||||
DVec_({mut data: ~[]})
|
||||
DVec {mut data: ~[]}
|
||||
}
|
||||
|
||||
/// Creates a new dvec with a single element
|
||||
pub fn from_elem<A>(e: A) -> DVec<A> {
|
||||
DVec_({mut data: ~[move e]})
|
||||
DVec {mut data: ~[move e]}
|
||||
}
|
||||
|
||||
/// Creates a new dvec with the contents of a vector
|
||||
pub fn from_vec<A>(v: ~[A]) -> DVec<A> {
|
||||
DVec_({mut data: move v})
|
||||
DVec {mut data: move v}
|
||||
}
|
||||
|
||||
/// Consumes the vector and returns its contents
|
||||
pub fn unwrap<A>(d: DVec<A>) -> ~[A] {
|
||||
let DVec_({data: v}) = move d;
|
||||
let DVec {data: v} = move d;
|
||||
move v
|
||||
}
|
||||
|
||||
|
@ -57,7 +57,7 @@ pub fn rights<T, U: Copy>(eithers: &[Either<T, U>]) -> ~[U] {
|
||||
|
||||
// XXX bad copies. take arg by val
|
||||
pub fn partition<T: Copy, U: Copy>(eithers: &[Either<T, U>])
|
||||
-> {lefts: ~[T], rights: ~[U]} {
|
||||
-> (~[T], ~[U]) {
|
||||
/*!
|
||||
* Extracts from a vector of either all the left values and right values
|
||||
*
|
||||
@ -73,7 +73,7 @@ pub fn partition<T: Copy, U: Copy>(eithers: &[Either<T, U>])
|
||||
Right(copy r) => rights.push(r)
|
||||
}
|
||||
}
|
||||
return {lefts: move lefts, rights: move rights};
|
||||
return (move lefts, move rights);
|
||||
}
|
||||
|
||||
// XXX bad copies
|
||||
@ -212,36 +212,36 @@ fn test_rights_empty() {
|
||||
#[test]
|
||||
fn test_partition() {
|
||||
let input = ~[Left(10), Right(11), Left(12), Right(13), Left(14)];
|
||||
let result = partition(input);
|
||||
assert (result.lefts[0] == 10);
|
||||
assert (result.lefts[1] == 12);
|
||||
assert (result.lefts[2] == 14);
|
||||
assert (result.rights[0] == 11);
|
||||
assert (result.rights[1] == 13);
|
||||
let (lefts, rights) = partition(input);
|
||||
assert (lefts[0] == 10);
|
||||
assert (lefts[1] == 12);
|
||||
assert (lefts[2] == 14);
|
||||
assert (rights[0] == 11);
|
||||
assert (rights[1] == 13);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_partition_no_lefts() {
|
||||
let input: ~[Either<int, int>] = ~[Right(10), Right(11)];
|
||||
let result = partition(input);
|
||||
assert (vec::len(result.lefts) == 0u);
|
||||
assert (vec::len(result.rights) == 2u);
|
||||
let (lefts, rights) = partition(input);
|
||||
assert (vec::len(lefts) == 0u);
|
||||
assert (vec::len(rights) == 2u);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_partition_no_rights() {
|
||||
let input: ~[Either<int, int>] = ~[Left(10), Left(11)];
|
||||
let result = partition(input);
|
||||
assert (vec::len(result.lefts) == 2u);
|
||||
assert (vec::len(result.rights) == 0u);
|
||||
let (lefts, rights) = partition(input);
|
||||
assert (vec::len(lefts) == 2u);
|
||||
assert (vec::len(rights) == 0u);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_partition_empty() {
|
||||
let input: ~[Either<int, int>] = ~[];
|
||||
let result = partition(input);
|
||||
assert (vec::len(result.lefts) == 0u);
|
||||
assert (vec::len(result.rights) == 0u);
|
||||
let (lefts, rights) = partition(input);
|
||||
assert (vec::len(lefts) == 0u);
|
||||
assert (vec::len(rights) == 0u);
|
||||
}
|
||||
|
||||
//
|
||||
|
@ -269,8 +269,8 @@ impl<T:Ord> &const T : Ord {
|
||||
#[test]
|
||||
pub fn test() {
|
||||
unsafe {
|
||||
type Pair = {mut fst: int, mut snd: int};
|
||||
let p = {mut fst: 10, mut snd: 20};
|
||||
struct Pair {mut fst: int, mut snd: int};
|
||||
let p = Pair {mut fst: 10, mut snd: 20};
|
||||
let pptr: *mut Pair = mut_addr_of(&p);
|
||||
let iptr: *mut int = cast::reinterpret_cast(&pptr);
|
||||
assert (*iptr == 10);;
|
||||
@ -278,7 +278,7 @@ pub fn test() {
|
||||
assert (*iptr == 30);
|
||||
assert (p.fst == 30);;
|
||||
|
||||
*pptr = {mut fst: 50, mut snd: 60};
|
||||
*pptr = Pair {mut fst: 50, mut snd: 60};
|
||||
assert (*iptr == 50);
|
||||
assert (p.fst == 50);
|
||||
assert (p.snd == 60);
|
||||
|
@ -224,8 +224,8 @@ Section: Adding to and removing from a string
|
||||
pub fn pop_char(s: &mut ~str) -> char {
|
||||
let end = len(*s);
|
||||
assert end > 0u;
|
||||
let {ch, prev} = char_range_at_reverse(*s, end);
|
||||
unsafe { raw::set_len(s, prev); }
|
||||
let CharRange {ch, next} = char_range_at_reverse(*s, end);
|
||||
unsafe { raw::set_len(s, next); }
|
||||
return ch;
|
||||
}
|
||||
|
||||
@ -237,7 +237,7 @@ pub fn pop_char(s: &mut ~str) -> char {
|
||||
* If the string does not contain any characters
|
||||
*/
|
||||
pub fn shift_char(s: &mut ~str) -> char {
|
||||
let {ch, next} = char_range_at(*s, 0u);
|
||||
let CharRange {ch, next} = char_range_at(*s, 0u);
|
||||
*s = unsafe { raw::slice_bytes(*s, next, len(*s)) };
|
||||
return ch;
|
||||
}
|
||||
@ -253,7 +253,7 @@ pub fn shift_char(s: &mut ~str) -> char {
|
||||
*/
|
||||
#[inline]
|
||||
pub fn view_shift_char(s: &a/str) -> (char, &a/str) {
|
||||
let {ch, next} = char_range_at(s, 0u);
|
||||
let CharRange {ch, next} = char_range_at(s, 0u);
|
||||
let next_s = unsafe { raw::view_bytes(s, next, len(s)) };
|
||||
return (ch, next_s);
|
||||
}
|
||||
@ -296,7 +296,7 @@ pub pure fn trim_right_chars(s: &str, chars_to_trim: &[char]) -> ~str {
|
||||
match rfind(s, |c| !chars_to_trim.contains(&c)) {
|
||||
None => ~"",
|
||||
Some(last) => {
|
||||
let {next, _} = char_range_at(s, last);
|
||||
let next = char_range_at(s, last).next;
|
||||
unsafe { raw::slice_bytes(s, 0u, next) }
|
||||
}
|
||||
}
|
||||
@ -328,7 +328,7 @@ pub pure fn trim_right(s: &str) -> ~str {
|
||||
match rfind(s, |c| !char::is_whitespace(c)) {
|
||||
None => ~"",
|
||||
Some(last) => {
|
||||
let {next, _} = char_range_at(s, last);
|
||||
let next = char_range_at(s, last).next;
|
||||
unsafe { raw::slice_bytes(s, 0u, next) }
|
||||
}
|
||||
}
|
||||
@ -365,7 +365,7 @@ pub pure fn chars(s: &str) -> ~[char] {
|
||||
let mut buf = ~[], i = 0;
|
||||
let len = len(s);
|
||||
while i < len {
|
||||
let {ch, next} = char_range_at(s, i);
|
||||
let CharRange {ch, next} = char_range_at(s, i);
|
||||
unsafe { buf.push(ch); }
|
||||
i = next;
|
||||
}
|
||||
@ -475,7 +475,7 @@ pure fn split_inner(s: &str, sepfn: fn(cc: char) -> bool, count: uint,
|
||||
let l = len(s);
|
||||
let mut result = ~[], i = 0u, start = 0u, done = 0u;
|
||||
while i < l && done < count {
|
||||
let {ch, next} = char_range_at(s, i);
|
||||
let CharRange {ch, next} = char_range_at(s, i);
|
||||
if sepfn(ch) {
|
||||
if allow_empty || start < i unsafe {
|
||||
result.push(unsafe { raw::slice_bytes(s, start, i)});
|
||||
@ -866,7 +866,7 @@ pub pure fn each_chari(s: &str, it: fn(uint, char) -> bool) {
|
||||
let mut pos = 0u, ch_pos = 0u;
|
||||
let len = len(s);
|
||||
while pos < len {
|
||||
let {ch, next} = char_range_at(s, pos);
|
||||
let CharRange {ch, next} = char_range_at(s, pos);
|
||||
pos = next;
|
||||
if !it(ch_pos, ch) { break; }
|
||||
ch_pos += 1u;
|
||||
@ -878,7 +878,7 @@ pub pure fn chars_each(s: &str, it: fn(char) -> bool) {
|
||||
let mut pos = 0u;
|
||||
let len = len(s);
|
||||
while (pos < len) {
|
||||
let {ch, next} = char_range_at(s, pos);
|
||||
let CharRange {ch, next} = char_range_at(s, pos);
|
||||
pos = next;
|
||||
if !it(ch) { return; }
|
||||
}
|
||||
@ -1144,7 +1144,7 @@ pub pure fn find_between(s: &str, start: uint, end: uint, f: fn(char) -> bool)
|
||||
assert is_char_boundary(s, start);
|
||||
let mut i = start;
|
||||
while i < end {
|
||||
let {ch, next} = char_range_at(s, i);
|
||||
let CharRange {ch, next} = char_range_at(s, i);
|
||||
if f(ch) { return Some(i); }
|
||||
i = next;
|
||||
}
|
||||
@ -1224,7 +1224,7 @@ pub pure fn rfind_between(s: &str, start: uint, end: uint,
|
||||
assert is_char_boundary(s, start);
|
||||
let mut i = start;
|
||||
while i > end {
|
||||
let {ch, prev} = char_range_at_reverse(s, i);
|
||||
let CharRange {ch, next: prev} = char_range_at_reverse(s, i);
|
||||
if f(ch) { return Some(prev); }
|
||||
i = prev;
|
||||
}
|
||||
@ -1538,7 +1538,7 @@ pub pure fn count_chars(s: &str, start: uint, end: uint) -> uint {
|
||||
assert is_char_boundary(s, end);
|
||||
let mut i = start, len = 0u;
|
||||
while i < end {
|
||||
let {next, _} = char_range_at(s, i);
|
||||
let next = char_range_at(s, i).next;
|
||||
len += 1u;
|
||||
i = next;
|
||||
}
|
||||
@ -1552,7 +1552,7 @@ pub pure fn count_bytes(s: &b/str, start: uint, n: uint) -> uint {
|
||||
let l = len(s);
|
||||
while cnt > 0u {
|
||||
assert end < l;
|
||||
let {next, _} = char_range_at(s, end);
|
||||
let next = char_range_at(s, end).next;
|
||||
cnt -= 1u;
|
||||
end = next;
|
||||
}
|
||||
@ -1595,7 +1595,7 @@ pub pure fn is_char_boundary(s: &str, index: uint) -> bool {
|
||||
* let s = "中华Việt Nam";
|
||||
* let i = 0u;
|
||||
* while i < str::len(s) {
|
||||
* let {ch, next} = str::char_range_at(s, i);
|
||||
* let CharRange {ch, next} = str::char_range_at(s, i);
|
||||
* std::io::println(fmt!("%u: %c",i,ch));
|
||||
* i = next;
|
||||
* }
|
||||
@ -1631,11 +1631,11 @@ pub pure fn is_char_boundary(s: &str, index: uint) -> bool {
|
||||
* If `i` is greater than or equal to the length of the string.
|
||||
* If `i` is not the index of the beginning of a valid UTF-8 character.
|
||||
*/
|
||||
pub pure fn char_range_at(s: &str, i: uint) -> {ch: char, next: uint} {
|
||||
pub pure fn char_range_at(s: &str, i: uint) -> CharRange {
|
||||
let b0 = s[i];
|
||||
let w = utf8_char_width(b0);
|
||||
assert (w != 0u);
|
||||
if w == 1u { return {ch: b0 as char, next: i + 1u}; }
|
||||
if w == 1u { return CharRange {ch: b0 as char, next: i + 1u}; }
|
||||
let mut val = 0u;
|
||||
let end = i + w;
|
||||
let mut i = i + 1u;
|
||||
@ -1650,7 +1650,7 @@ pub pure fn char_range_at(s: &str, i: uint) -> {ch: char, next: uint} {
|
||||
// the first to clip off the marker bits at the left of the byte, and then
|
||||
// a second (as uint) to get it to the right position.
|
||||
val += ((b0 << ((w + 1u) as u8)) as uint) << ((w - 1u) * 6u - w - 1u);
|
||||
return {ch: val as char, next: i};
|
||||
return CharRange {ch: val as char, next: i};
|
||||
}
|
||||
|
||||
/// Pluck a character out of a string
|
||||
@ -1658,13 +1658,18 @@ pub pure fn char_at(s: &str, i: uint) -> char {
|
||||
return char_range_at(s, i).ch;
|
||||
}
|
||||
|
||||
pub struct CharRange {
|
||||
ch: char,
|
||||
next: uint
|
||||
}
|
||||
|
||||
/**
|
||||
* Given a byte position and a str, return the previous char and its position
|
||||
*
|
||||
* This function can be used to iterate over a unicode string in reverse.
|
||||
*/
|
||||
pure fn char_range_at_reverse(ss: &str, start: uint)
|
||||
-> {ch: char, prev: uint} {
|
||||
-> CharRange {
|
||||
|
||||
let mut prev = start;
|
||||
|
||||
@ -1677,7 +1682,7 @@ pure fn char_range_at_reverse(ss: &str, start: uint)
|
||||
prev -= 1u;
|
||||
|
||||
let ch = char_at(ss, prev);
|
||||
return {ch:ch, prev:prev};
|
||||
return CharRange {ch:ch, next:prev};
|
||||
}
|
||||
|
||||
/**
|
||||
@ -1707,7 +1712,7 @@ pub pure fn all_between(s: &str, start: uint, end: uint,
|
||||
assert is_char_boundary(s, start);
|
||||
let mut i = start;
|
||||
while i < end {
|
||||
let {ch, next} = char_range_at(s, i);
|
||||
let CharRange {ch, next} = char_range_at(s, i);
|
||||
if !it(ch) { return false; }
|
||||
i = next;
|
||||
}
|
||||
|
@ -59,7 +59,7 @@ fn warn_if_multiple_versions(e: env, diag: span_handler,
|
||||
|
||||
if crate_cache.len() != 0u {
|
||||
let name = loader::crate_name_from_metas(*crate_cache.last().metas);
|
||||
let {lefts: matches, rights: non_matches} =
|
||||
let (matches, non_matches) =
|
||||
partition(crate_cache.map_to_vec(|entry| {
|
||||
let othername = loader::crate_name_from_metas(*entry.metas);
|
||||
if name == othername {
|
||||
|
@ -1206,9 +1206,11 @@ mod node {
|
||||
(*it).leaf = option::None;
|
||||
return option::None
|
||||
} else {
|
||||
let {ch, next} =
|
||||
let range =
|
||||
str::char_range_at(*aleaf.content,
|
||||
(*it).leaf_byte_pos + aleaf.byte_offset);
|
||||
let ch = range.ch;
|
||||
let next = range.next;
|
||||
(*it).leaf_byte_pos = next - aleaf.byte_offset;
|
||||
return option::Some(ch)
|
||||
}
|
||||
@ -1283,9 +1285,9 @@ mod tests {
|
||||
equal = false;
|
||||
} break; }
|
||||
option::Some(c) => {
|
||||
let {ch, next} = str::char_range_at(*sample, string_iter);
|
||||
string_iter = next;
|
||||
if ch != c { equal = false; break; }
|
||||
let range = str::char_range_at(*sample, string_iter);
|
||||
string_iter = range.next;
|
||||
if range.ch != c { equal = false; break; }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -277,12 +277,12 @@ priv fn do_strptime(s: &str, format: &str) -> Result<Tm, ~str> {
|
||||
|
||||
let mut i = 0u;
|
||||
while i < digits {
|
||||
let {ch, next} = str::char_range_at(str::from_slice(ss), pos);
|
||||
pos = next;
|
||||
let range = str::char_range_at(str::from_slice(ss), pos);
|
||||
pos = range.next;
|
||||
|
||||
match ch {
|
||||
match range.ch {
|
||||
'0' .. '9' => {
|
||||
value = value * 10_i32 + (ch as i32 - '0' as i32);
|
||||
value = value * 10_i32 + (range.ch as i32 - '0' as i32);
|
||||
}
|
||||
' ' if ws => (),
|
||||
_ => return None
|
||||
@ -294,14 +294,14 @@ priv fn do_strptime(s: &str, format: &str) -> Result<Tm, ~str> {
|
||||
}
|
||||
|
||||
fn parse_char(s: &str, pos: uint, c: char) -> Result<uint, ~str> {
|
||||
let {ch, next} = str::char_range_at(s, pos);
|
||||
let range = str::char_range_at(s, pos);
|
||||
|
||||
if c == ch {
|
||||
Ok(next)
|
||||
if c == range.ch {
|
||||
Ok(range.next)
|
||||
} else {
|
||||
Err(fmt!("Expected %?, found %?",
|
||||
str::from_char(c),
|
||||
str::from_char(ch)))
|
||||
str::from_char(range.ch)))
|
||||
}
|
||||
}
|
||||
|
||||
@ -619,19 +619,19 @@ priv fn do_strptime(s: &str, format: &str) -> Result<Tm, ~str> {
|
||||
let mut pos = pos;
|
||||
let len = str::len(s);
|
||||
while pos < len {
|
||||
let {ch, next} = str::char_range_at(s, pos);
|
||||
pos = next;
|
||||
if ch == ' ' { break; }
|
||||
let range = str::char_range_at(s, pos);
|
||||
pos = range.next;
|
||||
if range.ch == ' ' { break; }
|
||||
}
|
||||
|
||||
Ok(pos)
|
||||
}
|
||||
}
|
||||
'z' => {
|
||||
let {ch, next} = str::char_range_at(s, pos);
|
||||
let range = str::char_range_at(s, pos);
|
||||
|
||||
if ch == '+' || ch == '-' {
|
||||
match match_digits(s, next, 4u, false) {
|
||||
if range.ch == '+' || range.ch == '-' {
|
||||
match match_digits(s, range.next, 4u, false) {
|
||||
Some(item) => {
|
||||
let (v, pos) = item;
|
||||
if v == 0_i32 {
|
||||
@ -674,7 +674,9 @@ priv fn do_strptime(s: &str, format: &str) -> Result<Tm, ~str> {
|
||||
let mut result = Err(~"Invalid time");
|
||||
|
||||
while !rdr.eof() && pos < len {
|
||||
let {ch, next} = str::char_range_at(s, pos);
|
||||
let range = str::char_range_at(s, pos);
|
||||
let ch = range.ch;
|
||||
let next = range.next;
|
||||
|
||||
match rdr.read_char() {
|
||||
'%' => {
|
||||
|
@ -1773,6 +1773,7 @@ impl Parser {
|
||||
fields.push(self.parse_field(token::COLON));
|
||||
}
|
||||
self.expect(token::RBRACE);
|
||||
//self.warn(~"REC");
|
||||
return expr_rec(fields, base);
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user