Switched over a bunch of splitting funktions to non-allocating iterators

This commit is contained in:
Marvin Löbel 2013-03-24 07:51:18 +01:00
parent d74606ead6
commit b9de2b5787
13 changed files with 135 additions and 98 deletions

View File

@ -132,10 +132,10 @@ impl_NumStrConv_Integer!(u64)
// Special value strings as [u8] consts.
const inf_buf: [u8*3] = ['i' as u8, 'n' as u8, 'f' as u8];
const positive_inf_buf: [u8*4] = ['+' as u8, 'i' as u8, 'n' as u8, 'f' as u8];
const negative_inf_buf: [u8*4] = ['-' as u8, 'i' as u8, 'n' as u8, 'f' as u8];
const nan_buf: [u8*3] = ['N' as u8, 'a' as u8, 'N' as u8];
static inf_buf: [u8*3] = ['i' as u8, 'n' as u8, 'f' as u8];
static positive_inf_buf: [u8*4] = ['+' as u8, 'i' as u8, 'n' as u8, 'f' as u8];
static negative_inf_buf: [u8*4] = ['-' as u8, 'i' as u8, 'n' as u8, 'f' as u8];
static nan_buf: [u8*3] = ['N' as u8, 'a' as u8, 'N' as u8];
/**
* Converts a number to its string representation as a byte vector.

View File

@ -218,7 +218,8 @@ pub fn env() -> ~[(~str,~str)] {
fn env_convert(input: ~[~str]) -> ~[(~str, ~str)] {
let mut pairs = ~[];
for input.each |p| {
let vs = str::splitn_char(*p, '=', 1);
let mut vs = ~[];
for str::each_splitn_char(*p, '=', 1) |s| { vs.push(s.to_owned()) }
debug!("splitting: len: %u",
vs.len());
fail_unless!(vs.len() == 2);

View File

@ -381,7 +381,8 @@ impl ToStr for PosixPath {
impl GenericPath for PosixPath {
fn from_str(s: &str) -> PosixPath {
let mut components = str::split_nonempty(s, |c| c == '/');
let mut components = ~[];
for str::each_split_nonempty(s, |c| c == '/') |s| { components.push(s.to_owned()) }
let is_absolute = (s.len() != 0 && s[0] == '/' as u8);
return PosixPath { is_absolute: is_absolute,
components: components }
@ -504,9 +505,10 @@ impl GenericPath for PosixPath {
fn push_many(&self, cs: &[~str]) -> PosixPath {
let mut v = copy self.components;
for cs.each |e| {
let mut ss = str::split_nonempty(
*e,
|c| windows::is_sep(c as u8));
let mut ss = ~[];
for str::each_split_nonempty(*e, |c| windows::is_sep(c as u8)) |s| {
ss.push(s.to_owned())
}
unsafe { v.push_all_move(ss); }
}
PosixPath { is_absolute: self.is_absolute,
@ -515,7 +517,10 @@ impl GenericPath for PosixPath {
fn push(&self, s: &str) -> PosixPath {
let mut v = copy self.components;
let mut ss = str::split_nonempty(s, |c| windows::is_sep(c as u8));
let mut ss = ~[];
for str::each_split_nonempty(s, |c| windows::is_sep(c as u8)) |s| {
ss.push(s.to_owned())
}
unsafe { v.push_all_move(ss); }
PosixPath { components: v, ..copy *self }
}
@ -590,8 +595,10 @@ impl GenericPath for WindowsPath {
}
}
let mut components =
str::split_nonempty(rest, |c| windows::is_sep(c as u8));
let mut components = ~[];
for str::each_split_nonempty(rest, |c| windows::is_sep(c as u8)) |s| {
components.push(s.to_owned())
}
let is_absolute = (rest.len() != 0 && windows::is_sep(rest[0]));
return WindowsPath { host: host,
device: device,
@ -759,9 +766,10 @@ impl GenericPath for WindowsPath {
fn push_many(&self, cs: &[~str]) -> WindowsPath {
let mut v = copy self.components;
for cs.each |e| {
let mut ss = str::split_nonempty(
*e,
|c| windows::is_sep(c as u8));
let mut ss = ~[];
for str::each_split_nonempty(*e, |c| windows::is_sep(c as u8)) |s| {
ss.push(s.to_owned())
}
unsafe { v.push_all_move(ss); }
}
// tedious, but as-is, we can't use ..self
@ -775,7 +783,10 @@ impl GenericPath for WindowsPath {
fn push(&self, s: &str) -> WindowsPath {
let mut v = copy self.components;
let mut ss = str::split_nonempty(s, |c| windows::is_sep(c as u8));
let mut ss = ~[];
for str::each_split_nonempty(s, |c| windows::is_sep(c as u8)) |s| {
ss.push(s.to_owned())
}
unsafe { v.push_all_move(ss); }
return WindowsPath { components: v, ..copy *self }
}

View File

@ -327,7 +327,9 @@ impl RngUtil for @Rng {
*/
fn gen_char_from(&self, chars: &str) -> char {
fail_unless!(!chars.is_empty());
self.choose(str::chars(chars))
let mut cs = ~[];
for str::each_char(chars) |c| { cs.push(c) }
self.choose(cs)
}
/// Return a random bool

View File

@ -463,7 +463,7 @@ pub fn each_split_char_nonempty(s: &str, sep: char, it: &fn(&str) -> bool) {
}
fn each_split_char_inner(s: &str, sep: char, count: uint, allow_empty: bool,
allow_trailing_empty: bool), it: &fn(&str) -> bool) {
allow_trailing_empty: bool, it: &fn(&str) -> bool) {
if sep < 128u as char {
let b = sep as u8, l = len(s);
let mut done = 0u;
@ -513,8 +513,8 @@ pub fn each_split_nonempty(s: &str, sepfn: &fn(char) -> bool, it: &fn(&str) -> b
each_split_inner(s, sepfn, len(s), false, false, it)
}
pure fn each_split_inner(s: &str, sepfn: &fn(cc: char) -> bool, count: uint,
allow_empty: bool, allow_trailing_empty: bool), it: &fn(&str) -> bool) {
fn each_split_inner(s: &str, sepfn: &fn(cc: char) -> bool, count: uint,
allow_empty: bool, allow_trailing_empty: bool, it: &fn(&str) -> bool) {
let l = len(s);
let mut i = 0u, start = 0u, done = 0u;
while i < l && done < count {
@ -534,7 +534,7 @@ pure fn each_split_inner(s: &str, sepfn: &fn(cc: char) -> bool, count: uint,
}
// See Issue #1932 for why this is a naive search
fn iter_matches(s: &'a str, sep: &'b str, f: &fn(uint, uint)) {
fn iter_matches(s: &'a str, sep: &'b str, f: &fn(uint, uint) -> bool) {
let sep_len = len(sep), l = len(s);
fail_unless!(sep_len > 0u);
let mut i = 0u, match_start = 0u, match_i = 0u;
@ -545,7 +545,7 @@ fn iter_matches(s: &'a str, sep: &'b str, f: &fn(uint, uint)) {
match_i += 1u;
// Found a match
if match_i == sep_len {
f(match_start, i + 1u);
if !f(match_start, i + 1u) { return; }
match_i = 0u;
}
i += 1u;
@ -561,10 +561,10 @@ fn iter_matches(s: &'a str, sep: &'b str, f: &fn(uint, uint)) {
}
}
fn iter_between_matches(s: &'a str, sep: &'b str, f: &fn(uint, uint)) {
fn iter_between_matches(s: &'a str, sep: &'b str, f: &fn(uint, uint) -> bool) {
let mut last_end = 0u;
do iter_matches(s, sep) |from, to| {
f(last_end, from);
for iter_matches(s, sep) |from, to| {
if !f(last_end, from) { return; }
last_end = to;
}
f(last_end, len(s));
@ -580,13 +580,13 @@ fn iter_between_matches(s: &'a str, sep: &'b str, f: &fn(uint, uint)) {
* ~~~
*/
pub fn each_split_str(s: &'a str, sep: &'b str, it: &fn(&str) -> bool) {
do iter_between_matches(s, sep) |from, to| {
for iter_between_matches(s, sep) |from, to| {
if !it( unsafe { raw::slice_bytes(s, from, to) } ) { return; }
}
}
pub fn each_split_str_nonempty(s: &'a str, sep: &'b str, it: &fn(&str) -> bool) {
do iter_between_matches(s, sep) |from, to| {
for iter_between_matches(s, sep) |from, to| {
if to > from {
if !it( unsafe { raw::slice_bytes(s, from, to) } ) { return; }
}
@ -630,7 +630,7 @@ pub fn levdistance(s: &str, t: &str) -> uint {
/**
* Splits a string into a vector of the substrings separated by LF ('\n').
*/
pub fn each_line(s: &str, it: &fn(&str) -> bool) { each_split_char(s, '\n', it) }
pub fn each_line(s: &str, it: &fn(&str) -> bool) { each_split_char_no_trailing(s, '\n', it) }
/**
* Splits a string into a vector of the substrings separated by LF ('\n')
@ -656,52 +656,56 @@ pub fn each_word(s: &str, it: &fn(&str) -> bool) {
* each of which is less bytes long than a limit
*/
pub fn each_split_within(ss: &str, lim: uint, it: &fn(&str) -> bool) {
let words = str::words(ss);
// empty?
if words == ~[] { return ~[]; }
let mut rows : ~[~str] = ~[];
let mut row : ~str = ~"";
for words.each |wptr| {
let word = copy *wptr;
// if adding this word to the row would go over the limit,
// then start a new row
if row.len() + word.len() + 1 > lim {
rows.push(copy row); // save previous row
row = word; // start a new one
} else {
if row.len() > 0 { row += ~" " } // separate words
row += word; // append to this row
}
// Just for fun, let's write this as an automaton
enum SplitWithinState {
A, // Leading whitespace, initial state
B, // Words
C, // Internal and trailing whitespace
}
enum Whitespace { Ws, Cr }
enum LengthLimit { UnderLim, OverLim }
// save the last row
if row != ~"" { rows.push(row); }
let mut slice_start = 0;
let mut last_start = 0;
let mut last_end = 0;
let mut state = A;
rows
// NOTE: Finish change here
let mut cont = true;
let slice = || { cont = it(ss.slice(slice_start, last_end)) };
let mut last_slice_i = 0, last_word_i = 0, word_start = true;
for each_chari(s) |i, c| {
if (i - last_slice_i) <= lim {
if char::is_whitespace(c) {
let machine = |i: uint, c: char| {
let whitespace = if char::is_whitespace(c) { Ws } else { Cr };
let limit = if (i - slice_start + 1) <= lim { UnderLim } else { OverLim };
} else {
state = match (state, whitespace, limit) {
(A, Ws, _) => { A }
(A, Cr, _) => { slice_start = i; last_start = i; B }
}
} else {
(B, Cr, UnderLim) => { B }
(B, Cr, OverLim) if (i - last_start + 1) > lim
=> { fail!(~"word longer than limit!") }
(B, Cr, OverLim) => { slice(); slice_start = last_start; B }
(B, Ws, UnderLim) => { last_end = i; C }
(B, Ws, OverLim) => { last_end = i; slice(); A }
}
(C, Cr, UnderLim) => { last_start = i; B }
(C, Cr, OverLim) => { slice(); slice_start = i; last_start = i; last_end = i; B }
(C, Ws, OverLim) => { slice(); A }
(C, Ws, UnderLim) => { C }
};
cont
};
str::each_chari(ss, machine);
// Let the automaton 'run out'
let mut fake_i = ss.len();
while cont && match state { B | C => true, A => false } {
machine(fake_i, ' ');
fake_i += 1;
}
}
/// Convert a string to lowercase. ASCII only
pub fn to_lower(s: &str) -> ~str {
map(s,
@ -731,7 +735,7 @@ pub fn to_upper(s: &str) -> ~str {
*/
pub fn replace(s: &str, from: &str, to: &str) -> ~str {
let mut result = ~"", first = true;
do iter_between_matches(s, from) |start, end| {
for iter_between_matches(s, from) |start, end| {
if first {
first = false;
} else {
@ -2286,9 +2290,9 @@ pub trait StrSlice {
fn len(&self) -> uint;
fn char_len(&self) -> uint;
fn slice(&self, begin: uint, end: uint) -> &'self str;
fn split(&self, sepfn: &fn(char) -> bool) -> ~[~str];
fn split_char(&self, sep: char) -> ~[~str];
fn split_str(&self, sep: &'a str) -> ~[~str];
fn each_split(&self, sepfn: &fn(char) -> bool, it: &fn(&str) -> bool);
fn each_split_char(&self, sep: char, it: &fn(&str) -> bool);
fn each_split_str(&self, sep: &'a str, it: &fn(&str) -> bool);
fn starts_with(&self, needle: &'a str) -> bool;
fn substr(&self, begin: uint, n: uint) -> &'self str;
fn to_lower(&self) -> ~str;
@ -2408,20 +2412,24 @@ impl StrSlice for &'self str {
}
/// Splits a string into substrings using a character function
#[inline]
fn split(&self, sepfn: &fn(char) -> bool) -> ~[~str] {
split(*self, sepfn)
fn each_split(&self, sepfn: &fn(char) -> bool, it: &fn(&str) -> bool) {
each_split(*self, sepfn, it)
}
/**
* Splits a string into substrings at each occurrence of a given character
*/
#[inline]
fn split_char(&self, sep: char) -> ~[~str] { split_char(*self, sep) }
fn each_split_char(&self, sep: char, it: &fn(&str) -> bool) {
each_split_char(*self, sep, it)
}
/**
* Splits a string into a vector of the substrings separated by a given
* string
*/
#[inline]
fn split_str(&self, sep: &'a str) -> ~[~str] { split_str(*self, sep) }
fn each_split_str(&self, sep: &'a str, it: &fn(&str) -> bool) {
each_split_str(*self, sep, it)
}
/// Returns true if one string starts with another
#[inline]
fn starts_with(&self, needle: &'a str) -> bool {

View File

@ -120,7 +120,9 @@ pub fn get_used_libraries(cstore: @mut CStore) -> ~[~str] {
}
pub fn add_used_link_args(cstore: @mut CStore, args: &str) {
cstore.used_link_args.push_all(args.split_char(' '));
for args.each_split_char(' ') |s| {
cstore.used_link_args.push(s.to_owned());
}
}
pub fn get_used_link_args(cstore: @mut CStore) -> ~[~str] {

View File

@ -76,7 +76,7 @@ use syntax::visit::{visit_mod, visit_ty, vt};
use syntax::opt_vec::OptVec;
use core::option::{Some, get, is_some, is_none};
use core::str::{connect, split_str};
use core::str::{connect, each_split_str};
use core::hashmap::linear::LinearMap;
use std::oldmap::HashMap;
@ -1696,7 +1696,8 @@ pub impl Resolver {
entry: %s (%?)",
path_string, def_like);
let mut pieces = split_str(path_string, ~"::");
let mut pieces = ~[];
for each_split_str(path_string, "::") |s| { pieces.push(s.to_owned()) }
let final_ident_str = pieces.pop();
let final_ident = self.session.ident_of(final_ident_str);

View File

@ -16,12 +16,16 @@ pub trait ToBase64 {
fn to_base64(&self) -> ~str;
}
static CHARS: &'static[char] = &[
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'
];
impl ToBase64 for &'self [u8] {
fn to_base64(&self) -> ~str {
let chars = str::chars(
~"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
);
let mut s = ~"";
unsafe {
let len = self.len();
@ -35,10 +39,10 @@ impl ToBase64 for &'self [u8] {
(self[i + 2u] as uint);
// This 24-bit number gets separated into four 6-bit numbers.
str::push_char(&mut s, chars[(n >> 18u) & 63u]);
str::push_char(&mut s, chars[(n >> 12u) & 63u]);
str::push_char(&mut s, chars[(n >> 6u) & 63u]);
str::push_char(&mut s, chars[n & 63u]);
str::push_char(&mut s, CHARS[(n >> 18u) & 63u]);
str::push_char(&mut s, CHARS[(n >> 12u) & 63u]);
str::push_char(&mut s, CHARS[(n >> 6u) & 63u]);
str::push_char(&mut s, CHARS[n & 63u]);
i += 3u;
}
@ -49,17 +53,17 @@ impl ToBase64 for &'self [u8] {
0 => (),
1 => {
let n = (self[i] as uint) << 16u;
str::push_char(&mut s, chars[(n >> 18u) & 63u]);
str::push_char(&mut s, chars[(n >> 12u) & 63u]);
str::push_char(&mut s, CHARS[(n >> 18u) & 63u]);
str::push_char(&mut s, CHARS[(n >> 12u) & 63u]);
str::push_char(&mut s, '=');
str::push_char(&mut s, '=');
}
2 => {
let n = (self[i] as uint) << 16u |
(self[i + 1u] as uint) << 8u;
str::push_char(&mut s, chars[(n >> 18u) & 63u]);
str::push_char(&mut s, chars[(n >> 12u) & 63u]);
str::push_char(&mut s, chars[(n >> 6u) & 63u]);
str::push_char(&mut s, CHARS[(n >> 18u) & 63u]);
str::push_char(&mut s, CHARS[(n >> 12u) & 63u]);
str::push_char(&mut s, CHARS[(n >> 6u) & 63u]);
str::push_char(&mut s, '=');
}
_ => fail!(~"Algebra is broken, please alert the math police")

View File

@ -244,7 +244,8 @@ pub fn getopts(args: &[~str], opts: &[Opt]) -> Result {
let mut i_arg = None;
if cur[1] == '-' as u8 {
let tail = str::slice(cur, 2, curlen).to_owned();
let tail_eq = str::splitn_char(tail, '=', 1);
let mut tail_eq = ~[];
for str::each_splitn_char(tail, '=', 1) |s| { tail_eq.push(s.to_owned()) }
if tail_eq.len() <= 1 {
names = ~[Long(tail)];
} else {
@ -627,16 +628,16 @@ pub mod groups {
};
// Normalize desc to contain words seperated by one space character
let mut desc_normalized_whitespace = ~str
for desc.each_word |word| {
let mut desc_normalized_whitespace = ~"";
for str::each_word(desc) |word| {
desc_normalized_whitespace.push_str(word);
desc_normalized_whitespace.push_char(' ');
}
// FIXME: #5516
let mut desc_rows: ~[~str] = ~[];
for desc_normalized_whitespace.each_split_within(54) |substr| {
desc_rows.push(~substr);
let mut desc_rows = ~[];
for str::each_split_within(desc_normalized_whitespace, 54) |substr| {
desc_rows.push(substr.to_owned());
}
// FIXME: #5516

View File

@ -806,7 +806,8 @@ impl serialize::Decoder for Decoder<'self> {
}
fn read_char(&self) -> char {
let v = str::chars(self.read_owned_str());
let mut v = ~[];
for str::each_char(self.read_owned_str()) |c| { v.push(c) }
if v.len() != 1 { fail!(~"string must have one character") }
v[0]
}

View File

@ -197,7 +197,9 @@ pub mod v4 {
}
}
pub fn parse_to_ipv4_rep(ip: &str) -> result::Result<Ipv4Rep, ~str> {
let parts = vec::map(str::split_char(ip, '.'), |s| {
let mut parts = ~[];
for str::each_split_char(ip, '.') |s| { parts.push(s.to_owned()) }
let parts = vec::map(parts, |s| {
match uint::from_str(*s) {
Some(n) if n <= 255 => n,
_ => 256

View File

@ -344,8 +344,8 @@ fn userinfo_to_str(userinfo: &UserInfo) -> ~str {
fn query_from_str(rawquery: &str) -> Query {
let mut query: Query = ~[];
if str::len(rawquery) != 0 {
for str::split_char(rawquery, '&').each |p| {
let (k, v) = split_char_first(*p, '=');
for str::each_split_char(rawquery, '&') |p| {
let (k, v) = split_char_first(p, '=');
// FIXME(#3722): unsafe only because decode_inner does (string) IO
unsafe {query.push((decode_component(k), decode_component(v)));}
};

View File

@ -99,7 +99,8 @@ pub fn strip_doc_comment_decoration(comment: &str) -> ~str {
}
return do lines.map |line| {
let chars = str::chars(*line);
let mut chars = ~[];
for str::each_char(*line) |c| { chars.push(c) }
if i > chars.len() {
~""
} else {
@ -116,7 +117,10 @@ pub fn strip_doc_comment_decoration(comment: &str) -> ~str {
}
if comment.starts_with(~"/*") {
let lines = str::lines_any(comment.slice(3u, comment.len() - 2u).to_owned());
let mut lines = ~[];
for str::each_line_any(comment.slice(3u, comment.len() - 2u)) |line| {
lines.push(line.to_owned())
}
let lines = vertical_trim(lines);
let lines = block_trim(lines, ~"\t ", None);
let lines = block_trim(lines, ~"*", Some(1u));