Skip to content

Commit 53a9d5a

Browse files
committed
Start paring down std::str. Issue #855
1 parent cb55ef6 commit 53a9d5a

24 files changed

+398
-721
lines changed

src/lib/dbg.rs

+6
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@ fn debug_fn<T>(x: &T) { rustrt::debug_fn::<T>(x); }
4949
fn ptr_cast<T, U>(x: @T) -> @U { ret rustrt::debug_ptrcast::<T, U>(x); }
5050

5151
fn trap(s: str) { rustrt::debug_trap(s); }
52+
53+
fn refcount<T>(a: &@T) -> uint {
54+
let p: *uint = unsafe::reinterpret_cast(a);
55+
ret *p;
56+
}
57+
5258
// Local Variables:
5359
// mode: rust;
5460
// fill-column: 78;

src/lib/io.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ obj new_reader(rdr: buf_reader) {
8787

8888
}
8989
let b0 = c0 as u8;
90-
let w = str::utf8_char_width(b0);
90+
let w = istr::utf8_char_width(b0);
9191
assert (w > 0u);
9292
if w == 1u { ret b0 as char; }
9393
let val = 0u;

src/lib/istr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ concat, connect, to_upper, replace, char_slice, trim_left, trim_right, trim,
44
unshift_char, shift_char, pop_char, push_char, is_utf8, from_chars, to_chars,
55
char_len, char_at, bytes, is_ascii, shift_byte, pop_byte, unsafe_from_byte,
66
unsafe_from_bytes, from_char, char_range_at, str_from_cstr, sbuf,
7-
as_buf, push_byte;
7+
as_buf, push_byte, utf8_char_width, safe_slice;
88

99
export from_estr, to_estr, from_estrs, to_estrs;
1010

src/lib/str.rs

+46-47
Original file line numberDiff line numberDiff line change
@@ -2,57 +2,57 @@
22
import rustrt::sbuf;
33
import uint::le;
44
export sbuf;
5-
export rustrt;
5+
// export rustrt;
66
export eq;
77
export lteq;
8-
export hash;
9-
export is_utf8;
10-
export is_ascii;
11-
export alloc;
8+
// export hash;
9+
// export is_utf8;
10+
// export is_ascii;
11+
// export alloc;
1212
export byte_len;
1313
export buf;
14-
export bytes;
15-
export unsafe_from_byte;
16-
export str_from_cstr;
17-
export str_from_buf;
18-
export push_utf8_bytes;
14+
// export bytes;
15+
// export unsafe_from_byte;
16+
// export str_from_cstr;
17+
// export str_from_buf;
18+
// export push_utf8_bytes;
1919
export from_char;
20-
export from_chars;
21-
export utf8_char_width;
22-
export char_range_at;
20+
// export from_chars;
21+
// export utf8_char_width;
22+
// export char_range_at;
2323
export char_at;
2424
export char_len;
25-
export to_chars;
26-
export push_char;
27-
export pop_char;
28-
export shift_char;
29-
export unshift_char;
30-
export refcount;
31-
export index;
32-
export rindex;
25+
// export to_chars;
26+
// export push_char;
27+
// export pop_char;
28+
// export shift_char;
29+
// export unshift_char;
30+
// export refcount;
31+
// export index;
32+
// export rindex;
3333
export find;
34-
export starts_with;
35-
export ends_with;
34+
// export starts_with;
35+
// export ends_with;
3636
export substr;
37-
export slice;
38-
export shift_byte;
39-
export pop_byte;
40-
export push_byte;
41-
export unshift_byte;
42-
export split;
43-
export concat;
44-
export connect;
37+
// export slice;
38+
// export shift_byte;
39+
// export pop_byte;
40+
// export push_byte;
41+
// export unshift_byte;
42+
// export split;
43+
// export concat;
44+
// export connect;
4545
export to_upper;
46-
export safe_slice;
46+
// export safe_slice;
4747
export unsafe_from_bytes;
48-
export is_empty;
49-
export is_not_empty;
50-
export is_whitespace;
51-
export replace;
52-
export char_slice;
53-
export trim_left;
54-
export trim_right;
55-
export trim;
48+
// export is_empty;
49+
// export is_not_empty;
50+
// export is_whitespace;
51+
// export replace;
52+
// export char_slice;
53+
// export trim_left;
54+
// export trim_right;
55+
// export trim;
5656

5757
native "rust" mod rustrt {
5858
type sbuf;
@@ -339,7 +339,7 @@ fn index(s: str, c: u8) -> int {
339339
}
340340

341341
fn rindex(s: str, c: u8) -> int {
342-
let n: int = str::byte_len(s) as int;
342+
let n: int = byte_len(s) as int;
343343
while n >= 0 { if s[n] == c { ret n; } n -= 1; }
344344
ret n;
345345
}
@@ -390,15 +390,14 @@ fn slice(s: str, begin: uint, end: uint) -> str {
390390
// FIXME: Typestate precondition
391391

392392
assert (begin <= end);
393-
assert (end <= str::byte_len(s));
393+
assert (end <= byte_len(s));
394394
ret rustrt::str_slice(s, begin, end);
395395
}
396396

397397
fn safe_slice(s: str, begin: uint, end: uint) : le(begin, end) -> str {
398-
assert (end <=
399-
str::byte_len(s)); // would need some magic to
400-
// make this a precondition
401-
398+
// would need some magic to
399+
// make this a precondition
400+
assert (end <= byte_len(s));
402401

403402
ret rustrt::str_slice(s, begin, end);
404403
}
@@ -441,7 +440,7 @@ fn split(s: str, sep: u8) -> [str] {
441440
ends_with_sep = true;
442441
} else { accum += unsafe_from_byte(c); ends_with_sep = false; }
443442
}
444-
if str::byte_len(accum) != 0u || ends_with_sep { v += [accum]; }
443+
if byte_len(accum) != 0u || ends_with_sep { v += [accum]; }
445444
ret v;
446445
}
447446

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
// error-pattern: Unsatisfied precondition constraint (for example, le(a, b)
1+
// error-pattern:precondition constraint (for example, uint::le(a, b)
22
use std;
3-
import std::str::*;
3+
import std::istr::*;
44

55
fn main() {
66
let a: uint = 4u;
77
let b: uint = 1u;
8-
log_err safe_slice("kitties", a, b);
8+
log_err safe_slice(~"kitties", a, b);
99
}

src/test/compile-fail/no-constraint-prop.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// error-pattern:Unsatisfied precondition constraint (for example, le(b, d
22
use std;
3-
import std::str::*;
3+
import std::istr::*;
44
import std::uint::*;
55

66
fn main() {
@@ -16,5 +16,5 @@ fn main() {
1616
// the next statement, since it's not true in the
1717
// prestate.
1818
let d <- a;
19-
log safe_slice("kitties", b, d);
19+
log safe_slice(~"kitties", b, d);
2020
}

src/test/run-fail/fn-constraint.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
// error-pattern:Predicate le(a, b) failed
22
use std;
3-
import std::str::*;
3+
import std::istr::*;
44
import std::uint::le;
55

66
fn main() {
77
let a: uint = 4u;
88
let b: uint = 1u;
99
check (le(a, b));
10-
log_err safe_slice("kitties", a, b);
10+
log_err safe_slice(~"kitties", a, b);
1111
}

src/test/run-pass/alt-pattern-drop.rs

+9-8
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,17 @@
22

33
// -*- rust -*-
44
use std;
5-
import std::str;
65

6+
import std::dbg;
77

88
// FIXME: import std::dbg.const_refcount. Currently
99
// cross-crate const references don't work.
1010
const const_refcount: uint = 0x7bad_face_u;
1111

12-
tag t { make_t(str); clam; }
12+
tag t { make_t(@int); clam; }
1313

14-
fn foo(s: str) {
14+
fn foo(s: @int) {
15+
let count = dbg::refcount(s);
1516
let x: t = make_t(s); // ref up
1617

1718
alt x {
@@ -21,15 +22,15 @@ fn foo(s: str) {
2122
}
2223
_ { log "?"; fail; }
2324
}
24-
log str::refcount(s);
25-
assert (str::refcount(s) == const_refcount);
25+
log dbg::refcount(s);
26+
assert (dbg::refcount(s) == count + 1u);
2627
}
2728

2829
fn main() {
29-
let s: str = "hi"; // ref up
30+
let s: @int = @0; // ref up
3031

3132
foo(s); // ref up then down
3233

33-
log str::refcount(s);
34-
assert (str::refcount(s) == const_refcount);
34+
log dbg::refcount(s);
35+
assert (dbg::refcount(s) == 1u);
3536
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use std;
2-
import std::str::*;
2+
import std::istr::*;
33
import std::uint::*;
44

55
fn main() {
@@ -8,5 +8,5 @@ fn main() {
88
let c: uint = 17u;
99
check (le(a, b));
1010
c <- a;
11-
log safe_slice("kitties", c, b);
11+
log safe_slice(~"kitties", c, b);
1212
}
+2-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
use std;
2-
import std::str::*;
2+
import std::istr::*;
33
import std::uint::*;
44

55
fn main() {
66
let a: uint = 1u;
77
let b: uint = 4u;
88
check (le(a, b));
99
let c <- a;
10-
log safe_slice("kitties", c, b);
10+
log safe_slice(~"kitties", c, b);
1111
}
+2-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
use std;
2-
import std::str::*;
2+
import std::istr::*;
33
import std::uint::*;
44

55
fn main() {
66
let a: uint = 4u;
77
let b: uint = 1u;
88
check (le(b, a));
99
b <-> a;
10-
log safe_slice("kitties", a, b);
10+
log safe_slice(~"kitties", a, b);
1111
}

src/test/run-pass/constraint-prop.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
use std;
2-
import std::str::*;
2+
import std::istr::*;
33
import std::uint::*;
44

55
fn main() {
66
let a: uint = 1u;
77
let b: uint = 4u;
88
check (le(a, b));
99
let c = b;
10-
log safe_slice("kitties", a, c);
10+
log safe_slice(~"kitties", a, c);
1111
}

src/test/run-pass/fn-constraint.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
use std;
2-
import std::str::*;
2+
import std::istr::*;
33
import std::uint::*;
44

55
fn main() {
66
let a: uint = 1u;
77
let b: uint = 4u;
88
check (le(a, b));
9-
log safe_slice("kitties", a, b);
9+
log safe_slice(~"kitties", a, b);
1010
}

src/test/run-pass/hashmap-memory.rs

+14-13
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use std;
99
import option = std::option::t;
1010
import std::option::some;
1111
import std::option::none;
12-
import std::str;
1312
import std::istr;
1413
import std::vec;
1514
import std::map;
@@ -20,40 +19,42 @@ import std::comm::send;
2019
import std::comm::recv;
2120
import std::comm;
2221

23-
fn map(filename: str, emit: map_reduce::putter) { emit(filename, "1"); }
22+
fn map(filename: &istr, emit: map_reduce::putter) { emit(filename, ~"1"); }
2423

2524
mod map_reduce {
2625
export putter;
2726
export mapper;
2827
export map_reduce;
2928

30-
type putter = fn(str, str);
29+
type putter = fn(&istr, &istr);
3130

32-
type mapper = fn(str, putter);
31+
type mapper = fn(&istr, putter);
3332

3433
tag ctrl_proto { find_reducer([u8], chan<int>); mapper_done; }
3534

36-
fn start_mappers(ctrl: chan<ctrl_proto>, inputs: &[str]) {
37-
for i: str in inputs { task::spawn(bind map_task(ctrl, i)); }
35+
fn start_mappers(ctrl: chan<ctrl_proto>, inputs: &[istr]) {
36+
for i: istr in inputs {
37+
task::spawn(bind map_task(ctrl, i));
38+
}
3839
}
3940

40-
fn map_task(ctrl: chan<ctrl_proto>, input: str) {
41+
fn map_task(ctrl: chan<ctrl_proto>, input: -istr) {
4142

4243
let intermediates = map::new_str_hash();
4344

4445
fn emit(im: &map::hashmap<istr, int>, ctrl: chan<ctrl_proto>,
45-
key: str, val: str) {
46+
key: &istr, val: &istr) {
4647
let c;
47-
alt im.find(istr::from_estr(key)) {
48+
alt im.find(key) {
4849
some(_c) { c = _c }
4950
none. {
5051
let p = port();
5152
log_err "sending find_reducer";
52-
send(ctrl, find_reducer(str::bytes(key), chan(p)));
53+
send(ctrl, find_reducer(istr::bytes(key), chan(p)));
5354
log_err "receiving";
5455
c = recv(p);
5556
log_err c;
56-
im.insert(istr::from_estr(key), c);
57+
im.insert(key, c);
5758
}
5859
}
5960
}
@@ -62,7 +63,7 @@ mod map_reduce {
6263
send(ctrl, mapper_done);
6364
}
6465

65-
fn map_reduce(inputs: &[str]) {
66+
fn map_reduce(inputs: &[istr]) {
6667
let ctrl = port();
6768

6869
// This task becomes the master control task. It spawns others
@@ -93,5 +94,5 @@ mod map_reduce {
9394
}
9495

9596
fn main() {
96-
map_reduce::map_reduce(["../src/test/run-pass/hashmap-memory.rs"]);
97+
map_reduce::map_reduce([~"../src/test/run-pass/hashmap-memory.rs"]);
9798
}

0 commit comments

Comments
 (0)