@@ -81,14 +81,14 @@ mod ct {
81
81
82
82
// A fragment of the output sequence
83
83
enum Piece { PieceString ( ~str ) , PieceConv ( Conv ) , }
84
- type ErrorFn = fn @( ~ str ) -> ! ;
84
+ type ErrorFn = fn @( & str ) -> ! ;
85
85
86
- fn parse_fmt_string( s : ~ str , error : ErrorFn ) -> ~[ Piece ] {
86
+ fn parse_fmt_string( s : & str , error : ErrorFn ) -> ~[ Piece ] {
87
87
let mut pieces: ~[ Piece ] = ~[ ] ;
88
88
let lim = str:: len ( s) ;
89
89
let mut buf = ~"";
90
- fn flush_buf ( +buf : ~str , & pieces: ~[ Piece ] ) -> ~str {
91
- if str :: len ( buf ) > 0 {
90
+ fn flush_buf ( +buf : ~str , pieces : & mut ~[ Piece ] ) -> ~str {
91
+ if buf . len ( ) > 0 {
92
92
let piece = PieceString ( move buf) ;
93
93
pieces. push ( move piece) ;
94
94
}
@@ -108,17 +108,17 @@ mod ct {
108
108
buf += curr2;
109
109
i += 1;
110
110
} else {
111
- buf = flush_buf(move buf, pieces);
111
+ buf = flush_buf(move buf, &mut pieces);
112
112
let rs = parse_conversion(s, i, lim, error);
113
113
pieces.push(copy rs.piece);
114
114
i = rs.next;
115
115
}
116
116
} else { buf += curr; i += size; }
117
117
}
118
- flush_buf(move buf, pieces);
118
+ flush_buf(move buf, &mut pieces);
119
119
move pieces
120
120
}
121
- fn peek_num(s: ~ str, i: uint, lim: uint) ->
121
+ fn peek_num(s: & str, i: uint, lim: uint) ->
122
122
Option<{num: uint, next: uint}> {
123
123
let mut j = i;
124
124
let mut accum = 0u;
@@ -140,7 +140,7 @@ mod ct {
140
140
None
141
141
}
142
142
}
143
- fn parse_conversion(s: ~ str, i: uint, lim: uint, error: ErrorFn) ->
143
+ fn parse_conversion(s: & str, i: uint, lim: uint, error: ErrorFn) ->
144
144
{piece: Piece, next: uint} {
145
145
let parm = parse_parameter(s, i, lim);
146
146
let flags = parse_flags(s, parm.next, lim);
@@ -155,7 +155,7 @@ mod ct {
155
155
ty: ty.ty}),
156
156
next: ty.next};
157
157
}
158
- fn parse_parameter(s: ~ str, i: uint, lim: uint) ->
158
+ fn parse_parameter(s: & str, i: uint, lim: uint) ->
159
159
{param: Option<int>, next: uint} {
160
160
if i >= lim { return {param: None, next: i}; }
161
161
let num = peek_num(s, i, lim);
@@ -170,34 +170,35 @@ mod ct {
170
170
}
171
171
};
172
172
}
173
- fn parse_flags(s: ~ str, i: uint, lim: uint) ->
173
+ fn parse_flags(s: & str, i: uint, lim: uint) ->
174
174
{flags: ~[Flag], next: uint} {
175
175
let noflags: ~[Flag] = ~[];
176
176
if i >= lim { return {flags: move noflags, next: i}; }
177
177
178
- fn more_ (f: Flag, s: ~ str, i: uint, lim: uint) ->
178
+ fn more (f: Flag, s: & str, i: uint, lim: uint) ->
179
179
{flags: ~[Flag], next: uint} {
180
180
let next = parse_flags(s, i + 1u, lim);
181
181
let rest = copy next.flags;
182
182
let j = next.next;
183
183
let curr: ~[Flag] = ~[f];
184
184
return {flags: vec::append(move curr, rest), next: j};
185
185
}
186
- let more = |x, copy s| more_(x, copy s, i, lim);
186
+ // Unfortunate, but because s is borrowed, can't use a closure
187
+ // fn more(f: Flag, s: &str) { more_(f, s, i, lim); }
187
188
let f = s[i];
188
189
return if f == '-' as u8 {
189
- more(FlagLeftJustify)
190
+ more(FlagLeftJustify, s, i, lim )
190
191
} else if f == '0' as u8 {
191
- more(FlagLeftZeroPad)
192
+ more(FlagLeftZeroPad, s, i, lim )
192
193
} else if f == ' ' as u8 {
193
- more(FlagSpaceForSign)
194
+ more(FlagSpaceForSign, s, i, lim )
194
195
} else if f == '+' as u8 {
195
- more(FlagSignAlways)
196
+ more(FlagSignAlways, s, i, lim )
196
197
} else if f == '#' as u8 {
197
- more(FlagAlternate)
198
+ more(FlagAlternate, s, i, lim )
198
199
} else { {flags: move noflags, next: i} };
199
200
}
200
- fn parse_count(s: ~ str, i: uint, lim: uint)
201
+ fn parse_count(s: & str, i: uint, lim: uint)
201
202
-> {count: Count, next: uint} {
202
203
return if i >= lim {
203
204
{count: CountImplied, next: i}
@@ -219,7 +220,7 @@ mod ct {
219
220
}
220
221
};
221
222
}
222
- fn parse_precision(s: ~ str, i: uint, lim: uint) ->
223
+ fn parse_precision(s: & str, i: uint, lim: uint) ->
223
224
{count: Count, next: uint} {
224
225
return if i >= lim {
225
226
{count: CountImplied, next: i}
@@ -235,7 +236,7 @@ mod ct {
235
236
}
236
237
} else { { count: CountImplied , next: i} } ;
237
238
}
238
- fn parse_type ( s : ~ str , i : uint , lim : uint , error : ErrorFn ) ->
239
+ fn parse_type ( s : & str , i : uint , lim : uint , error : ErrorFn ) ->
239
240
{ ty: Ty , next: uint} {
240
241
if i >= lim { error ( ~"missing type in conversion") ; }
241
242
let tstr = str:: slice ( s, i, i+1 u) ;
@@ -269,10 +270,7 @@ mod ct {
269
270
}
270
271
}
271
272
272
- // Functions used by the fmt extension at runtime. For now there are a lot of
273
- // decisions made a runtime. If it proves worthwhile then some of these
274
- // conditions can be evaluated at compile-time. For now though it's cleaner to
275
- // implement it 0this way, I think.
273
+ // OLD CODE -- eventually remove
276
274
mod rt {
277
275
#[legacy_exports];
278
276
const flag_none : u32 = 0u32;
@@ -328,7 +326,7 @@ mod rt {
328
326
let mut unpadded = match cv. precision {
329
327
CountImplied => s. to_unique ( ) ,
330
328
CountIs ( max) => if max as uint < str:: char_len ( s ) {
331
- str:: substr ( s, 0 u , max as uint )
329
+ str:: substr ( s, 0 , max as uint )
332
330
} else {
333
331
s. to_unique ( )
334
332
}
@@ -338,7 +336,7 @@ mod rt {
338
336
pure fn conv_float ( cv : Conv , f : float ) -> ~str {
339
337
let ( to_str, digits) = match cv. precision {
340
338
CountIs ( c) => ( float:: to_str_exact, c as uint ) ,
341
- CountImplied => ( float:: to_str, 6 u )
339
+ CountImplied => ( float:: to_str, 6 )
342
340
} ;
343
341
let mut s = unsafe { to_str ( f, digits) } ;
344
342
if 0.0 <= f {
@@ -404,16 +402,17 @@ mod rt {
404
402
pure fn ne ( other : & PadMode ) -> bool { !self . eq ( other) }
405
403
}
406
404
407
- fn pad ( cv : Conv , & s: ~str , mode : PadMode ) -> ~str {
405
+ fn pad ( cv : Conv , +s : ~str , mode : PadMode ) -> ~str {
406
+ let mut s = move s; // sadtimes
408
407
let uwidth : uint = match cv. width {
409
- CountImplied => return copy s,
408
+ CountImplied => return s,
410
409
CountIs ( width) => {
411
410
// FIXME: width should probably be uint (see Issue #1996)
412
411
width as uint
413
412
}
414
413
} ;
415
414
let strlen = str:: char_len ( s) ;
416
- if uwidth <= strlen { return copy s; }
415
+ if uwidth <= strlen { return s; }
417
416
let mut padchar = ' ' ;
418
417
let diff = uwidth - strlen;
419
418
if have_flag ( cv. flags , flag_left_justify) {
@@ -444,7 +443,7 @@ mod rt {
444
443
// zeros. It may make sense to convert zero padding to a precision
445
444
// instead.
446
445
447
- if signed && zero_padding && str :: len ( s ) > 0 u {
446
+ if signed && zero_padding && s . len ( ) > 0 {
448
447
let head = str:: shift_char ( & mut s) ;
449
448
if head == '+' || head == '-' || head == ' ' {
450
449
let headstr = str:: from_chars ( vec:: from_elem ( 1 u, head) ) ;
@@ -461,7 +460,12 @@ mod rt {
461
460
}
462
461
}
463
462
464
- // XXX remove after snapshots
463
+ // NEW CODE
464
+
465
+ // Functions used by the fmt extension at runtime. For now there are a lot of
466
+ // decisions made a runtime. If it proves worthwhile then some of these
467
+ // conditions can be evaluated at compile-time. For now though it's cleaner to
468
+ // implement it 0this way, I think.
465
469
mod rt2 {
466
470
#[ legacy_exports] ;
467
471
const flag_none : u32 = 0u32 ;
@@ -477,7 +481,7 @@ mod rt2 {
477
481
type Conv = { flags : u32 , width : Count , precision : Count , ty: Ty } ;
478
482
479
483
pure fn conv_int ( cv : Conv , i : int ) -> ~str {
480
- let radix = 10 u ;
484
+ let radix = 10 ;
481
485
let prec = get_int_precision ( cv) ;
482
486
let mut s : ~str = int_to_str_prec ( i, radix, prec) ;
483
487
if 0 <= i {
@@ -511,7 +515,7 @@ mod rt2 {
511
515
let mut s = str:: from_char ( c) ;
512
516
return unsafe { pad ( cv, s, PadNozero ) } ;
513
517
}
514
- pure fn conv_str ( cv : Conv , s : & str ) -> ~str {
518
+ pure fn conv_str ( cv : Conv , + s : & str ) -> ~str {
515
519
// For strings, precision is the maximum characters
516
520
// displayed
517
521
let mut unpadded = match cv. precision {
@@ -539,8 +543,8 @@ mod rt2 {
539
543
}
540
544
return unsafe { pad(cv, s, PadFloat) };
541
545
}
542
- pure fn conv_poly<T>(cv: Conv, v: T) -> ~str {
543
- let s = sys::log_str(& v);
546
+ pure fn conv_poly<T>(cv: Conv, v: & T) -> ~str {
547
+ let s = sys::log_str(v);
544
548
return conv_str(cv, s);
545
549
}
546
550
@@ -593,16 +597,17 @@ mod rt2 {
593
597
pure fn ne ( other : & PadMode ) -> bool { !self . eq ( other) }
594
598
}
595
599
596
- fn pad ( cv : Conv , & s: ~str , mode : PadMode ) -> ~str {
600
+ fn pad ( cv : Conv , +s : ~str , mode : PadMode ) -> ~str {
601
+ let mut s = move s; // sadtimes
597
602
let uwidth : uint = match cv. width {
598
- CountImplied => return copy s,
603
+ CountImplied => return s,
599
604
CountIs ( width) => {
600
605
// FIXME: width should probably be uint (see Issue #1996)
601
606
width as uint
602
607
}
603
608
} ;
604
609
let strlen = str:: char_len ( s) ;
605
- if uwidth <= strlen { return copy s; }
610
+ if uwidth <= strlen { return s; }
606
611
let mut padchar = ' ' ;
607
612
let diff = uwidth - strlen;
608
613
if have_flag ( cv. flags , flag_left_justify) {
@@ -633,7 +638,7 @@ mod rt2 {
633
638
// zeros. It may make sense to convert zero padding to a precision
634
639
// instead.
635
640
636
- if signed && zero_padding && str :: len ( s ) > 0 u {
641
+ if signed && zero_padding && s . len ( ) > 0 {
637
642
let head = str:: shift_char ( & mut s) ;
638
643
if head == '+' || head == '-' || head == ' ' {
639
644
let headstr = str:: from_chars ( vec:: from_elem ( 1 u, head) ) ;
0 commit comments