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