@@ -24,7 +24,7 @@ use result::Result::{Ok, Err};
24
24
use result;
25
25
use slice:: SliceExt ;
26
26
use slice;
27
- use str:: { StrExt , Utf8Error } ;
27
+ use str:: { mod , StrExt , Utf8Error } ;
28
28
29
29
pub use self :: num:: radix;
30
30
pub use self :: num:: Radix ;
@@ -57,7 +57,7 @@ pub struct Error;
57
57
/// library. The `write!` macro accepts an instance of `io::Writer`, and the
58
58
/// `io::Writer` trait is favored over implementing this trait.
59
59
#[ experimental = "waiting for core and I/O reconciliation" ]
60
- pub trait FormatWriter {
60
+ pub trait Writer {
61
61
/// Writes a slice of bytes into this writer, returning whether the write
62
62
/// succeeded.
63
63
///
@@ -68,7 +68,7 @@ pub trait FormatWriter {
68
68
/// # Errors
69
69
///
70
70
/// This function will return an instance of `FormatError` on error.
71
- fn write ( & mut self , bytes : & [ u8 ] ) -> Result ;
71
+ fn write_str ( & mut self , s : & str ) -> Result ;
72
72
73
73
/// Glue for usage of the `write!` macro with implementers of this trait.
74
74
///
@@ -88,7 +88,7 @@ pub struct Formatter<'a> {
88
88
width : Option < uint > ,
89
89
precision : Option < uint > ,
90
90
91
- buf : & ' a mut ( FormatWriter +' a ) ,
91
+ buf : & ' a mut ( Writer +' a ) ,
92
92
curarg : slice:: Iter < ' a , Argument < ' a > > ,
93
93
args : & ' a [ Argument < ' a > ] ,
94
94
}
@@ -258,17 +258,6 @@ pub trait UpperExp for Sized? {
258
258
fn fmt ( & self , & mut Formatter ) -> Result ;
259
259
}
260
260
261
- static DEFAULT_ARGUMENT : rt:: Argument < ' static > = rt:: Argument {
262
- position : rt:: ArgumentNext ,
263
- format : rt:: FormatSpec {
264
- fill : ' ' ,
265
- align : rt:: AlignUnknown ,
266
- flags : 0 ,
267
- precision : rt:: CountImplied ,
268
- width : rt:: CountImplied ,
269
- }
270
- } ;
271
-
272
261
/// The `write` function takes an output stream, a precompiled format string,
273
262
/// and a list of arguments. The arguments will be formatted according to the
274
263
/// specified format string into the output stream provided.
@@ -279,7 +268,7 @@ static DEFAULT_ARGUMENT: rt::Argument<'static> = rt::Argument {
279
268
/// * args - the precompiled arguments generated by `format_args!`
280
269
#[ experimental = "libcore and I/O have yet to be reconciled, and this is an \
281
270
implementation detail which should not otherwise be exported"]
282
- pub fn write ( output : & mut FormatWriter , args : Arguments ) -> Result {
271
+ pub fn write ( output : & mut Writer , args : Arguments ) -> Result {
283
272
let mut formatter = Formatter {
284
273
flags : 0 ,
285
274
width : None ,
@@ -296,16 +285,16 @@ pub fn write(output: &mut FormatWriter, args: Arguments) -> Result {
296
285
match args. fmt {
297
286
None => {
298
287
// We can use default formatting parameters for all arguments.
299
- for _ in range ( 0 , args. args . len ( ) ) {
300
- try!( formatter. buf . write ( pieces . next ( ) . unwrap ( ) . as_bytes ( ) ) ) ;
301
- try!( formatter . run ( & DEFAULT_ARGUMENT ) ) ;
288
+ for ( arg , piece ) in args. args . iter ( ) . zip ( pieces . by_ref ( ) ) {
289
+ try!( formatter. buf . write_str ( * piece ) ) ;
290
+ try!( ( arg . formatter ) ( arg . value , & mut formatter ) ) ;
302
291
}
303
292
}
304
293
Some ( fmt) => {
305
294
// Every spec has a corresponding argument that is preceded by
306
295
// a string piece.
307
296
for ( arg, piece) in fmt. iter ( ) . zip ( pieces. by_ref ( ) ) {
308
- try!( formatter. buf . write ( piece. as_bytes ( ) ) ) ;
297
+ try!( formatter. buf . write_str ( * piece) ) ;
309
298
try!( formatter. run ( arg) ) ;
310
299
}
311
300
}
@@ -314,7 +303,7 @@ pub fn write(output: &mut FormatWriter, args: Arguments) -> Result {
314
303
// There can be only one trailing string piece left.
315
304
match pieces. next ( ) {
316
305
Some ( piece) => {
317
- try!( formatter. buf . write ( piece. as_bytes ( ) ) ) ;
306
+ try!( formatter. buf . write_str ( * piece) ) ;
318
307
}
319
308
None => { }
320
309
}
@@ -378,7 +367,7 @@ impl<'a> Formatter<'a> {
378
367
pub fn pad_integral ( & mut self ,
379
368
is_positive : bool ,
380
369
prefix : & str ,
381
- buf : & [ u8 ] )
370
+ buf : & str )
382
371
-> Result {
383
372
use char:: Char ;
384
373
use fmt:: rt:: { FlagAlternate , FlagSignPlus , FlagSignAwareZeroPad } ;
@@ -402,9 +391,10 @@ impl<'a> Formatter<'a> {
402
391
for c in sign. into_iter ( ) {
403
392
let mut b = [ 0 ; 4 ] ;
404
393
let n = c. encode_utf8 ( & mut b) . unwrap_or ( 0 ) ;
405
- try!( f. buf . write ( b[ ..n] ) ) ;
394
+ let b = unsafe { str:: from_utf8_unchecked ( b[ 0 ..n] ) } ;
395
+ try!( f. buf . write_str ( b) ) ;
406
396
}
407
- if prefixed { f. buf . write ( prefix. as_bytes ( ) ) }
397
+ if prefixed { f. buf . write_str ( prefix) }
408
398
else { Ok ( ( ) ) }
409
399
} ;
410
400
@@ -413,24 +403,26 @@ impl<'a> Formatter<'a> {
413
403
// If there's no minimum length requirements then we can just
414
404
// write the bytes.
415
405
None => {
416
- try!( write_prefix ( self ) ) ; self . buf . write ( buf)
406
+ try!( write_prefix ( self ) ) ; self . buf . write_str ( buf)
417
407
}
418
408
// Check if we're over the minimum width, if so then we can also
419
409
// just write the bytes.
420
410
Some ( min) if width >= min => {
421
- try!( write_prefix ( self ) ) ; self . buf . write ( buf)
411
+ try!( write_prefix ( self ) ) ; self . buf . write_str ( buf)
422
412
}
423
413
// The sign and prefix goes before the padding if the fill character
424
414
// is zero
425
415
Some ( min) if self . flags & ( 1 << ( FlagSignAwareZeroPad as uint ) ) != 0 => {
426
416
self . fill = '0' ;
427
417
try!( write_prefix ( self ) ) ;
428
- self . with_padding ( min - width, rt:: AlignRight , |f| f. buf . write ( buf) )
418
+ self . with_padding ( min - width, rt:: AlignRight , |f| {
419
+ f. buf . write_str ( buf)
420
+ } )
429
421
}
430
422
// Otherwise, the sign and prefix goes after the padding
431
423
Some ( min) => {
432
424
self . with_padding ( min - width, rt:: AlignRight , |f| {
433
- try!( write_prefix ( f) ) ; f. buf . write ( buf)
425
+ try!( write_prefix ( f) ) ; f. buf . write_str ( buf)
434
426
} )
435
427
}
436
428
}
@@ -451,7 +443,7 @@ impl<'a> Formatter<'a> {
451
443
pub fn pad ( & mut self , s : & str ) -> Result {
452
444
// Make sure there's a fast path up front
453
445
if self . width . is_none ( ) && self . precision . is_none ( ) {
454
- return self . buf . write ( s . as_bytes ( ) ) ;
446
+ return self . buf . write_str ( s ) ;
455
447
}
456
448
// The `precision` field can be interpreted as a `max-width` for the
457
449
// string being formatted
@@ -463,7 +455,7 @@ impl<'a> Formatter<'a> {
463
455
let char_len = s. char_len ( ) ;
464
456
if char_len >= max {
465
457
let nchars = :: cmp:: min ( max, char_len) ;
466
- return self . buf . write ( s. slice_chars ( 0 , nchars) . as_bytes ( ) ) ;
458
+ return self . buf . write_str ( s. slice_chars ( 0 , nchars) ) ;
467
459
}
468
460
}
469
461
None => { }
@@ -472,17 +464,17 @@ impl<'a> Formatter<'a> {
472
464
match self . width {
473
465
// If we're under the maximum length, and there's no minimum length
474
466
// requirements, then we can just emit the string
475
- None => self . buf . write ( s . as_bytes ( ) ) ,
467
+ None => self . buf . write_str ( s ) ,
476
468
// If we're under the maximum width, check if we're over the minimum
477
469
// width, if so it's as easy as just emitting the string.
478
470
Some ( width) if s. char_len ( ) >= width => {
479
- self . buf . write ( s . as_bytes ( ) )
471
+ self . buf . write_str ( s )
480
472
}
481
473
// If we're under both the maximum and the minimum width, then fill
482
474
// up the minimum width with the specified string + some alignment.
483
475
Some ( width) => {
484
476
self . with_padding ( width - s. char_len ( ) , rt:: AlignLeft , |me| {
485
- me. buf . write ( s . as_bytes ( ) )
477
+ me. buf . write_str ( s )
486
478
} )
487
479
}
488
480
}
@@ -507,15 +499,16 @@ impl<'a> Formatter<'a> {
507
499
508
500
let mut fill = [ 0u8 ; 4 ] ;
509
501
let len = self . fill . encode_utf8 ( & mut fill) . unwrap_or ( 0 ) ;
502
+ let fill = unsafe { str:: from_utf8_unchecked ( fill[ ..len] ) } ;
510
503
511
504
for _ in range ( 0 , pre_pad) {
512
- try!( self . buf . write ( fill[ ..len ] ) ) ;
505
+ try!( self . buf . write_str ( fill) ) ;
513
506
}
514
507
515
508
try!( f ( self ) ) ;
516
509
517
510
for _ in range ( 0 , post_pad) {
518
- try!( self . buf . write ( fill[ ..len ] ) ) ;
511
+ try!( self . buf . write_str ( fill) ) ;
519
512
}
520
513
521
514
Ok ( ( ) )
@@ -524,8 +517,8 @@ impl<'a> Formatter<'a> {
524
517
/// Writes some data to the underlying buffer contained within this
525
518
/// formatter.
526
519
#[ unstable = "reconciling core and I/O may alter this definition" ]
527
- pub fn write ( & mut self , data : & [ u8 ] ) -> Result {
528
- self . buf . write ( data)
520
+ pub fn write_str ( & mut self , data : & str ) -> Result {
521
+ self . buf . write_str ( data)
529
522
}
530
523
531
524
/// Writes some formatted information into this instance
@@ -616,7 +609,9 @@ impl Show for char {
616
609
impl < T > Pointer for * const T {
617
610
fn fmt ( & self , f : & mut Formatter ) -> Result {
618
611
f. flags |= 1 << ( rt:: FlagAlternate as uint ) ;
619
- LowerHex :: fmt ( & ( * self as uint ) , f)
612
+ let ret = LowerHex :: fmt ( & ( * self as uint ) , f) ;
613
+ f. flags &= !( 1 << ( rt:: FlagAlternate as uint ) ) ;
614
+ ret
620
615
}
621
616
}
622
617
0 commit comments