@@ -370,15 +370,15 @@ pub fn formatAddress(value: anytype, options: FormatOptions, writer: anytype) @T
370
370
.Pointer = > | info | {
371
371
try writer .writeAll (@typeName (info .child ) ++ "@" );
372
372
if (info .size == .Slice )
373
- try formatInt (@ptrToInt (value .ptr ), 16 , false , FormatOptions {}, writer )
373
+ try formatInt (@ptrToInt (value .ptr ), 16 , .lower , FormatOptions {}, writer )
374
374
else
375
- try formatInt (@ptrToInt (value ), 16 , false , FormatOptions {}, writer );
375
+ try formatInt (@ptrToInt (value ), 16 , .lower , FormatOptions {}, writer );
376
376
return ;
377
377
},
378
378
.Optional = > | info | {
379
379
if (@typeInfo (info .child ) == .Pointer ) {
380
380
try writer .writeAll (@typeName (info .child ) ++ "@" );
381
- try formatInt (@ptrToInt (value ), 16 , false , FormatOptions {}, writer );
381
+ try formatInt (@ptrToInt (value ), 16 , .lower , FormatOptions {}, writer );
382
382
return ;
383
383
}
384
384
},
@@ -651,7 +651,7 @@ pub fn formatIntValue(
651
651
writer : anytype ,
652
652
) ! void {
653
653
comptime var radix = 10 ;
654
- comptime var uppercase = false ;
654
+ comptime var case : Case = .lower ;
655
655
656
656
const int_value = if (@TypeOf (value ) == comptime_int ) blk : {
657
657
const Int = math .IntFittingRange (value , value );
@@ -660,7 +660,7 @@ pub fn formatIntValue(
660
660
661
661
if (fmt .len == 0 or comptime std .mem .eql (u8 , fmt , "d" )) {
662
662
radix = 10 ;
663
- uppercase = false ;
663
+ case = .lower ;
664
664
} else if (comptime std .mem .eql (u8 , fmt , "c" )) {
665
665
if (@typeInfo (@TypeOf (int_value )).Int .bits <= 8 ) {
666
666
return formatAsciiChar (@as (u8 , int_value ), options , writer );
@@ -675,21 +675,21 @@ pub fn formatIntValue(
675
675
}
676
676
} else if (comptime std .mem .eql (u8 , fmt , "b" )) {
677
677
radix = 2 ;
678
- uppercase = false ;
678
+ case = .lower ;
679
679
} else if (comptime std .mem .eql (u8 , fmt , "x" )) {
680
680
radix = 16 ;
681
- uppercase = false ;
681
+ case = .lower ;
682
682
} else if (comptime std .mem .eql (u8 , fmt , "X" )) {
683
683
radix = 16 ;
684
- uppercase = true ;
684
+ case = .upper ;
685
685
} else if (comptime std .mem .eql (u8 , fmt , "o" )) {
686
686
radix = 8 ;
687
- uppercase = false ;
687
+ case = .lower ;
688
688
} else {
689
689
@compileError ("Unsupported format string '" ++ fmt ++ "' for type '" ++ @typeName (@TypeOf (value )) ++ "'" );
690
690
}
691
691
692
- return formatInt (int_value , radix , uppercase , options , writer );
692
+ return formatInt (int_value , radix , case , options , writer );
693
693
}
694
694
695
695
fn formatFloatValue (
@@ -724,8 +724,10 @@ fn formatFloatValue(
724
724
return formatBuf (buf_stream .getWritten (), options , writer );
725
725
}
726
726
727
- fn formatSliceHexImpl (comptime uppercase : bool ) type {
728
- const charset = "0123456789" ++ if (uppercase ) "ABCDEF" else "abcdef" ;
727
+ pub const Case = enum { lower , upper };
728
+
729
+ fn formatSliceHexImpl (comptime case : Case ) type {
730
+ const charset = "0123456789" ++ if (case == .upper ) "ABCDEF" else "abcdef" ;
729
731
730
732
return struct {
731
733
pub fn f (
@@ -745,23 +747,23 @@ fn formatSliceHexImpl(comptime uppercase: bool) type {
745
747
};
746
748
}
747
749
748
- const formatSliceHexLower = formatSliceHexImpl (false ).f ;
749
- const formatSliceHexUpper = formatSliceHexImpl (true ).f ;
750
+ const formatSliceHexLower = formatSliceHexImpl (.lower ).f ;
751
+ const formatSliceHexUpper = formatSliceHexImpl (.upper ).f ;
750
752
751
753
/// Return a Formatter for a []const u8 where every byte is formatted as a pair
752
754
/// of lowercase hexadecimal digits.
753
755
pub fn fmtSliceHexLower (bytes : []const u8 ) std.fmt.Formatter (formatSliceHexLower ) {
754
756
return .{ .data = bytes };
755
757
}
756
758
757
- /// Return a Formatter for a []const u8 where every byte is formatted as a pair
759
+ /// Return a Formatter for a []const u8 where every byte is formatted as pair
758
760
/// of uppercase hexadecimal digits.
759
761
pub fn fmtSliceHexUpper (bytes : []const u8 ) std.fmt.Formatter (formatSliceHexUpper ) {
760
762
return .{ .data = bytes };
761
763
}
762
764
763
- fn formatSliceEscapeImpl (comptime uppercase : bool ) type {
764
- const charset = "0123456789" ++ if (uppercase ) "ABCDEF" else "abcdef" ;
765
+ fn formatSliceEscapeImpl (comptime case : Case ) type {
766
+ const charset = "0123456789" ++ if (case == .upper ) "ABCDEF" else "abcdef" ;
765
767
766
768
return struct {
767
769
pub fn f (
@@ -788,8 +790,8 @@ fn formatSliceEscapeImpl(comptime uppercase: bool) type {
788
790
};
789
791
}
790
792
791
- const formatSliceEscapeLower = formatSliceEscapeImpl (false ).f ;
792
- const formatSliceEscapeUpper = formatSliceEscapeImpl (true ).f ;
793
+ const formatSliceEscapeLower = formatSliceEscapeImpl (.lower ).f ;
794
+ const formatSliceEscapeUpper = formatSliceEscapeImpl (.upper ).f ;
793
795
794
796
/// Return a Formatter for a []const u8 where every non-printable ASCII
795
797
/// character is escaped as \xNN, where NN is the character in lowercase
@@ -1034,13 +1036,13 @@ pub fn formatFloatScientific(
1034
1036
if (exp > -10 and exp < 10 ) {
1035
1037
try writer .writeAll ("0" );
1036
1038
}
1037
- try formatInt (exp , 10 , false , FormatOptions { .width = 0 }, writer );
1039
+ try formatInt (exp , 10 , .lower , FormatOptions { .width = 0 }, writer );
1038
1040
} else {
1039
1041
try writer .writeAll ("-" );
1040
1042
if (exp > -10 and exp < 10 ) {
1041
1043
try writer .writeAll ("0" );
1042
1044
}
1043
- try formatInt (- exp , 10 , false , FormatOptions { .width = 0 }, writer );
1045
+ try formatInt (- exp , 10 , .lower , FormatOptions { .width = 0 }, writer );
1044
1046
}
1045
1047
}
1046
1048
@@ -1133,7 +1135,7 @@ pub fn formatFloatHexadecimal(
1133
1135
1134
1136
// +1 for the decimal part.
1135
1137
var buf : [1 + mantissa_digits ]u8 = undefined ;
1136
- const N = formatIntBuf (& buf , mantissa , 16 , false , .{ .fill = '0' , .width = 1 + mantissa_digits });
1138
+ const N = formatIntBuf (& buf , mantissa , 16 , .lower , .{ .fill = '0' , .width = 1 + mantissa_digits });
1137
1139
1138
1140
try writer .writeAll ("0x" );
1139
1141
try writer .writeByte (buf [0 ]);
@@ -1150,7 +1152,7 @@ pub fn formatFloatHexadecimal(
1150
1152
try writer .writeByteNTimes ('0' , precision - trimmed .len );
1151
1153
};
1152
1154
try writer .writeAll ("p" );
1153
- try formatInt (exponent - exponent_bias , 10 , false , .{}, writer );
1155
+ try formatInt (exponent - exponent_bias , 10 , .lower , .{}, writer );
1154
1156
}
1155
1157
1156
1158
/// Print a float of the format x.yyyyy where the number of y is specified by the precision argument.
@@ -1299,7 +1301,7 @@ pub fn formatFloatDecimal(
1299
1301
pub fn formatInt (
1300
1302
value : anytype ,
1301
1303
base : u8 ,
1302
- uppercase : bool ,
1304
+ case : Case ,
1303
1305
options : FormatOptions ,
1304
1306
writer : anytype ,
1305
1307
) ! void {
@@ -1326,7 +1328,7 @@ pub fn formatInt(
1326
1328
while (true ) {
1327
1329
const digit = a % base ;
1328
1330
index -= 1 ;
1329
- buf [index ] = digitToChar (@intCast (u8 , digit ), uppercase );
1331
+ buf [index ] = digitToChar (@intCast (u8 , digit ), case );
1330
1332
a /= base ;
1331
1333
if (a == 0 ) break ;
1332
1334
}
@@ -1348,9 +1350,9 @@ pub fn formatInt(
1348
1350
return formatBuf (buf [index .. ], options , writer );
1349
1351
}
1350
1352
1351
- pub fn formatIntBuf (out_buf : []u8 , value : anytype , base : u8 , uppercase : bool , options : FormatOptions ) usize {
1353
+ pub fn formatIntBuf (out_buf : []u8 , value : anytype , base : u8 , case : Case , options : FormatOptions ) usize {
1352
1354
var fbs = std .io .fixedBufferStream (out_buf );
1353
- formatInt (value , base , uppercase , options , fbs .writer ()) catch unreachable ;
1355
+ formatInt (value , base , case , options , fbs .writer ()) catch unreachable ;
1354
1356
return fbs .pos ;
1355
1357
}
1356
1358
@@ -1365,7 +1367,7 @@ fn formatDuration(ns: u64, comptime fmt: []const u8, options: std.fmt.FormatOpti
1365
1367
}) | unit | {
1366
1368
if (ns_remaining >= unit .ns ) {
1367
1369
const units = ns_remaining / unit .ns ;
1368
- try formatInt (units , 10 , false , .{}, writer );
1370
+ try formatInt (units , 10 , .lower , .{}, writer );
1369
1371
try writer .writeByte (unit .sep );
1370
1372
ns_remaining -= units * unit .ns ;
1371
1373
if (ns_remaining == 0 ) return ;
@@ -1379,12 +1381,12 @@ fn formatDuration(ns: u64, comptime fmt: []const u8, options: std.fmt.FormatOpti
1379
1381
}) | unit | {
1380
1382
const kunits = ns_remaining * 1000 / unit .ns ;
1381
1383
if (kunits >= 1000 ) {
1382
- try formatInt (kunits / 1000 , 10 , false , .{}, writer );
1384
+ try formatInt (kunits / 1000 , 10 , .lower , .{}, writer );
1383
1385
const frac = kunits % 1000 ;
1384
1386
if (frac > 0 ) {
1385
1387
// Write up to 3 decimal places
1386
1388
var buf = [_ ]u8 { '.' , 0 , 0 , 0 };
1387
- _ = formatIntBuf (buf [1.. ], frac , 10 , false , .{ .fill = '0' , .width = 3 });
1389
+ _ = formatIntBuf (buf [1.. ], frac , 10 , .lower , .{ .fill = '0' , .width = 3 });
1388
1390
var end : usize = 4 ;
1389
1391
while (end > 1 ) : (end -= 1 ) {
1390
1392
if (buf [end - 1 ] != '0' ) break ;
@@ -1396,7 +1398,7 @@ fn formatDuration(ns: u64, comptime fmt: []const u8, options: std.fmt.FormatOpti
1396
1398
}
1397
1399
}
1398
1400
1399
- try formatInt (ns_remaining , 10 , false , .{}, writer );
1401
+ try formatInt (ns_remaining , 10 , .lower , .{}, writer );
1400
1402
try writer .writeAll ("ns" );
1401
1403
return ;
1402
1404
}
@@ -1673,10 +1675,10 @@ pub fn charToDigit(c: u8, radix: u8) (error{InvalidCharacter}!u8) {
1673
1675
return value ;
1674
1676
}
1675
1677
1676
- pub fn digitToChar (digit : u8 , uppercase : bool ) u8 {
1678
+ pub fn digitToChar (digit : u8 , case : Case ) u8 {
1677
1679
return switch (digit ) {
1678
1680
0... 9 = > digit + '0' ,
1679
- 10... 35 = > digit + ((if (uppercase ) @as (u8 , 'A' ) else @as (u8 , 'a' )) - 10 ),
1681
+ 10... 35 = > digit + ((if (case == .upper ) @as (u8 , 'A' ) else @as (u8 , 'a' )) - 10 ),
1680
1682
else = > unreachable ,
1681
1683
};
1682
1684
}
@@ -1728,25 +1730,25 @@ test "bufPrintInt" {
1728
1730
var buffer : [100 ]u8 = undefined ;
1729
1731
const buf = buffer [0.. ];
1730
1732
1731
- try std .testing .expectEqualSlices (u8 , "-1" , bufPrintIntToSlice (buf , @as (i1 , -1 ), 10 , false , FormatOptions {}));
1733
+ try std .testing .expectEqualSlices (u8 , "-1" , bufPrintIntToSlice (buf , @as (i1 , -1 ), 10 , .lower , FormatOptions {}));
1732
1734
1733
- try std .testing .expectEqualSlices (u8 , "-101111000110000101001110" , bufPrintIntToSlice (buf , @as (i32 , -12345678 ), 2 , false , FormatOptions {}));
1734
- try std .testing .expectEqualSlices (u8 , "-12345678" , bufPrintIntToSlice (buf , @as (i32 , -12345678 ), 10 , false , FormatOptions {}));
1735
- try std .testing .expectEqualSlices (u8 , "-bc614e" , bufPrintIntToSlice (buf , @as (i32 , -12345678 ), 16 , false , FormatOptions {}));
1736
- try std .testing .expectEqualSlices (u8 , "-BC614E" , bufPrintIntToSlice (buf , @as (i32 , -12345678 ), 16 , true , FormatOptions {}));
1735
+ try std .testing .expectEqualSlices (u8 , "-101111000110000101001110" , bufPrintIntToSlice (buf , @as (i32 , -12345678 ), 2 , .lower , FormatOptions {}));
1736
+ try std .testing .expectEqualSlices (u8 , "-12345678" , bufPrintIntToSlice (buf , @as (i32 , -12345678 ), 10 , .lower , FormatOptions {}));
1737
+ try std .testing .expectEqualSlices (u8 , "-bc614e" , bufPrintIntToSlice (buf , @as (i32 , -12345678 ), 16 , .lower , FormatOptions {}));
1738
+ try std .testing .expectEqualSlices (u8 , "-BC614E" , bufPrintIntToSlice (buf , @as (i32 , -12345678 ), 16 , .upper , FormatOptions {}));
1737
1739
1738
- try std .testing .expectEqualSlices (u8 , "12345678" , bufPrintIntToSlice (buf , @as (u32 , 12345678 ), 10 , true , FormatOptions {}));
1740
+ try std .testing .expectEqualSlices (u8 , "12345678" , bufPrintIntToSlice (buf , @as (u32 , 12345678 ), 10 , .upper , FormatOptions {}));
1739
1741
1740
- try std .testing .expectEqualSlices (u8 , " 666" , bufPrintIntToSlice (buf , @as (u32 , 666 ), 10 , false , FormatOptions { .width = 6 }));
1741
- try std .testing .expectEqualSlices (u8 , " 1234" , bufPrintIntToSlice (buf , @as (u32 , 0x1234 ), 16 , false , FormatOptions { .width = 6 }));
1742
- try std .testing .expectEqualSlices (u8 , "1234" , bufPrintIntToSlice (buf , @as (u32 , 0x1234 ), 16 , false , FormatOptions { .width = 1 }));
1742
+ try std .testing .expectEqualSlices (u8 , " 666" , bufPrintIntToSlice (buf , @as (u32 , 666 ), 10 , .lower , FormatOptions { .width = 6 }));
1743
+ try std .testing .expectEqualSlices (u8 , " 1234" , bufPrintIntToSlice (buf , @as (u32 , 0x1234 ), 16 , .lower , FormatOptions { .width = 6 }));
1744
+ try std .testing .expectEqualSlices (u8 , "1234" , bufPrintIntToSlice (buf , @as (u32 , 0x1234 ), 16 , .lower , FormatOptions { .width = 1 }));
1743
1745
1744
- try std .testing .expectEqualSlices (u8 , "+42" , bufPrintIntToSlice (buf , @as (i32 , 42 ), 10 , false , FormatOptions { .width = 3 }));
1745
- try std .testing .expectEqualSlices (u8 , "-42" , bufPrintIntToSlice (buf , @as (i32 , -42 ), 10 , false , FormatOptions { .width = 3 }));
1746
+ try std .testing .expectEqualSlices (u8 , "+42" , bufPrintIntToSlice (buf , @as (i32 , 42 ), 10 , .lower , FormatOptions { .width = 3 }));
1747
+ try std .testing .expectEqualSlices (u8 , "-42" , bufPrintIntToSlice (buf , @as (i32 , -42 ), 10 , .lower , FormatOptions { .width = 3 }));
1746
1748
}
1747
1749
1748
- pub fn bufPrintIntToSlice (buf : []u8 , value : anytype , base : u8 , uppercase : bool , options : FormatOptions ) []u8 {
1749
- return buf [0.. formatIntBuf (buf , value , base , uppercase , options )];
1750
+ pub fn bufPrintIntToSlice (buf : []u8 , value : anytype , base : u8 , case : Case , options : FormatOptions ) []u8 {
1751
+ return buf [0.. formatIntBuf (buf , value , base , case , options )];
1750
1752
}
1751
1753
1752
1754
pub fn comptimePrint (comptime fmt : []const u8 , args : anytype ) * const [count (fmt , args ):0 ]u8 {
@@ -2412,10 +2414,6 @@ test "vector" {
2412
2414
// https://github.com/ziglang/zig/issues/4486
2413
2415
return error .SkipZigTest ;
2414
2416
}
2415
- if (builtin .target .cpu .arch == .wasm32 ) {
2416
- // https://github.com/ziglang/zig/issues/5339
2417
- return error .SkipZigTest ;
2418
- }
2419
2417
2420
2418
const vbool : std .meta .Vector (4 , bool ) = [_ ]bool { true , false , true , false };
2421
2419
const vi64 : std .meta .Vector (4 , i64 ) = [_ ]i64 { -2 , -1 , 0 , 1 };
0 commit comments