From ee1c75488db15011f61858217a7d82f92b2ca281 Mon Sep 17 00:00:00 2001 From: Kevin Cantu Date: Wed, 1 Feb 2012 03:17:24 -0800 Subject: [PATCH 01/11] Actually export the str::unsafe module for now --- src/libcore/str.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/libcore/str.rs b/src/libcore/str.rs index 9f40dd5c3a48c..a24c6e871e88f 100644 --- a/src/libcore/str.rs +++ b/src/libcore/str.rs @@ -98,7 +98,9 @@ export escape_char, as_buf, //buf, - sbuf; + sbuf, + + unsafe; From 3ec515b3aa105da032c13646c0cdb21416fba68f Mon Sep 17 00:00:00 2001 From: Kevin Cantu Date: Wed, 1 Feb 2012 03:25:04 -0800 Subject: [PATCH 02/11] Copy str::slice -> str::unsafe::slice (and unsafe_slice) --- src/libcore/str.rs | 49 +++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 46 insertions(+), 3 deletions(-) diff --git a/src/libcore/str.rs b/src/libcore/str.rs index a24c6e871e88f..02bd21199e5bf 100644 --- a/src/libcore/str.rs +++ b/src/libcore/str.rs @@ -425,7 +425,7 @@ Failure: If `begin` + `len` is is greater than the byte length of the string */ fn substr(s: str, begin: uint, len: uint) -> str { - ret slice(s, begin, begin + len); + ret unsafe::slice(s, begin, begin + len); } /* @@ -712,7 +712,7 @@ fn replace(s: str, from: str, to: str) : is_not_empty(from) -> str { if byte_len(s) == 0u { ret ""; } else if starts_with(s, from) { - ret to + replace(slice(s, byte_len(from), byte_len(s)), from, to); + ret to + replace(unsafe::slice(s, byte_len(from), byte_len(s)), from, to); } else { let idx = find(s, from); if idx == -1 { @@ -1346,7 +1346,9 @@ mod unsafe { export // UNSAFE from_bytes, - from_byte; + from_byte, + slice, + safe_slice; // Function: unsafe::from_bytes // @@ -1364,6 +1366,47 @@ mod unsafe { // Converts a byte to a string. Does not verify that the byte is // valid UTF-8. unsafe fn from_byte(u: u8) -> str { unsafe::from_bytes([u]) } + + /* + Function: slice + + Takes a bytewise slice from a string. Returns the substring from + [`begin`..`end`). + + This function is not unicode-safe. + + Failure: + + - If begin is greater than end. + - If end is greater than the length of the string. + + FIXME: rename to byte_slice + */ + unsafe fn slice(s: str, begin: uint, end: uint) -> str unsafe { + // FIXME: Typestate precondition + assert (begin <= end); + assert (end <= byte_len(s)); + + let v: [u8] = ::unsafe::reinterpret_cast(s); + let v2 = vec::slice(v, begin, end); + ::unsafe::leak(v); + v2 += [0u8]; + let s2: str = ::unsafe::reinterpret_cast(v2); + ::unsafe::leak(v2); + ret s2; + } + + /* + Function: safe_slice + + FIXME: rename to safe_range_byte_slice + */ + unsafe fn safe_slice(s: str, begin: uint, end: uint) : uint::le(begin, end) -> str { + // would need some magic to make this a precondition + assert (end <= byte_len(s)); + ret slice(s, begin, end); + } + } From 1611417340efe5d29f07471745d2f50f2b21391f Mon Sep 17 00:00:00 2001 From: Kevin Cantu Date: Wed, 1 Feb 2012 03:32:15 -0800 Subject: [PATCH 03/11] Make it work 1 --- src/libcore/str.rs | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/libcore/str.rs b/src/libcore/str.rs index 02bd21199e5bf..e0ad5f575fce6 100644 --- a/src/libcore/str.rs +++ b/src/libcore/str.rs @@ -424,7 +424,7 @@ Failure: If `begin` + `len` is is greater than the byte length of the string */ -fn substr(s: str, begin: uint, len: uint) -> str { +fn substr(s: str, begin: uint, len: uint) -> str unsafe { ret unsafe::slice(s, begin, begin + len); } @@ -706,13 +706,14 @@ Returns: The original string with all occurances of `from` replaced with `to` */ -fn replace(s: str, from: str, to: str) : is_not_empty(from) -> str { +fn replace(s: str, from: str, to: str) : is_not_empty(from) -> str unsafe { // FIXME (694): Shouldn't have to check this check (is_not_empty(from)); if byte_len(s) == 0u { ret ""; } else if starts_with(s, from) { - ret to + replace(unsafe::slice(s, byte_len(from), byte_len(s)), from, to); + ret to + replace(unsafe::slice(s, byte_len(from), byte_len(s)), + from, to); } else { let idx = find(s, from); if idx == -1 { @@ -1401,7 +1402,8 @@ mod unsafe { FIXME: rename to safe_range_byte_slice */ - unsafe fn safe_slice(s: str, begin: uint, end: uint) : uint::le(begin, end) -> str { + unsafe fn safe_slice(s: str, begin: uint, end: uint) + : uint::le(begin, end) -> str { // would need some magic to make this a precondition assert (end <= byte_len(s)); ret slice(s, begin, end); @@ -1634,7 +1636,7 @@ mod tests { } #[test] - fn test_slice() { + fn test_unsafe_slice() unsafe { assert (eq("ab", slice("abc", 0u, 2u))); assert (eq("bc", slice("abc", 1u, 3u))); assert (eq("", slice("abc", 1u, 1u))); @@ -1651,7 +1653,7 @@ mod tests { ret rs; } assert (eq(half_a_million_letter_a(), - slice(a_million_letter_a(), 0u, 500000u))); + unsafe::slice(a_million_letter_a(), 0u, 500000u))); } #[test] From b0cd348b4054dccb8201a7efd83ff1eda4202e5f Mon Sep 17 00:00:00 2001 From: Kevin Cantu Date: Wed, 1 Feb 2012 03:36:07 -0800 Subject: [PATCH 04/11] Propagating unsafe::slice 1 --- src/cargo/cargo.rs | 12 +++++++----- src/fuzzer/fuzzer.rs | 4 ++-- src/libstd/json.rs | 6 +++--- 3 files changed, 12 insertions(+), 10 deletions(-) diff --git a/src/cargo/cargo.rs b/src/cargo/cargo.rs index fd39797b29fc0..87e8be7578731 100644 --- a/src/cargo/cargo.rs +++ b/src/cargo/cargo.rs @@ -573,7 +573,7 @@ fn install_named_specific(c: cargo, wd: str, src: str, name: str) { error("Can't find package " + src + "/" + name); } -fn cmd_install(c: cargo, argv: [str]) { +fn cmd_install(c: cargo, argv: [str]) unsafe { // cargo install if vec::len(argv) < 3u { cmd_usage(); @@ -596,8 +596,9 @@ fn cmd_install(c: cargo, argv: [str]) { let uuid = rest(target, 5u); let idx = str::index(uuid, '/' as u8); if idx != -1 { - let source = str::slice(uuid, 0u, idx as uint); - uuid = str::slice(uuid, idx as uint + 1u, str::byte_len(uuid)); + let source = str::unsafe::slice(uuid, 0u, idx as uint); + uuid = str::unsafe::slice(uuid, idx as uint + 1u, + str::byte_len(uuid)); install_uuid_specific(c, wd, source, uuid); } else { install_uuid(c, wd, uuid); @@ -606,8 +607,9 @@ fn cmd_install(c: cargo, argv: [str]) { let name = target; let idx = str::index(name, '/' as u8); if idx != -1 { - let source = str::slice(name, 0u, idx as uint); - name = str::slice(name, idx as uint + 1u, str::byte_len(name)); + let source = str::unsafe::slice(name, 0u, idx as uint); + name = str::unsafe::slice(name, idx as uint + 1u, + str::byte_len(name)); install_named_specific(c, wd, source, name); } else { install_named(c, wd, name); diff --git a/src/fuzzer/fuzzer.rs b/src/fuzzer/fuzzer.rs index 7d5156a95a005..a6b1c13943a08 100644 --- a/src/fuzzer/fuzzer.rs +++ b/src/fuzzer/fuzzer.rs @@ -283,10 +283,10 @@ fn check_variants_T( } } -fn last_part(filename: str) -> str { +fn last_part(filename: str) -> str unsafe { let ix = str::rindex(filename, 47u8 /* '/' */); assert ix >= 0; - str::slice(filename, ix as uint + 1u, str::byte_len(filename) - 3u) + str::unsafe::slice(filename, ix as uint + 1u, str::byte_len(filename) - 3u) } enum happiness { passed, cleanly_rejected(str), known_bug(str), failed(str), } diff --git a/src/libstd/json.rs b/src/libstd/json.rs index 306bcf6e16a21..cf0c236afeb73 100644 --- a/src/libstd/json.rs +++ b/src/libstd/json.rs @@ -228,9 +228,9 @@ fn from_str_float(s: str) -> (option, str) { fn from_str_bool(s: str) -> (option, str) { if (str::starts_with(s, "true")) { - (some(boolean(true)), str::slice(s, 4u, str::byte_len(s))) + (some(boolean(true)), str::char_slice(s, 4u, str::char_len(s))) } else if (str::starts_with(s, "false")) { - (some(boolean(false)), str::slice(s, 5u, str::byte_len(s))) + (some(boolean(false)), str::char_slice(s, 5u, str::char_len(s))) } else { (none, s) } @@ -238,7 +238,7 @@ fn from_str_bool(s: str) -> (option, str) { fn from_str_null(s: str) -> (option, str) { if (str::starts_with(s, "null")) { - (some(null), str::slice(s, 4u, str::byte_len(s))) + (some(null), str::char_slice(s, 4u, str::char_len(s))) } else { (none, s) } From ebff23f22aa2999a9949d8db082cfb7160801cc5 Mon Sep 17 00:00:00 2001 From: Kevin Cantu Date: Wed, 1 Feb 2012 03:41:58 -0800 Subject: [PATCH 05/11] Propagating unsafe::slice 2 --- src/comp/back/link.rs | 4 ++-- src/comp/middle/debuginfo.rs | 4 ++-- src/comp/syntax/parse/lexer.rs | 8 ++++---- src/libstd/fs.rs | 4 ++-- src/libstd/getopts.rs | 8 ++++---- 5 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/comp/back/link.rs b/src/comp/back/link.rs index b7a16d7e863b8..5e9e86bc87350 100644 --- a/src/comp/back/link.rs +++ b/src/comp/back/link.rs @@ -565,7 +565,7 @@ fn mangle_internal_name_by_seq(ccx: @crate_ctxt, flav: str) -> str { fn link_binary(sess: session, obj_filename: str, out_filename: str, - lm: link_meta) { + lm: link_meta) unsafe { // Converts a library file name into a gcc -l argument fn unlib(config: @session::config, filename: str) -> str { let rmlib = fn@(filename: str) -> str { @@ -573,7 +573,7 @@ fn link_binary(sess: session, (config.os == session::os_linux || config.os == session::os_freebsd) && str::find(filename, "lib") == 0 { - ret str::slice(filename, 3u, + ret str::unsafe::slice(filename, 3u, str::byte_len(filename)); } else { ret filename; } }; diff --git a/src/comp/middle/debuginfo.rs b/src/comp/middle/debuginfo.rs index 177bbef06eac3..117c522f00537 100644 --- a/src/comp/middle/debuginfo.rs +++ b/src/comp/middle/debuginfo.rs @@ -157,7 +157,7 @@ fn cached_metadata(cache: metadata_cache, mdtag: int, } fn create_compile_unit(cx: @crate_ctxt, full_path: str) - -> @metadata { + -> @metadata unsafe { let cache = get_cache(cx); let tg = CompileUnitTag; alt cached_metadata::<@metadata>(cache, tg, @@ -168,7 +168,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::slice(full_path, str::byte_len(work_dir), + str::unsafe::slice(full_path, str::byte_len(work_dir), str::byte_len(full_path)) } else { full_path diff --git a/src/comp/syntax/parse/lexer.rs b/src/comp/syntax/parse/lexer.rs index 03fc401710619..d4e67e812d101 100644 --- a/src/comp/syntax/parse/lexer.rs +++ b/src/comp/syntax/parse/lexer.rs @@ -24,10 +24,10 @@ type reader = @{ impl reader for reader { fn is_eof() -> bool { self.curr == -1 as char } - fn get_str_from(start: uint) -> str { + 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::slice(*self.src, start - 1u, self.pos - 1u); + ret str::unsafe::slice(*self.src, start - 1u, self.pos - 1u); } fn next() -> char { if self.pos < self.len { @@ -579,11 +579,11 @@ fn all_whitespace(s: str, begin: uint, end: uint) -> bool { ret true; } -fn trim_whitespace_prefix_and_push_line(&lines: [str], s: str, col: uint) { +fn trim_whitespace_prefix_and_push_line(&lines: [str], s: str, col: uint) unsafe { let s1; if all_whitespace(s, 0u, col) { if col < str::byte_len(s) { - s1 = str::slice(s, col, str::byte_len(s)); + s1 = str::unsafe::slice(s, col, str::byte_len(s)); } else { s1 = ""; } } else { s1 = s; } log(debug, "pushing line: " + s1); diff --git a/src/libstd/fs.rs b/src/libstd/fs.rs index cbfca163c1838..26a9931a0cb09 100644 --- a/src/libstd/fs.rs +++ b/src/libstd/fs.rs @@ -63,7 +63,7 @@ path separators in the path then the returned path is identical to the provided path. If an empty path is provided or the path ends with a path separator then an empty path is returned. */ -fn basename(p: path) -> path { +fn basename(p: path) -> path unsafe { let i: int = str::rindex(p, os_fs::path_sep as u8); if i == -1 { i = str::rindex(p, os_fs::alt_path_sep as u8); @@ -71,7 +71,7 @@ fn basename(p: path) -> path { } let len = str::byte_len(p); if i + 1 as uint >= len { ret p; } - ret str::slice(p, i + 1 as uint, len); + ret str::unsafe::slice(p, i + 1 as uint, len); } diff --git a/src/libstd/getopts.rs b/src/libstd/getopts.rs index 585ea48725f94..5510dd3231a07 100644 --- a/src/libstd/getopts.rs +++ b/src/libstd/getopts.rs @@ -209,7 +209,7 @@ ok(match) - On success. Use functions such as , etc. to interrogate results. err(fail_) - On failure. Use to get an error message. */ -fn getopts(args: [str], opts: [opt]) -> result { +fn getopts(args: [str], opts: [opt]) -> result unsafe { let n_opts = vec::len::(opts); fn f(_x: uint) -> [optval] { ret []; } let vals = vec::init_fn_mut::<[optval]>(n_opts, f); @@ -229,14 +229,14 @@ fn getopts(args: [str], opts: [opt]) -> result { let names; let i_arg = option::none::; if cur[1] == '-' as u8 { - let tail = str::slice(cur, 2u, curlen); + let tail = str::unsafe::slice(cur, 2u, curlen); let eq = str::index(tail, '=' as u8); if eq == -1 { names = [long(tail)]; } else { - names = [long(str::slice(tail, 0u, eq as uint))]; + names = [long(str::unsafe::slice(tail, 0u, eq as uint))]; i_arg = - option::some::(str::slice(tail, + option::some::(str::unsafe::slice(tail, (eq as uint) + 1u, curlen - 2u)); } From 66b232a8d234f0f6adddf85f75f52187979899e1 Mon Sep 17 00:00:00 2001 From: Kevin Cantu Date: Wed, 1 Feb 2012 03:45:44 -0800 Subject: [PATCH 06/11] Propagating unsafe::slice 3 --- src/comp/back/link.rs | 4 ++-- src/comp/syntax/codemap.rs | 6 +++--- src/comp/syntax/parse/lexer.rs | 3 ++- src/compiletest/errors.rs | 6 +++--- src/compiletest/header.rs | 4 ++-- 5 files changed, 12 insertions(+), 11 deletions(-) diff --git a/src/comp/back/link.rs b/src/comp/back/link.rs index 5e9e86bc87350..dbd6219e29f63 100644 --- a/src/comp/back/link.rs +++ b/src/comp/back/link.rs @@ -565,9 +565,9 @@ fn mangle_internal_name_by_seq(ccx: @crate_ctxt, flav: str) -> str { fn link_binary(sess: session, obj_filename: str, out_filename: str, - lm: link_meta) unsafe { + lm: link_meta) { // Converts a library file name into a gcc -l argument - fn unlib(config: @session::config, filename: str) -> str { + fn unlib(config: @session::config, filename: str) -> str unsafe { let rmlib = fn@(filename: str) -> str { if config.os == session::os_macos || (config.os == session::os_linux || diff --git a/src/comp/syntax/codemap.rs b/src/comp/syntax/codemap.rs index 0b893376bdbfa..eab92ab6ae9ec 100644 --- a/src/comp/syntax/codemap.rs +++ b/src/comp/syntax/codemap.rs @@ -108,7 +108,7 @@ fn span_to_lines(sp: span, cm: codemap::codemap) -> @file_lines { ret @{name: lo.filename, lines: lines}; } -fn get_line(fm: filemap, line: int) -> str { +fn get_line(fm: filemap, line: int) -> str unsafe { let begin: uint = fm.lines[line].byte - fm.start_pos.byte; let end: uint; if line as uint < vec::len(fm.lines) - 1u { @@ -118,11 +118,11 @@ fn get_line(fm: filemap, line: int) -> str { // parsed. If we just slice the rest of the string, we'll print out // the remainder of the file, which is undesirable. end = str::byte_len(*fm.src); - let rest = str::slice(*fm.src, begin, end); + let rest = str::unsafe::slice(*fm.src, begin, end); let newline = str::index(rest, '\n' as u8); if newline != -1 { end = begin + (newline as uint); } } - ret str::slice(*fm.src, begin, end); + ret str::unsafe::slice(*fm.src, begin, end); } fn get_filemap(cm: codemap, filename: str) -> filemap { diff --git a/src/comp/syntax/parse/lexer.rs b/src/comp/syntax/parse/lexer.rs index d4e67e812d101..30dacd27d4eb3 100644 --- a/src/comp/syntax/parse/lexer.rs +++ b/src/comp/syntax/parse/lexer.rs @@ -579,7 +579,8 @@ fn all_whitespace(s: str, begin: uint, end: uint) -> bool { ret true; } -fn trim_whitespace_prefix_and_push_line(&lines: [str], s: str, col: uint) unsafe { +fn trim_whitespace_prefix_and_push_line(&lines: [str], + s: str, col: uint) unsafe { let s1; if all_whitespace(s, 0u, col) { if col < str::byte_len(s) { diff --git a/src/compiletest/errors.rs b/src/compiletest/errors.rs index e0c4bf6b654c7..014419a2aeb42 100644 --- a/src/compiletest/errors.rs +++ b/src/compiletest/errors.rs @@ -22,7 +22,7 @@ fn load_errors(testfile: str) -> [expected_error] { ret error_patterns; } -fn parse_expected(line_num: uint, line: str) -> [expected_error] { +fn parse_expected(line_num: uint, line: str) -> [expected_error] unsafe { let error_tag = "//!"; let idx0 = str::find(line, error_tag); if idx0 < 0 { ret []; } @@ -41,11 +41,11 @@ fn parse_expected(line_num: uint, line: str) -> [expected_error] { 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::slice(line, start_kind, idx)); + let kind = str::to_lower(str::unsafe::slice(line, start_kind, idx)); // Extract msg: while idx < len && line[idx] == (' ' as u8) { idx += 1u; } - let msg = str::slice(line, idx, len); + let msg = str::unsafe::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 ec881caebbcf1..0f6665282102f 100644 --- a/src/compiletest/header.rs +++ b/src/compiletest/header.rs @@ -104,12 +104,12 @@ fn parse_name_directive(line: str, directive: str) -> bool { } fn parse_name_value_directive(line: str, - directive: str) -> option { + directive: str) -> option unsafe { let keycolon = directive + ":"; if str::find(line, keycolon) >= 0 { let colon = str::find(line, keycolon) as uint; let value = - str::slice(line, colon + str::byte_len(keycolon), + str::unsafe::slice(line, colon + str::byte_len(keycolon), str::byte_len(line)); #debug("%s: %s", directive, value); option::some(value) From de64e1813de61f93169c5667917b6da6600764fd Mon Sep 17 00:00:00 2001 From: Kevin Cantu Date: Wed, 1 Feb 2012 03:53:09 -0800 Subject: [PATCH 07/11] Propagating unsafe::slice 4 --- src/test/compile-fail/fn-constraint.rs | 4 ++-- src/test/compile-fail/no-constraint-prop.rs | 4 ++-- src/test/run-fail/fn-constraint.rs | 4 ++-- src/test/run-pass/constraint-prop-expr-move.rs | 4 ++-- src/test/run-pass/constraint-prop-move.rs | 4 ++-- src/test/run-pass/constraint-prop-swap.rs | 4 ++-- src/test/run-pass/constraint-prop.rs | 4 ++-- src/test/run-pass/fn-constraint.rs | 4 ++-- 8 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src/test/compile-fail/fn-constraint.rs b/src/test/compile-fail/fn-constraint.rs index aadbecc40a7c1..fadec3f97304f 100644 --- a/src/test/compile-fail/fn-constraint.rs +++ b/src/test/compile-fail/fn-constraint.rs @@ -2,8 +2,8 @@ use std; import str::*; -fn main() { +fn main() unsafe { let a: uint = 4u; let b: uint = 1u; - log(error, safe_slice("kitties", a, b)); + log(error, str::unsafe::safe_slice("kitties", a, b)); } diff --git a/src/test/compile-fail/no-constraint-prop.rs b/src/test/compile-fail/no-constraint-prop.rs index 2a03395f1c161..78f3a3cf40c0d 100644 --- a/src/test/compile-fail/no-constraint-prop.rs +++ b/src/test/compile-fail/no-constraint-prop.rs @@ -3,7 +3,7 @@ use std; import str::*; import uint::*; -fn main() { +fn main() unsafe { let a: uint = 1u; let b: uint = 4u; let c: uint = 5u; @@ -16,5 +16,5 @@ fn main() { // the next statement, since it's not true in the // prestate. let d <- a; - log(debug, safe_slice("kitties", b, d)); + log(debug, str::unsafe::safe_slice("kitties", b, d)); } diff --git a/src/test/run-fail/fn-constraint.rs b/src/test/run-fail/fn-constraint.rs index 40e259a0446a0..9d9974abf7182 100644 --- a/src/test/run-fail/fn-constraint.rs +++ b/src/test/run-fail/fn-constraint.rs @@ -3,9 +3,9 @@ use std; import str::*; import uint::le; -fn main() { +fn main() unsafe { let a: uint = 4u; let b: uint = 1u; check (le(a, b)); - log(error, safe_slice("kitties", a, b)); + log(error, str::unsafe::safe_slice("kitties", a, b)); } diff --git a/src/test/run-pass/constraint-prop-expr-move.rs b/src/test/run-pass/constraint-prop-expr-move.rs index 7848d9b9bd539..105e84e9fdf1f 100644 --- a/src/test/run-pass/constraint-prop-expr-move.rs +++ b/src/test/run-pass/constraint-prop-expr-move.rs @@ -2,11 +2,11 @@ use std; import str::*; import uint::*; -fn main() { +fn main() unsafe { let a: uint = 1u; let b: uint = 4u; let c: uint = 17u; check (le(a, b)); c <- a; - log(debug, safe_slice("kitties", c, b)); + log(debug, str::unsafe::safe_slice("kitties", c, b)); } diff --git a/src/test/run-pass/constraint-prop-move.rs b/src/test/run-pass/constraint-prop-move.rs index 347ea13f0051d..0cc748e2c835c 100644 --- a/src/test/run-pass/constraint-prop-move.rs +++ b/src/test/run-pass/constraint-prop-move.rs @@ -2,10 +2,10 @@ use std; import str::*; import uint::*; -fn main() { +fn main() unsafe { let a: uint = 1u; let b: uint = 4u; check (le(a, b)); let c <- a; - log(debug, safe_slice("kitties", c, b)); + log(debug, str::unsafe::safe_slice("kitties", c, b)); } diff --git a/src/test/run-pass/constraint-prop-swap.rs b/src/test/run-pass/constraint-prop-swap.rs index 748044893ed5f..d7eb9fbf06647 100644 --- a/src/test/run-pass/constraint-prop-swap.rs +++ b/src/test/run-pass/constraint-prop-swap.rs @@ -2,10 +2,10 @@ use std; import str::*; import uint::*; -fn main() { +fn main() unsafe { let a: uint = 4u; let b: uint = 1u; check (le(b, a)); b <-> a; - log(debug, safe_slice("kitties", a, b)); + log(debug, str::unsafe::safe_slice("kitties", a, b)); } diff --git a/src/test/run-pass/constraint-prop.rs b/src/test/run-pass/constraint-prop.rs index f15c0f4121d77..53c9a3bd96508 100644 --- a/src/test/run-pass/constraint-prop.rs +++ b/src/test/run-pass/constraint-prop.rs @@ -2,10 +2,10 @@ use std; import str::*; import uint::*; -fn main() { +fn main() unsafe { let a: uint = 1u; let b: uint = 4u; check (le(a, b)); let c = b; - log(debug, safe_slice("kitties", a, c)); + log(debug, str::unsafe::safe_slice("kitties", a, c)); } diff --git a/src/test/run-pass/fn-constraint.rs b/src/test/run-pass/fn-constraint.rs index 68106f298617f..11383faed3f5b 100644 --- a/src/test/run-pass/fn-constraint.rs +++ b/src/test/run-pass/fn-constraint.rs @@ -2,9 +2,9 @@ use std; import str::*; import uint::*; -fn main() { +fn main() unsafe { let a: uint = 1u; let b: uint = 4u; check (le(a, b)); - log(debug, safe_slice("kitties", a, b)); + log(debug, str::unsafe::safe_slice("kitties", a, b)); } From e4e58f7aa6defdaee75fd65b5b6bf6289170d782 Mon Sep 17 00:00:00 2001 From: Kevin Cantu Date: Wed, 1 Feb 2012 04:02:14 -0800 Subject: [PATCH 08/11] Stop exporting str::slice and str::safe_slice (use unsafe instead) --- src/libcore/str.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libcore/str.rs b/src/libcore/str.rs index e0ad5f575fce6..da6f05bd8e53d 100644 --- a/src/libcore/str.rs +++ b/src/libcore/str.rs @@ -38,8 +38,8 @@ export chars, substr, char_slice, - slice, - safe_slice, + //slice, + //safe_slice, split, splitn, split_str, From 17751b1c43e6f9116dea775e632085b3679902cc Mon Sep 17 00:00:00 2001 From: Kevin Cantu Date: Wed, 1 Feb 2012 04:20:21 -0800 Subject: [PATCH 09/11] Rename (again) str::unsafe::slice -> str::unsafe::slice_bytes and str::unsafe::safe_slice -> str::unsafe::slice_bytes_safe_range --- src/cargo/cargo.rs | 8 +-- src/comp/back/link.rs | 2 +- src/comp/middle/debuginfo.rs | 2 +- src/comp/syntax/codemap.rs | 4 +- src/comp/syntax/parse/lexer.rs | 4 +- src/compiletest/errors.rs | 4 +- src/compiletest/header.rs | 2 +- src/fuzzer/fuzzer.rs | 2 +- src/libcore/str.rs | 72 ++++--------------- src/libstd/fs.rs | 2 +- src/libstd/getopts.rs | 7 +- src/test/compile-fail/fn-constraint.rs | 2 +- src/test/compile-fail/no-constraint-prop.rs | 2 +- src/test/run-fail/fn-constraint.rs | 2 +- .../run-pass/constraint-prop-expr-move.rs | 2 +- src/test/run-pass/constraint-prop-move.rs | 2 +- src/test/run-pass/constraint-prop-swap.rs | 2 +- src/test/run-pass/constraint-prop.rs | 2 +- src/test/run-pass/fn-constraint.rs | 2 +- 19 files changed, 39 insertions(+), 86 deletions(-) diff --git a/src/cargo/cargo.rs b/src/cargo/cargo.rs index 87e8be7578731..0aef3ff9e1338 100644 --- a/src/cargo/cargo.rs +++ b/src/cargo/cargo.rs @@ -596,8 +596,8 @@ fn cmd_install(c: cargo, argv: [str]) unsafe { let uuid = rest(target, 5u); let idx = str::index(uuid, '/' as u8); if idx != -1 { - let source = str::unsafe::slice(uuid, 0u, idx as uint); - uuid = str::unsafe::slice(uuid, idx as uint + 1u, + let source = str::unsafe::slice_bytes(uuid, 0u, idx as uint); + uuid = str::unsafe::slice_bytes(uuid, idx as uint + 1u, str::byte_len(uuid)); install_uuid_specific(c, wd, source, uuid); } else { @@ -607,8 +607,8 @@ fn cmd_install(c: cargo, argv: [str]) unsafe { let name = target; let idx = str::index(name, '/' as u8); if idx != -1 { - let source = str::unsafe::slice(name, 0u, idx as uint); - name = str::unsafe::slice(name, idx as uint + 1u, + let source = str::unsafe::slice_bytes(name, 0u, idx as uint); + name = str::unsafe::slice_bytes(name, idx as uint + 1u, str::byte_len(name)); install_named_specific(c, wd, source, name); } else { diff --git a/src/comp/back/link.rs b/src/comp/back/link.rs index dbd6219e29f63..f545b43e9ffbd 100644 --- a/src/comp/back/link.rs +++ b/src/comp/back/link.rs @@ -573,7 +573,7 @@ fn link_binary(sess: session, (config.os == session::os_linux || config.os == session::os_freebsd) && str::find(filename, "lib") == 0 { - ret str::unsafe::slice(filename, 3u, + ret str::unsafe::slice_bytes(filename, 3u, str::byte_len(filename)); } else { ret filename; } }; diff --git a/src/comp/middle/debuginfo.rs b/src/comp/middle/debuginfo.rs index 117c522f00537..cebcd245b2f99 100644 --- a/src/comp/middle/debuginfo.rs +++ b/src/comp/middle/debuginfo.rs @@ -168,7 +168,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(full_path, str::byte_len(work_dir), + str::unsafe::slice_bytes(full_path, str::byte_len(work_dir), str::byte_len(full_path)) } else { full_path diff --git a/src/comp/syntax/codemap.rs b/src/comp/syntax/codemap.rs index eab92ab6ae9ec..eb6d1fd1d9d95 100644 --- a/src/comp/syntax/codemap.rs +++ b/src/comp/syntax/codemap.rs @@ -118,11 +118,11 @@ fn get_line(fm: filemap, line: int) -> str unsafe { // parsed. If we just slice the rest of the string, we'll print out // the remainder of the file, which is undesirable. end = str::byte_len(*fm.src); - let rest = str::unsafe::slice(*fm.src, begin, end); + let rest = str::unsafe::slice_bytes(*fm.src, begin, end); let newline = str::index(rest, '\n' as u8); if newline != -1 { end = begin + (newline as uint); } } - ret str::unsafe::slice(*fm.src, begin, end); + ret str::unsafe::slice_bytes(*fm.src, begin, end); } fn get_filemap(cm: codemap, filename: str) -> filemap { diff --git a/src/comp/syntax/parse/lexer.rs b/src/comp/syntax/parse/lexer.rs index 30dacd27d4eb3..4d7ee27eb9d10 100644 --- a/src/comp/syntax/parse/lexer.rs +++ b/src/comp/syntax/parse/lexer.rs @@ -27,7 +27,7 @@ impl reader for reader { 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(*self.src, start - 1u, self.pos - 1u); + ret str::unsafe::slice_bytes(*self.src, start - 1u, self.pos - 1u); } fn next() -> char { if self.pos < self.len { @@ -584,7 +584,7 @@ fn trim_whitespace_prefix_and_push_line(&lines: [str], let s1; if all_whitespace(s, 0u, col) { if col < str::byte_len(s) { - s1 = str::unsafe::slice(s, col, str::byte_len(s)); + s1 = str::unsafe::slice_bytes(s, col, str::byte_len(s)); } else { s1 = ""; } } else { s1 = s; } log(debug, "pushing line: " + s1); diff --git a/src/compiletest/errors.rs b/src/compiletest/errors.rs index 014419a2aeb42..3703badf52793 100644 --- a/src/compiletest/errors.rs +++ b/src/compiletest/errors.rs @@ -41,11 +41,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(line, start_kind, idx)); + let kind = str::to_lower(str::unsafe::slice_bytes(line, start_kind, idx)); // Extract msg: while idx < len && line[idx] == (' ' as u8) { idx += 1u; } - let msg = str::unsafe::slice(line, idx, len); + let msg = str::unsafe::slice_bytes(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 0f6665282102f..e09861141c4af 100644 --- a/src/compiletest/header.rs +++ b/src/compiletest/header.rs @@ -109,7 +109,7 @@ fn parse_name_value_directive(line: str, if str::find(line, keycolon) >= 0 { let colon = str::find(line, keycolon) as uint; let value = - str::unsafe::slice(line, colon + str::byte_len(keycolon), + str::unsafe::slice_bytes(line, colon + str::byte_len(keycolon), str::byte_len(line)); #debug("%s: %s", directive, value); option::some(value) diff --git a/src/fuzzer/fuzzer.rs b/src/fuzzer/fuzzer.rs index a6b1c13943a08..a5cfb8db3f7cf 100644 --- a/src/fuzzer/fuzzer.rs +++ b/src/fuzzer/fuzzer.rs @@ -286,7 +286,7 @@ fn check_variants_T( fn last_part(filename: str) -> str unsafe { let ix = str::rindex(filename, 47u8 /* '/' */); assert ix >= 0; - str::unsafe::slice(filename, ix as uint + 1u, str::byte_len(filename) - 3u) + str::unsafe::slice_bytes(filename, ix as uint + 1u, str::byte_len(filename) - 3u) } enum happiness { passed, cleanly_rejected(str), known_bug(str), failed(str), } diff --git a/src/libcore/str.rs b/src/libcore/str.rs index da6f05bd8e53d..0fdd53790c882 100644 --- a/src/libcore/str.rs +++ b/src/libcore/str.rs @@ -38,8 +38,6 @@ export chars, substr, char_slice, - //slice, - //safe_slice, split, splitn, split_str, @@ -425,7 +423,7 @@ Failure: If `begin` + `len` is is greater than the byte length of the string */ fn substr(s: str, begin: uint, len: uint) -> str unsafe { - ret unsafe::slice(s, begin, begin + len); + ret unsafe::slice_bytes(s, begin, begin + len); } /* @@ -446,48 +444,6 @@ fn char_slice(s: str, begin: uint, end: uint) -> str { from_chars(vec::slice(chars(s), begin, end)) } -/* -Function: slice - -Takes a bytewise slice from a string. Returns the substring from -[`begin`..`end`). - -This function is not unicode-safe. - -Failure: - -- If begin is greater than end. -- If end is greater than the length of the string. - -FIXME: rename to slice_byte or slice_byte_unsafe -*/ -fn slice(s: str, begin: uint, end: uint) -> str unsafe { - // FIXME: Typestate precondition - assert (begin <= end); - assert (end <= byte_len(s)); - - let v: [u8] = ::unsafe::reinterpret_cast(s); - let v2 = vec::slice(v, begin, end); - ::unsafe::leak(v); - v2 += [0u8]; - let s2: str = ::unsafe::reinterpret_cast(v2); - ::unsafe::leak(v2); - ret s2; -} - -/* -Function: safe_slice - -FIXME: make sure char_slice / slice / byte_slice - have these preconditions and assertions -FIXME: this shouldn't be mistaken for a UTF-8 safe slice -*/ -fn safe_slice(s: str, begin: uint, end: uint) : uint::le(begin, end) -> str { - // would need some magic to make this a precondition - assert (end <= byte_len(s)); - ret slice(s, begin, end); -} - /* Function: split @@ -712,7 +668,7 @@ fn replace(s: str, from: str, to: str) : is_not_empty(from) -> str unsafe { if byte_len(s) == 0u { ret ""; } else if starts_with(s, from) { - ret to + replace(unsafe::slice(s, byte_len(from), byte_len(s)), + ret to + replace(unsafe::slice_bytes(s, byte_len(from), byte_len(s)), from, to); } else { let idx = find(s, from); @@ -1348,8 +1304,8 @@ mod unsafe { // UNSAFE from_bytes, from_byte, - slice, - safe_slice; + slice_bytes, + slice_bytes_safe_range; // Function: unsafe::from_bytes // @@ -1371,19 +1327,15 @@ mod unsafe { /* Function: slice - Takes a bytewise slice from a string. Returns the substring from - [`begin`..`end`). - - This function is not unicode-safe. + Takes a bytewise (not UTF-8) slice from a string. + Returns the substring from [`begin`..`end`). Failure: - If begin is greater than end. - If end is greater than the length of the string. - - FIXME: rename to byte_slice */ - unsafe fn slice(s: str, begin: uint, end: uint) -> str unsafe { + unsafe fn slice_bytes(s: str, begin: uint, end: uint) -> str unsafe { // FIXME: Typestate precondition assert (begin <= end); assert (end <= byte_len(s)); @@ -1398,15 +1350,15 @@ mod unsafe { } /* - Function: safe_slice + Function: slice_bytes_safe_range - FIXME: rename to safe_range_byte_slice + Like slice_bytes, with a precondition */ - unsafe fn safe_slice(s: str, begin: uint, end: uint) + unsafe fn slice_bytes_safe_range(s: str, begin: uint, end: uint) : uint::le(begin, end) -> str { // would need some magic to make this a precondition assert (end <= byte_len(s)); - ret slice(s, begin, end); + ret slice_bytes(s, begin, end); } } @@ -1653,7 +1605,7 @@ mod tests { ret rs; } assert (eq(half_a_million_letter_a(), - unsafe::slice(a_million_letter_a(), 0u, 500000u))); + unsafe::slice_bytes(a_million_letter_a(), 0u, 500000u))); } #[test] diff --git a/src/libstd/fs.rs b/src/libstd/fs.rs index 26a9931a0cb09..1e5c9657d1e0f 100644 --- a/src/libstd/fs.rs +++ b/src/libstd/fs.rs @@ -71,7 +71,7 @@ fn basename(p: path) -> path unsafe { } let len = str::byte_len(p); if i + 1 as uint >= len { ret p; } - ret str::unsafe::slice(p, i + 1 as uint, len); + ret str::unsafe::slice_bytes(p, i + 1 as uint, len); } diff --git a/src/libstd/getopts.rs b/src/libstd/getopts.rs index 5510dd3231a07..bc96cb65efa68 100644 --- a/src/libstd/getopts.rs +++ b/src/libstd/getopts.rs @@ -229,14 +229,15 @@ 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(cur, 2u, curlen); + let tail = str::unsafe::slice_bytes(cur, 2u, curlen); let eq = str::index(tail, '=' as u8); if eq == -1 { names = [long(tail)]; } else { - names = [long(str::unsafe::slice(tail, 0u, eq as uint))]; + names = + [long(str::unsafe::slice_bytes(tail,0u,eq as uint))]; i_arg = - option::some::(str::unsafe::slice(tail, + option::some::(str::unsafe::slice_bytes(tail, (eq as uint) + 1u, curlen - 2u)); } diff --git a/src/test/compile-fail/fn-constraint.rs b/src/test/compile-fail/fn-constraint.rs index fadec3f97304f..b8a4a164c7eec 100644 --- a/src/test/compile-fail/fn-constraint.rs +++ b/src/test/compile-fail/fn-constraint.rs @@ -5,5 +5,5 @@ import str::*; fn main() unsafe { let a: uint = 4u; let b: uint = 1u; - log(error, str::unsafe::safe_slice("kitties", a, b)); + log(error, str::unsafe::slice_bytes_safe_range("kitties", a, b)); } diff --git a/src/test/compile-fail/no-constraint-prop.rs b/src/test/compile-fail/no-constraint-prop.rs index 78f3a3cf40c0d..ae5a024863b7d 100644 --- a/src/test/compile-fail/no-constraint-prop.rs +++ b/src/test/compile-fail/no-constraint-prop.rs @@ -16,5 +16,5 @@ fn main() unsafe { // the next statement, since it's not true in the // prestate. let d <- a; - log(debug, str::unsafe::safe_slice("kitties", b, d)); + log(debug, str::unsafe::slice_bytes_safe_range("kitties", b, d)); } diff --git a/src/test/run-fail/fn-constraint.rs b/src/test/run-fail/fn-constraint.rs index 9d9974abf7182..5277b1bc6e131 100644 --- a/src/test/run-fail/fn-constraint.rs +++ b/src/test/run-fail/fn-constraint.rs @@ -7,5 +7,5 @@ fn main() unsafe { let a: uint = 4u; let b: uint = 1u; check (le(a, b)); - log(error, str::unsafe::safe_slice("kitties", a, b)); + log(error, str::unsafe::slice_bytes_safe_range("kitties", a, b)); } diff --git a/src/test/run-pass/constraint-prop-expr-move.rs b/src/test/run-pass/constraint-prop-expr-move.rs index 105e84e9fdf1f..1f8e6f69b2730 100644 --- a/src/test/run-pass/constraint-prop-expr-move.rs +++ b/src/test/run-pass/constraint-prop-expr-move.rs @@ -8,5 +8,5 @@ fn main() unsafe { let c: uint = 17u; check (le(a, b)); c <- a; - log(debug, str::unsafe::safe_slice("kitties", c, b)); + log(debug, str::unsafe::slice_bytes_safe_range("kitties", c, b)); } diff --git a/src/test/run-pass/constraint-prop-move.rs b/src/test/run-pass/constraint-prop-move.rs index 0cc748e2c835c..12124e8801f21 100644 --- a/src/test/run-pass/constraint-prop-move.rs +++ b/src/test/run-pass/constraint-prop-move.rs @@ -7,5 +7,5 @@ fn main() unsafe { let b: uint = 4u; check (le(a, b)); let c <- a; - log(debug, str::unsafe::safe_slice("kitties", c, b)); + log(debug, str::unsafe::slice_bytes_safe_range("kitties", c, b)); } diff --git a/src/test/run-pass/constraint-prop-swap.rs b/src/test/run-pass/constraint-prop-swap.rs index d7eb9fbf06647..6c9af246dbda6 100644 --- a/src/test/run-pass/constraint-prop-swap.rs +++ b/src/test/run-pass/constraint-prop-swap.rs @@ -7,5 +7,5 @@ fn main() unsafe { let b: uint = 1u; check (le(b, a)); b <-> a; - log(debug, str::unsafe::safe_slice("kitties", a, b)); + log(debug, str::unsafe::slice_bytes_safe_range("kitties", a, b)); } diff --git a/src/test/run-pass/constraint-prop.rs b/src/test/run-pass/constraint-prop.rs index 53c9a3bd96508..62cee45c9581e 100644 --- a/src/test/run-pass/constraint-prop.rs +++ b/src/test/run-pass/constraint-prop.rs @@ -7,5 +7,5 @@ fn main() unsafe { let b: uint = 4u; check (le(a, b)); let c = b; - log(debug, str::unsafe::safe_slice("kitties", a, c)); + log(debug, str::unsafe::slice_bytes_safe_range("kitties", a, c)); } diff --git a/src/test/run-pass/fn-constraint.rs b/src/test/run-pass/fn-constraint.rs index 11383faed3f5b..2d5e28f209ca4 100644 --- a/src/test/run-pass/fn-constraint.rs +++ b/src/test/run-pass/fn-constraint.rs @@ -6,5 +6,5 @@ fn main() unsafe { let a: uint = 1u; let b: uint = 4u; check (le(a, b)); - log(debug, str::unsafe::safe_slice("kitties", a, b)); + log(debug, str::unsafe::slice_bytes_safe_range("kitties", a, b)); } From 9a23f63e8844fa2df8f10325f1c5add237bcb2bd Mon Sep 17 00:00:00 2001 From: Kevin Cantu Date: Wed, 1 Feb 2012 04:30:19 -0800 Subject: [PATCH 10/11] Make the tests work, too --- src/libcore/str.rs | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/libcore/str.rs b/src/libcore/str.rs index 0fdd53790c882..5f54bb2deadee 100644 --- a/src/libcore/str.rs +++ b/src/libcore/str.rs @@ -841,6 +841,8 @@ Function: index Returns the index of the first matching byte. Returns -1 if no match is found. + +FIXME: UTF-8 */ fn index(s: str, c: u8) -> int { let i: int = 0; @@ -853,6 +855,8 @@ Function: rindex Returns the index of the last matching byte. Returns -1 if no match is found. + +FIXME: UTF-8 */ fn rindex(s: str, c: u8) -> int { let n: int = byte_len(s) as int; @@ -874,6 +878,8 @@ needle - The string to look for Returns: The index of the first occurance of `needle`, or -1 if not found. + +FIXME: UTF-8? */ fn find(haystack: str, needle: str) -> int { let haystack_len: int = byte_len(haystack) as int; @@ -1589,9 +1595,9 @@ mod tests { #[test] fn test_unsafe_slice() unsafe { - assert (eq("ab", slice("abc", 0u, 2u))); - assert (eq("bc", slice("abc", 1u, 3u))); - assert (eq("", slice("abc", 1u, 1u))); + assert (eq("ab", unsafe::slice_bytes("abc", 0u, 2u))); + assert (eq("bc", unsafe::slice_bytes("abc", 1u, 3u))); + assert (eq("", unsafe::slice_bytes("abc", 1u, 1u))); fn a_million_letter_a() -> str { let i = 0; let rs = ""; From b93a73f393a00d19e12d18680af8a665f6389085 Mon Sep 17 00:00:00 2001 From: Kevin Cantu Date: Wed, 1 Feb 2012 04:36:33 -0800 Subject: [PATCH 11/11] Rename str::char_slice -> str::slice --- src/cargo/cargo.rs | 2 +- src/libcore/str.rs | 34 +++++++++++++++++----------------- src/libstd/json.rs | 18 +++++++++--------- src/rustdoc/unindent_pass.rs | 2 +- 4 files changed, 28 insertions(+), 28 deletions(-) diff --git a/src/cargo/cargo.rs b/src/cargo/cargo.rs index 0aef3ff9e1338..0acd3e4f1c8e5 100644 --- a/src/cargo/cargo.rs +++ b/src/cargo/cargo.rs @@ -162,7 +162,7 @@ fn rest(s: str, start: uint) -> str { if (start >= str::char_len(s)) { "" } else { - str::char_slice(s, start, str::char_len(s)) + str::slice(s, start, str::char_len(s)) } } diff --git a/src/libcore/str.rs b/src/libcore/str.rs index 5f54bb2deadee..dd16e5887f229 100644 --- a/src/libcore/str.rs +++ b/src/libcore/str.rs @@ -37,7 +37,7 @@ export bytes, chars, substr, - char_slice, + slice, split, splitn, split_str, @@ -427,7 +427,7 @@ fn substr(s: str, begin: uint, len: uint) -> str unsafe { } /* -Function: char_slice +Function: slice Unicode-safe slice. Returns a slice of the given string containing the characters in the range [`begin`..`end`). `begin` and `end` are @@ -438,9 +438,9 @@ Failure: - If begin is greater than end - If end is greater than the character length of the string -FIXME: rename to slice(), make faster by avoiding char conversion +FIXME: make faster by avoiding char conversion */ -fn char_slice(s: str, begin: uint, end: uint) -> str { +fn slice(s: str, begin: uint, end: uint) -> str { from_chars(vec::slice(chars(s), begin, end)) } @@ -620,7 +620,7 @@ fn windowed(nn: uint, ss: str) -> [str] { let ii = 0u; while ii+nn <= len { - let w = char_slice( ss, ii, ii+nn ); + let w = slice( ss, ii, ii+nn ); vec::push(ww,w); ii += 1u; } @@ -675,8 +675,8 @@ fn replace(s: str, from: str, to: str) : is_not_empty(from) -> str unsafe { if idx == -1 { ret s; } - ret char_slice(s, 0u, idx as uint) + to + - replace(char_slice(s, idx as uint + char_len(from), char_len(s)), + ret slice(s, 0u, idx as uint) + to + + replace(slice(s, idx as uint + char_len(from), char_len(s)), from, to); } } @@ -1658,17 +1658,17 @@ mod tests { } #[test] - fn test_char_slice() { - assert (eq("ab", char_slice("abc", 0u, 2u))); - assert (eq("bc", char_slice("abc", 1u, 3u))); - assert (eq("", char_slice("abc", 1u, 1u))); - assert (eq("\u65e5", char_slice("\u65e5\u672c", 0u, 1u))); + fn test_slice() { + assert (eq("ab", slice("abc", 0u, 2u))); + assert (eq("bc", slice("abc", 1u, 3u))); + assert (eq("", slice("abc", 1u, 1u))); + assert (eq("\u65e5", slice("\u65e5\u672c", 0u, 1u))); let data = "ประเทศไทย中华"; - assert (eq("ป", char_slice(data, 0u, 1u))); - assert (eq("ร", char_slice(data, 1u, 2u))); - assert (eq("华", char_slice(data, 10u, 11u))); - assert (eq("", char_slice(data, 1u, 1u))); + assert (eq("ป", slice(data, 0u, 1u))); + assert (eq("ร", slice(data, 1u, 2u))); + assert (eq("华", slice(data, 10u, 11u))); + assert (eq("", slice(data, 1u, 1u))); fn a_million_letter_X() -> str { let i = 0; @@ -1683,7 +1683,7 @@ mod tests { ret rs; } assert (eq(half_a_million_letter_X(), - char_slice(a_million_letter_X(), 0u, 500000u))); + slice(a_million_letter_X(), 0u, 500000u))); } #[test] diff --git a/src/libstd/json.rs b/src/libstd/json.rs index cf0c236afeb73..bb1d0fb64b6b2 100644 --- a/src/libstd/json.rs +++ b/src/libstd/json.rs @@ -71,7 +71,7 @@ fn to_str(j: json) -> str { fn rest(s: str) -> str { assert(str::char_len(s) >= 1u); - str::char_slice(s, 1u, str::char_len(s)) + str::slice(s, 1u, str::char_len(s)) } fn from_str_str(s: str) -> (option, str) { @@ -99,7 +99,7 @@ fn from_str_str(s: str) -> (option, str) { cont; } else if (c == '"') { ret (some(string(res)), - str::char_slice(s, pos, str::char_len(s))); + str::slice(s, pos, str::char_len(s))); } res = res + str::from_char(c); } @@ -200,12 +200,12 @@ fn from_str_float(s: str) -> (option, str) { } '.' { break; } _ { ret (some(num(neg * res)), - str::char_slice(s, opos, str::char_len(s))); } + str::slice(s, opos, str::char_len(s))); } } } if pos == len { - ret (some(num(neg * res)), str::char_slice(s, pos, str::char_len(s))); + ret (some(num(neg * res)), str::slice(s, pos, str::char_len(s))); } let dec = 1f; @@ -220,17 +220,17 @@ fn from_str_float(s: str) -> (option, str) { res += (((c as int) - ('0' as int)) as float) * dec; } _ { ret (some(num(neg * res)), - str::char_slice(s, opos, str::char_len(s))); } + str::slice(s, opos, str::char_len(s))); } } } - ret (some(num(neg * res)), str::char_slice(s, pos, str::char_len(s))); + ret (some(num(neg * res)), str::slice(s, pos, str::char_len(s))); } fn from_str_bool(s: str) -> (option, str) { if (str::starts_with(s, "true")) { - (some(boolean(true)), str::char_slice(s, 4u, str::char_len(s))) + (some(boolean(true)), str::slice(s, 4u, str::char_len(s))) } else if (str::starts_with(s, "false")) { - (some(boolean(false)), str::char_slice(s, 5u, str::char_len(s))) + (some(boolean(false)), str::slice(s, 5u, str::char_len(s))) } else { (none, s) } @@ -238,7 +238,7 @@ fn from_str_bool(s: str) -> (option, str) { fn from_str_null(s: str) -> (option, str) { if (str::starts_with(s, "null")) { - (some(null), str::char_slice(s, 4u, str::char_len(s))) + (some(null), str::slice(s, 4u, str::char_len(s))) } else { (none, s) } diff --git a/src/rustdoc/unindent_pass.rs b/src/rustdoc/unindent_pass.rs index 8d6aec7330039..9c43ce4486414 100644 --- a/src/rustdoc/unindent_pass.rs +++ b/src/rustdoc/unindent_pass.rs @@ -68,7 +68,7 @@ fn unindent(s: str) -> str { line } else { assert str::byte_len(line) >= min_indent; - str::char_slice(line, min_indent, str::char_len(line)) + str::slice(line, min_indent, str::char_len(line)) } }; str::connect(unindented, "\n")