@@ -103,7 +103,9 @@ pub mod ct {
103
103
use vec;
104
104
105
105
pub enum Signedness { Signed , Unsigned , }
106
+
106
107
pub enum Caseness { CaseUpper , CaseLower , }
108
+
107
109
pub enum Ty {
108
110
TyBool ,
109
111
TyStr ,
@@ -115,13 +117,15 @@ pub mod ct {
115
117
TyFloat ,
116
118
TyPoly ,
117
119
}
120
+
118
121
pub enum Flag {
119
122
FlagLeftJustify ,
120
123
FlagLeftZeroPad ,
121
124
FlagSpaceForSign ,
122
125
FlagSignAlways ,
123
126
FlagAlternate ,
124
127
}
128
+
125
129
pub enum Count {
126
130
CountIs ( uint ) ,
127
131
CountIsParam ( uint ) ,
@@ -136,22 +140,23 @@ pub mod ct {
136
140
137
141
impl < T > Parsed < T > {
138
142
static pure fn new( val: T , next : uint ) -> Parsed < T > {
139
- Parsed { val : val , next : next }
143
+ Parsed { val : val , next : next }
140
144
}
141
145
}
142
146
143
147
// A formatted conversion from an expression to a string
144
- pub struct Conv
145
- { param : Option < uint > ,
146
- flags : ~[ Flag ] ,
147
- width : Count ,
148
- precision : Count ,
149
- ty : Ty }
150
-
148
+ pub struct Conv {
149
+ param : Option < uint > ,
150
+ flags : ~[ Flag ] ,
151
+ width : Count ,
152
+ precision : Count ,
153
+ ty : Ty
154
+ }
151
155
152
156
// A fragment of the output sequence
153
157
pub enum Piece { PieceString ( ~str ) , PieceConv ( Conv ) , }
154
- pub type ErrorFn = fn @( & str ) -> ! ;
158
+
159
+ pub type ErrorFn = @fn ( & str ) -> !;
155
160
156
161
pub fn parse_fmt_string ( s : & str , err : ErrorFn ) -> ~[ Piece ] {
157
162
fn push_slice ( ps : & mut ~[ Piece ] , s : & str , from : uint , to : uint ) {
@@ -190,57 +195,61 @@ pub mod ct {
190
195
push_slice ( & mut pieces, s, h, i) ;
191
196
pieces
192
197
}
193
- pub fn peek_num ( s : & str , i : uint , lim : uint ) ->
194
- Option < Parsed < uint > > {
195
- let mut j = i;
196
- let mut accum = 0 u ;
198
+
199
+ pub fn peek_num ( s : & str , i : uint , lim : uint ) -> Option < Parsed < uint > > {
200
+ let mut i = i;
201
+ let mut accum = 0 ;
197
202
let mut found = false ;
198
- while j < lim {
199
- match char:: to_digit ( s[ j] as char , 10 ) {
203
+
204
+ while i < lim {
205
+ match char:: to_digit ( s[ i] as char , 10 ) {
200
206
Some ( x) => {
201
207
found = true ;
202
208
accum *= 10 ;
203
209
accum += x;
204
- j += 1 ;
205
- } ,
210
+ i += 1 ;
211
+ }
206
212
None => break
207
213
}
208
214
}
215
+
209
216
if found {
210
- Some ( Parsed :: new ( accum, j ) )
217
+ Some ( Parsed :: new ( accum, i ) )
211
218
} else {
212
219
None
213
220
}
214
221
}
215
- pub fn parse_conversion ( s : & str , i : uint , lim : uint ,
216
- err : ErrorFn ) ->
217
- Parsed < Piece > {
218
- let parm = parse_parameter ( s, i, lim) ;
222
+
223
+ pub fn parse_conversion ( s : & str , i : uint , lim : uint , err : ErrorFn ) ->
224
+ Parsed < Piece > {
225
+ let param = parse_parameter ( s, i, lim) ;
219
226
// avoid copying ~[Flag] by destructuring
220
227
let Parsed { val : flags_val, next : flags_next} = parse_flags ( s,
221
- parm . next , lim) ;
228
+ param . next , lim) ;
222
229
let width = parse_count ( s, flags_next, lim) ;
223
230
let prec = parse_precision ( s, width. next , lim) ;
224
231
let ty = parse_type ( s, prec. next , lim, err) ;
225
- Parsed :: new (
226
- PieceConv ( Conv { param : parm . val ,
227
- flags : flags_val ,
228
- width : width . val ,
229
- precision : prec . val ,
230
- ty : ty . val } ) ,
231
- ty. next )
232
+
233
+ Parsed :: new ( PieceConv ( Conv {
234
+ param : param . val ,
235
+ flags : flags_val ,
236
+ width : width . val ,
237
+ precision : prec . val ,
238
+ ty : ty . val } ) , ty. next )
232
239
}
240
+
233
241
pub fn parse_parameter ( s : & str , i : uint , lim : uint ) ->
234
- Parsed < Option < uint > > {
242
+ Parsed < Option < uint > > {
235
243
if i >= lim { return Parsed :: new ( None , i) ; }
244
+
236
245
match peek_num ( s, i, lim) {
237
246
Some ( num) if num. next < lim && s[ num. next ] == '$' as u8 =>
238
247
Parsed :: new ( Some ( num. val ) , num. next + 1 ) ,
239
248
_ => Parsed :: new ( None , i)
240
249
}
241
250
}
242
- pub fn parse_flags ( s : & str , i : uint , lim : uint ) ->
243
- Parsed < ~[ Flag ] > {
251
+
252
+ pub fn parse_flags ( s : & str , i : uint , lim : uint ) -> Parsed < ~[ Flag ] > {
244
253
let mut i = i;
245
254
let mut flags = ~[ ] ;
246
255
@@ -260,44 +269,45 @@ pub mod ct {
260
269
261
270
Parsed :: new ( flags, i)
262
271
}
263
- pub fn parse_count ( s : & str , i : uint , lim : uint )
264
- -> Parsed < Count > {
265
- if i >= lim {
266
- Parsed :: new ( CountImplied , i)
267
- } else if s[ i] == '*' as u8 {
268
- let param = parse_parameter ( s, i + 1 , lim) ;
269
- let j = param. next ;
270
- match param. val {
271
- None => Parsed :: new ( CountIsNextParam , j) ,
272
- Some ( n) => Parsed :: new ( CountIsParam ( n) , j)
273
- }
274
- } else {
275
- match peek_num ( s, i, lim) {
276
- None => Parsed :: new ( CountImplied , i) ,
277
- Some ( num) => Parsed :: new (
278
- CountIs ( num. val ) ,
279
- num. next
280
- )
281
- }
272
+
273
+ pub fn parse_count ( s : & str , i : uint , lim : uint ) -> Parsed < Count > {
274
+ if i >= lim {
275
+ Parsed :: new ( CountImplied , i)
276
+ } else if s[ i] == '*' as u8 {
277
+ let param = parse_parameter ( s, i + 1 , lim) ;
278
+ let j = param. next ;
279
+
280
+ match param. val {
281
+ None => Parsed :: new ( CountIsNextParam , j) ,
282
+ Some ( n) => Parsed :: new ( CountIsParam ( n) , j)
283
+ }
284
+ } else {
285
+ match peek_num ( s, i, lim) {
286
+ None => Parsed :: new ( CountImplied , i) ,
287
+ Some ( num) => Parsed :: new ( CountIs ( num. val ) , num. next )
282
288
}
289
+ }
283
290
}
284
- pub fn parse_precision ( s : & str , i : uint , lim : uint ) ->
285
- Parsed < Count > {
286
- if i < lim && s[ i] == '.' as u8 {
287
- let count = parse_count ( s, i + 1 u, lim) ;
288
291
292
+ pub fn parse_precision ( s : & str , i : uint , lim : uint ) -> Parsed < Count > {
293
+ if i < lim && s[ i] == '.' as u8 {
294
+ let count = parse_count ( s, i + 1 , lim) ;
289
295
290
- // If there were no digits specified, i.e. the precision
291
- // was ".", then the precision is 0
292
- match count. val {
293
- CountImplied => Parsed :: new ( CountIs ( 0 ) , count. next ) ,
294
- _ => count
295
- }
296
- } else { Parsed :: new ( CountImplied , i) }
296
+ // If there were no digits specified, i.e. the precision
297
+ // was ".", then the precision is 0
298
+ match count. val {
299
+ CountImplied => Parsed :: new ( CountIs ( 0 ) , count. next ) ,
300
+ _ => count
301
+ }
302
+ } else {
303
+ Parsed :: new ( CountImplied , i)
304
+ }
297
305
}
306
+
298
307
pub fn parse_type( s : & str , i : uint , lim : uint , err : ErrorFn ) ->
299
- Parsed < Ty > {
308
+ Parsed < Ty > {
300
309
if i >= lim { err ( ~"missing type in conversion") ; }
310
+
301
311
// FIXME (#2249): Do we really want two signed types here?
302
312
// How important is it to be printf compatible?
303
313
let t = match s[ i] {
@@ -314,6 +324,7 @@ pub mod ct {
314
324
'?' as u8 => TyPoly ,
315
325
_ => err ( ~"unknown type in conversion: " + s.substr(i, 1))
316
326
};
327
+
317
328
Parsed::new(t, i + 1)
318
329
}
319
330
}
0 commit comments