Skip to content

Commit 2083c47

Browse files
committed
---
yaml --- r: 16242 b: refs/heads/try c: 6e00852 h: refs/heads/master v: v3
1 parent fd4f596 commit 2083c47

File tree

3 files changed

+46
-27
lines changed

3 files changed

+46
-27
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
refs/heads/master: 61b1875c16de39c166b0f4d54bba19f9c6777d1a
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
44
refs/heads/snap-stage3: 4a81779abd786ff22d71434c6d9a5917ea4cdfff
5-
refs/heads/try: 70353cdbc4492ee05f2a53cb48d380b10b81fa71
5+
refs/heads/try: 6e0085210c54150f794d20791b2e9c1fda6049fc
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105

branches/try/src/libcore/uint-template.rs

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -100,37 +100,40 @@ fn to_str(num: T, radix: uint) -> str {
100100
assert (0u < radix && radix <= 16u);
101101
let mut n = num;
102102
let radix = radix as T;
103-
fn digit(n: T) -> char {
104-
ret alt n {
105-
0u as T { '0' }
106-
1u as T { '1' }
107-
2u as T { '2' }
108-
3u as T { '3' }
109-
4u as T { '4' }
110-
5u as T { '5' }
111-
6u as T { '6' }
112-
7u as T { '7' }
113-
8u as T { '8' }
114-
9u as T { '9' }
115-
10u as T { 'a' }
116-
11u as T { 'b' }
117-
12u as T { 'c' }
118-
13u as T { 'd' }
119-
14u as T { 'e' }
120-
15u as T { 'f' }
121-
_ { fail }
122-
};
103+
fn digit(n: T) -> u8 {
104+
if n <= 9u as T {
105+
n as u8 + '0' as u8
106+
} else if n <= 15u as T {
107+
(n - 10 as T) as u8 + 'a' as u8
108+
} else {
109+
fail;
110+
}
123111
}
124112
if n == 0u as T { ret "0"; }
125-
let mut s: str = "";
113+
114+
let mut buf: [mut u8] = [mut];
115+
vec::reserve(buf, 20u); // Enough room to hold any number
116+
126117
while n != 0u as T {
127-
s += str::from_byte(digit(n % radix) as u8);
118+
buf += [digit(n % radix)];
128119
n /= radix;
129120
}
130-
let mut s1: str = "";
131-
let mut len: uint = str::len(s);
132-
while len != 0u { len -= 1u; s1 += str::from_byte(s[len]); }
133-
ret s1;
121+
122+
buf += [0u8];
123+
124+
let mut start_idx = 0u;
125+
let mut end_idx = buf.len() - 2u;
126+
while start_idx < end_idx {
127+
vec::swap(buf, start_idx, end_idx);
128+
start_idx += 1u;
129+
end_idx -= 1u;
130+
}
131+
132+
unsafe {
133+
let s = unsafe::reinterpret_cast(buf);
134+
unsafe::forget(buf);
135+
ret s;
136+
}
134137
}
135138

136139
#[doc = "Convert to a string"]
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
fn main(args: [str]) {
2+
let args = if os::getenv("RUST_BENCH").is_some() {
3+
["", "10000000"]
4+
} else if args.len() <= 1u {
5+
["", "100000"]
6+
} else {
7+
args
8+
};
9+
10+
let n = uint::from_str(args[1]).get();
11+
12+
for uint::range(0u, n) {|i|
13+
let x = uint::to_str(i, 10u);
14+
log(debug, x);
15+
}
16+
}

0 commit comments

Comments
 (0)