@@ -371,6 +371,10 @@ test "fmt.parseInt" {
371
371
assert (%% parseInt (i32 , "-10" , 10 ) == -10 );
372
372
assert (%% parseInt (i32 , "+10" , 10 ) == 10 );
373
373
assert (if (parseInt (i32 , " 10" , 10 )) | _ | false else | err | err == error .InvalidChar );
374
+ assert (if (parseInt (i32 , "10 " , 10 )) | _ | false else | err | err == error .InvalidChar );
375
+ assert (if (parseInt (u32 , "-10" , 10 )) | _ | false else | err | err == error .InvalidChar );
376
+ assert (%% parseInt (u8 , "255" , 10 ) == 255 );
377
+ assert (if (parseInt (u8 , "256" , 10 )) | _ | false else | err | err == error .Overflow );
374
378
}
375
379
376
380
pub fn parseUnsigned (comptime T : type , buf : []const u8 , radix : u8 ) - > % T {
@@ -412,14 +416,16 @@ const BufPrintContext = struct {
412
416
remaining : []u8 ,
413
417
};
414
418
419
+ error BufferTooSmall ;
415
420
fn bufPrintWrite (context : & BufPrintContext , bytes : []const u8 ) - > % void {
421
+ if (context .remaining .len < bytes .len ) return error .BufferTooSmall ;
416
422
mem .copy (u8 , context .remaining , bytes );
417
423
context .remaining = context .remaining [bytes .len .. ];
418
424
}
419
425
420
- pub fn bufPrint (buf : []u8 , comptime fmt : []const u8 , args : ... ) - > []u8 {
426
+ pub fn bufPrint (buf : []u8 , comptime fmt : []const u8 , args : ... ) - > % []u8 {
421
427
var context = BufPrintContext { .remaining = buf , };
422
- %% format (& context , bufPrintWrite , fmt , args );
428
+ % return format (& context , bufPrintWrite , fmt , args );
423
429
return buf [0.. buf .len - context .remaining .len ];
424
430
}
425
431
@@ -475,31 +481,31 @@ test "fmt.format" {
475
481
{
476
482
var buf1 : [32 ]u8 = undefined ;
477
483
const value : ? i32 = 1234 ;
478
- const result = bufPrint (buf1 [0.. ], "nullable: {}\n " , value );
484
+ const result = %% bufPrint (buf1 [0.. ], "nullable: {}\n " , value );
479
485
assert (mem .eql (u8 , result , "nullable: 1234\n " ));
480
486
}
481
487
{
482
488
var buf1 : [32 ]u8 = undefined ;
483
489
const value : ? i32 = null ;
484
- const result = bufPrint (buf1 [0.. ], "nullable: {}\n " , value );
490
+ const result = %% bufPrint (buf1 [0.. ], "nullable: {}\n " , value );
485
491
assert (mem .eql (u8 , result , "nullable: null\n " ));
486
492
}
487
493
{
488
494
var buf1 : [32 ]u8 = undefined ;
489
495
const value : % i32 = 1234 ;
490
- const result = bufPrint (buf1 [0.. ], "error union: {}\n " , value );
496
+ const result = %% bufPrint (buf1 [0.. ], "error union: {}\n " , value );
491
497
assert (mem .eql (u8 , result , "error union: 1234\n " ));
492
498
}
493
499
{
494
500
var buf1 : [32 ]u8 = undefined ;
495
501
const value : % i32 = error .InvalidChar ;
496
- const result = bufPrint (buf1 [0.. ], "error union: {}\n " , value );
502
+ const result = %% bufPrint (buf1 [0.. ], "error union: {}\n " , value );
497
503
assert (mem .eql (u8 , result , "error union: error.InvalidChar\n " ));
498
504
}
499
505
{
500
506
var buf1 : [32 ]u8 = undefined ;
501
507
const value : u3 = 0b101 ;
502
- const result = bufPrint (buf1 [0.. ], "u3: {}\n " , value );
508
+ const result = %% bufPrint (buf1 [0.. ], "u3: {}\n " , value );
503
509
assert (mem .eql (u8 , result , "u3: 5\n " ));
504
510
}
505
511
@@ -509,28 +515,28 @@ test "fmt.format" {
509
515
{
510
516
var buf1 : [32 ]u8 = undefined ;
511
517
const value : f32 = 12.34 ;
512
- const result = bufPrint (buf1 [0.. ], "f32: {}\n " , value );
518
+ const result = %% bufPrint (buf1 [0.. ], "f32: {}\n " , value );
513
519
assert (mem .eql (u8 , result , "f32: 1.23400001e1\n " ));
514
520
}
515
521
{
516
522
var buf1 : [32 ]u8 = undefined ;
517
523
const value : f64 = -12.34e10 ;
518
- const result = bufPrint (buf1 [0.. ], "f64: {}\n " , value );
524
+ const result = %% bufPrint (buf1 [0.. ], "f64: {}\n " , value );
519
525
assert (mem .eql (u8 , result , "f64: -1.234e11\n " ));
520
526
}
521
527
{
522
528
var buf1 : [32 ]u8 = undefined ;
523
- const result = bufPrint (buf1 [0.. ], "f64: {}\n " , math .nan_f64 );
529
+ const result = %% bufPrint (buf1 [0.. ], "f64: {}\n " , math .nan_f64 );
524
530
assert (mem .eql (u8 , result , "f64: NaN\n " ));
525
531
}
526
532
{
527
533
var buf1 : [32 ]u8 = undefined ;
528
- const result = bufPrint (buf1 [0.. ], "f64: {}\n " , math .inf_f64 );
534
+ const result = %% bufPrint (buf1 [0.. ], "f64: {}\n " , math .inf_f64 );
529
535
assert (mem .eql (u8 , result , "f64: Infinity\n " ));
530
536
}
531
537
{
532
538
var buf1 : [32 ]u8 = undefined ;
533
- const result = bufPrint (buf1 [0.. ], "f64: {}\n " , - math .inf_f64 );
539
+ const result = %% bufPrint (buf1 [0.. ], "f64: {}\n " , - math .inf_f64 );
534
540
assert (mem .eql (u8 , result , "f64: -Infinity\n " ));
535
541
}
536
542
}
0 commit comments