| author | |
| committer | |
| log | e4977f3e89fcc164a4d02cd38eb066cfe1a1124f |
| tree | 7cba9d333f6ebae6208e8487080bea896086acf7 |
| parent | d5e21a4f1a2920ef7bbe3c54feab1a3b5119bf77 |
| parent | a34a51ef6eb4bd8dfab14bd8bfe1193d5573eacf |
| signature |
std: use BoundedArray less9 files changed, 192 insertions(+), 163 deletions(-)
deps/aro/aro/Compilation.zig+18-17| ... | ... | @@ -582,10 +582,9 @@ fn generateFloatMacros(w: anytype, prefix: []const u8, semantics: target_util.FP |
| 582 | 582 | }, |
| 583 | 583 | ); |
| 584 | 584 | |
| 585 | var defPrefix = std.BoundedArray(u8, 32).init(0) catch unreachable; | |
| 586 | defPrefix.writer().print("__{s}_", .{prefix}) catch return error.OutOfMemory; | |
| 587 | ||
| 588 | const prefix_slice = defPrefix.constSlice(); | |
| 585 | var def_prefix_buf: [32]u8 = undefined; | |
| 586 | const prefix_slice = std.fmt.bufPrint(&def_prefix_buf, "__{s}_", .{prefix}) catch | |
| 587 | return error.OutOfMemory; | |
| 589 | 588 | |
| 590 | 589 | try w.print("#define {s}DENORM_MIN__ {s}{s}\n", .{ prefix_slice, denormMin, ext }); |
| 591 | 590 | try w.print("#define {s}HAS_DENORM__\n", .{prefix_slice}); |
| ... | ... | @@ -770,18 +769,18 @@ fn generateExactWidthType(comp: *const Compilation, w: anytype, mapper: StrInt.T |
| 770 | 769 | ty = if (unsigned) comp.types.int64.makeIntegerUnsigned() else comp.types.int64; |
| 771 | 770 | } |
| 772 | 771 | |
| 773 | var prefix = std.BoundedArray(u8, 16).init(0) catch unreachable; | |
| 774 | prefix.writer().print("{s}{d}", .{ if (unsigned) "__UINT" else "__INT", width }) catch return error.OutOfMemory; | |
| 772 | var buffer: [16]u8 = undefined; | |
| 773 | const suffix = "_TYPE__"; | |
| 774 | const full = std.fmt.bufPrint(&buffer, "{s}{d}{s}", .{ | |
| 775 | if (unsigned) "__UINT" else "__INT", width, suffix, | |
| 776 | }) catch return error.OutOfMemory; | |
| 775 | 777 | |
| 776 | { | |
| 777 | const len = prefix.len; | |
| 778 | defer prefix.resize(len) catch unreachable; // restoring previous size | |
| 779 | prefix.appendSliceAssumeCapacity("_TYPE__"); | |
| 780 | try generateTypeMacro(w, mapper, prefix.constSlice(), ty, comp.langopts); | |
| 781 | } | |
| 778 | try generateTypeMacro(w, mapper, full, ty, comp.langopts); | |
| 779 | ||
| 780 | const prefix = full[0 .. full.len - suffix.len]; // remove "_TYPE__" | |
| 782 | 781 | |
| 783 | try comp.generateFmt(prefix.constSlice(), w, ty); | |
| 784 | try comp.generateSuffixMacro(prefix.constSlice(), w, ty); | |
| 782 | try comp.generateFmt(prefix, w, ty); | |
| 783 | try comp.generateSuffixMacro(prefix, w, ty); | |
| 785 | 784 | } |
| 786 | 785 | |
| 787 | 786 | pub fn hasFloat128(comp: *const Compilation) bool { |
| ... | ... | @@ -908,10 +907,12 @@ fn generateExactWidthIntMax(comp: *const Compilation, w: anytype, specifier: Typ |
| 908 | 907 | ty = if (unsigned) comp.types.int64.makeIntegerUnsigned() else comp.types.int64; |
| 909 | 908 | } |
| 910 | 909 | |
| 911 | var name = std.BoundedArray(u8, 6).init(0) catch unreachable; | |
| 912 | name.writer().print("{s}{d}", .{ if (unsigned) "UINT" else "INT", bit_count }) catch return error.OutOfMemory; | |
| 910 | var name_buffer: [6]u8 = undefined; | |
| 911 | const name = std.fmt.bufPrint(&name_buffer, "{s}{d}", .{ | |
| 912 | if (unsigned) "UINT" else "INT", bit_count, | |
| 913 | }) catch return error.OutOfMemory; | |
| 913 | 914 | |
| 914 | return comp.generateIntMax(w, name.constSlice(), ty); | |
| 915 | return comp.generateIntMax(w, name, ty); | |
| 915 | 916 | } |
| 916 | 917 | |
| 917 | 918 | fn generateIntWidth(comp: *Compilation, w: anytype, name: []const u8, ty: Type) !void { |
deps/aro/aro/Driver/GCCDetector.zig+24-17| ... | ... | @@ -29,7 +29,7 @@ pub fn appendToolPath(self: *const GCCDetector, tc: *Toolchain) !void { |
| 29 | 29 | }, .program); |
| 30 | 30 | } |
| 31 | 31 | |
| 32 | fn addDefaultGCCPrefixes(prefixes: *PathPrefixes, tc: *const Toolchain) !void { | |
| 32 | fn addDefaultGCCPrefixes(prefixes: *std.ArrayListUnmanaged([]const u8), tc: *const Toolchain) !void { | |
| 33 | 33 | const sysroot = tc.getSysroot(); |
| 34 | 34 | const target = tc.getTarget(); |
| 35 | 35 | if (sysroot.len == 0 and target.os.tag == .linux and tc.filesystem.exists("/opt/rh")) { |
| ... | ... | @@ -57,14 +57,12 @@ fn addDefaultGCCPrefixes(prefixes: *PathPrefixes, tc: *const Toolchain) !void { |
| 57 | 57 | } |
| 58 | 58 | } |
| 59 | 59 | |
| 60 | const PathPrefixes = std.BoundedArray([]const u8, 16); | |
| 61 | ||
| 62 | 60 | fn collectLibDirsAndTriples( |
| 63 | 61 | tc: *Toolchain, |
| 64 | lib_dirs: *PathPrefixes, | |
| 65 | triple_aliases: *PathPrefixes, | |
| 66 | biarch_libdirs: *PathPrefixes, | |
| 67 | biarch_triple_aliases: *PathPrefixes, | |
| 62 | lib_dirs: *std.ArrayListUnmanaged([]const u8), | |
| 63 | triple_aliases: *std.ArrayListUnmanaged([]const u8), | |
| 64 | biarch_libdirs: *std.ArrayListUnmanaged([]const u8), | |
| 65 | biarch_triple_aliases: *std.ArrayListUnmanaged([]const u8), | |
| 68 | 66 | ) !void { |
| 69 | 67 | const AArch64LibDirs: [2][]const u8 = .{ "/lib64", "/lib" }; |
| 70 | 68 | const AArch64Triples: [4][]const u8 = .{ "aarch64-none-linux-gnu", "aarch64-linux-gnu", "aarch64-redhat-linux", "aarch64-suse-linux" }; |
| ... | ... | @@ -408,10 +406,18 @@ pub fn discover(self: *GCCDetector, tc: *Toolchain) !void { |
| 408 | 406 | else |
| 409 | 407 | target_util.get32BitArchVariant(target); |
| 410 | 408 | |
| 411 | var candidate_lib_dirs: PathPrefixes = .{}; | |
| 412 | var candidate_triple_aliases: PathPrefixes = .{}; | |
| 413 | var candidate_biarch_lib_dirs: PathPrefixes = .{}; | |
| 414 | var candidate_biarch_triple_aliases: PathPrefixes = .{}; | |
| 409 | var candidate_lib_dirs_buffer: [16][]const u8 = undefined; | |
| 410 | var candidate_lib_dirs = std.ArrayListUnmanaged([]const u8).initBuffer(&candidate_lib_dirs_buffer); | |
| 411 | ||
| 412 | var candidate_triple_aliases_buffer: [16][]const u8 = undefined; | |
| 413 | var candidate_triple_aliases = std.ArrayListUnmanaged([]const u8).initBuffer(&candidate_triple_aliases_buffer); | |
| 414 | ||
| 415 | var candidate_biarch_lib_dirs_buffer: [16][]const u8 = undefined; | |
| 416 | var candidate_biarch_lib_dirs = std.ArrayListUnmanaged([]const u8).initBuffer(&candidate_biarch_lib_dirs_buffer); | |
| 417 | ||
| 418 | var candidate_biarch_triple_aliases_buffer: [16][]const u8 = undefined; | |
| 419 | var candidate_biarch_triple_aliases = std.ArrayListUnmanaged([]const u8).initBuffer(&candidate_biarch_triple_aliases_buffer); | |
| 420 | ||
| 415 | 421 | try collectLibDirsAndTriples( |
| 416 | 422 | tc, |
| 417 | 423 | &candidate_lib_dirs, |
| ... | ... | @@ -433,7 +439,8 @@ pub fn discover(self: *GCCDetector, tc: *Toolchain) !void { |
| 433 | 439 | } |
| 434 | 440 | } |
| 435 | 441 | |
| 436 | var prefixes: PathPrefixes = .{}; | |
| 442 | var prefixes_buf: [16][]const u8 = undefined; | |
| 443 | var prefixes = std.ArrayListUnmanaged([]const u8).initBuffer(&prefixes_buf); | |
| 437 | 444 | const gcc_toolchain_dir = gccToolchainDir(tc); |
| 438 | 445 | if (gcc_toolchain_dir.len != 0) { |
| 439 | 446 | const adjusted = if (gcc_toolchain_dir[gcc_toolchain_dir.len - 1] == '/') |
| ... | ... | @@ -455,10 +462,10 @@ pub fn discover(self: *GCCDetector, tc: *Toolchain) !void { |
| 455 | 462 | } |
| 456 | 463 | |
| 457 | 464 | const v0 = GCCVersion.parse("0.0.0"); |
| 458 | for (prefixes.constSlice()) |prefix| { | |
| 465 | for (prefixes.items) |prefix| { | |
| 459 | 466 | if (!tc.filesystem.exists(prefix)) continue; |
| 460 | 467 | |
| 461 | for (candidate_lib_dirs.constSlice()) |suffix| { | |
| 468 | for (candidate_lib_dirs.items) |suffix| { | |
| 462 | 469 | defer fib.reset(); |
| 463 | 470 | const lib_dir = std.fs.path.join(fib.allocator(), &.{ prefix, suffix }) catch continue; |
| 464 | 471 | if (!tc.filesystem.exists(lib_dir)) continue; |
| ... | ... | @@ -467,17 +474,17 @@ pub fn discover(self: *GCCDetector, tc: *Toolchain) !void { |
| 467 | 474 | const gcc_cross_dir_exists = tc.filesystem.joinedExists(&.{ lib_dir, "/gcc-cross" }); |
| 468 | 475 | |
| 469 | 476 | try self.scanLibDirForGCCTriple(tc, target, lib_dir, triple_str, false, gcc_dir_exists, gcc_cross_dir_exists); |
| 470 | for (candidate_triple_aliases.constSlice()) |candidate| { | |
| 477 | for (candidate_triple_aliases.items) |candidate| { | |
| 471 | 478 | try self.scanLibDirForGCCTriple(tc, target, lib_dir, candidate, false, gcc_dir_exists, gcc_cross_dir_exists); |
| 472 | 479 | } |
| 473 | 480 | } |
| 474 | for (candidate_biarch_lib_dirs.constSlice()) |suffix| { | |
| 481 | for (candidate_biarch_lib_dirs.items) |suffix| { | |
| 475 | 482 | const lib_dir = std.fs.path.join(fib.allocator(), &.{ prefix, suffix }) catch continue; |
| 476 | 483 | if (!tc.filesystem.exists(lib_dir)) continue; |
| 477 | 484 | |
| 478 | 485 | const gcc_dir_exists = tc.filesystem.joinedExists(&.{ lib_dir, "/gcc" }); |
| 479 | 486 | const gcc_cross_dir_exists = tc.filesystem.joinedExists(&.{ lib_dir, "/gcc-cross" }); |
| 480 | for (candidate_biarch_triple_aliases.constSlice()) |candidate| { | |
| 487 | for (candidate_biarch_triple_aliases.items) |candidate| { | |
| 481 | 488 | try self.scanLibDirForGCCTriple(tc, target, lib_dir, candidate, true, gcc_dir_exists, gcc_cross_dir_exists); |
| 482 | 489 | } |
| 483 | 490 | } |
deps/aro/aro/Parser.zig+2-2| ... | ... | @@ -7796,7 +7796,7 @@ fn stringLiteral(p: *Parser) Error!Result { |
| 7796 | 7796 | } |
| 7797 | 7797 | }, |
| 7798 | 7798 | }; |
| 7799 | for (char_literal_parser.errors.constSlice()) |item| { | |
| 7799 | for (char_literal_parser.errors()) |item| { | |
| 7800 | 7800 | try p.errExtra(item.tag, p.tok_i, item.extra); |
| 7801 | 7801 | } |
| 7802 | 7802 | } |
| ... | ... | @@ -7911,7 +7911,7 @@ fn charLiteral(p: *Parser) Error!Result { |
| 7911 | 7911 | char_literal_parser.err(.char_lit_too_wide, .{ .none = {} }); |
| 7912 | 7912 | } |
| 7913 | 7913 | |
| 7914 | for (char_literal_parser.errors.constSlice()) |item| { | |
| 7914 | for (char_literal_parser.errors()) |item| { | |
| 7915 | 7915 | try p.errExtra(item.tag, p.tok_i, item.extra); |
| 7916 | 7916 | } |
| 7917 | 7917 | } |
deps/aro/aro/text_literal.zig+18-6| ... | ... | @@ -157,7 +157,8 @@ pub const Parser = struct { |
| 157 | 157 | max_codepoint: u21, |
| 158 | 158 | /// We only want to issue a max of 1 error per char literal |
| 159 | 159 | errored: bool = false, |
| 160 | errors: std.BoundedArray(CharDiagnostic, 4) = .{}, | |
| 160 | errors_buffer: [4]CharDiagnostic, | |
| 161 | errors_len: usize, | |
| 161 | 162 | comp: *const Compilation, |
| 162 | 163 | |
| 163 | 164 | pub fn init(literal: []const u8, kind: Kind, max_codepoint: u21, comp: *const Compilation) Parser { |
| ... | ... | @@ -166,6 +167,8 @@ pub const Parser = struct { |
| 166 | 167 | .comp = comp, |
| 167 | 168 | .kind = kind, |
| 168 | 169 | .max_codepoint = max_codepoint, |
| 170 | .errors_buffer = undefined, | |
| 171 | .errors_len = 0, | |
| 169 | 172 | }; |
| 170 | 173 | } |
| 171 | 174 | |
| ... | ... | @@ -178,19 +181,28 @@ pub const Parser = struct { |
| 178 | 181 | }; |
| 179 | 182 | } |
| 180 | 183 | |
| 184 | pub fn errors(p: *Parser) []CharDiagnostic { | |
| 185 | return p.errors_buffer[0..p.errors_len]; | |
| 186 | } | |
| 187 | ||
| 181 | 188 | pub fn err(self: *Parser, tag: Diagnostics.Tag, extra: Diagnostics.Message.Extra) void { |
| 182 | 189 | if (self.errored) return; |
| 183 | 190 | self.errored = true; |
| 184 | 191 | const diagnostic = .{ .tag = tag, .extra = extra }; |
| 185 | self.errors.append(diagnostic) catch { | |
| 186 | _ = self.errors.pop(); | |
| 187 | self.errors.append(diagnostic) catch unreachable; | |
| 188 | }; | |
| 192 | if (self.errors_len == self.errors_buffer.len) { | |
| 193 | self.errors_buffer[self.errors_buffer.len - 1] = diagnostic; | |
| 194 | } else { | |
| 195 | self.errors_buffer[self.errors_len] = diagnostic; | |
| 196 | self.errors_len += 1; | |
| 197 | } | |
| 189 | 198 | } |
| 190 | 199 | |
| 191 | 200 | pub fn warn(self: *Parser, tag: Diagnostics.Tag, extra: Diagnostics.Message.Extra) void { |
| 192 | 201 | if (self.errored) return; |
| 193 | self.errors.append(.{ .tag = tag, .extra = extra }) catch {}; | |
| 202 | if (self.errors_len < self.errors_buffer.len) { | |
| 203 | self.errors_buffer[self.errors_len] = .{ .tag = tag, .extra = extra }; | |
| 204 | self.errors_len += 1; | |
| 205 | } | |
| 194 | 206 | } |
| 195 | 207 | |
| 196 | 208 | pub fn next(self: *Parser) ?Item { |
lib/std/array_list.zig+11| ... | ... | @@ -633,6 +633,17 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ |
| 633 | 633 | return self; |
| 634 | 634 | } |
| 635 | 635 | |
| 636 | /// Initialize with externally-managed memory. The buffer determines the | |
| 637 | /// capacity, and the length is set to zero. | |
| 638 | /// When initialized this way, all methods that accept an Allocator | |
| 639 | /// argument are illegal to call. | |
| 640 | pub fn initBuffer(buffer: Slice) Self { | |
| 641 | return .{ | |
| 642 | .items = buffer[0..0], | |
| 643 | .capacity = buffer.len, | |
| 644 | }; | |
| 645 | } | |
| 646 | ||
| 636 | 647 | /// Release all allocated memory. |
| 637 | 648 | pub fn deinit(self: *Self, allocator: Allocator) void { |
| 638 | 649 | allocator.free(self.allocatedSlice()); |
lib/std/crypto/ff.zig+86-91| ... | ... | @@ -12,7 +12,6 @@ const math = std.math; |
| 12 | 12 | const mem = std.mem; |
| 13 | 13 | const meta = std.meta; |
| 14 | 14 | const testing = std.testing; |
| 15 | const BoundedArray = std.BoundedArray; | |
| 16 | 15 | const assert = std.debug.assert; |
| 17 | 16 | const Endian = std.builtin.Endian; |
| 18 | 17 | |
| ... | ... | @@ -63,46 +62,54 @@ pub fn Uint(comptime max_bits: comptime_int) type { |
| 63 | 62 | |
| 64 | 63 | return struct { |
| 65 | 64 | const Self = @This(); |
| 66 | ||
| 67 | 65 | const max_limbs_count = math.divCeil(usize, max_bits, t_bits) catch unreachable; |
| 68 | const Limbs = BoundedArray(Limb, max_limbs_count); | |
| 69 | limbs: Limbs, | |
| 66 | ||
| 67 | limbs_buffer: [max_limbs_count]Limb, | |
| 68 | /// The number of active limbs. | |
| 69 | limbs_len: usize, | |
| 70 | 70 | |
| 71 | 71 | /// Number of bytes required to serialize an integer. |
| 72 | 72 | pub const encoded_bytes = math.divCeil(usize, max_bits, 8) catch unreachable; |
| 73 | 73 | |
| 74 | // Returns the number of active limbs. | |
| 75 | fn limbs_count(self: Self) usize { | |
| 76 | return self.limbs.len; | |
| 74 | /// Constant slice of active limbs. | |
| 75 | fn limbsConst(self: *const Self) []const Limb { | |
| 76 | return self.limbs_buffer[0..self.limbs_len]; | |
| 77 | } | |
| 78 | ||
| 79 | /// Mutable slice of active limbs. | |
| 80 | fn limbs(self: *Self) []Limb { | |
| 81 | return self.limbs_buffer[0..self.limbs_len]; | |
| 77 | 82 | } |
| 78 | 83 | |
| 79 | 84 | // Removes limbs whose value is zero from the active limbs. |
| 80 | 85 | fn normalize(self: Self) Self { |
| 81 | 86 | var res = self; |
| 82 | if (self.limbs_count() < 2) { | |
| 87 | if (self.limbs_len < 2) { | |
| 83 | 88 | return res; |
| 84 | 89 | } |
| 85 | var i = self.limbs_count() - 1; | |
| 86 | while (i > 0 and res.limbs.get(i) == 0) : (i -= 1) {} | |
| 87 | res.limbs.resize(i + 1) catch unreachable; | |
| 90 | var i = self.limbs_len - 1; | |
| 91 | while (i > 0 and res.limbsConst()[i] == 0) : (i -= 1) {} | |
| 92 | res.limbs_len = i + 1; | |
| 93 | assert(res.limbs_len <= res.limbs_buffer.len); | |
| 88 | 94 | return res; |
| 89 | 95 | } |
| 90 | 96 | |
| 91 | 97 | /// The zero integer. |
| 92 | pub const zero = zero: { | |
| 93 | var limbs = Limbs.init(0) catch unreachable; | |
| 94 | limbs.appendNTimesAssumeCapacity(0, max_limbs_count); | |
| 95 | break :zero Self{ .limbs = limbs }; | |
| 98 | pub const zero: Self = .{ | |
| 99 | .limbs_buffer = [1]Limb{0} ** max_limbs_count, | |
| 100 | .limbs_len = max_limbs_count, | |
| 96 | 101 | }; |
| 97 | 102 | |
| 98 | 103 | /// Creates a new big integer from a primitive type. |
| 99 | 104 | /// This function may not run in constant time. |
| 100 | pub fn fromPrimitive(comptime T: type, x_: T) OverflowError!Self { | |
| 101 | var x = x_; | |
| 102 | var out = Self.zero; | |
| 103 | for (0..out.limbs.capacity()) |i| { | |
| 104 | const t = if (@bitSizeOf(T) > t_bits) @as(TLimb, @truncate(x)) else x; | |
| 105 | out.limbs.set(i, t); | |
| 105 | pub fn fromPrimitive(comptime T: type, init_value: T) OverflowError!Self { | |
| 106 | var x = init_value; | |
| 107 | var out: Self = .{ | |
| 108 | .limbs_buffer = undefined, | |
| 109 | .limbs_len = max_limbs_count, | |
| 110 | }; | |
| 111 | for (&out.limbs_buffer) |*limb| { | |
| 112 | limb.* = if (@bitSizeOf(T) > t_bits) @as(TLimb, @truncate(x)) else x; | |
| 106 | 113 | x = math.shr(T, x, t_bits); |
| 107 | 114 | } |
| 108 | 115 | if (x != 0) { |
| ... | ... | @@ -115,13 +122,13 @@ pub fn Uint(comptime max_bits: comptime_int) type { |
| 115 | 122 | /// This function may not run in constant time. |
| 116 | 123 | pub fn toPrimitive(self: Self, comptime T: type) OverflowError!T { |
| 117 | 124 | var x: T = 0; |
| 118 | var i = self.limbs_count() - 1; | |
| 125 | var i = self.limbs_len - 1; | |
| 119 | 126 | while (true) : (i -= 1) { |
| 120 | 127 | if (@bitSizeOf(T) >= t_bits and math.shr(T, x, @bitSizeOf(T) - t_bits) != 0) { |
| 121 | 128 | return error.Overflow; |
| 122 | 129 | } |
| 123 | 130 | x = math.shl(T, x, t_bits); |
| 124 | const v = math.cast(T, self.limbs.get(i)) orelse return error.Overflow; | |
| 131 | const v = math.cast(T, self.limbsConst()[i]) orelse return error.Overflow; | |
| 125 | 132 | x |= v; |
| 126 | 133 | if (i == 0) break; |
| 127 | 134 | } |
| ... | ... | @@ -140,9 +147,9 @@ pub fn Uint(comptime max_bits: comptime_int) type { |
| 140 | 147 | .big => bytes.len - 1, |
| 141 | 148 | .little => 0, |
| 142 | 149 | }; |
| 143 | for (0..self.limbs.len) |i| { | |
| 150 | for (0..self.limbs_len) |i| { | |
| 144 | 151 | var remaining_bits = t_bits; |
| 145 | var limb = self.limbs.get(i); | |
| 152 | var limb = self.limbsConst()[i]; | |
| 146 | 153 | while (remaining_bits >= 8) { |
| 147 | 154 | bytes[out_i] |= math.shl(u8, @as(u8, @truncate(limb)), shift); |
| 148 | 155 | const consumed = 8 - shift; |
| ... | ... | @@ -152,7 +159,7 @@ pub fn Uint(comptime max_bits: comptime_int) type { |
| 152 | 159 | switch (endian) { |
| 153 | 160 | .big => { |
| 154 | 161 | if (out_i == 0) { |
| 155 | if (i != self.limbs.len - 1 or limb != 0) { | |
| 162 | if (i != self.limbs_len - 1 or limb != 0) { | |
| 156 | 163 | return error.Overflow; |
| 157 | 164 | } |
| 158 | 165 | return; |
| ... | ... | @@ -162,7 +169,7 @@ pub fn Uint(comptime max_bits: comptime_int) type { |
| 162 | 169 | .little => { |
| 163 | 170 | out_i += 1; |
| 164 | 171 | if (out_i == bytes.len) { |
| 165 | if (i != self.limbs.len - 1 or limb != 0) { | |
| 172 | if (i != self.limbs_len - 1 or limb != 0) { | |
| 166 | 173 | return error.Overflow; |
| 167 | 174 | } |
| 168 | 175 | return; |
| ... | ... | @@ -187,20 +194,20 @@ pub fn Uint(comptime max_bits: comptime_int) type { |
| 187 | 194 | }; |
| 188 | 195 | while (true) { |
| 189 | 196 | const bi = bytes[i]; |
| 190 | out.limbs.set(out_i, out.limbs.get(out_i) | math.shl(Limb, bi, shift)); | |
| 197 | out.limbs()[out_i] |= math.shl(Limb, bi, shift); | |
| 191 | 198 | shift += 8; |
| 192 | 199 | if (shift >= t_bits) { |
| 193 | 200 | shift -= t_bits; |
| 194 | out.limbs.set(out_i, @as(TLimb, @truncate(out.limbs.get(out_i)))); | |
| 201 | out.limbs()[out_i] = @as(TLimb, @truncate(out.limbs()[out_i])); | |
| 195 | 202 | const overflow = math.shr(Limb, bi, 8 - shift); |
| 196 | 203 | out_i += 1; |
| 197 | if (out_i >= out.limbs.len) { | |
| 204 | if (out_i >= out.limbs_len) { | |
| 198 | 205 | if (overflow != 0 or i != 0) { |
| 199 | 206 | return error.Overflow; |
| 200 | 207 | } |
| 201 | 208 | break; |
| 202 | 209 | } |
| 203 | out.limbs.set(out_i, overflow); | |
| 210 | out.limbs()[out_i] = overflow; | |
| 204 | 211 | } |
| 205 | 212 | switch (endian) { |
| 206 | 213 | .big => { |
| ... | ... | @@ -218,32 +225,31 @@ pub fn Uint(comptime max_bits: comptime_int) type { |
| 218 | 225 | |
| 219 | 226 | /// Returns `true` if both integers are equal. |
| 220 | 227 | pub fn eql(x: Self, y: Self) bool { |
| 221 | return crypto.utils.timingSafeEql([max_limbs_count]Limb, x.limbs.buffer, y.limbs.buffer); | |
| 228 | return crypto.utils.timingSafeEql([max_limbs_count]Limb, x.limbs_buffer, y.limbs_buffer); | |
| 222 | 229 | } |
| 223 | 230 | |
| 224 | 231 | /// Compares two integers. |
| 225 | 232 | pub fn compare(x: Self, y: Self) math.Order { |
| 226 | 233 | return crypto.utils.timingSafeCompare( |
| 227 | 234 | Limb, |
| 228 | x.limbs.constSlice(), | |
| 229 | y.limbs.constSlice(), | |
| 235 | x.limbsConst(), | |
| 236 | y.limbsConst(), | |
| 230 | 237 | .little, |
| 231 | 238 | ); |
| 232 | 239 | } |
| 233 | 240 | |
| 234 | 241 | /// Returns `true` if the integer is zero. |
| 235 | 242 | pub fn isZero(x: Self) bool { |
| 236 | const x_limbs = x.limbs.constSlice(); | |
| 237 | 243 | var t: Limb = 0; |
| 238 | for (0..x.limbs_count()) |i| { | |
| 239 | t |= x_limbs[i]; | |
| 244 | for (x.limbsConst()) |elem| { | |
| 245 | t |= elem; | |
| 240 | 246 | } |
| 241 | 247 | return ct.eql(t, 0); |
| 242 | 248 | } |
| 243 | 249 | |
| 244 | 250 | /// Returns `true` if the integer is odd. |
| 245 | 251 | pub fn isOdd(x: Self) bool { |
| 246 | return @as(bool, @bitCast(@as(u1, @truncate(x.limbs.get(0))))); | |
| 252 | return @as(u1, @truncate(x.limbsConst()[0])) != 0; | |
| 247 | 253 | } |
| 248 | 254 | |
| 249 | 255 | /// Adds `y` to `x`, and returns `true` if the operation overflowed. |
| ... | ... | @@ -258,39 +264,31 @@ pub fn Uint(comptime max_bits: comptime_int) type { |
| 258 | 264 | |
| 259 | 265 | // Replaces the limbs of `x` with the limbs of `y` if `on` is `true`. |
| 260 | 266 | fn cmov(x: *Self, on: bool, y: Self) void { |
| 261 | const x_limbs = x.limbs.slice(); | |
| 262 | const y_limbs = y.limbs.constSlice(); | |
| 263 | for (0..y.limbs_count()) |i| { | |
| 264 | x_limbs[i] = ct.select(on, y_limbs[i], x_limbs[i]); | |
| 267 | for (x.limbs(), y.limbsConst()) |*x_limb, y_limb| { | |
| 268 | x_limb.* = ct.select(on, y_limb, x_limb.*); | |
| 265 | 269 | } |
| 266 | 270 | } |
| 267 | 271 | |
| 268 | // Adds `y` to `x` if `on` is `true`, and returns `true` if the operation overflowed. | |
| 272 | // Adds `y` to `x` if `on` is `true`, and returns `true` if the | |
| 273 | // operation overflowed. | |
| 269 | 274 | fn conditionalAddWithOverflow(x: *Self, on: bool, y: Self) u1 { |
| 270 | assert(x.limbs_count() == y.limbs_count()); // Operands must have the same size. | |
| 271 | const x_limbs = x.limbs.slice(); | |
| 272 | const y_limbs = y.limbs.constSlice(); | |
| 273 | ||
| 274 | 275 | var carry: u1 = 0; |
| 275 | for (0..x.limbs_count()) |i| { | |
| 276 | const res = x_limbs[i] + y_limbs[i] + carry; | |
| 277 | x_limbs[i] = ct.select(on, @as(TLimb, @truncate(res)), x_limbs[i]); | |
| 278 | carry = @as(u1, @truncate(res >> t_bits)); | |
| 276 | for (x.limbs(), y.limbsConst()) |*x_limb, y_limb| { | |
| 277 | const res = x_limb.* + y_limb + carry; | |
| 278 | x_limb.* = ct.select(on, @as(TLimb, @truncate(res)), x_limb.*); | |
| 279 | carry = @truncate(res >> t_bits); | |
| 279 | 280 | } |
| 280 | 281 | return carry; |
| 281 | 282 | } |
| 282 | 283 | |
| 283 | // Subtracts `y` from `x` if `on` is `true`, and returns `true` if the operation overflowed. | |
| 284 | // Subtracts `y` from `x` if `on` is `true`, and returns `true` if the | |
| 285 | // operation overflowed. | |
| 284 | 286 | fn conditionalSubWithOverflow(x: *Self, on: bool, y: Self) u1 { |
| 285 | assert(x.limbs_count() == y.limbs_count()); // Operands must have the same size. | |
| 286 | const x_limbs = x.limbs.slice(); | |
| 287 | const y_limbs = y.limbs.constSlice(); | |
| 288 | ||
| 289 | 287 | var borrow: u1 = 0; |
| 290 | for (0..x.limbs_count()) |i| { | |
| 291 | const res = x_limbs[i] -% y_limbs[i] -% borrow; | |
| 292 | x_limbs[i] = ct.select(on, @as(TLimb, @truncate(res)), x_limbs[i]); | |
| 293 | borrow = @as(u1, @truncate(res >> t_bits)); | |
| 288 | for (x.limbs(), y.limbsConst()) |*x_limb, y_limb| { | |
| 289 | const res = x_limb.* -% y_limb -% borrow; | |
| 290 | x_limb.* = ct.select(on, @as(TLimb, @truncate(res)), x_limb.*); | |
| 291 | borrow = @truncate(res >> t_bits); | |
| 294 | 292 | } |
| 295 | 293 | return borrow; |
| 296 | 294 | } |
| ... | ... | @@ -315,7 +313,7 @@ fn Fe_(comptime bits: comptime_int) type { |
| 315 | 313 | |
| 316 | 314 | // The number of active limbs to represent the field element. |
| 317 | 315 | fn limbs_count(self: Self) usize { |
| 318 | return self.v.limbs_count(); | |
| 316 | return self.v.limbs_len; | |
| 319 | 317 | } |
| 320 | 318 | |
| 321 | 319 | /// Creates a field element from a primitive. |
| ... | ... | @@ -398,7 +396,7 @@ pub fn Modulus(comptime max_bits: comptime_int) type { |
| 398 | 396 | |
| 399 | 397 | // Number of active limbs in the modulus. |
| 400 | 398 | fn limbs_count(self: Self) usize { |
| 401 | return self.v.limbs_count(); | |
| 399 | return self.v.limbs_len; | |
| 402 | 400 | } |
| 403 | 401 | |
| 404 | 402 | /// Actual size of the modulus, in bits. |
| ... | ... | @@ -409,7 +407,7 @@ pub fn Modulus(comptime max_bits: comptime_int) type { |
| 409 | 407 | /// Returns the element `1`. |
| 410 | 408 | pub fn one(self: Self) Fe { |
| 411 | 409 | var fe = self.zero; |
| 412 | fe.v.limbs.set(0, 1); | |
| 410 | fe.v.limbs()[0] = 1; | |
| 413 | 411 | return fe; |
| 414 | 412 | } |
| 415 | 413 | |
| ... | ... | @@ -419,10 +417,10 @@ pub fn Modulus(comptime max_bits: comptime_int) type { |
| 419 | 417 | if (!v_.isOdd()) return error.EvenModulus; |
| 420 | 418 | |
| 421 | 419 | var v = v_.normalize(); |
| 422 | const hi = v.limbs.get(v.limbs_count() - 1); | |
| 423 | const lo = v.limbs.get(0); | |
| 420 | const hi = v.limbsConst()[v.limbs_len - 1]; | |
| 421 | const lo = v.limbsConst()[0]; | |
| 424 | 422 | |
| 425 | if (v.limbs_count() < 2 and lo < 3) { | |
| 423 | if (v.limbs_len < 2 and lo < 3) { | |
| 426 | 424 | return error.ModulusTooSmall; |
| 427 | 425 | } |
| 428 | 426 | |
| ... | ... | @@ -481,18 +479,19 @@ pub fn Modulus(comptime max_bits: comptime_int) type { |
| 481 | 479 | const new_len = self.limbs_count(); |
| 482 | 480 | if (fe.limbs_count() < new_len) return error.Overflow; |
| 483 | 481 | var acc: Limb = 0; |
| 484 | for (fe.v.limbs.constSlice()[new_len..]) |limb| { | |
| 482 | for (fe.v.limbsConst()[new_len..]) |limb| { | |
| 485 | 483 | acc |= limb; |
| 486 | 484 | } |
| 487 | 485 | if (acc != 0) return error.Overflow; |
| 488 | try fe.v.limbs.resize(new_len); | |
| 486 | if (new_len > fe.v.limbs_buffer.len) return error.Overflow; | |
| 487 | fe.v.limbs_len = new_len; | |
| 489 | 488 | } |
| 490 | 489 | |
| 491 | 490 | // Computes R^2 for the Montgomery representation. |
| 492 | 491 | fn computeRR(self: *Self) void { |
| 493 | 492 | self.rr = self.zero; |
| 494 | 493 | const n = self.rr.limbs_count(); |
| 495 | self.rr.v.limbs.set(n - 1, 1); | |
| 494 | self.rr.v.limbs()[n - 1] = 1; | |
| 496 | 495 | for ((n - 1)..(2 * n)) |_| { |
| 497 | 496 | self.shiftIn(&self.rr, 0); |
| 498 | 497 | } |
| ... | ... | @@ -502,9 +501,9 @@ pub fn Modulus(comptime max_bits: comptime_int) type { |
| 502 | 501 | /// Computes x << t_bits + y (mod m) |
| 503 | 502 | fn shiftIn(self: Self, x: *Fe, y: Limb) void { |
| 504 | 503 | var d = self.zero; |
| 505 | const x_limbs = x.v.limbs.slice(); | |
| 506 | const d_limbs = d.v.limbs.slice(); | |
| 507 | const m_limbs = self.v.limbs.constSlice(); | |
| 504 | const x_limbs = x.v.limbs(); | |
| 505 | const d_limbs = d.v.limbs(); | |
| 506 | const m_limbs = self.v.limbsConst(); | |
| 508 | 507 | |
| 509 | 508 | var need_sub = false; |
| 510 | 509 | var i: usize = t_bits - 1; |
| ... | ... | @@ -569,18 +568,18 @@ pub fn Modulus(comptime max_bits: comptime_int) type { |
| 569 | 568 | /// Reduces an arbitrary `Uint`, converting it to a field element. |
| 570 | 569 | pub fn reduce(self: Self, x: anytype) Fe { |
| 571 | 570 | var out = self.zero; |
| 572 | var i = x.limbs_count() - 1; | |
| 571 | var i = x.limbs_len - 1; | |
| 573 | 572 | if (self.limbs_count() >= 2) { |
| 574 | 573 | const start = @min(i, self.limbs_count() - 2); |
| 575 | 574 | var j = start; |
| 576 | 575 | while (true) : (j -= 1) { |
| 577 | out.v.limbs.set(j, x.limbs.get(i)); | |
| 576 | out.v.limbs()[j] = x.limbsConst()[i]; | |
| 578 | 577 | i -= 1; |
| 579 | 578 | if (j == 0) break; |
| 580 | 579 | } |
| 581 | 580 | } |
| 582 | 581 | while (true) : (i -= 1) { |
| 583 | self.shiftIn(&out, x.limbs.get(i)); | |
| 582 | self.shiftIn(&out, x.limbsConst()[i]); | |
| 584 | 583 | if (i == 0) break; |
| 585 | 584 | } |
| 586 | 585 | return out; |
| ... | ... | @@ -591,10 +590,10 @@ pub fn Modulus(comptime max_bits: comptime_int) type { |
| 591 | 590 | assert(d.limbs_count() == y.limbs_count()); |
| 592 | 591 | assert(d.limbs_count() == self.limbs_count()); |
| 593 | 592 | |
| 594 | const a_limbs = x.v.limbs.constSlice(); | |
| 595 | const b_limbs = y.v.limbs.constSlice(); | |
| 596 | const d_limbs = d.v.limbs.slice(); | |
| 597 | const m_limbs = self.v.limbs.constSlice(); | |
| 593 | const a_limbs = x.v.limbsConst(); | |
| 594 | const b_limbs = y.v.limbsConst(); | |
| 595 | const d_limbs = d.v.limbs(); | |
| 596 | const m_limbs = self.v.limbsConst(); | |
| 598 | 597 | |
| 599 | 598 | var overflow: u1 = 0; |
| 600 | 599 | for (0..self.limbs_count()) |i| { |
| ... | ... | @@ -685,7 +684,7 @@ pub fn Modulus(comptime max_bits: comptime_int) type { |
| 685 | 684 | const k: u1 = @truncate(b >> j); |
| 686 | 685 | if (k != 0) { |
| 687 | 686 | const t = self.montgomeryMul(out, x_m); |
| 688 | @memcpy(out.v.limbs.slice(), t.v.limbs.constSlice()); | |
| 687 | @memcpy(out.v.limbs(), t.v.limbsConst()); | |
| 689 | 688 | } |
| 690 | 689 | if (j == 0) break; |
| 691 | 690 | } |
| ... | ... | @@ -731,7 +730,7 @@ pub fn Modulus(comptime max_bits: comptime_int) type { |
| 731 | 730 | } |
| 732 | 731 | const t1 = self.montgomeryMul(out, t0); |
| 733 | 732 | if (public) { |
| 734 | @memcpy(out.v.limbs.slice(), t1.v.limbs.constSlice()); | |
| 733 | @memcpy(out.v.limbs(), t1.v.limbsConst()); | |
| 735 | 734 | } else { |
| 736 | 735 | out.v.cmov(!ct.eql(k, 0), t1.v); |
| 737 | 736 | } |
| ... | ... | @@ -790,9 +789,9 @@ pub fn Modulus(comptime max_bits: comptime_int) type { |
| 790 | 789 | pub fn powPublic(self: Self, x: Fe, e: Fe) NullExponentError!Fe { |
| 791 | 790 | var e_normalized = Fe{ .v = e.v.normalize() }; |
| 792 | 791 | var buf_: [Fe.encoded_bytes]u8 = undefined; |
| 793 | var buf = buf_[0 .. math.divCeil(usize, e_normalized.v.limbs_count() * t_bits, 8) catch unreachable]; | |
| 792 | var buf = buf_[0 .. math.divCeil(usize, e_normalized.v.limbs_len * t_bits, 8) catch unreachable]; | |
| 794 | 793 | e_normalized.toBytes(buf, .little) catch unreachable; |
| 795 | const leading = @clz(e_normalized.v.limbs.get(e_normalized.v.limbs_count() - carry_bits)); | |
| 794 | const leading = @clz(e_normalized.v.limbsConst()[e_normalized.v.limbs_len - carry_bits]); | |
| 796 | 795 | buf = buf[0 .. buf.len - leading / 8]; |
| 797 | 796 | return self.powWithEncodedPublicExponent(x, buf, .little); |
| 798 | 797 | } |
| ... | ... | @@ -835,20 +834,16 @@ const ct_protected = struct { |
| 835 | 834 | |
| 836 | 835 | // Compares two big integers in constant time, returning true if x < y. |
| 837 | 836 | fn limbsCmpLt(x: anytype, y: @TypeOf(x)) bool { |
| 838 | assert(x.limbs_count() == y.limbs_count()); | |
| 839 | const x_limbs = x.limbs.constSlice(); | |
| 840 | const y_limbs = y.limbs.constSlice(); | |
| 841 | ||
| 842 | 837 | var c: u1 = 0; |
| 843 | for (0..x.limbs_count()) |i| { | |
| 844 | c = @as(u1, @truncate((x_limbs[i] -% y_limbs[i] -% c) >> t_bits)); | |
| 838 | for (x.limbsConst(), y.limbsConst()) |x_limb, y_limb| { | |
| 839 | c = @truncate((x_limb -% y_limb -% c) >> t_bits); | |
| 845 | 840 | } |
| 846 | return @as(bool, @bitCast(c)); | |
| 841 | return c != 0; | |
| 847 | 842 | } |
| 848 | 843 | |
| 849 | 844 | // Compares two big integers in constant time, returning true if x >= y. |
| 850 | 845 | fn limbsCmpGeq(x: anytype, y: @TypeOf(x)) bool { |
| 851 | return @as(bool, @bitCast(1 - @intFromBool(ct.limbsCmpLt(x, y)))); | |
| 846 | return !ct.limbsCmpLt(x, y); | |
| 852 | 847 | } |
| 853 | 848 | |
| 854 | 849 | // Multiplies two limbs and returns the result as a wide limb. |
lib/std/fs.zig+15-14| ... | ... | @@ -2204,28 +2204,29 @@ pub const Dir = struct { |
| 2204 | 2204 | name: []const u8, |
| 2205 | 2205 | parent_dir: Dir, |
| 2206 | 2206 | iter: IterableDir.Iterator, |
| 2207 | }; | |
| 2208 | 2207 | |
| 2209 | var stack = std.BoundedArray(StackItem, 16){}; | |
| 2210 | defer { | |
| 2211 | for (stack.slice()) |*item| { | |
| 2212 | item.iter.dir.close(); | |
| 2208 | fn closeAll(items: []@This()) void { | |
| 2209 | for (items) |*item| item.iter.dir.close(); | |
| 2213 | 2210 | } |
| 2214 | } | |
| 2211 | }; | |
| 2215 | 2212 | |
| 2216 | stack.appendAssumeCapacity(StackItem{ | |
| 2213 | var stack_buffer: [16]StackItem = undefined; | |
| 2214 | var stack = std.ArrayListUnmanaged(StackItem).initBuffer(&stack_buffer); | |
| 2215 | defer StackItem.closeAll(stack.items); | |
| 2216 | ||
| 2217 | stack.appendAssumeCapacity(.{ | |
| 2217 | 2218 | .name = sub_path, |
| 2218 | 2219 | .parent_dir = self, |
| 2219 | 2220 | .iter = initial_iterable_dir.iterateAssumeFirstIteration(), |
| 2220 | 2221 | }); |
| 2221 | 2222 | |
| 2222 | process_stack: while (stack.len != 0) { | |
| 2223 | var top = &(stack.slice()[stack.len - 1]); | |
| 2223 | process_stack: while (stack.items.len != 0) { | |
| 2224 | var top = &stack.items[stack.items.len - 1]; | |
| 2224 | 2225 | while (try top.iter.next()) |entry| { |
| 2225 | 2226 | var treat_as_dir = entry.kind == .directory; |
| 2226 | 2227 | handle_entry: while (true) { |
| 2227 | 2228 | if (treat_as_dir) { |
| 2228 | if (stack.ensureUnusedCapacity(1)) { | |
| 2229 | if (stack.unusedCapacitySlice().len >= 1) { | |
| 2229 | 2230 | var iterable_dir = top.iter.dir.openIterableDir(entry.name, .{ .no_follow = true }) catch |err| switch (err) { |
| 2230 | 2231 | error.NotDir => { |
| 2231 | 2232 | treat_as_dir = false; |
| ... | ... | @@ -2251,13 +2252,13 @@ pub const Dir = struct { |
| 2251 | 2252 | error.DeviceBusy, |
| 2252 | 2253 | => |e| return e, |
| 2253 | 2254 | }; |
| 2254 | stack.appendAssumeCapacity(StackItem{ | |
| 2255 | stack.appendAssumeCapacity(.{ | |
| 2255 | 2256 | .name = entry.name, |
| 2256 | 2257 | .parent_dir = top.iter.dir, |
| 2257 | 2258 | .iter = iterable_dir.iterateAssumeFirstIteration(), |
| 2258 | 2259 | }); |
| 2259 | 2260 | continue :process_stack; |
| 2260 | } else |_| { | |
| 2261 | } else { | |
| 2261 | 2262 | try top.iter.dir.deleteTreeMinStackSizeWithKindHint(entry.name, entry.kind); |
| 2262 | 2263 | break :handle_entry; |
| 2263 | 2264 | } |
| ... | ... | @@ -2301,7 +2302,7 @@ pub const Dir = struct { |
| 2301 | 2302 | // pop the value from the stack. |
| 2302 | 2303 | const parent_dir = top.parent_dir; |
| 2303 | 2304 | const name = top.name; |
| 2304 | _ = stack.pop(); | |
| 2305 | stack.items.len -= 1; | |
| 2305 | 2306 | |
| 2306 | 2307 | var need_to_retry: bool = false; |
| 2307 | 2308 | parent_dir.deleteDir(name) catch |err| switch (err) { |
| ... | ... | @@ -2374,7 +2375,7 @@ pub const Dir = struct { |
| 2374 | 2375 | }; |
| 2375 | 2376 | // We know there is room on the stack since we are just re-adding |
| 2376 | 2377 | // the StackItem that we previously popped. |
| 2377 | stack.appendAssumeCapacity(StackItem{ | |
| 2378 | stack.appendAssumeCapacity(.{ | |
| 2378 | 2379 | .name = name, |
| 2379 | 2380 | .parent_dir = parent_dir, |
| 2380 | 2381 | .iter = iterable_dir.iterateAssumeFirstIteration(), |
src/resinator/parse.zig+7-4| ... | ... | @@ -1246,13 +1246,16 @@ pub const Parser = struct { |
| 1246 | 1246 | self.nextToken(.normal) catch unreachable; |
| 1247 | 1247 | switch (statement_type) { |
| 1248 | 1248 | .file_version, .product_version => { |
| 1249 | var parts = std.BoundedArray(*Node, 4){}; | |
| 1249 | var parts_buffer: [4]*Node = undefined; | |
| 1250 | var parts = std.ArrayListUnmanaged(*Node).initBuffer(&parts_buffer); | |
| 1250 | 1251 | |
| 1251 | while (parts.len < 4) { | |
| 1252 | while (true) { | |
| 1252 | 1253 | const value = try self.parseExpression(.{ .allowed_types = .{ .number = true } }); |
| 1253 | 1254 | parts.addOneAssumeCapacity().* = value; |
| 1254 | 1255 | |
| 1255 | if (parts.len == 4 or !(try self.parseOptionalToken(.comma))) { | |
| 1256 | if (parts.unusedCapacitySlice().len == 0 or | |
| 1257 | !(try self.parseOptionalToken(.comma))) | |
| 1258 | { | |
| 1256 | 1259 | break; |
| 1257 | 1260 | } |
| 1258 | 1261 | } |
| ... | ... | @@ -1260,7 +1263,7 @@ pub const Parser = struct { |
| 1260 | 1263 | const node = try self.state.arena.create(Node.VersionStatement); |
| 1261 | 1264 | node.* = .{ |
| 1262 | 1265 | .type = type_token, |
| 1263 | .parts = try self.state.arena.dupe(*Node, parts.slice()), | |
| 1266 | .parts = try self.state.arena.dupe(*Node, parts.items), | |
| 1264 | 1267 | }; |
| 1265 | 1268 | return &node.base; |
| 1266 | 1269 | }, |
tools/gen_spirv_spec.zig+11-12| ... | ... | @@ -601,9 +601,8 @@ fn renderFieldName(writer: anytype, operands: []const g.Operand, field_index: us |
| 601 | 601 | const operand = operands[field_index]; |
| 602 | 602 | |
| 603 | 603 | // Should be enough for all names - adjust as needed. |
| 604 | var name_buffer = std.BoundedArray(u8, 64){ | |
| 605 | .buffer = undefined, | |
| 606 | }; | |
| 604 | var name_backing_buffer: [64]u8 = undefined; | |
| 605 | var name_buffer = std.ArrayListUnmanaged(u8).initBuffer(&name_backing_buffer); | |
| 607 | 606 | |
| 608 | 607 | derive_from_kind: { |
| 609 | 608 | // Operand names are often in the json encoded as "'Name'" (with two sets of quotes). |
| ... | ... | @@ -617,33 +616,33 @@ fn renderFieldName(writer: anytype, operands: []const g.Operand, field_index: us |
| 617 | 616 | // Use the same loop to transform to snake-case. |
| 618 | 617 | for (name) |c| { |
| 619 | 618 | switch (c) { |
| 620 | 'a'...'z', '0'...'9' => try name_buffer.append(c), | |
| 621 | 'A'...'Z' => try name_buffer.append(std.ascii.toLower(c)), | |
| 622 | ' ', '~' => try name_buffer.append('_'), | |
| 619 | 'a'...'z', '0'...'9' => name_buffer.appendAssumeCapacity(c), | |
| 620 | 'A'...'Z' => name_buffer.appendAssumeCapacity(std.ascii.toLower(c)), | |
| 621 | ' ', '~' => name_buffer.appendAssumeCapacity('_'), | |
| 623 | 622 | else => break :derive_from_kind, |
| 624 | 623 | } |
| 625 | 624 | } |
| 626 | 625 | |
| 627 | 626 | // Assume there are no duplicate 'name' fields. |
| 628 | try writer.print("{}", .{std.zig.fmtId(name_buffer.slice())}); | |
| 627 | try writer.print("{}", .{std.zig.fmtId(name_buffer.items)}); | |
| 629 | 628 | return; |
| 630 | 629 | } |
| 631 | 630 | |
| 632 | 631 | // Translate to snake case. |
| 633 | name_buffer.len = 0; | |
| 632 | name_buffer.items.len = 0; | |
| 634 | 633 | for (operand.kind, 0..) |c, i| { |
| 635 | 634 | switch (c) { |
| 636 | 'a'...'z', '0'...'9' => try name_buffer.append(c), | |
| 635 | 'a'...'z', '0'...'9' => name_buffer.appendAssumeCapacity(c), | |
| 637 | 636 | 'A'...'Z' => if (i > 0 and std.ascii.isLower(operand.kind[i - 1])) { |
| 638 | try name_buffer.appendSlice(&[_]u8{ '_', std.ascii.toLower(c) }); | |
| 637 | name_buffer.appendSliceAssumeCapacity(&[_]u8{ '_', std.ascii.toLower(c) }); | |
| 639 | 638 | } else { |
| 640 | try name_buffer.append(std.ascii.toLower(c)); | |
| 639 | name_buffer.appendAssumeCapacity(std.ascii.toLower(c)); | |
| 641 | 640 | }, |
| 642 | 641 | else => unreachable, // Assume that the name is valid C-syntax (and contains no underscores). |
| 643 | 642 | } |
| 644 | 643 | } |
| 645 | 644 | |
| 646 | try writer.print("{}", .{std.zig.fmtId(name_buffer.slice())}); | |
| 645 | try writer.print("{}", .{std.zig.fmtId(name_buffer.items)}); | |
| 647 | 646 | |
| 648 | 647 | // For fields derived from type name, there could be any amount. |
| 649 | 648 | // Simply check against all other fields, and if another similar one exists, add a number. |