@@ -154,39 +154,41 @@ pub mod ct {
154
154
pub type ErrorFn = fn @( & str ) -> ! ;
155
155
156
156
pub fn parse_fmt_string ( s : & str , err : ErrorFn ) -> ~[ Piece ] {
157
- let mut pieces: ~[ Piece ] = ~[ ] ;
158
- let lim = str:: len ( s) ;
159
- let mut buf = ~"";
160
- fn flush_buf ( buf : ~str , pieces : & mut ~[ Piece ] ) -> ~str {
161
- if buf. len ( ) > 0 {
162
- let piece = PieceString ( move buf) ;
163
- pieces. push ( move piece) ;
157
+ fn push_slice ( ps : & mut ~[ Piece ] , s : & str , from : uint , to : uint ) {
158
+ if to > from {
159
+ ps. push ( PieceString ( s. slice ( from, to) ) ) ;
164
160
}
165
- return ~"";
166
161
}
162
+
163
+ let lim = s. len ( ) ;
164
+ let mut h = 0 ;
167
165
let mut i = 0 ;
166
+ let mut pieces = ~[ ] ;
167
+
168
168
while i < lim {
169
- let size = str:: utf8_char_width ( s[ i] ) ;
170
- let curr = str:: slice ( s, i, i+size) ;
171
- if curr == ~"%" {
169
+ if s[ i] == '%' as u8 {
172
170
i += 1 ;
171
+
173
172
if i >= lim {
174
173
err ( ~"unterminated conversion at end of string") ;
175
- }
176
- let curr2 = str:: slice ( s, i, i+1 ) ;
177
- if curr2 == ~"%" {
178
- buf += curr2;
174
+ } else if s[ i] == '%' as u8 {
175
+ push_slice ( & mut pieces, s, h, i) ;
179
176
i += 1 ;
180
177
} else {
181
- buf = flush_buf(move buf, &mut pieces );
182
- let rs = parse_conversion(s, i, lim, err);
183
- pieces.push(copy rs. val);
184
- i = rs. next;
178
+ push_slice ( & mut pieces , s , h , i - 1 ) ;
179
+ let Parsed { val , next } = parse_conversion ( s, i, lim, err) ;
180
+ pieces. push ( val) ;
181
+ i = next;
185
182
}
186
- } else { buf += curr; i += size; }
183
+
184
+ h = i;
185
+ } else {
186
+ i += str:: utf8_char_width ( s[ i] ) ;
187
+ }
187
188
}
188
- flush_buf(move buf, &mut pieces);
189
- move pieces
189
+
190
+ push_slice ( & mut pieces, s, h, i) ;
191
+ pieces
190
192
}
191
193
pub fn peek_num ( s : & str , i : uint , lim : uint ) ->
192
194
Option < Parsed < uint > > {
0 commit comments