提交 1b957c09 编写于 作者: K Kevin Cantu 提交者: Marijn Haverbeke

(core::str) replace uses of unsafe::slice_bytes; replace find[_from]_bytes with find[_from]

上级 cec05348
......@@ -478,7 +478,7 @@ fn crate_meta_vers(sess: session, _crate: ast::crate,
}
fn truncated_sha1_result(sha: sha1) -> str unsafe {
ret str::unsafe::slice_bytes(sha.result_str(), 0u, 16u);
ret str::slice(sha.result_str(), 0u, 16u);
}
......@@ -567,12 +567,12 @@ fn link_binary(sess: session,
// Converts a library file name into a cc -l argument
fn unlib(config: @session::config, filename: str) -> str unsafe {
let rmlib = fn@(filename: str) -> str {
let found = str::find_bytes(filename, "lib");
let found = str::find(filename, "lib");
if config.os == session::os_macos ||
(config.os == session::os_linux ||
config.os == session::os_freebsd) &&
option::is_some(found) && option::get(found) == 0u {
ret str::unsafe::slice_bytes(filename, 3u,
ret str::slice(filename, 3u,
str::len_bytes(filename));
} else { ret filename; }
};
......
......@@ -167,7 +167,7 @@ fn create_compile_unit(cx: crate_ctxt, full_path: str)
let work_dir = cx.sess.working_dir;
let file_path = if str::starts_with(full_path, work_dir) {
str::unsafe::slice_bytes(full_path, str::len_bytes(work_dir),
str::slice(full_path, str::len_bytes(work_dir),
str::len_bytes(full_path))
} else {
full_path
......
......@@ -162,7 +162,7 @@ fn get_line(fm: filemap, line: int) -> str unsafe {
some(e) { e }
none { str::len_bytes(*fm.src) }
};
str::unsafe::slice_bytes(*fm.src, begin, end)
str::slice(*fm.src, begin, end)
}
fn lookup_byte_offset(cm: codemap::codemap, chpos: uint)
......
......@@ -24,7 +24,7 @@ fn is_eof() -> bool { self.curr == -1 as char }
fn get_str_from(start: uint) -> str unsafe {
// I'm pretty skeptical about this subtraction. What if there's a
// multi-byte character before the mark?
ret str::unsafe::slice_bytes(*self.src, start - 1u, self.pos - 1u);
ret str::slice(*self.src, start - 1u, self.pos - 1u);
}
fn next() -> char {
if self.pos < self.len {
......@@ -611,7 +611,7 @@ fn trim_whitespace_prefix_and_push_line(&lines: [str],
let s1;
if all_whitespace(s, 0u, col) {
if col < str::len_bytes(s) {
s1 = str::unsafe::slice_bytes(s, col, str::len_bytes(s));
s1 = str::slice(s, col, str::len_bytes(s));
} else { s1 = ""; }
} else { s1 = s; }
log(debug, "pushing line: " + s1);
......
......@@ -131,7 +131,7 @@ fn parameterized(cx: ctxt, base: str, tps: [ty::t]) -> str {
fn ty_to_short_str(cx: ctxt, typ: t) -> str unsafe {
let s = encoder::encoded_ty(cx, typ);
if str::len_bytes(s) >= 32u { s = str::unsafe::slice_bytes(s, 0u, 32u); }
if str::len_bytes(s) >= 32u { s = str::slice(s, 0u, 32u); }
ret s;
}
......
......@@ -25,7 +25,7 @@ fn load_errors(testfile: str) -> [expected_error] {
fn parse_expected(line_num: uint, line: str) -> [expected_error] unsafe {
let error_tag = "//!";
let idx;
alt str::find_bytes(line, error_tag) {
alt str::find(line, error_tag) {
option::none { ret []; }
option::some(nn) { idx = (nn as uint) + str::len_bytes(error_tag); }
}
......@@ -43,11 +43,11 @@ fn parse_expected(line_num: uint, line: str) -> [expected_error] unsafe {
while idx < len && line[idx] == (' ' as u8) { idx += 1u; }
let start_kind = idx;
while idx < len && line[idx] != (' ' as u8) { idx += 1u; }
let kind = str::to_lower(str::unsafe::slice_bytes(line, start_kind, idx));
let kind = str::to_lower(str::slice(line, start_kind, idx));
// Extract msg:
while idx < len && line[idx] == (' ' as u8) { idx += 1u; }
let msg = str::unsafe::slice_bytes(line, idx, len);
let msg = str::slice(line, idx, len);
#debug("line=%u kind=%s msg=%s", line_num - adjust_line, kind, msg);
......
......@@ -106,10 +106,10 @@ fn parse_name_directive(line: str, directive: str) -> bool {
fn parse_name_value_directive(line: str,
directive: str) -> option<str> unsafe {
let keycolon = directive + ":";
alt str::find_bytes(line, keycolon) {
alt str::find(line, keycolon) {
option::some(colon) {
let value =
str::unsafe::slice_bytes(line,
str::slice(line,
colon + str::len_bytes(keycolon),
str::len_bytes(line));
#debug("%s: %s", directive, value);
......
......@@ -93,13 +93,13 @@ fn flush_buf(buf: str, &pieces: [piece]) -> str {
}
let i = 0u;
while i < lim {
let curr = str::unsafe::slice_bytes(s, i, i+1u);
let curr = str::slice(s, i, i+1u);
if str::eq(curr, "%") {
i += 1u;
if i >= lim {
error("unterminated conversion at end of string");
}
let curr2 = str::unsafe::slice_bytes(s, i, i+1u);
let curr2 = str::slice(s, i, i+1u);
if str::eq(curr2, "%") {
buf += curr2;
i += 1u;
......@@ -225,7 +225,7 @@ fn parse_precision(s: str, i: uint, lim: uint) ->
fn parse_type(s: str, i: uint, lim: uint, error: error_fn) ->
{ty: ty, next: uint} unsafe {
if i >= lim { error("missing type in conversion"); }
let tstr = str::unsafe::slice_bytes(s, i, i+1u);
let tstr = str::slice(s, i, i+1u);
// TODO: Do we really want two signed types here?
// How important is it to be printf compatible?
let t =
......@@ -439,7 +439,7 @@ fn have_precision(cv: conv) -> bool {
let headstr = str::from_bytes([head]);
// FIXME: not UTF-8 safe
let bytelen = str::len_bytes(s);
let numpart = str::unsafe::slice_bytes(s, 1u, bytelen);
let numpart = str::slice(s, 1u, bytelen);
ret headstr + padstr + numpart;
}
}
......
......@@ -75,9 +75,9 @@
index_from,
rindex,
//rindex_chars,
find,
find_from,
find_chars,
find_bytes,
find_from_bytes,
contains,
starts_with,
ends_with,
......@@ -385,7 +385,7 @@ fn chars(s: str) -> [char] {
/*
Function: substr
Take a substring of another. Returns a string containing `len` chars
Take a substring of another. Returns a string containing `len` bytes
starting at char offset `begin`.
Failure:
......@@ -393,7 +393,7 @@ fn chars(s: str) -> [char] {
If `begin` + `len` is is greater than the char length of the string
*/
fn substr(s: str, begin: uint, len: uint) -> str {
ret slice_chars(s, begin, begin + len);
ret slice(s, begin, begin + len);
}
// Function: slice
......@@ -696,7 +696,7 @@ fn replace(s: str, from: str, to: str) -> str unsafe {
from, to);
} else {
let idx;
alt find_bytes(s, from) {
alt find(s, from) {
some(x) { idx = x; }
none { ret s; }
}
......@@ -977,21 +977,21 @@ fn rindex_chars(ss: str, cc: char) -> option<uint> {
ret none;
}
//Function: find_bytes
//Function: find
//
// Find the byte position of the first instance of one string
// within another, or return option::none
fn find_bytes(haystack: str, needle: str) -> option<uint> {
find_from_bytes(haystack, needle, 0u, len_bytes(haystack))
fn find(haystack: str, needle: str) -> option<uint> {
find_from(haystack, needle, 0u, len_bytes(haystack))
}
//Function: find_from_bytes
//Function: find_from
//
// Find the byte position of the first instance of one string
// within another, or return option::none
//
// FIXME: Boyer-Moore should be significantly faster
fn find_from_bytes(haystack: str, needle: str, start: uint, end:uint)
fn find_from(haystack: str, needle: str, start: uint, end:uint)
-> option<uint> {
assert end <= len_bytes(haystack);
......@@ -1020,7 +1020,7 @@ fn match_at(haystack: str, needle: str, ii: uint) -> bool {
// Find the char position of the first instance of one string
// within another, or return option::none
fn find_chars(haystack: str, needle: str) -> option<uint> {
alt find_bytes(haystack, needle) {
alt find(haystack, needle) {
none { ret none; }
some(nn) { ret some(b2c_pos(haystack, nn)); }
}
......@@ -1056,7 +1056,7 @@ fn b2c_pos(ss: str, bpos: uint) -> uint {
needle - The string to look for
*/
fn contains(haystack: str, needle: str) -> bool {
option::is_some(find_bytes(haystack, needle))
option::is_some(find(haystack, needle))
}
/*
......@@ -1479,8 +1479,8 @@ mod unsafe {
export
from_bytes,
from_byte,
slice_bytes,
slice_bytes_safe_range,
slice_bytes, // FIXME: stop exporting
slice_bytes_safe_range, // FIXME: stop exporting
push_byte,
push_bytes, // note: wasn't exported
pop_byte,
......@@ -1840,45 +1840,45 @@ fn test_words () {
}
#[test]
fn test_find_bytes() {
fn test_find() {
// byte positions
assert (find_bytes("banana", "apple pie") == none);
assert (find_bytes("", "") == some(0u));
assert (find("banana", "apple pie") == none);
assert (find("", "") == some(0u));
let data = "ประเทศไทย中华Việt Nam";
assert (find_bytes(data, "") == some(0u));
assert (find_bytes(data, "ประเ") == some( 0u));
assert (find_bytes(data, "ะเ") == some( 6u));
assert (find_bytes(data, "中华") == some(27u));
assert (find_bytes(data, "ไท华") == none);
assert (find(data, "") == some(0u));
assert (find(data, "ประเ") == some( 0u));
assert (find(data, "ะเ") == some( 6u));
assert (find(data, "中华") == some(27u));
assert (find(data, "ไท华") == none);
}
#[test]
fn test_find_from_bytes() {
fn test_find_from() {
// byte positions
assert (find_from_bytes("", "", 0u, 0u) == some(0u));
assert (find_from("", "", 0u, 0u) == some(0u));
let data = "abcabc";
assert find_from_bytes(data, "ab", 0u, 6u) == some(0u);
assert find_from_bytes(data, "ab", 2u, 6u) == some(3u);
assert find_from_bytes(data, "ab", 2u, 4u) == none;
assert find_from(data, "ab", 0u, 6u) == some(0u);
assert find_from(data, "ab", 2u, 6u) == some(3u);
assert find_from(data, "ab", 2u, 4u) == none;
let data = "ประเทศไทย中华Việt Nam";
data += data;
assert find_from_bytes(data, "", 0u, 43u) == some(0u);
assert find_from_bytes(data, "", 6u, 43u) == some(6u);
assert find_from(data, "", 0u, 43u) == some(0u);
assert find_from(data, "", 6u, 43u) == some(6u);
assert find_from_bytes(data, "ประ", 0u, 43u) == some( 0u);
assert find_from_bytes(data, "ทศไ", 0u, 43u) == some(12u);
assert find_from_bytes(data, "ย中", 0u, 43u) == some(24u);
assert find_from_bytes(data, "iệt", 0u, 43u) == some(34u);
assert find_from_bytes(data, "Nam", 0u, 43u) == some(40u);
assert find_from(data, "ประ", 0u, 43u) == some( 0u);
assert find_from(data, "ทศไ", 0u, 43u) == some(12u);
assert find_from(data, "ย中", 0u, 43u) == some(24u);
assert find_from(data, "iệt", 0u, 43u) == some(34u);
assert find_from(data, "Nam", 0u, 43u) == some(40u);
assert find_from_bytes(data, "ประ", 43u, 86u) == some(43u);
assert find_from_bytes(data, "ทศไ", 43u, 86u) == some(55u);
assert find_from_bytes(data, "ย中", 43u, 86u) == some(67u);
assert find_from_bytes(data, "iệt", 43u, 86u) == some(77u);
assert find_from_bytes(data, "Nam", 43u, 86u) == some(83u);
assert find_from(data, "ประ", 43u, 86u) == some(43u);
assert find_from(data, "ทศไ", 43u, 86u) == some(55u);
assert find_from(data, "ย中", 43u, 86u) == some(67u);
assert find_from(data, "iệt", 43u, 86u) == some(77u);
assert find_from(data, "Nam", 43u, 86u) == some(83u);
}
#[test]
......@@ -1912,7 +1912,7 @@ fn t(a: str, b: str, start: int) {
t("hello", "el", 1);
assert "ะเทศไท"
== substr("ประเทศไทย中华Việt Nam", 2u, 6u);
== substr("ประเทศไทย中华Việt Nam", 6u, 18u);
}
#[test]
......
......@@ -229,7 +229,7 @@ fn getopts(args: [str], opts: [opt]) -> result unsafe {
let names;
let i_arg = option::none::<str>;
if cur[1] == '-' as u8 {
let tail = str::unsafe::slice_bytes(cur, 2u, curlen);
let tail = str::slice(cur, 2u, curlen);
let tail_eq = str::splitn_char(tail, '=', 1u);
if vec::len(tail_eq) <= 1u {
names = [long(tail)];
......
......@@ -1345,7 +1345,7 @@ fn rope_to_string(r: rope) -> str {
fn aux(str: @mutable str, node: @node::node) unsafe {
alt(*node) {
node::leaf(x) {
*str += str::unsafe::slice_bytes(
*str += str::slice(
*x.content, x.byte_offset,
x.byte_offset + x.byte_len);
}
......
......@@ -372,7 +372,7 @@ fn check_vec_eq(v0: [u8], v1: [u8]) {
let left = len;
while left > 0u {
let take = (left + 1u) / 2u;
sh.input_str(str::unsafe::slice_bytes(t.input, len - left,
sh.input_str(str::slice(t.input, len - left,
take + len - left));
left = left - take;
}
......
......@@ -56,10 +56,10 @@ mod c { }\
fn d() { }"
);
let idx_a = option::get(str::find_bytes(markdown, "# Module `a`"));
let idx_b = option::get(str::find_bytes(markdown, "## Function `b`"));
let idx_c = option::get(str::find_bytes(markdown, "# Module `c`"));
let idx_d = option::get(str::find_bytes(markdown, "## Function `d`"));
let idx_a = option::get(str::find(markdown, "# Module `a`"));
let idx_b = option::get(str::find(markdown, "## Function `b`"));
let idx_c = option::get(str::find(markdown, "# Module `c`"));
let idx_d = option::get(str::find(markdown, "## Function `d`"));
assert idx_b < idx_d;
assert idx_d < idx_a;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册