File tree Expand file tree Collapse file tree 3 files changed +46
-27
lines changed Expand file tree Collapse file tree 3 files changed +46
-27
lines changed Original file line number Diff line number Diff line change 2
2
refs/heads/master: 61b1875c16de39c166b0f4d54bba19f9c6777d1a
3
3
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
4
4
refs/heads/snap-stage3: 4a81779abd786ff22d71434c6d9a5917ea4cdfff
5
- refs/heads/try: 70353cdbc4492ee05f2a53cb48d380b10b81fa71
5
+ refs/heads/try: 6e0085210c54150f794d20791b2e9c1fda6049fc
6
6
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
Original file line number Diff line number Diff line change @@ -100,37 +100,40 @@ fn to_str(num: T, radix: uint) -> str {
100
100
assert ( 0 u < radix && radix <= 16 u) ;
101
101
let mut n = num;
102
102
let radix = radix as T ;
103
- fn digit ( n : T ) -> char {
104
- ret alt n {
105
- 0 u as T { '0' }
106
- 1 u as T { '1' }
107
- 2 u as T { '2' }
108
- 3 u as T { '3' }
109
- 4 u as T { '4' }
110
- 5 u as T { '5' }
111
- 6 u as T { '6' }
112
- 7 u as T { '7' }
113
- 8 u as T { '8' }
114
- 9 u as T { '9' }
115
- 10 u as T { 'a' }
116
- 11 u as T { 'b' }
117
- 12 u as T { 'c' }
118
- 13 u as T { 'd' }
119
- 14 u as T { 'e' }
120
- 15 u as T { 'f' }
121
- _ { fail }
122
- } ;
103
+ fn digit ( n : T ) -> u8 {
104
+ if n <= 9 u as T {
105
+ n as u8 + '0' as u8
106
+ } else if n <= 15 u as T {
107
+ ( n - 10 as T ) as u8 + 'a' as u8
108
+ } else {
109
+ fail;
110
+ }
123
111
}
124
112
if n == 0 u as T { ret "0" ; }
125
- let mut s: str = "" ;
113
+
114
+ let mut buf: [ mut u8] = [ mut] ;
115
+ vec:: reserve ( buf, 20 u) ; // Enough room to hold any number
116
+
126
117
while n != 0 u as T {
127
- s += str :: from_byte ( digit ( n % radix) as u8 ) ;
118
+ buf += [ digit ( n % radix) ] ;
128
119
n /= radix;
129
120
}
130
- let mut s1: str = "" ;
131
- let mut len: uint = str:: len ( s) ;
132
- while len != 0 u { len -= 1 u; s1 += str:: from_byte ( s[ len] ) ; }
133
- ret s1;
121
+
122
+ buf += [ 0u8 ] ;
123
+
124
+ let mut start_idx = 0 u;
125
+ let mut end_idx = buf. len ( ) - 2 u;
126
+ while start_idx < end_idx {
127
+ vec:: swap ( buf, start_idx, end_idx) ;
128
+ start_idx += 1 u;
129
+ end_idx -= 1 u;
130
+ }
131
+
132
+ unsafe {
133
+ let s = unsafe :: reinterpret_cast ( buf) ;
134
+ unsafe :: forget ( buf) ;
135
+ ret s;
136
+ }
134
137
}
135
138
136
139
#[ doc = "Convert to a string" ]
Original file line number Diff line number Diff line change
1
+ fn main ( args : [ str ] ) {
2
+ let args = if os:: getenv ( "RUST_BENCH" ) . is_some ( ) {
3
+ [ "" , "10000000" ]
4
+ } else if args. len ( ) <= 1 u {
5
+ [ "" , "100000" ]
6
+ } else {
7
+ args
8
+ } ;
9
+
10
+ let n = uint:: from_str ( args[ 1 ] ) . get ( ) ;
11
+
12
+ for uint:: range( 0 u, n) { |i|
13
+ let x = uint:: to_str( i, 10 u) ;
14
+ log ( debug, x) ;
15
+ }
16
+ }
You can’t perform that action at this time.
0 commit comments