From 1b957c0942007e60ec9ea6773c964ea7bdc199af Mon Sep 17 00:00:00 2001 From: Kevin Cantu Date: Thu, 23 Feb 2012 00:45:25 -0800 Subject: [PATCH] (core::str) replace uses of unsafe::slice_bytes; replace find[_from]_bytes with find[_from] --- src/comp/back/link.rs | 6 +-- src/comp/middle/trans/debuginfo.rs | 2 +- src/comp/syntax/codemap.rs | 2 +- src/comp/syntax/parse/lexer.rs | 4 +- src/comp/util/ppaux.rs | 2 +- src/compiletest/errors.rs | 6 +-- src/compiletest/header.rs | 4 +- src/libcore/extfmt.rs | 8 +-- src/libcore/str.rs | 80 +++++++++++++++--------------- src/libstd/getopts.rs | 2 +- src/libstd/rope.rs | 2 +- src/libstd/sha1.rs | 2 +- src/rustdoc/markdown_pass.rs | 8 +-- 13 files changed, 64 insertions(+), 64 deletions(-) diff --git a/src/comp/back/link.rs b/src/comp/back/link.rs index 5b7d7a35c53..572fde7bd3f 100644 --- a/src/comp/back/link.rs +++ b/src/comp/back/link.rs @@ -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; } }; diff --git a/src/comp/middle/trans/debuginfo.rs b/src/comp/middle/trans/debuginfo.rs index 39da8cf2c57..dff4f26ae99 100644 --- a/src/comp/middle/trans/debuginfo.rs +++ b/src/comp/middle/trans/debuginfo.rs @@ -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 diff --git a/src/comp/syntax/codemap.rs b/src/comp/syntax/codemap.rs index abe5a242261..e9b4569df6c 100644 --- a/src/comp/syntax/codemap.rs +++ b/src/comp/syntax/codemap.rs @@ -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) diff --git a/src/comp/syntax/parse/lexer.rs b/src/comp/syntax/parse/lexer.rs index 113ca056ed4..0328afc9f20 100644 --- a/src/comp/syntax/parse/lexer.rs +++ b/src/comp/syntax/parse/lexer.rs @@ -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); diff --git a/src/comp/util/ppaux.rs b/src/comp/util/ppaux.rs index 547c2d5ee82..18bd05f2b8d 100644 --- a/src/comp/util/ppaux.rs +++ b/src/comp/util/ppaux.rs @@ -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; } diff --git a/src/compiletest/errors.rs b/src/compiletest/errors.rs index f6b32e2fd31..1184fc5f0f2 100644 --- a/src/compiletest/errors.rs +++ b/src/compiletest/errors.rs @@ -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); diff --git a/src/compiletest/header.rs b/src/compiletest/header.rs index a976c5fb98c..8071edad2fe 100644 --- a/src/compiletest/header.rs +++ b/src/compiletest/header.rs @@ -106,10 +106,10 @@ fn parse_name_directive(line: str, directive: str) -> bool { fn parse_name_value_directive(line: str, directive: str) -> option 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); diff --git a/src/libcore/extfmt.rs b/src/libcore/extfmt.rs index 5fca4d69f3a..079451498f1 100644 --- a/src/libcore/extfmt.rs +++ b/src/libcore/extfmt.rs @@ -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; } } diff --git a/src/libcore/str.rs b/src/libcore/str.rs index b9eea353e21..fb7ec4eec2e 100644 --- a/src/libcore/str.rs +++ b/src/libcore/str.rs @@ -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 { 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 { - find_from_bytes(haystack, needle, 0u, len_bytes(haystack)) +fn find(haystack: str, needle: str) -> option { + 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 { 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 { - 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] diff --git a/src/libstd/getopts.rs b/src/libstd/getopts.rs index e925cd16e33..25681185f9b 100644 --- a/src/libstd/getopts.rs +++ b/src/libstd/getopts.rs @@ -229,7 +229,7 @@ fn getopts(args: [str], opts: [opt]) -> result unsafe { let names; let i_arg = option::none::; 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)]; diff --git a/src/libstd/rope.rs b/src/libstd/rope.rs index 3f330c3586d..4351ceb2d5a 100644 --- a/src/libstd/rope.rs +++ b/src/libstd/rope.rs @@ -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); } diff --git a/src/libstd/sha1.rs b/src/libstd/sha1.rs index c28d67a7526..1b63f87f7c6 100644 --- a/src/libstd/sha1.rs +++ b/src/libstd/sha1.rs @@ -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; } diff --git a/src/rustdoc/markdown_pass.rs b/src/rustdoc/markdown_pass.rs index 1f82e892c0e..d473f25d090 100644 --- a/src/rustdoc/markdown_pass.rs +++ b/src/rustdoc/markdown_pass.rs @@ -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; -- GitLab