aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/compiler/aro/aro/LangOpts.zig2
-rw-r--r--lib/compiler/aro/aro/Preprocessor.zig2
-rw-r--r--lib/compiler/aro/aro/Tokenizer.zig2
-rw-r--r--lib/compiler/build_runner.zig22
-rw-r--r--lib/compiler/resinator/errors.zig2
-rw-r--r--lib/compiler/resinator/ico.zig4
-rw-r--r--lib/compiler/resinator/rc.zig91
-rw-r--r--lib/docs/wasm/markdown/Document.zig2
-rw-r--r--lib/std/Build.zig18
-rw-r--r--lib/std/Build/Step/Compile.zig16
-rw-r--r--lib/std/Build/Step/InstallArtifact.zig11
-rw-r--r--lib/std/Build/Step/Options.zig9
-rw-r--r--lib/std/Target/Query.zig11
-rw-r--r--lib/std/Thread.zig6
-rw-r--r--lib/std/Thread/Pool.zig2
-rw-r--r--lib/std/Thread/RwLock.zig4
-rw-r--r--lib/std/c/wasi.zig6
-rw-r--r--lib/std/comptime_string_map.zig320
-rw-r--r--lib/std/crypto/Certificate.zig10
-rw-r--r--lib/std/crypto/ecdsa.zig8
-rw-r--r--lib/std/crypto/ff.zig37
-rw-r--r--lib/std/crypto/sha2.zig45
-rw-r--r--lib/std/fmt.zig116
-rw-r--r--lib/std/fs/Dir.zig65
-rw-r--r--lib/std/fs/test.zig8
-rw-r--r--lib/std/http/Client.zig2
-rw-r--r--lib/std/macho.zig6
-rw-r--r--lib/std/math.zig25
-rw-r--r--lib/std/meta.zig6
-rw-r--r--lib/std/net.zig2
-rw-r--r--lib/std/os/linux.zig2
-rw-r--r--lib/std/os/linux/bpf.zig2
-rw-r--r--lib/std/os/windows.zig103
-rw-r--r--lib/std/posix.zig10
-rw-r--r--lib/std/static_string_map.zig502
-rw-r--r--lib/std/std.zig6
-rw-r--r--lib/std/zig/AstGen.zig14
-rw-r--r--lib/std/zig/BuiltinFn.zig2
-rw-r--r--lib/std/zig/LibCInstallation.zig74
-rw-r--r--lib/std/zig/WindowsSdk.zig430
-rw-r--r--lib/std/zig/primitives.zig2
-rw-r--r--lib/std/zig/render.zig8
-rw-r--r--lib/std/zig/system.zig73
-rw-r--r--lib/std/zig/system/linux.zig27
-rw-r--r--lib/std/zig/tokenizer.zig2
45 files changed, 1300 insertions, 817 deletions
diff --git a/lib/compiler/aro/aro/LangOpts.zig b/lib/compiler/aro/aro/LangOpts.zig
index f2c15c599b..e7b2ebf6c0 100644
--- a/lib/compiler/aro/aro/LangOpts.zig
+++ b/lib/compiler/aro/aro/LangOpts.zig
@@ -47,7 +47,7 @@ pub const Standard = enum {
/// Working Draft for ISO C23 with GNU extensions
gnu23,
- const NameMap = std.ComptimeStringMap(Standard, .{
+ const NameMap = std.StaticStringMap(Standard).initComptime(.{
.{ "c89", .c89 }, .{ "c90", .c89 }, .{ "iso9899:1990", .c89 },
.{ "iso9899:199409", .iso9899 }, .{ "gnu89", .gnu89 }, .{ "gnu90", .gnu89 },
.{ "c99", .c99 }, .{ "iso9899:1999", .c99 }, .{ "c9x", .c99 },
diff --git a/lib/compiler/aro/aro/Preprocessor.zig b/lib/compiler/aro/aro/Preprocessor.zig
index dc7be1ffc5..06193bf3e4 100644
--- a/lib/compiler/aro/aro/Preprocessor.zig
+++ b/lib/compiler/aro/aro/Preprocessor.zig
@@ -1709,7 +1709,7 @@ fn expandFuncMacro(
}
if (!pp.comp.langopts.standard.atLeast(.c23)) break :res not_found;
- const attrs = std.ComptimeStringMap([]const u8, .{
+ const attrs = std.StaticStringMap([]const u8).initComptime(.{
.{ "deprecated", "201904L\n" },
.{ "fallthrough", "201904L\n" },
.{ "maybe_unused", "201904L\n" },
diff --git a/lib/compiler/aro/aro/Tokenizer.zig b/lib/compiler/aro/aro/Tokenizer.zig
index 0f2b2ac4b7..c5a84b8cc0 100644
--- a/lib/compiler/aro/aro/Tokenizer.zig
+++ b/lib/compiler/aro/aro/Tokenizer.zig
@@ -872,7 +872,7 @@ pub const Token = struct {
};
}
- const all_kws = std.ComptimeStringMap(Id, .{
+ const all_kws = std.StaticStringMap(Id).initComptime(.{
.{ "auto", auto: {
@setEvalBranchQuota(3000);
break :auto .keyword_auto;
diff --git a/lib/compiler/build_runner.zig b/lib/compiler/build_runner.zig
index eb5f52ab44..01dc437208 100644
--- a/lib/compiler/build_runner.zig
+++ b/lib/compiler/build_runner.zig
@@ -70,6 +70,10 @@ pub fn main() !void {
.zig_exe = zig_exe,
.env_map = try process.getEnvMap(arena),
.global_cache_root = global_cache_directory,
+ .host = .{
+ .query = .{},
+ .result = try std.zig.system.resolveTargetQuery(.{}),
+ },
};
graph.cache.addPrefix(.{ .path = null, .handle = std.fs.cwd() });
@@ -142,12 +146,6 @@ pub fn main() !void {
arg, text,
});
};
- } else if (mem.eql(u8, arg, "--host-target")) {
- graph.host_query_options.arch_os_abi = nextArgOrFatal(args, &arg_idx);
- } else if (mem.eql(u8, arg, "--host-cpu")) {
- graph.host_query_options.cpu_features = nextArgOrFatal(args, &arg_idx);
- } else if (mem.eql(u8, arg, "--host-dynamic-linker")) {
- graph.host_query_options.dynamic_linker = nextArgOrFatal(args, &arg_idx);
} else if (mem.eql(u8, arg, "--prefix-lib-dir")) {
dir_list.lib_dir = nextArgOrFatal(args, &arg_idx);
} else if (mem.eql(u8, arg, "--prefix-exe-dir")) {
@@ -283,14 +281,6 @@ pub fn main() !void {
}
}
- const host_query = std.Build.parseTargetQuery(graph.host_query_options) catch |err| switch (err) {
- error.ParseFailed => process.exit(1),
- };
- builder.host = .{
- .query = .{},
- .result = try std.zig.system.resolveTargetQuery(host_query),
- };
-
const stderr = std.io.getStdErr();
const ttyconf = get_tty_conf(color, stderr);
switch (ttyconf) {
@@ -1171,10 +1161,6 @@ fn usage(b: *std.Build, out_stream: anytype) !void {
\\ --sysroot [path] Set the system root directory (usually /)
\\ --libc [file] Provide a file which specifies libc paths
\\
- \\ --host-target [triple] Use the provided target as the host
- \\ --host-cpu [cpu] Use the provided CPU as the host
- \\ --host-dynamic-linker [path] Use the provided dynamic linker as the host
- \\
\\ --system [pkgdir] Disable package fetching; enable all integrations
\\ -fsys=[name] Enable a system integration
\\ -fno-sys=[name] Disable a system integration
diff --git a/lib/compiler/resinator/errors.zig b/lib/compiler/resinator/errors.zig
index 44a9d46163..909824c594 100644
--- a/lib/compiler/resinator/errors.zig
+++ b/lib/compiler/resinator/errors.zig
@@ -240,7 +240,7 @@ pub const ErrorDetails = struct {
// see https://github.com/ziglang/zig/issues/15395
_: u26 = 0,
- pub const strings = std.ComptimeStringMap([]const u8, .{
+ pub const strings = std.StaticStringMap([]const u8).initComptime(.{
.{ "number", "number" },
.{ "number_expression", "number expression" },
.{ "string_literal", "quoted string literal" },
diff --git a/lib/compiler/resinator/ico.zig b/lib/compiler/resinator/ico.zig
index 310db6ece2..664def038b 100644
--- a/lib/compiler/resinator/ico.zig
+++ b/lib/compiler/resinator/ico.zig
@@ -232,7 +232,7 @@ test "icon data size too small" {
try std.testing.expectError(error.ImpossibleDataSize, read(std.testing.allocator, fbs.reader(), data.len));
}
-pub const ImageFormat = enum {
+pub const ImageFormat = enum(u2) {
dib,
png,
riff,
@@ -272,7 +272,7 @@ pub const BitmapHeader = extern struct {
}
/// https://en.wikipedia.org/wiki/BMP_file_format#DIB_header_(bitmap_information_header)
- pub const Version = enum {
+ pub const Version = enum(u3) {
unknown,
@"win2.0", // Windows 2.0 or later
@"nt3.1", // Windows NT, 3.1x or later
diff --git a/lib/compiler/resinator/rc.zig b/lib/compiler/resinator/rc.zig
index 00cb455058..a434e26c80 100644
--- a/lib/compiler/resinator/rc.zig
+++ b/lib/compiler/resinator/rc.zig
@@ -47,7 +47,10 @@ pub const Resource = enum {
fontdir_num,
manifest_num,
- const map = std.ComptimeStringMapWithEql(Resource, .{
+ const map = std.StaticStringMapWithEql(
+ Resource,
+ std.static_string_map.eqlAsciiIgnoreCase,
+ ).initComptime(.{
.{ "ACCELERATORS", .accelerators },
.{ "BITMAP", .bitmap },
.{ "CURSOR", .cursor },
@@ -67,7 +70,7 @@ pub const Resource = enum {
.{ "TOOLBAR", .toolbar },
.{ "VERSIONINFO", .versioninfo },
.{ "VXD", .vxd },
- }, std.comptime_string_map.eqlAsciiIgnoreCase);
+ });
pub fn fromString(bytes: SourceBytes) Resource {
const maybe_ordinal = res.NameOrOrdinal.maybeOrdinalFromString(bytes);
@@ -157,20 +160,26 @@ pub const OptionalStatements = enum {
menu,
style,
- pub const map = std.ComptimeStringMapWithEql(OptionalStatements, .{
+ pub const map = std.StaticStringMapWithEql(
+ OptionalStatements,
+ std.static_string_map.eqlAsciiIgnoreCase,
+ ).initComptime(.{
.{ "CHARACTERISTICS", .characteristics },
.{ "LANGUAGE", .language },
.{ "VERSION", .version },
- }, std.comptime_string_map.eqlAsciiIgnoreCase);
+ });
- pub const dialog_map = std.ComptimeStringMapWithEql(OptionalStatements, .{
+ pub const dialog_map = std.StaticStringMapWithEql(
+ OptionalStatements,
+ std.static_string_map.eqlAsciiIgnoreCase,
+ ).initComptime(.{
.{ "CAPTION", .caption },
.{ "CLASS", .class },
.{ "EXSTYLE", .exstyle },
.{ "FONT", .font },
.{ "MENU", .menu },
.{ "STYLE", .style },
- }, std.comptime_string_map.eqlAsciiIgnoreCase);
+ });
};
pub const Control = enum {
@@ -197,7 +206,10 @@ pub const Control = enum {
state3,
userbutton,
- pub const map = std.ComptimeStringMapWithEql(Control, .{
+ pub const map = std.StaticStringMapWithEql(
+ Control,
+ std.static_string_map.eqlAsciiIgnoreCase,
+ ).initComptime(.{
.{ "AUTO3STATE", .auto3state },
.{ "AUTOCHECKBOX", .autocheckbox },
.{ "AUTORADIOBUTTON", .autoradiobutton },
@@ -220,7 +232,7 @@ pub const Control = enum {
.{ "SCROLLBAR", .scrollbar },
.{ "STATE3", .state3 },
.{ "USERBUTTON", .userbutton },
- }, std.comptime_string_map.eqlAsciiIgnoreCase);
+ });
pub fn hasTextParam(control: Control) bool {
switch (control) {
@@ -231,14 +243,17 @@ pub const Control = enum {
};
pub const ControlClass = struct {
- pub const map = std.ComptimeStringMapWithEql(res.ControlClass, .{
+ pub const map = std.StaticStringMapWithEql(
+ res.ControlClass,
+ std.static_string_map.eqlAsciiIgnoreCase,
+ ).initComptime(.{
.{ "BUTTON", .button },
.{ "EDIT", .edit },
.{ "STATIC", .static },
.{ "LISTBOX", .listbox },
.{ "SCROLLBAR", .scrollbar },
.{ "COMBOBOX", .combobox },
- }, std.comptime_string_map.eqlAsciiIgnoreCase);
+ });
/// Like `map.get` but works on WTF16 strings, for use with parsed
/// string literals ("BUTTON", or even "\x42UTTON")
@@ -280,10 +295,13 @@ pub const MenuItem = enum {
menuitem,
popup,
- pub const map = std.ComptimeStringMapWithEql(MenuItem, .{
+ pub const map = std.StaticStringMapWithEql(
+ MenuItem,
+ std.static_string_map.eqlAsciiIgnoreCase,
+ ).initComptime(.{
.{ "MENUITEM", .menuitem },
.{ "POPUP", .popup },
- }, std.comptime_string_map.eqlAsciiIgnoreCase);
+ });
pub fn isSeparator(bytes: []const u8) bool {
return std.ascii.eqlIgnoreCase(bytes, "SEPARATOR");
@@ -297,14 +315,17 @@ pub const MenuItem = enum {
menubarbreak,
menubreak,
- pub const map = std.ComptimeStringMapWithEql(Option, .{
+ pub const map = std.StaticStringMapWithEql(
+ Option,
+ std.static_string_map.eqlAsciiIgnoreCase,
+ ).initComptime(.{
.{ "CHECKED", .checked },
.{ "GRAYED", .grayed },
.{ "HELP", .help },
.{ "INACTIVE", .inactive },
.{ "MENUBARBREAK", .menubarbreak },
.{ "MENUBREAK", .menubreak },
- }, std.comptime_string_map.eqlAsciiIgnoreCase);
+ });
};
};
@@ -312,10 +333,13 @@ pub const ToolbarButton = enum {
button,
separator,
- pub const map = std.ComptimeStringMapWithEql(ToolbarButton, .{
+ pub const map = std.StaticStringMapWithEql(
+ ToolbarButton,
+ std.static_string_map.eqlAsciiIgnoreCase,
+ ).initComptime(.{
.{ "BUTTON", .button },
.{ "SEPARATOR", .separator },
- }, std.comptime_string_map.eqlAsciiIgnoreCase);
+ });
};
pub const VersionInfo = enum {
@@ -327,7 +351,10 @@ pub const VersionInfo = enum {
file_type,
file_subtype,
- pub const map = std.ComptimeStringMapWithEql(VersionInfo, .{
+ pub const map = std.StaticStringMapWithEql(
+ VersionInfo,
+ std.static_string_map.eqlAsciiIgnoreCase,
+ ).initComptime(.{
.{ "FILEVERSION", .file_version },
.{ "PRODUCTVERSION", .product_version },
.{ "FILEFLAGSMASK", .file_flags_mask },
@@ -335,17 +362,20 @@ pub const VersionInfo = enum {
.{ "FILEOS", .file_os },
.{ "FILETYPE", .file_type },
.{ "FILESUBTYPE", .file_subtype },
- }, std.comptime_string_map.eqlAsciiIgnoreCase);
+ });
};
pub const VersionBlock = enum {
block,
value,
- pub const map = std.ComptimeStringMapWithEql(VersionBlock, .{
+ pub const map = std.StaticStringMapWithEql(
+ VersionBlock,
+ std.static_string_map.eqlAsciiIgnoreCase,
+ ).initComptime(.{
.{ "BLOCK", .block },
.{ "VALUE", .value },
- }, std.comptime_string_map.eqlAsciiIgnoreCase);
+ });
};
/// Keywords that are be the first token in a statement and (if so) dictate how the rest
@@ -356,12 +386,15 @@ pub const TopLevelKeywords = enum {
characteristics,
stringtable,
- pub const map = std.ComptimeStringMapWithEql(TopLevelKeywords, .{
+ pub const map = std.StaticStringMapWithEql(
+ TopLevelKeywords,
+ std.static_string_map.eqlAsciiIgnoreCase,
+ ).initComptime(.{
.{ "LANGUAGE", .language },
.{ "VERSION", .version },
.{ "CHARACTERISTICS", .characteristics },
.{ "STRINGTABLE", .stringtable },
- }, std.comptime_string_map.eqlAsciiIgnoreCase);
+ });
};
pub const CommonResourceAttributes = enum {
@@ -375,7 +408,10 @@ pub const CommonResourceAttributes = enum {
shared,
nonshared,
- pub const map = std.ComptimeStringMapWithEql(CommonResourceAttributes, .{
+ pub const map = std.StaticStringMapWithEql(
+ CommonResourceAttributes,
+ std.static_string_map.eqlAsciiIgnoreCase,
+ ).initComptime(.{
.{ "PRELOAD", .preload },
.{ "LOADONCALL", .loadoncall },
.{ "FIXED", .fixed },
@@ -385,7 +421,7 @@ pub const CommonResourceAttributes = enum {
.{ "IMPURE", .impure },
.{ "SHARED", .shared },
.{ "NONSHARED", .nonshared },
- }, std.comptime_string_map.eqlAsciiIgnoreCase);
+ });
};
pub const AcceleratorTypeAndOptions = enum {
@@ -396,12 +432,15 @@ pub const AcceleratorTypeAndOptions = enum {
shift,
control,
- pub const map = std.ComptimeStringMapWithEql(AcceleratorTypeAndOptions, .{
+ pub const map = std.StaticStringMapWithEql(
+ AcceleratorTypeAndOptions,
+ std.static_string_map.eqlAsciiIgnoreCase,
+ ).initComptime(.{
.{ "VIRTKEY", .virtkey },
.{ "ASCII", .ascii },
.{ "NOINVERT", .noinvert },
.{ "ALT", .alt },
.{ "SHIFT", .shift },
.{ "CONTROL", .control },
- }, std.comptime_string_map.eqlAsciiIgnoreCase);
+ });
};
diff --git a/lib/docs/wasm/markdown/Document.zig b/lib/docs/wasm/markdown/Document.zig
index f3c0fdeed0..8eec97415c 100644
--- a/lib/docs/wasm/markdown/Document.zig
+++ b/lib/docs/wasm/markdown/Document.zig
@@ -131,7 +131,7 @@ pub const Node = struct {
}
};
- pub const TableCellAlignment = enum {
+ pub const TableCellAlignment = enum(u2) {
unset,
left,
center,
diff --git a/lib/std/Build.zig b/lib/std/Build.zig
index a3d091858a..5516a55556 100644
--- a/lib/std/Build.zig
+++ b/lib/std/Build.zig
@@ -82,7 +82,7 @@ enable_wine: bool = false,
/// that contains the path `aarch64-linux-gnu/lib/ld-linux-aarch64.so.1`.
glibc_runtimes_dir: ?[]const u8 = null,
-/// Information about the native target. Computed before build() is invoked.
+/// Deprecated. Use `b.graph.host`.
host: ResolvedTarget,
dep_prefix: []const u8 = "",
@@ -118,8 +118,9 @@ pub const Graph = struct {
zig_exe: [:0]const u8,
env_map: EnvMap,
global_cache_root: Cache.Directory,
- host_query_options: std.Target.Query.ParseOptions = .{},
needed_lazy_dependencies: std.StringArrayHashMapUnmanaged(void) = .{},
+ /// Information about the native target. Computed before build() is invoked.
+ host: ResolvedTarget,
};
const AvailableDeps = []const struct { []const u8, []const u8 };
@@ -297,7 +298,7 @@ pub fn create(
.zig_lib_dir = null,
.install_path = undefined,
.args = null,
- .host = undefined,
+ .host = graph.host,
.modules = std.StringArrayHashMap(*Module).init(arena),
.named_writefiles = std.StringArrayHashMap(*Step.WriteFile).init(arena),
.initialized_deps = initialized_deps,
@@ -1974,7 +1975,7 @@ pub fn dependencyFromBuildZig(
const dep_name = for (b.available_deps) |dep| {
if (mem.eql(u8, dep[1], pkg_hash)) break dep[1];
} else break :find_dep;
- return dependencyInner(b, dep_name, pkg.build_root, pkg.build_zig, pkg.deps, args);
+ return dependencyInner(b, dep_name, pkg.build_root, pkg.build_zig, pkg_hash, pkg.deps, args);
}
const full_path = b.pathFromRoot("build.zig.zon");
@@ -2489,14 +2490,9 @@ pub const ResolvedTarget = struct {
/// various parts of the API.
pub fn resolveTargetQuery(b: *Build, query: Target.Query) ResolvedTarget {
if (query.isNative()) {
- var adjusted = b.host;
- if (query.ofmt) |ofmt| {
- adjusted.query.ofmt = ofmt;
- adjusted.result.ofmt = ofmt;
- }
- return adjusted;
+ // Hot path. This is faster than querying the native CPU and OS again.
+ return b.graph.host;
}
-
return .{
.query = query,
.result = std.zig.system.resolveTargetQuery(query) catch
diff --git a/lib/std/Build/Step/Compile.zig b/lib/std/Build/Step/Compile.zig
index c36fd8c4ca..79bade4963 100644
--- a/lib/std/Build/Step/Compile.zig
+++ b/lib/std/Build/Step/Compile.zig
@@ -614,6 +614,10 @@ pub fn isStaticLibrary(self: *const Compile) bool {
return self.kind == .lib and self.linkage != .dynamic;
}
+pub fn isDll(self: *Compile) bool {
+ return self.isDynamicLibrary() and self.rootModuleTarget().os.tag == .windows;
+}
+
pub fn producesPdbFile(self: *Compile) bool {
const target = self.rootModuleTarget();
// TODO: Is this right? Isn't PDB for *any* PE/COFF file?
@@ -632,7 +636,7 @@ pub fn producesPdbFile(self: *Compile) bool {
}
pub fn producesImplib(self: *Compile) bool {
- return self.isDynamicLibrary() and self.rootModuleTarget().os.tag == .windows;
+ return self.isDll();
}
pub fn linkLibC(self: *Compile) void {
@@ -1011,16 +1015,6 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void {
};
try zig_args.append(cmd);
- if (!mem.eql(u8, b.graph.host_query_options.arch_os_abi, "native")) {
- try zig_args.appendSlice(&.{ "--host-target", b.graph.host_query_options.arch_os_abi });
- }
- if (b.graph.host_query_options.cpu_features) |cpu| {
- try zig_args.appendSlice(&.{ "--host-cpu", cpu });
- }
- if (b.graph.host_query_options.dynamic_linker) |dl| {
- try zig_args.appendSlice(&.{ "--host-dynamic-linker", dl });
- }
-
if (b.reference_trace) |some| {
try zig_args.append(try std.fmt.allocPrint(arena, "-freference-trace={d}", .{some}));
}
diff --git a/lib/std/Build/Step/InstallArtifact.zig b/lib/std/Build/Step/InstallArtifact.zig
index 7ebe8fdaf0..d9f163fce5 100644
--- a/lib/std/Build/Step/InstallArtifact.zig
+++ b/lib/std/Build/Step/InstallArtifact.zig
@@ -57,8 +57,8 @@ pub fn create(owner: *std.Build, artifact: *Step.Compile, options: Options) *Ins
.disabled => null,
.default => switch (artifact.kind) {
.obj => @panic("object files have no standard installation procedure"),
- .exe, .@"test" => InstallDir{ .bin = {} },
- .lib => InstallDir{ .lib = {} },
+ .exe, .@"test" => .bin,
+ .lib => if (artifact.isDll()) .bin else .lib,
},
.override => |o| o,
};
@@ -77,15 +77,12 @@ pub fn create(owner: *std.Build, artifact: *Step.Compile, options: Options) *Ins
},
.h_dir = switch (options.h_dir) {
.disabled => null,
- .default => switch (artifact.kind) {
- .lib => .header,
- else => null,
- },
+ .default => if (artifact.kind == .lib) .header else null,
.override => |o| o,
},
.implib_dir = switch (options.implib_dir) {
.disabled => null,
- .default => if (artifact.producesImplib()) dest_dir else null,
+ .default => if (artifact.producesImplib()) .lib else null,
.override => |o| o,
},
diff --git a/lib/std/Build/Step/Options.zig b/lib/std/Build/Step/Options.zig
index 178e2c3b96..56682b61be 100644
--- a/lib/std/Build/Step/Options.zig
+++ b/lib/std/Build/Step/Options.zig
@@ -516,6 +516,10 @@ test Options {
.zig_exe = "test",
.env_map = std.process.EnvMap.init(arena.allocator()),
.global_cache_root = .{ .path = "test", .handle = std.fs.cwd() },
+ .host = .{
+ .query = .{},
+ .result = try std.zig.system.resolveTargetQuery(.{}),
+ },
};
var builder = try std.Build.create(
@@ -525,11 +529,6 @@ test Options {
&.{},
);
- builder.host = .{
- .query = .{},
- .result = try std.zig.system.resolveTargetQuery(.{}),
- };
-
const options = builder.addOptions();
const KeywordEnum = enum {
diff --git a/lib/std/Target/Query.zig b/lib/std/Target/Query.zig
index a924c62964..8871e360a5 100644
--- a/lib/std/Target/Query.zig
+++ b/lib/std/Target/Query.zig
@@ -362,12 +362,16 @@ pub fn isNativeAbi(self: Query) bool {
return self.os_tag == null and self.abi == null;
}
-pub fn isNative(self: Query) bool {
+pub fn isNativeTriple(self: Query) bool {
return self.isNativeCpu() and self.isNativeOs() and self.isNativeAbi();
}
+pub fn isNative(self: Query) bool {
+ return self.isNativeTriple() and self.ofmt == null;
+}
+
pub fn canDetectLibC(self: Query) bool {
- if (self.isNative()) return true;
+ if (self.isNativeOs()) return true;
if (self.os_tag) |os| {
if (builtin.os.tag == .macos and os.isDarwin()) return true;
if (os == .linux and self.abi == .android) return true;
@@ -386,9 +390,8 @@ fn formatVersion(version: SemanticVersion, writer: anytype) !void {
}
pub fn zigTriple(self: Query, allocator: Allocator) Allocator.Error![]u8 {
- if (self.isNative()) {
+ if (self.isNativeTriple())
return allocator.dupe(u8, "native");
- }
const arch_name = if (self.cpu_arch) |arch| @tagName(arch) else "native";
const os_name = if (self.os_tag) |os_tag| @tagName(os_tag) else "native";
diff --git a/lib/std/Thread.zig b/lib/std/Thread.zig
index d1c8a24f0a..503f646dc4 100644
--- a/lib/std/Thread.zig
+++ b/lib/std/Thread.zig
@@ -844,19 +844,19 @@ const WasiThreadImpl = struct {
const bad_fn_ret = "expected return type of startFn to be 'u8', 'noreturn', 'void', or '!void'";
switch (@typeInfo(@typeInfo(@TypeOf(f)).Fn.return_type.?)) {
.NoReturn, .Void => {
- @call(.auto, w, args);
+ @call(.auto, f, w.args);
},
.Int => |info| {
if (info.bits != 8) {
@compileError(bad_fn_ret);
}
- _ = @call(.auto, w, args); // WASI threads don't support exit status, ignore value
+ _ = @call(.auto, f, w.args); // WASI threads don't support exit status, ignore value
},
.ErrorUnion => |info| {
if (info.payload != void) {
@compileError(bad_fn_ret);
}
- @call(.auto, f, args) catch |err| {
+ @call(.auto, f, w.args) catch |err| {
std.debug.print("error: {s}\n", .{@errorName(err)});
if (@errorReturnTrace()) |trace| {
std.debug.dumpStackTrace(trace.*);
diff --git a/lib/std/Thread/Pool.zig b/lib/std/Thread/Pool.zig
index a96b4255e2..516f06da0e 100644
--- a/lib/std/Thread/Pool.zig
+++ b/lib/std/Thread/Pool.zig
@@ -89,7 +89,7 @@ pub fn spawn(pool: *Pool, comptime func: anytype, args: anytype) !void {
fn runFn(runnable: *Runnable) void {
const run_node: *RunQueue.Node = @fieldParentPtr("data", runnable);
- const closure: *@This() = @fieldParentPtr("run_node", run_node);
+ const closure: *@This() = @alignCast(@fieldParentPtr("run_node", run_node));
@call(.auto, func, closure.arguments);
// The thread pool's allocator is protected by the mutex.
diff --git a/lib/std/Thread/RwLock.zig b/lib/std/Thread/RwLock.zig
index c4a8755907..0e8da6239f 100644
--- a/lib/std/Thread/RwLock.zig
+++ b/lib/std/Thread/RwLock.zig
@@ -41,7 +41,9 @@ pub fn tryLockShared(rwl: *RwLock) bool {
return rwl.impl.tryLockShared();
}
-/// Blocks until shared lock ownership is acquired.
+/// Obtains shared lock ownership.
+/// Blocks if another thread has exclusive ownership.
+/// May block if another thread is attempting to get exclusive ownership.
pub fn lockShared(rwl: *RwLock) void {
return rwl.impl.lockShared();
}
diff --git a/lib/std/c/wasi.zig b/lib/std/c/wasi.zig
index c7533dce92..56f1e77805 100644
--- a/lib/std/c/wasi.zig
+++ b/lib/std/c/wasi.zig
@@ -40,12 +40,6 @@ pub const E = wasi.errno_t;
pub const CLOCK = wasi.clockid_t;
pub const IOV_MAX = 1024;
-pub const LOCK = struct {
- pub const SH = 0x1;
- pub const EX = 0x2;
- pub const NB = 0x4;
- pub const UN = 0x8;
-};
pub const S = struct {
pub const IEXEC = @compileError("TODO audit this");
pub const IFBLK = 0x6000;
diff --git a/lib/std/comptime_string_map.zig b/lib/std/comptime_string_map.zig
deleted file mode 100644
index 2bfcad1e77..0000000000
--- a/lib/std/comptime_string_map.zig
+++ /dev/null
@@ -1,320 +0,0 @@
-const std = @import("std.zig");
-const mem = std.mem;
-
-/// Comptime string map optimized for small sets of disparate string keys.
-/// Works by separating the keys by length at comptime and only checking strings of
-/// equal length at runtime.
-///
-/// `kvs_list` expects a list of `struct { []const u8, V }` (key-value pair) tuples.
-/// You can pass `struct { []const u8 }` (only keys) tuples if `V` is `void`.
-pub fn ComptimeStringMap(
- comptime V: type,
- comptime kvs_list: anytype,
-) type {
- return ComptimeStringMapWithEql(V, kvs_list, defaultEql);
-}
-
-/// Like `std.mem.eql`, but takes advantage of the fact that the lengths
-/// of `a` and `b` are known to be equal.
-pub fn defaultEql(a: []const u8, b: []const u8) bool {
- if (a.ptr == b.ptr) return true;
- for (a, b) |a_elem, b_elem| {
- if (a_elem != b_elem) return false;
- }
- return true;
-}
-
-/// Like `std.ascii.eqlIgnoreCase` but takes advantage of the fact that
-/// the lengths of `a` and `b` are known to be equal.
-pub fn eqlAsciiIgnoreCase(a: []const u8, b: []const u8) bool {
- if (a.ptr == b.ptr) return true;
- for (a, b) |a_c, b_c| {
- if (std.ascii.toLower(a_c) != std.ascii.toLower(b_c)) return false;
- }
- return true;
-}
-
-/// ComptimeStringMap, but accepts an equality function (`eql`).
-/// The `eql` function is only called to determine the equality
-/// of equal length strings. Any strings that are not equal length
-/// are never compared using the `eql` function.
-pub fn ComptimeStringMapWithEql(
- comptime V: type,
- comptime kvs_list: anytype,
- comptime eql: fn (a: []const u8, b: []const u8) bool,
-) type {
- const empty_list = kvs_list.len == 0;
- const precomputed = blk: {
- @setEvalBranchQuota(1500);
- const KV = struct {
- key: []const u8,
- value: V,
- };
- if (empty_list)
- break :blk .{};
- var sorted_kvs: [kvs_list.len]KV = undefined;
- for (kvs_list, 0..) |kv, i| {
- if (V != void) {
- sorted_kvs[i] = .{ .key = kv.@"0", .value = kv.@"1" };
- } else {
- sorted_kvs[i] = .{ .key = kv.@"0", .value = {} };
- }
- }
-
- const SortContext = struct {
- kvs: []KV,
-
- pub fn lessThan(ctx: @This(), a: usize, b: usize) bool {
- return ctx.kvs[a].key.len < ctx.kvs[b].key.len;
- }
-
- pub fn swap(ctx: @This(), a: usize, b: usize) void {
- return std.mem.swap(KV, &ctx.kvs[a], &ctx.kvs[b]);
- }
- };
- mem.sortUnstableContext(0, sorted_kvs.len, SortContext{ .kvs = &sorted_kvs });
-
- const min_len = sorted_kvs[0].key.len;
- const max_len = sorted_kvs[sorted_kvs.len - 1].key.len;
- var len_indexes: [max_len + 1]usize = undefined;
- var len: usize = 0;
- var i: usize = 0;
- while (len <= max_len) : (len += 1) {
- // find the first keyword len == len
- while (len > sorted_kvs[i].key.len) {
- i += 1;
- }
- len_indexes[len] = i;
- }
- break :blk .{
- .min_len = min_len,
- .max_len = max_len,
- .sorted_kvs = sorted_kvs,
- .len_indexes = len_indexes,
- };
- };
-
- return struct {
- /// Array of `struct { key: []const u8, value: V }` where `value` is `void{}` if `V` is `void`.
- /// Sorted by `key` length.
- pub const kvs = precomputed.sorted_kvs;
-
- /// Checks if the map has a value for the key.
- pub fn has(str: []const u8) bool {
- return get(str) != null;
- }
-
- /// Returns the value for the key if any, else null.
- pub fn get(str: []const u8) ?V {
- if (empty_list)
- return null;
-
- return precomputed.sorted_kvs[getIndex(str) orelse return null].value;
- }
-
- pub fn getIndex(str: []const u8) ?usize {
- if (empty_list)
- return null;
-
- if (str.len < precomputed.min_len or str.len > precomputed.max_len)
- return null;
-
- var i = precomputed.len_indexes[str.len];
- while (true) {
- const kv = precomputed.sorted_kvs[i];
- if (kv.key.len != str.len)
- return null;
- if (eql(kv.key, str))
- return i;
- i += 1;
- if (i >= precomputed.sorted_kvs.len)
- return null;
- }
- }
- };
-}
-
-const TestEnum = enum {
- A,
- B,
- C,
- D,
- E,
-};
-
-test "list literal of list literals" {
- const map = ComptimeStringMap(TestEnum, .{
- .{ "these", .D },
- .{ "have", .A },
- .{ "nothing", .B },
- .{ "incommon", .C },
- .{ "samelen", .E },
- });
-
- try testMap(map);
-
- // Default comparison is case sensitive
- try std.testing.expect(null == map.get("NOTHING"));
-}
-
-test "array of structs" {
- const KV = struct { []const u8, TestEnum };
- const map = ComptimeStringMap(TestEnum, [_]KV{
- .{ "these", .D },
- .{ "have", .A },
- .{ "nothing", .B },
- .{ "incommon", .C },
- .{ "samelen", .E },
- });
-
- try testMap(map);
-}
-
-test "slice of structs" {
- const KV = struct { []const u8, TestEnum };
- const slice: []const KV = &[_]KV{
- .{ "these", .D },
- .{ "have", .A },
- .{ "nothing", .B },
- .{ "incommon", .C },
- .{ "samelen", .E },
- };
- const map = ComptimeStringMap(TestEnum, slice);
-
- try testMap(map);
-}
-
-fn testMap(comptime map: anytype) !void {
- try std.testing.expectEqual(TestEnum.A, map.get("have").?);
- try std.testing.expectEqual(TestEnum.B, map.get("nothing").?);
- try std.testing.expect(null == map.get("missing"));
- try std.testing.expectEqual(TestEnum.D, map.get("these").?);
- try std.testing.expectEqual(TestEnum.E, map.get("samelen").?);
-
- try std.testing.expect(!map.has("missing"));
- try std.testing.expect(map.has("these"));
-
- try std.testing.expect(null == map.get(""));
- try std.testing.expect(null == map.get("averylongstringthathasnomatches"));
-}
-
-test "void value type, slice of structs" {
- const KV = struct { []const u8 };
- const slice: []const KV = &[_]KV{
- .{"these"},
- .{"have"},
- .{"nothing"},
- .{"incommon"},
- .{"samelen"},
- };
- const map = ComptimeStringMap(void, slice);
-
- try testSet(map);
-
- // Default comparison is case sensitive
- try std.testing.expect(null == map.get("NOTHING"));
-}
-
-test "void value type, list literal of list literals" {
- const map = ComptimeStringMap(void, .{
- .{"these"},
- .{"have"},
- .{"nothing"},
- .{"incommon"},
- .{"samelen"},
- });
-
- try testSet(map);
-}
-
-fn testSet(comptime map: anytype) !void {
- try std.testing.expectEqual({}, map.get("have").?);
- try std.testing.expectEqual({}, map.get("nothing").?);
- try std.testing.expect(null == map.get("missing"));
- try std.testing.expectEqual({}, map.get("these").?);
- try std.testing.expectEqual({}, map.get("samelen").?);
-
- try std.testing.expect(!map.has("missing"));
- try std.testing.expect(map.has("these"));
-
- try std.testing.expect(null == map.get(""));
- try std.testing.expect(null == map.get("averylongstringthathasnomatches"));
-}
-
-test "ComptimeStringMapWithEql" {
- const map = ComptimeStringMapWithEql(TestEnum, .{
- .{ "these", .D },
- .{ "have", .A },
- .{ "nothing", .B },
- .{ "incommon", .C },
- .{ "samelen", .E },
- }, eqlAsciiIgnoreCase);
-
- try testMap(map);
- try std.testing.expectEqual(TestEnum.A, map.get("HAVE").?);
- try std.testing.expectEqual(TestEnum.E, map.get("SameLen").?);
- try std.testing.expect(null == map.get("SameLength"));
-
- try std.testing.expect(map.has("ThESe"));
-}
-
-test "empty" {
- const m1 = ComptimeStringMap(usize, .{});
- try std.testing.expect(null == m1.get("anything"));
-
- const m2 = ComptimeStringMapWithEql(usize, .{}, eqlAsciiIgnoreCase);
- try std.testing.expect(null == m2.get("anything"));
-}
-
-test "redundant entries" {
- const map = ComptimeStringMap(TestEnum, .{
- .{ "redundant", .D },
- .{ "theNeedle", .A },
- .{ "redundant", .B },
- .{ "re" ++ "dundant", .C },
- .{ "redun" ++ "dant", .E },
- });
-
- // No promises about which one you get:
- try std.testing.expect(null != map.get("redundant"));
-
- // Default map is not case sensitive:
- try std.testing.expect(null == map.get("REDUNDANT"));
-
- try std.testing.expectEqual(TestEnum.A, map.get("theNeedle").?);
-}
-
-test "redundant insensitive" {
- const map = ComptimeStringMapWithEql(TestEnum, .{
- .{ "redundant", .D },
- .{ "theNeedle", .A },
- .{ "redundanT", .B },
- .{ "RE" ++ "dundant", .C },
- .{ "redun" ++ "DANT", .E },
- }, eqlAsciiIgnoreCase);
-
- // No promises about which result you'll get ...
- try std.testing.expect(null != map.get("REDUNDANT"));
- try std.testing.expect(null != map.get("ReDuNdAnT"));
-
- try std.testing.expectEqual(TestEnum.A, map.get("theNeedle").?);
-}
-
-test "comptime-only value" {
- const map = std.ComptimeStringMap(type, .{
- .{ "a", struct {
- pub const foo = 1;
- } },
- .{ "b", struct {
- pub const foo = 2;
- } },
- .{ "c", struct {
- pub const foo = 3;
- } },
- });
-
- try std.testing.expect(map.get("a").?.foo == 1);
- try std.testing.expect(map.get("b").?.foo == 2);
- try std.testing.expect(map.get("c").?.foo == 3);
- try std.testing.expect(map.get("d") == null);
-}
diff --git a/lib/std/crypto/Certificate.zig b/lib/std/crypto/Certificate.zig
index b1cc4fc095..1e3bb9ca0b 100644
--- a/lib/std/crypto/Certificate.zig
+++ b/lib/std/crypto/Certificate.zig
@@ -19,7 +19,7 @@ pub const Algorithm = enum {
md5WithRSAEncryption,
curveEd25519,
- pub const map = std.ComptimeStringMap(Algorithm, .{
+ pub const map = std.StaticStringMap(Algorithm).initComptime(.{
.{ &[_]u8{ 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x05 }, .sha1WithRSAEncryption },
.{ &[_]u8{ 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x0B }, .sha256WithRSAEncryption },
.{ &[_]u8{ 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x0C }, .sha384WithRSAEncryption },
@@ -52,7 +52,7 @@ pub const AlgorithmCategory = enum {
X9_62_id_ecPublicKey,
curveEd25519,
- pub const map = std.ComptimeStringMap(AlgorithmCategory, .{
+ pub const map = std.StaticStringMap(AlgorithmCategory).initComptime(.{
.{ &[_]u8{ 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x01 }, .rsaEncryption },
.{ &[_]u8{ 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x02, 0x01 }, .X9_62_id_ecPublicKey },
.{ &[_]u8{ 0x2B, 0x65, 0x70 }, .curveEd25519 },
@@ -73,7 +73,7 @@ pub const Attribute = enum {
pkcs9_emailAddress,
domainComponent,
- pub const map = std.ComptimeStringMap(Attribute, .{
+ pub const map = std.StaticStringMap(Attribute).initComptime(.{
.{ &[_]u8{ 0x55, 0x04, 0x03 }, .commonName },
.{ &[_]u8{ 0x55, 0x04, 0x05 }, .serialNumber },
.{ &[_]u8{ 0x55, 0x04, 0x06 }, .countryName },
@@ -94,7 +94,7 @@ pub const NamedCurve = enum {
secp521r1,
X9_62_prime256v1,
- pub const map = std.ComptimeStringMap(NamedCurve, .{
+ pub const map = std.StaticStringMap(NamedCurve).initComptime(.{
.{ &[_]u8{ 0x2B, 0x81, 0x04, 0x00, 0x22 }, .secp384r1 },
.{ &[_]u8{ 0x2B, 0x81, 0x04, 0x00, 0x23 }, .secp521r1 },
.{ &[_]u8{ 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x03, 0x01, 0x07 }, .X9_62_prime256v1 },
@@ -130,7 +130,7 @@ pub const ExtensionId = enum {
netscape_cert_type,
netscape_comment,
- pub const map = std.ComptimeStringMap(ExtensionId, .{
+ pub const map = std.StaticStringMap(ExtensionId).initComptime(.{
.{ &[_]u8{ 0x55, 0x04, 0x03 }, .commonName },
.{ &[_]u8{ 0x55, 0x1D, 0x01 }, .authority_key_identifier },
.{ &[_]u8{ 0x55, 0x1D, 0x07 }, .subject_alt_name },
diff --git a/lib/std/crypto/ecdsa.zig b/lib/std/crypto/ecdsa.zig
index b2751ce20a..5551f990d2 100644
--- a/lib/std/crypto/ecdsa.zig
+++ b/lib/std/crypto/ecdsa.zig
@@ -83,7 +83,7 @@ pub fn Ecdsa(comptime Curve: type, comptime Hash: type) type {
/// Length (in bytes) of a raw signature.
pub const encoded_length = Curve.scalar.encoded_length * 2;
/// Maximum length (in bytes) of a DER-encoded signature.
- pub const der_encoded_max_length = encoded_length + 2 + 2 * 3;
+ pub const der_encoded_length_max = encoded_length + 2 + 2 * 3;
/// The R component of an ECDSA signature.
r: Curve.scalar.CompressedScalar,
@@ -122,9 +122,9 @@ pub fn Ecdsa(comptime Curve: type, comptime Hash: type) type {
}
/// Encode the signature using the DER format.
- /// The maximum length of the DER encoding is der_encoded_max_length.
- /// The function returns a slice, that can be shorter than der_encoded_max_length.
- pub fn toDer(self: Signature, buf: *[der_encoded_max_length]u8) []u8 {
+ /// The maximum length of the DER encoding is der_encoded_length_max.
+ /// The function returns a slice, that can be shorter than der_encoded_length_max.
+ pub fn toDer(self: Signature, buf: *[der_encoded_length_max]u8) []u8 {
var fb = io.fixedBufferStream(buf);
const w = fb.writer();
const r_len = @as(u8, @intCast(self.r.len + (self.r[0] >> 7)));
diff --git a/lib/std/crypto/ff.zig b/lib/std/crypto/ff.zig
index b917ce7a43..31792318d0 100644
--- a/lib/std/crypto/ff.zig
+++ b/lib/std/crypto/ff.zig
@@ -843,7 +843,7 @@ const ct_protected = struct {
// Compares two big integers in constant time, returning true if x >= y.
fn limbsCmpGeq(x: anytype, y: @TypeOf(x)) bool {
- return !ct.limbsCmpLt(x, y);
+ return !limbsCmpLt(x, y);
}
// Multiplies two limbs and returns the result as a wide limb.
@@ -878,11 +878,11 @@ const ct_unprotected = struct {
// Compares two big integers in constant time, returning true if x < y.
fn limbsCmpLt(x: anytype, y: @TypeOf(x)) bool {
- assert(x.limbs_count() == y.limbs_count());
- const x_limbs = x.limbs.constSlice();
- const y_limbs = y.limbs.constSlice();
+ const x_limbs = x.limbsConst();
+ const y_limbs = y.limbsConst();
+ assert(x_limbs.len == y_limbs.len);
- var i = x.limbs_count();
+ var i = x_limbs.len;
while (i != 0) {
i -= 1;
if (x_limbs[i] != y_limbs[i]) {
@@ -894,7 +894,7 @@ const ct_unprotected = struct {
// Compares two big integers in constant time, returning true if x >= y.
fn limbsCmpGeq(x: anytype, y: @TypeOf(x)) bool {
- return !ct.limbsCmpLt(x, y);
+ return !limbsCmpLt(x, y);
}
// Multiplies two limbs and returns the result as a wide limb.
@@ -961,3 +961,28 @@ test "finite field arithmetic" {
try testing.expect(x_sq3.eql(x_sq4));
try m.fromMontgomery(&x);
}
+
+fn testCt(ct_: anytype) !void {
+ if (builtin.zig_backend == .stage2_c) return error.SkipZigTest;
+
+ const l0: Limb = 0;
+ const l1: Limb = 1;
+ try testing.expectEqual(l1, ct_.select(true, l1, l0));
+ try testing.expectEqual(l0, ct_.select(false, l1, l0));
+ try testing.expectEqual(false, ct_.eql(l1, l0));
+ try testing.expectEqual(true, ct_.eql(l1, l1));
+
+ const M = Modulus(256);
+ const m = try M.fromPrimitive(u256, 3429938563481314093726330772853735541133072814650493833233);
+ const x = try M.Fe.fromPrimitive(u256, m, 80169837251094269539116136208111827396136208141182357733);
+ const y = try M.Fe.fromPrimitive(u256, m, 24620149608466364616251608466389896540098571);
+ try testing.expectEqual(false, ct_.limbsCmpLt(x.v, y.v));
+ try testing.expectEqual(true, ct_.limbsCmpGeq(x.v, y.v));
+
+ try testing.expectEqual(WideLimb{ .hi = 0, .lo = 0x88 }, ct_.mulWide(1 << 3, (1 << 4) + 1));
+}
+
+test ct {
+ try testCt(ct_protected);
+ try testCt(ct_unprotected);
+}
diff --git a/lib/std/crypto/sha2.zig b/lib/std/crypto/sha2.zig
index 31884c7381..8debe2cf40 100644
--- a/lib/std/crypto/sha2.zig
+++ b/lib/std/crypto/sha2.zig
@@ -533,6 +533,18 @@ const Sha512Params = Sha2Params64{
.digest_bits = 512,
};
+const Sha512224Params = Sha2Params64{
+ .iv0 = 0x8C3D37C819544DA2,
+ .iv1 = 0x73E1996689DCD4D6,
+ .iv2 = 0x1DFAB7AE32FF9C82,
+ .iv3 = 0x679DD514582F9FCF,
+ .iv4 = 0x0F6D2B697BD44DA8,
+ .iv5 = 0x77E36F7304C48942,
+ .iv6 = 0x3F9D85A86A1D36C8,
+ .iv7 = 0x1112E6AD91D692A1,
+ .digest_bits = 224,
+};
+
const Sha512256Params = Sha2Params64{
.iv0 = 0x22312194FC2BF72C,
.iv1 = 0x9F555FA3C84C64C2,
@@ -563,6 +575,9 @@ pub const Sha384 = Sha2x64(Sha384Params);
/// SHA-512
pub const Sha512 = Sha2x64(Sha512Params);
+/// SHA-512/224
+pub const Sha512224 = Sha2x64(Sha512224Params);
+
/// SHA-512/256
pub const Sha512256 = Sha2x64(Sha512256Params);
@@ -665,6 +680,14 @@ fn Sha2x64(comptime params: Sha2Params64) type {
for (rr, 0..) |s, j| {
mem.writeInt(u64, out[8 * j ..][0..8], s, .big);
}
+
+ const bytes_left = params.digest_bits / 8 % 8;
+ if (bytes_left > 0) {
+ const rest = d.s[(params.digest_bits / 64)];
+ var buf: [8]u8 = undefined;
+ std.mem.writeInt(u64, &buf, rest, .big);
+ @memcpy(out[params.digest_bits / 64 * 8 ..], buf[0..bytes_left]);
+ }
}
pub fn finalResult(d: *Self) [digest_length]u8 {
@@ -875,3 +898,25 @@ test "sha512 aligned final" {
h.update(&block);
h.final(out[0..]);
}
+
+test "sha512-224 single" {
+ const h1 = "6ed0dd02806fa89e25de060c19d3ac86cabb87d6a0ddd05c333b84f4";
+ try htest.assertEqualHash(Sha512224, h1, "");
+
+ const h2 = "4634270f707b6a54daae7530460842e20e37ed265ceee9a43e8924aa";
+ try htest.assertEqualHash(Sha512224, h2, "abc");
+
+ const h3 = "23fec5bb94d60b23308192640b0c453335d664734fe40e7268674af9";
+ try htest.assertEqualHash(Sha512224, h3, "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu");
+}
+
+test "sha512-256 single" {
+ const h1 = "c672b8d1ef56ed28ab87c3622c5114069bdd3ad7b8f9737498d0c01ecef0967a";
+ try htest.assertEqualHash(Sha512256, h1, "");
+
+ const h2 = "53048e2681941ef99b2e29b76b4c7dabe4c2d0c634fc6d46e0e2f13107e7af23";
+ try htest.assertEqualHash(Sha512256, h2, "abc");
+
+ const h3 = "3928e184fb8690f840da3988121d31be65cb9d3ef83ee6146feac861e19b563a";
+ try htest.assertEqualHash(Sha512256, h3, "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu");
+}
diff --git a/lib/std/fmt.zig b/lib/std/fmt.zig
index 37c9f77fec..8965d1bc0e 100644
--- a/lib/std/fmt.zig
+++ b/lib/std/fmt.zig
@@ -1501,9 +1501,9 @@ pub fn parseInt(comptime T: type, buf: []const u8, base: u8) ParseIntError!T {
}
test parseInt {
- try std.testing.expect((try parseInt(i32, "-10", 10)) == -10);
- try std.testing.expect((try parseInt(i32, "+10", 10)) == 10);
- try std.testing.expect((try parseInt(u32, "+10", 10)) == 10);
+ try std.testing.expectEqual(-10, try parseInt(i32, "-10", 10));
+ try std.testing.expectEqual(10, try parseInt(i32, "+10", 10));
+ try std.testing.expectEqual(10, try parseInt(u32, "+10", 10));
try std.testing.expectError(error.Overflow, parseInt(u32, "-10", 10));
try std.testing.expectError(error.InvalidCharacter, parseInt(u32, " 10", 10));
try std.testing.expectError(error.InvalidCharacter, parseInt(u32, "10 ", 10));
@@ -1511,17 +1511,17 @@ test parseInt {
try std.testing.expectError(error.InvalidCharacter, parseInt(u32, "0x_10_", 10));
try std.testing.expectError(error.InvalidCharacter, parseInt(u32, "0x10_", 10));
try std.testing.expectError(error.InvalidCharacter, parseInt(u32, "0x_10", 10));
- try std.testing.expect((try parseInt(u8, "255", 10)) == 255);
+ try std.testing.expectEqual(255, try parseInt(u8, "255", 10));
try std.testing.expectError(error.Overflow, parseInt(u8, "256", 10));
// +0 and -0 should work for unsigned
- try std.testing.expect((try parseInt(u8, "-0", 10)) == 0);
- try std.testing.expect((try parseInt(u8, "+0", 10)) == 0);
+ try std.testing.expectEqual(0, try parseInt(u8, "-0", 10));
+ try std.testing.expectEqual(0, try parseInt(u8, "+0", 10));
// ensure minInt is parsed correctly
- try std.testing.expect((try parseInt(i1, "-1", 10)) == math.minInt(i1));
- try std.testing.expect((try parseInt(i8, "-128", 10)) == math.minInt(i8));
- try std.testing.expect((try parseInt(i43, "-4398046511104", 10)) == math.minInt(i43));
+ try std.testing.expectEqual(math.minInt(i1), try parseInt(i1, "-1", 10));
+ try std.testing.expectEqual(math.minInt(i8), try parseInt(i8, "-128", 10));
+ try std.testing.expectEqual(math.minInt(i43), try parseInt(i43, "-4398046511104", 10));
// empty string or bare +- is invalid
try std.testing.expectError(error.InvalidCharacter, parseInt(u32, "", 10));
@@ -1532,22 +1532,22 @@ test parseInt {
try std.testing.expectError(error.InvalidCharacter, parseInt(i32, "-", 10));
// autodectect the base
- try std.testing.expect((try parseInt(i32, "111", 0)) == 111);
- try std.testing.expect((try parseInt(i32, "1_1_1", 0)) == 111);
- try std.testing.expect((try parseInt(i32, "1_1_1", 0)) == 111);
- try std.testing.expect((try parseInt(i32, "+0b111", 0)) == 7);
- try std.testing.expect((try parseInt(i32, "+0B111", 0)) == 7);
- try std.testing.expect((try parseInt(i32, "+0b1_11", 0)) == 7);
- try std.testing.expect((try parseInt(i32, "+0o111", 0)) == 73);
- try std.testing.expect((try parseInt(i32, "+0O111", 0)) == 73);
- try std.testing.expect((try parseInt(i32, "+0o11_1", 0)) == 73);
- try std.testing.expect((try parseInt(i32, "+0x111", 0)) == 273);
- try std.testing.expect((try parseInt(i32, "-0b111", 0)) == -7);
- try std.testing.expect((try parseInt(i32, "-0b11_1", 0)) == -7);
- try std.testing.expect((try parseInt(i32, "-0o111", 0)) == -73);
- try std.testing.expect((try parseInt(i32, "-0x111", 0)) == -273);
- try std.testing.expect((try parseInt(i32, "-0X111", 0)) == -273);
- try std.testing.expect((try parseInt(i32, "-0x1_11", 0)) == -273);
+ try std.testing.expectEqual(111, try parseInt(i32, "111", 0));
+ try std.testing.expectEqual(111, try parseInt(i32, "1_1_1", 0));
+ try std.testing.expectEqual(111, try parseInt(i32, "1_1_1", 0));
+ try std.testing.expectEqual(7, try parseInt(i32, "+0b111", 0));
+ try std.testing.expectEqual(7, try parseInt(i32, "+0B111", 0));
+ try std.testing.expectEqual(7, try parseInt(i32, "+0b1_11", 0));
+ try std.testing.expectEqual(73, try parseInt(i32, "+0o111", 0));
+ try std.testing.expectEqual(73, try parseInt(i32, "+0O111", 0));
+ try std.testing.expectEqual(73, try parseInt(i32, "+0o11_1", 0));
+ try std.testing.expectEqual(273, try parseInt(i32, "+0x111", 0));
+ try std.testing.expectEqual(-7, try parseInt(i32, "-0b111", 0));
+ try std.testing.expectEqual(-7, try parseInt(i32, "-0b11_1", 0));
+ try std.testing.expectEqual(-73, try parseInt(i32, "-0o111", 0));
+ try std.testing.expectEqual(-273, try parseInt(i32, "-0x111", 0));
+ try std.testing.expectEqual(-273, try parseInt(i32, "-0X111", 0));
+ try std.testing.expectEqual(-273, try parseInt(i32, "-0x1_11", 0));
// bare binary/octal/decimal prefix is invalid
try std.testing.expectError(error.InvalidCharacter, parseInt(u32, "0b", 0));
@@ -1643,31 +1643,31 @@ pub fn parseUnsigned(comptime T: type, buf: []const u8, base: u8) ParseIntError!
}
test parseUnsigned {
- try std.testing.expect((try parseUnsigned(u16, "050124", 10)) == 50124);
- try std.testing.expect((try parseUnsigned(u16, "65535", 10)) == 65535);
- try std.testing.expect((try parseUnsigned(u16, "65_535", 10)) == 65535);
+ try std.testing.expectEqual(50124, try parseUnsigned(u16, "050124", 10));
+ try std.testing.expectEqual(65535, try parseUnsigned(u16, "65535", 10));
+ try std.testing.expectEqual(65535, try parseUnsigned(u16, "65_535", 10));
try std.testing.expectError(error.Overflow, parseUnsigned(u16, "65536", 10));
- try std.testing.expect((try parseUnsigned(u64, "0ffffffffffffffff", 16)) == 0xffffffffffffffff);
- try std.testing.expect((try parseUnsigned(u64, "0f_fff_fff_fff_fff_fff", 16)) == 0xffffffffffffffff);
+ try std.testing.expectEqual(0xffffffffffffffff, try parseUnsigned(u64, "0ffffffffffffffff", 16));
+ try std.testing.expectEqual(0xffffffffffffffff, try parseUnsigned(u64, "0f_fff_fff_fff_fff_fff", 16));
try std.testing.expectError(error.Overflow, parseUnsigned(u64, "10000000000000000", 16));
- try std.testing.expect((try parseUnsigned(u32, "DeadBeef", 16)) == 0xDEADBEEF);
+ try std.testing.expectEqual(0xDEADBEEF, try parseUnsigned(u32, "DeadBeef", 16));
- try std.testing.expect((try parseUnsigned(u7, "1", 10)) == 1);
- try std.testing.expect((try parseUnsigned(u7, "1000", 2)) == 8);
+ try std.testing.expectEqual(1, try parseUnsigned(u7, "1", 10));
+ try std.testing.expectEqual(8, try parseUnsigned(u7, "1000", 2));
try std.testing.expectError(error.InvalidCharacter, parseUnsigned(u32, "f", 10));
try std.testing.expectError(error.InvalidCharacter, parseUnsigned(u8, "109", 8));
- try std.testing.expect((try parseUnsigned(u32, "NUMBER", 36)) == 1442151747);
+ try std.testing.expectEqual(1442151747, try parseUnsigned(u32, "NUMBER", 36));
// these numbers should fit even though the base itself doesn't fit in the destination type
- try std.testing.expect((try parseUnsigned(u1, "0", 10)) == 0);
- try std.testing.expect((try parseUnsigned(u1, "1", 10)) == 1);
+ try std.testing.expectEqual(0, try parseUnsigned(u1, "0", 10));
+ try std.testing.expectEqual(1, try parseUnsigned(u1, "1", 10));
try std.testing.expectError(error.Overflow, parseUnsigned(u1, "2", 10));
- try std.testing.expect((try parseUnsigned(u1, "001", 16)) == 1);
- try std.testing.expect((try parseUnsigned(u2, "3", 16)) == 3);
+ try std.testing.expectEqual(1, try parseUnsigned(u1, "001", 16));
+ try std.testing.expectEqual(3, try parseUnsigned(u2, "3", 16));
try std.testing.expectError(error.Overflow, parseUnsigned(u2, "4", 16));
// parseUnsigned does not expect a sign
@@ -1717,15 +1717,15 @@ pub fn parseIntSizeSuffix(buf: []const u8, digit_base: u8) ParseIntError!usize {
}
test parseIntSizeSuffix {
- try std.testing.expect(try parseIntSizeSuffix("2", 10) == 2);
- try std.testing.expect(try parseIntSizeSuffix("2B", 10) == 2);
- try std.testing.expect(try parseIntSizeSuffix("2kB", 10) == 2000);
- try std.testing.expect(try parseIntSizeSuffix("2k", 10) == 2000);
- try std.testing.expect(try parseIntSizeSuffix("2KiB", 10) == 2048);
- try std.testing.expect(try parseIntSizeSuffix("2Ki", 10) == 2048);
- try std.testing.expect(try parseIntSizeSuffix("aKiB", 16) == 10240);
- try std.testing.expect(parseIntSizeSuffix("", 10) == error.InvalidCharacter);
- try std.testing.expect(parseIntSizeSuffix("2iB", 10) == error.InvalidCharacter);
+ try std.testing.expectEqual(2, try parseIntSizeSuffix("2", 10));
+ try std.testing.expectEqual(2, try parseIntSizeSuffix("2B", 10));
+ try std.testing.expectEqual(2000, try parseIntSizeSuffix("2kB", 10));
+ try std.testing.expectEqual(2000, try parseIntSizeSuffix("2k", 10));
+ try std.testing.expectEqual(2048, try parseIntSizeSuffix("2KiB", 10));
+ try std.testing.expectEqual(2048, try parseIntSizeSuffix("2Ki", 10));
+ try std.testing.expectEqual(10240, try parseIntSizeSuffix("aKiB", 16));
+ try std.testing.expectError(error.InvalidCharacter, parseIntSizeSuffix("", 10));
+ try std.testing.expectError(error.InvalidCharacter, parseIntSizeSuffix("2iB", 10));
}
pub const parseFloat = @import("fmt/parse_float.zig").parseFloat;
@@ -1854,7 +1854,7 @@ test "parse u64 digit too big" {
test "parse unsigned comptime" {
comptime {
- try std.testing.expect((try parseUnsigned(usize, "2", 10)) == 2);
+ try std.testing.expectEqual(2, try parseUnsigned(usize, "2", 10));
}
}
@@ -1963,15 +1963,15 @@ test "buffer" {
var buf1: [32]u8 = undefined;
var fbs = std.io.fixedBufferStream(&buf1);
try formatType(1234, "", FormatOptions{}, fbs.writer(), std.options.fmt_max_depth);
- try std.testing.expect(mem.eql(u8, fbs.getWritten(), "1234"));
+ try std.testing.expectEqualStrings("1234", fbs.getWritten());
fbs.reset();
try formatType('a', "c", FormatOptions{}, fbs.writer(), std.options.fmt_max_depth);
- try std.testing.expect(mem.eql(u8, fbs.getWritten(), "a"));
+ try std.testing.expectEqualStrings("a", fbs.getWritten());
fbs.reset();
try formatType(0b1100, "b", FormatOptions{}, fbs.writer(), std.options.fmt_max_depth);
- try std.testing.expect(mem.eql(u8, fbs.getWritten(), "1100"));
+ try std.testing.expectEqualStrings("1100", fbs.getWritten());
}
}
@@ -2372,10 +2372,10 @@ test "union" {
var buf: [100]u8 = undefined;
const uu_result = try bufPrint(buf[0..], "{}", .{uu_inst});
- try std.testing.expect(mem.eql(u8, uu_result[0..18], "fmt.test.union.UU@"));
+ try std.testing.expectEqualStrings("fmt.test.union.UU@", uu_result[0..18]);
const eu_result = try bufPrint(buf[0..], "{}", .{eu_inst});
- try std.testing.expect(mem.eql(u8, eu_result[0..18], "fmt.test.union.EU@"));
+ try std.testing.expectEqualStrings("fmt.test.union.EU@", eu_result[0..18]);
}
test "struct.self-referential" {
@@ -2476,7 +2476,7 @@ test "formatIntValue with comptime_int" {
var buf: [20]u8 = undefined;
var fbs = std.io.fixedBufferStream(&buf);
try formatIntValue(value, "", FormatOptions{}, fbs.writer());
- try std.testing.expect(mem.eql(u8, fbs.getWritten(), "123456789123456789"));
+ try std.testing.expectEqualStrings("123456789123456789", fbs.getWritten());
}
test "formatFloatValue with comptime_float" {
@@ -2542,19 +2542,19 @@ test "formatType max_depth" {
var buf: [1000]u8 = undefined;
var fbs = std.io.fixedBufferStream(&buf);
try formatType(inst, "", FormatOptions{}, fbs.writer(), 0);
- try std.testing.expect(mem.eql(u8, fbs.getWritten(), "fmt.test.formatType max_depth.S{ ... }"));
+ try std.testing.expectEqualStrings("fmt.test.formatType max_depth.S{ ... }", fbs.getWritten());
fbs.reset();
try formatType(inst, "", FormatOptions{}, fbs.writer(), 1);
- try std.testing.expect(mem.eql(u8, fbs.getWritten(), "fmt.test.formatType max_depth.S{ .a = fmt.test.formatType max_depth.S{ ... }, .tu = fmt.test.formatType max_depth.TU{ ... }, .e = fmt.test.formatType max_depth.E.Two, .vec = (10.200,2.220) }"));
+ try std.testing.expectEqualStrings("fmt.test.formatType max_depth.S{ .a = fmt.test.formatType max_depth.S{ ... }, .tu = fmt.test.formatType max_depth.TU{ ... }, .e = fmt.test.formatType max_depth.E.Two, .vec = (10.200,2.220) }", fbs.getWritten());
fbs.reset();
try formatType(inst, "", FormatOptions{}, fbs.writer(), 2);
- try std.testing.expect(mem.eql(u8, fbs.getWritten(), "fmt.test.formatType max_depth.S{ .a = fmt.test.formatType max_depth.S{ .a = fmt.test.formatType max_depth.S{ ... }, .tu = fmt.test.formatType max_depth.TU{ ... }, .e = fmt.test.formatType max_depth.E.Two, .vec = (10.200,2.220) }, .tu = fmt.test.formatType max_depth.TU{ .ptr = fmt.test.formatType max_depth.TU{ ... } }, .e = fmt.test.formatType max_depth.E.Two, .vec = (10.200,2.220) }"));
+ try std.testing.expectEqualStrings("fmt.test.formatType max_depth.S{ .a = fmt.test.formatType max_depth.S{ .a = fmt.test.formatType max_depth.S{ ... }, .tu = fmt.test.formatType max_depth.TU{ ... }, .e = fmt.test.formatType max_depth.E.Two, .vec = (10.200,2.220) }, .tu = fmt.test.formatType max_depth.TU{ .ptr = fmt.test.formatType max_depth.TU{ ... } }, .e = fmt.test.formatType max_depth.E.Two, .vec = (10.200,2.220) }", fbs.getWritten());
fbs.reset();
try formatType(inst, "", FormatOptions{}, fbs.writer(), 3);
- try std.testing.expect(mem.eql(u8, fbs.getWritten(), "fmt.test.formatType max_depth.S{ .a = fmt.test.formatType max_depth.S{ .a = fmt.test.formatType max_depth.S{ .a = fmt.test.formatType max_depth.S{ ... }, .tu = fmt.test.formatType max_depth.TU{ ... }, .e = fmt.test.formatType max_depth.E.Two, .vec = (10.200,2.220) }, .tu = fmt.test.formatType max_depth.TU{ .ptr = fmt.test.formatType max_depth.TU{ ... } }, .e = fmt.test.formatType max_depth.E.Two, .vec = (10.200,2.220) }, .tu = fmt.test.formatType max_depth.TU{ .ptr = fmt.test.formatType max_depth.TU{ .ptr = fmt.test.formatType max_depth.TU{ ... } } }, .e = fmt.test.formatType max_depth.E.Two, .vec = (10.200,2.220) }"));
+ try std.testing.expectEqualStrings("fmt.test.formatType max_depth.S{ .a = fmt.test.formatType max_depth.S{ .a = fmt.test.formatType max_depth.S{ .a = fmt.test.formatType max_depth.S{ ... }, .tu = fmt.test.formatType max_depth.TU{ ... }, .e = fmt.test.formatType max_depth.E.Two, .vec = (10.200,2.220) }, .tu = fmt.test.formatType max_depth.TU{ .ptr = fmt.test.formatType max_depth.TU{ ... } }, .e = fmt.test.formatType max_depth.E.Two, .vec = (10.200,2.220) }, .tu = fmt.test.formatType max_depth.TU{ .ptr = fmt.test.formatType max_depth.TU{ .ptr = fmt.test.formatType max_depth.TU{ ... } } }, .e = fmt.test.formatType max_depth.E.Two, .vec = (10.200,2.220) }", fbs.getWritten());
}
test "positional" {
diff --git a/lib/std/fs/Dir.zig b/lib/std/fs/Dir.zig
index b6222f942f..bd0a8fe4e8 100644
--- a/lib/std/fs/Dir.zig
+++ b/lib/std/fs/Dir.zig
@@ -800,7 +800,7 @@ pub fn openFile(self: Dir, sub_path: []const u8, flags: File.OpenFlags) File.Ope
const path_w = try windows.sliceToPrefixedFileW(self.fd, sub_path);
return self.openFileW(path_w.span(), flags);
}
- if (native_os == .wasi) {
+ if (native_os == .wasi and !builtin.link_libc) {
var base: std.os.wasi.rights_t = .{};
if (flags.isRead()) {
base.FD_READ = true;
@@ -834,17 +834,25 @@ pub fn openFileZ(self: Dir, sub_path: [*:0]const u8, flags: File.OpenFlags) File
const path_w = try windows.cStrToPrefixedFileW(self.fd, sub_path);
return self.openFileW(path_w.span(), flags);
},
- .wasi => {
+ // Use the libc API when libc is linked because it implements things
+ // such as opening absolute file paths.
+ .wasi => if (!builtin.link_libc) {
return openFile(self, mem.sliceTo(sub_path, 0), flags);
},
else => {},
}
- var os_flags: posix.O = .{
- .ACCMODE = switch (flags.mode) {
- .read_only => .RDONLY,
- .write_only => .WRONLY,
- .read_write => .RDWR,
+ var os_flags: posix.O = switch (native_os) {
+ .wasi => .{
+ .read = flags.mode != .write_only,
+ .write = flags.mode != .read_only,
+ },
+ else => .{
+ .ACCMODE = switch (flags.mode) {
+ .read_only => .RDONLY,
+ .write_only => .WRONLY,
+ .read_write => .RDWR,
+ },
},
};
if (@hasField(posix.O, "CLOEXEC")) os_flags.CLOEXEC = true;
@@ -1393,7 +1401,7 @@ pub fn openDir(self: Dir, sub_path: []const u8, args: OpenDirOptions) OpenError!
const sub_path_w = try windows.sliceToPrefixedFileW(self.fd, sub_path);
return self.openDirW(sub_path_w.span().ptr, args);
},
- .wasi => {
+ .wasi => if (!builtin.link_libc) {
var base: std.os.wasi.rights_t = .{
.FD_FILESTAT_GET = true,
.FD_FDSTAT_SET_FLAGS = true,
@@ -1438,11 +1446,10 @@ pub fn openDir(self: Dir, sub_path: []const u8, args: OpenDirOptions) OpenError!
};
return .{ .fd = fd };
},
- else => {
- const sub_path_c = try posix.toPosixPath(sub_path);
- return self.openDirZ(&sub_path_c, args);
- },
+ else => {},
}
+ const sub_path_c = try posix.toPosixPath(sub_path);
+ return self.openDirZ(&sub_path_c, args);
}
/// Same as `openDir` except the parameter is null-terminated.
@@ -1452,7 +1459,9 @@ pub fn openDirZ(self: Dir, sub_path_c: [*:0]const u8, args: OpenDirOptions) Open
const sub_path_w = try windows.cStrToPrefixedFileW(self.fd, sub_path_c);
return self.openDirW(sub_path_w.span().ptr, args);
},
- .wasi => {
+ // Use the libc API when libc is linked because it implements things
+ // such as opening absolute directory paths.
+ .wasi => if (!builtin.link_libc) {
return openDir(self, mem.sliceTo(sub_path_c, 0), args);
},
.haiku => {
@@ -1476,19 +1485,27 @@ pub fn openDirZ(self: Dir, sub_path_c: [*:0]const u8, args: OpenDirOptions) Open
else => |err| return posix.unexpectedErrno(err),
}
},
- else => {
- var symlink_flags: posix.O = .{
- .ACCMODE = .RDONLY,
- .NOFOLLOW = args.no_follow,
- .DIRECTORY = true,
- .CLOEXEC = true,
- };
- if (@hasField(posix.O, "PATH") and !args.iterate)
- symlink_flags.PATH = true;
+ else => {},
+ }
- return self.openDirFlagsZ(sub_path_c, symlink_flags);
+ var symlink_flags: posix.O = switch (native_os) {
+ .wasi => .{
+ .read = true,
+ .NOFOLLOW = args.no_follow,
+ .DIRECTORY = true,
},
- }
+ else => .{
+ .ACCMODE = .RDONLY,
+ .NOFOLLOW = args.no_follow,
+ .DIRECTORY = true,
+ .CLOEXEC = true,
+ },
+ };
+
+ if (@hasField(posix.O, "PATH") and !args.iterate)
+ symlink_flags.PATH = true;
+
+ return self.openDirFlagsZ(sub_path_c, symlink_flags);
}
/// Same as `openDir` except the path parameter is WTF-16 LE encoded, NT-prefixed.
diff --git a/lib/std/fs/test.zig b/lib/std/fs/test.zig
index 3aa932cf01..488632b34c 100644
--- a/lib/std/fs/test.zig
+++ b/lib/std/fs/test.zig
@@ -1641,7 +1641,7 @@ test "walker" {
// iteration order of walker is undefined, so need lookup maps to check against
- const expected_paths = std.ComptimeStringMap(void, .{
+ const expected_paths = std.StaticStringMap(void).initComptime(.{
.{"dir1"},
.{"dir2"},
.{"dir3"},
@@ -1651,7 +1651,7 @@ test "walker" {
.{"dir3" ++ fs.path.sep_str ++ "sub2" ++ fs.path.sep_str ++ "subsub1"},
});
- const expected_basenames = std.ComptimeStringMap(void, .{
+ const expected_basenames = std.StaticStringMap(void).initComptime(.{
.{"dir1"},
.{"dir2"},
.{"dir3"},
@@ -1661,8 +1661,8 @@ test "walker" {
.{"subsub1"},
});
- for (expected_paths.kvs) |kv| {
- try tmp.dir.makePath(kv.key);
+ for (expected_paths.keys()) |key| {
+ try tmp.dir.makePath(key);
}
var walker = try tmp.dir.walk(testing.allocator);
diff --git a/lib/std/http/Client.zig b/lib/std/http/Client.zig
index 837bdc63c7..9da1e428f6 100644
--- a/lib/std/http/Client.zig
+++ b/lib/std/http/Client.zig
@@ -1570,7 +1570,7 @@ pub const RequestOptions = struct {
};
fn validateUri(uri: Uri, arena: Allocator) !struct { Connection.Protocol, Uri } {
- const protocol_map = std.ComptimeStringMap(Connection.Protocol, .{
+ const protocol_map = std.StaticStringMap(Connection.Protocol).initComptime(.{
.{ "http", .plain },
.{ "ws", .plain },
.{ "https", .tls },
diff --git a/lib/std/macho.zig b/lib/std/macho.zig
index a5eb561f64..d477befbc4 100644
--- a/lib/std/macho.zig
+++ b/lib/std/macho.zig
@@ -1230,6 +1230,12 @@ pub const MH_APP_EXTENSION_SAFE = 0x02000000;
/// The external symbols listed in the nlist symbol table do not include all the symbols listed in the dyld info.
pub const MH_NLIST_OUTOFSYNC_WITH_DYLDINFO = 0x04000000;
+/// Allow LC_MIN_VERSION_MACOS and LC_BUILD_VERSION load commands with the platforms macOS, iOSMac, iOSSimulator, tvOSSimulator and watchOSSimulator.
+pub const MH_SIM_SUPPORT = 0x08000000;
+
+/// Only for use on dylibs. When this bit is set, the dylib is part of the dyld shared cache, rather than loose in the filesystem.
+pub const MH_DYLIB_IN_CACHE = 0x80000000;
+
// Constants for the flags field of the fat_header
/// the fat magic number
diff --git a/lib/std/math.zig b/lib/std/math.zig
index 19dd82ec67..858e9243bf 100644
--- a/lib/std/math.zig
+++ b/lib/std/math.zig
@@ -1588,6 +1588,31 @@ pub const Order = enum {
try testing.expect(Order.invert(order(-1, 0)) == .gt);
}
+ pub fn differ(self: Order) ?Order {
+ return if (self != .eq) self else null;
+ }
+
+ test differ {
+ const neg: i32 = -1;
+ const zero: i32 = 0;
+ const pos: i32 = 1;
+ try testing.expect(order(zero, neg).differ() orelse
+ order(pos, zero) == .gt);
+ try testing.expect(order(zero, zero).differ() orelse
+ order(zero, zero) == .eq);
+ try testing.expect(order(pos, pos).differ() orelse
+ order(neg, zero) == .lt);
+ try testing.expect(order(zero, zero).differ() orelse
+ order(pos, neg).differ() orelse
+ order(neg, zero) == .gt);
+ try testing.expect(order(pos, pos).differ() orelse
+ order(pos, pos).differ() orelse
+ order(neg, neg) == .eq);
+ try testing.expect(order(zero, pos).differ() orelse
+ order(neg, pos).differ() orelse
+ order(pos, neg) == .lt);
+ }
+
pub fn compare(self: Order, op: CompareOperator) bool {
return switch (self) {
.lt => switch (op) {
diff --git a/lib/std/meta.zig b/lib/std/meta.zig
index 3ad2c2de13..e8296f78a3 100644
--- a/lib/std/meta.zig
+++ b/lib/std/meta.zig
@@ -19,7 +19,7 @@ pub const isTag = @compileError("deprecated; use 'tagged_value == @field(E, tag_
/// Returns the variant of an enum type, `T`, which is named `str`, or `null` if no such variant exists.
pub fn stringToEnum(comptime T: type, str: []const u8) ?T {
- // Using ComptimeStringMap here is more performant, but it will start to take too
+ // Using StaticStringMap here is more performant, but it will start to take too
// long to compile if the enum is large enough, due to the current limits of comptime
// performance when doing things like constructing lookup maps at comptime.
// TODO The '100' here is arbitrary and should be increased when possible:
@@ -34,7 +34,7 @@ pub fn stringToEnum(comptime T: type, str: []const u8) ?T {
}
break :build_kvs kvs_array[0..];
};
- const map = std.ComptimeStringMap(T, kvs);
+ const map = std.StaticStringMap(T).initComptime(kvs);
return map.get(str);
} else {
inline for (@typeInfo(T).Enum.fields) |enumField| {
@@ -719,7 +719,7 @@ pub fn TagPayloadByName(comptime U: type, comptime tag_name: []const u8) type {
return field_info.type;
}
- unreachable;
+ @compileError("no field '" ++ tag_name ++ "' in union '" ++ @typeName(U) ++ "'");
}
/// Given a tagged union type, and an enum, return the type of the union field
diff --git a/lib/std/net.zig b/lib/std/net.zig
index b12fb1932d..3126bfa93b 100644
--- a/lib/std/net.zig
+++ b/lib/std/net.zig
@@ -271,7 +271,7 @@ pub const Ip4Address = extern struct {
sa: posix.sockaddr.in,
pub fn parse(buf: []const u8, port: u16) IPv4ParseError!Ip4Address {
- var result = Ip4Address{
+ var result: Ip4Address = .{
.sa = .{
.port = mem.nativeToBig(u16, port),
.addr = undefined,
diff --git a/lib/std/os/linux.zig b/lib/std/os/linux.zig
index 69cef35e98..93581b1107 100644
--- a/lib/std/os/linux.zig
+++ b/lib/std/os/linux.zig
@@ -1893,7 +1893,7 @@ pub fn sched_setaffinity(pid: pid_t, set: *const cpu_set_t) !void {
switch (std.os.errno(rc)) {
.SUCCESS => return,
- else => |err| return std.os.unexpectedErrno(err),
+ else => |err| return std.posix.unexpectedErrno(err),
}
}
diff --git a/lib/std/os/linux/bpf.zig b/lib/std/os/linux/bpf.zig
index 5e02485b15..61c704fce9 100644
--- a/lib/std/os/linux/bpf.zig
+++ b/lib/std/os/linux/bpf.zig
@@ -1,6 +1,6 @@
const std = @import("../../std.zig");
const errno = linux.E.init;
-const unexpectedErrno = std.os.unexpectedErrno;
+const unexpectedErrno = std.posix.unexpectedErrno;
const expectEqual = std.testing.expectEqual;
const expectError = std.testing.expectError;
const expect = std.testing.expect;
diff --git a/lib/std/os/windows.zig b/lib/std/os/windows.zig
index 1c0b7d9d80..2e32e4676e 100644
--- a/lib/std/os/windows.zig
+++ b/lib/std/os/windows.zig
@@ -1369,6 +1369,61 @@ pub fn GetFinalPathNameByHandle(
}
return out_buffer[0..total_len];
+ } else if (mountmgrIsVolumeName(symlink)) {
+ // If the symlink is a volume GUID like \??\Volume{383da0b0-717f-41b6-8c36-00500992b58d},
+ // then it is a volume mounted as a path rather than a drive letter. We need to
+ // query the mount manager again to get the DOS path for the volume.
+
+ // 49 is the maximum length accepted by mountmgrIsVolumeName
+ const vol_input_size = @sizeOf(MOUNTMGR_TARGET_NAME) + (49 * 2);
+ var vol_input_buf: [vol_input_size]u8 align(@alignOf(MOUNTMGR_TARGET_NAME)) = [_]u8{0} ** vol_input_size;
+ // Note: If the path exceeds MAX_PATH, the Disk Management GUI doesn't accept the full path,
+ // and instead if must be specified using a shortened form (e.g. C:\FOO~1\BAR~1\<...>).
+ // However, just to be sure we can handle any path length, we use PATH_MAX_WIDE here.
+ const min_output_size = @sizeOf(MOUNTMGR_VOLUME_PATHS) + (PATH_MAX_WIDE * 2);
+ var vol_output_buf: [min_output_size]u8 align(@alignOf(MOUNTMGR_VOLUME_PATHS)) = undefined;
+
+ var vol_input_struct: *MOUNTMGR_TARGET_NAME = @ptrCast(&vol_input_buf[0]);
+ vol_input_struct.DeviceNameLength = @intCast(symlink.len * 2);
+ @memcpy(@as([*]WCHAR, &vol_input_struct.DeviceName)[0..symlink.len], symlink);
+
+ DeviceIoControl(mgmt_handle, IOCTL_MOUNTMGR_QUERY_DOS_VOLUME_PATH, &vol_input_buf, &vol_output_buf) catch |err| switch (err) {
+ error.AccessDenied => return error.Unexpected,
+ else => |e| return e,
+ };
+ const volume_paths_struct: *const MOUNTMGR_VOLUME_PATHS = @ptrCast(&vol_output_buf[0]);
+ const volume_path = std.mem.sliceTo(@as(
+ [*]const u16,
+ &volume_paths_struct.MultiSz,
+ )[0 .. volume_paths_struct.MultiSzLength / 2], 0);
+
+ if (out_buffer.len < volume_path.len + file_name_u16.len) return error.NameTooLong;
+
+ // `out_buffer` currently contains the memory of `file_name_u16`, so it can overlap with where
+ // we want to place the filename before returning. Here are the possible overlapping cases:
+ //
+ // out_buffer: [filename]
+ // dest: [___(a)___] [___(b)___]
+ //
+ // In the case of (a), we need to copy forwards, and in the case of (b) we need
+ // to copy backwards. We also need to do this before copying the volume path because
+ // it could overwrite the file_name_u16 memory.
+ const file_name_dest = out_buffer[volume_path.len..][0..file_name_u16.len];
+ const file_name_byte_offset = @intFromPtr(file_name_u16.ptr) - @intFromPtr(out_buffer.ptr);
+ const file_name_index = file_name_byte_offset / @sizeOf(u16);
+ if (volume_path.len > file_name_index)
+ mem.copyBackwards(u16, file_name_dest, file_name_u16)
+ else
+ mem.copyForwards(u16, file_name_dest, file_name_u16);
+ @memcpy(out_buffer[0..volume_path.len], volume_path);
+ const total_len = volume_path.len + file_name_u16.len;
+
+ // Validate that DOS does not contain any spurious nul bytes.
+ if (mem.indexOfScalar(u16, out_buffer[0..total_len], 0)) |_| {
+ return error.BadPathName;
+ }
+
+ return out_buffer[0..total_len];
}
}
@@ -1379,6 +1434,32 @@ pub fn GetFinalPathNameByHandle(
}
}
+/// Equivalent to the MOUNTMGR_IS_VOLUME_NAME macro in mountmgr.h
+fn mountmgrIsVolumeName(name: []const u16) bool {
+ return (name.len == 48 or (name.len == 49 and name[48] == mem.nativeToLittle(u16, '\\'))) and
+ name[0] == mem.nativeToLittle(u16, '\\') and
+ (name[1] == mem.nativeToLittle(u16, '?') or name[1] == mem.nativeToLittle(u16, '\\')) and
+ name[2] == mem.nativeToLittle(u16, '?') and
+ name[3] == mem.nativeToLittle(u16, '\\') and
+ mem.startsWith(u16, name[4..], std.unicode.utf8ToUtf16LeStringLiteral("Volume{")) and
+ name[19] == mem.nativeToLittle(u16, '-') and
+ name[24] == mem.nativeToLittle(u16, '-') and
+ name[29] == mem.nativeToLittle(u16, '-') and
+ name[34] == mem.nativeToLittle(u16, '-') and
+ name[47] == mem.nativeToLittle(u16, '}');
+}
+
+test mountmgrIsVolumeName {
+ const L = std.unicode.utf8ToUtf16LeStringLiteral;
+ try std.testing.expect(mountmgrIsVolumeName(L("\\\\?\\Volume{383da0b0-717f-41b6-8c36-00500992b58d}")));
+ try std.testing.expect(mountmgrIsVolumeName(L("\\??\\Volume{383da0b0-717f-41b6-8c36-00500992b58d}")));
+ try std.testing.expect(mountmgrIsVolumeName(L("\\\\?\\Volume{383da0b0-717f-41b6-8c36-00500992b58d}\\")));
+ try std.testing.expect(mountmgrIsVolumeName(L("\\??\\Volume{383da0b0-717f-41b6-8c36-00500992b58d}\\")));
+ try std.testing.expect(!mountmgrIsVolumeName(L("\\\\.\\Volume{383da0b0-717f-41b6-8c36-00500992b58d}")));
+ try std.testing.expect(!mountmgrIsVolumeName(L("\\??\\Volume{383da0b0-717f-41b6-8c36-00500992b58d}\\foo")));
+ try std.testing.expect(!mountmgrIsVolumeName(L("\\??\\Volume{383da0b0-717f-41b6-8c36-00500992b58}")));
+}
+
test GetFinalPathNameByHandle {
if (builtin.os.tag != .windows)
return;
@@ -3672,7 +3753,15 @@ pub const SEC_LARGE_PAGES = 0x80000000;
pub const HKEY = *opaque {};
pub const HKEY_CLASSES_ROOT: HKEY = @ptrFromInt(0x80000000);
+pub const HKEY_CURRENT_USER: HKEY = @ptrFromInt(0x80000001);
pub const HKEY_LOCAL_MACHINE: HKEY = @ptrFromInt(0x80000002);
+pub const HKEY_USERS: HKEY = @ptrFromInt(0x80000003);
+pub const HKEY_PERFORMANCE_DATA: HKEY = @ptrFromInt(0x80000004);
+pub const HKEY_PERFORMANCE_TEXT: HKEY = @ptrFromInt(0x80000050);
+pub const HKEY_PERFORMANCE_NLSTEXT: HKEY = @ptrFromInt(0x80000060);
+pub const HKEY_CURRENT_CONFIG: HKEY = @ptrFromInt(0x80000005);
+pub const HKEY_DYN_DATA: HKEY = @ptrFromInt(0x80000006);
+pub const HKEY_CURRENT_USER_LOCAL_SETTINGS: HKEY = @ptrFromInt(0x80000007);
/// Combines the STANDARD_RIGHTS_REQUIRED, KEY_QUERY_VALUE, KEY_SET_VALUE, KEY_CREATE_SUB_KEY,
/// KEY_ENUMERATE_SUB_KEYS, KEY_NOTIFY, and KEY_CREATE_LINK access rights.
@@ -4837,6 +4926,8 @@ pub const SYMLINK_FLAG_RELATIVE: ULONG = 0x1;
pub const SYMBOLIC_LINK_FLAG_DIRECTORY: DWORD = 0x1;
pub const SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE: DWORD = 0x2;
+pub const MOUNTMGRCONTROLTYPE = 0x0000006D;
+
pub const MOUNTMGR_MOUNT_POINT = extern struct {
SymbolicLinkNameOffset: ULONG,
SymbolicLinkNameLength: USHORT,
@@ -4853,7 +4944,17 @@ pub const MOUNTMGR_MOUNT_POINTS = extern struct {
NumberOfMountPoints: ULONG,
MountPoints: [1]MOUNTMGR_MOUNT_POINT,
};
-pub const IOCTL_MOUNTMGR_QUERY_POINTS: ULONG = 0x6d0008;
+pub const IOCTL_MOUNTMGR_QUERY_POINTS = CTL_CODE(MOUNTMGRCONTROLTYPE, 2, .METHOD_BUFFERED, FILE_ANY_ACCESS);
+
+pub const MOUNTMGR_TARGET_NAME = extern struct {
+ DeviceNameLength: USHORT,
+ DeviceName: [1]WCHAR,
+};
+pub const MOUNTMGR_VOLUME_PATHS = extern struct {
+ MultiSzLength: ULONG,
+ MultiSz: [1]WCHAR,
+};
+pub const IOCTL_MOUNTMGR_QUERY_DOS_VOLUME_PATH = CTL_CODE(MOUNTMGRCONTROLTYPE, 12, .METHOD_BUFFERED, FILE_ANY_ACCESS);
pub const OBJECT_INFORMATION_CLASS = enum(c_int) {
ObjectBasicInformation = 0,
diff --git a/lib/std/posix.zig b/lib/std/posix.zig
index e80e64b45e..9b6a38eb45 100644
--- a/lib/std/posix.zig
+++ b/lib/std/posix.zig
@@ -542,7 +542,7 @@ pub fn reboot(cmd: RebootCommand) RebootError!void {
))) {
.SUCCESS => {},
.PERM => return error.PermissionDenied,
- else => |err| return std.os.unexpectedErrno(err),
+ else => |err| return std.posix.unexpectedErrno(err),
}
switch (cmd) {
.CAD_OFF => {},
@@ -1602,6 +1602,10 @@ pub fn openZ(file_path: [*:0]const u8, flags: O, perm: mode_t) OpenError!fd_t {
.PERM => return error.AccessDenied,
.EXIST => return error.PathAlreadyExists,
.BUSY => return error.DeviceBusy,
+ .ILSEQ => |err| if (native_os == .wasi)
+ return error.InvalidUtf8
+ else
+ return unexpectedErrno(err),
else => |err| return unexpectedErrno(err),
}
}
@@ -1771,6 +1775,10 @@ pub fn openatZ(dir_fd: fd_t, file_path: [*:0]const u8, flags: O, mode: mode_t) O
.OPNOTSUPP => return error.FileLocksNotSupported,
.AGAIN => return error.WouldBlock,
.TXTBSY => return error.FileBusy,
+ .ILSEQ => |err| if (native_os == .wasi)
+ return error.InvalidUtf8
+ else
+ return unexpectedErrno(err),
else => |err| return unexpectedErrno(err),
}
}
diff --git a/lib/std/static_string_map.zig b/lib/std/static_string_map.zig
new file mode 100644
index 0000000000..1136bde587
--- /dev/null
+++ b/lib/std/static_string_map.zig
@@ -0,0 +1,502 @@
+const std = @import("std.zig");
+const mem = std.mem;
+
+/// Static string map optimized for small sets of disparate string keys.
+/// Works by separating the keys by length at initialization and only checking
+/// strings of equal length at runtime.
+pub fn StaticStringMap(comptime V: type) type {
+ return StaticStringMapWithEql(V, defaultEql);
+}
+
+/// Like `std.mem.eql`, but takes advantage of the fact that the lengths
+/// of `a` and `b` are known to be equal.
+pub fn defaultEql(a: []const u8, b: []const u8) bool {
+ if (a.ptr == b.ptr) return true;
+ for (a, b) |a_elem, b_elem| {
+ if (a_elem != b_elem) return false;
+ }
+ return true;
+}
+
+/// Like `std.ascii.eqlIgnoreCase` but takes advantage of the fact that
+/// the lengths of `a` and `b` are known to be equal.
+pub fn eqlAsciiIgnoreCase(a: []const u8, b: []const u8) bool {
+ if (a.ptr == b.ptr) return true;
+ for (a, b) |a_c, b_c| {
+ if (std.ascii.toLower(a_c) != std.ascii.toLower(b_c)) return false;
+ }
+ return true;
+}
+
+/// StaticStringMap, but accepts an equality function (`eql`).
+/// The `eql` function is only called to determine the equality
+/// of equal length strings. Any strings that are not equal length
+/// are never compared using the `eql` function.
+pub fn StaticStringMapWithEql(
+ comptime V: type,
+ comptime eql: fn (a: []const u8, b: []const u8) bool,
+) type {
+ return struct {
+ kvs: *const KVs = &empty_kvs,
+ len_indexes: [*]const u32 = &empty_len_indexes,
+ len_indexes_len: u32 = 0,
+ min_len: u32 = std.math.maxInt(u32),
+ max_len: u32 = 0,
+
+ pub const KV = struct {
+ key: []const u8,
+ value: V,
+ };
+
+ const Self = @This();
+ const KVs = struct {
+ keys: [*]const []const u8,
+ values: [*]const V,
+ len: u32,
+ };
+ const empty_kvs = KVs{
+ .keys = &empty_keys,
+ .values = &empty_vals,
+ .len = 0,
+ };
+ const empty_len_indexes = [0]u32{};
+ const empty_keys = [0][]const u8{};
+ const empty_vals = [0]V{};
+
+ /// Returns a map backed by static, comptime allocated memory.
+ ///
+ /// `kvs_list` must be either a list of `struct { []const u8, V }`
+ /// (key-value pair) tuples, or a list of `struct { []const u8 }`
+ /// (only keys) tuples if `V` is `void`.
+ pub inline fn initComptime(comptime kvs_list: anytype) Self {
+ comptime {
+ @setEvalBranchQuota(30 * kvs_list.len);
+ var self = Self{};
+ if (kvs_list.len == 0)
+ return self;
+
+ var sorted_keys: [kvs_list.len][]const u8 = undefined;
+ var sorted_vals: [kvs_list.len]V = undefined;
+
+ self.initSortedKVs(kvs_list, &sorted_keys, &sorted_vals);
+ const final_keys = sorted_keys;
+ const final_vals = sorted_vals;
+ self.kvs = &.{
+ .keys = &final_keys,
+ .values = &final_vals,
+ .len = @intCast(kvs_list.len),
+ };
+
+ var len_indexes: [self.max_len + 1]u32 = undefined;
+ self.initLenIndexes(&len_indexes);
+ const final_len_indexes = len_indexes;
+ self.len_indexes = &final_len_indexes;
+ self.len_indexes_len = @intCast(len_indexes.len);
+ return self;
+ }
+ }
+
+ /// Returns a map backed by memory allocated with `allocator`.
+ ///
+ /// Handles `kvs_list` the same way as `initComptime()`.
+ pub fn init(kvs_list: anytype, allocator: mem.Allocator) !Self {
+ var self = Self{};
+ if (kvs_list.len == 0)
+ return self;
+
+ const sorted_keys = try allocator.alloc([]const u8, kvs_list.len);
+ errdefer allocator.free(sorted_keys);
+ const sorted_vals = try allocator.alloc(V, kvs_list.len);
+ errdefer allocator.free(sorted_vals);
+ const kvs = try allocator.create(KVs);
+ errdefer allocator.destroy(kvs);
+
+ self.initSortedKVs(kvs_list, sorted_keys, sorted_vals);
+ kvs.* = .{
+ .keys = sorted_keys.ptr,
+ .values = sorted_vals.ptr,
+ .len = kvs_list.len,
+ };
+ self.kvs = kvs;
+
+ const len_indexes = try allocator.alloc(u32, self.max_len + 1);
+ self.initLenIndexes(len_indexes);
+ self.len_indexes = len_indexes.ptr;
+ self.len_indexes_len = @intCast(len_indexes.len);
+ return self;
+ }
+
+ /// this method should only be used with init() and not with initComptime().
+ pub fn deinit(self: Self, allocator: mem.Allocator) void {
+ allocator.free(self.len_indexes[0..self.len_indexes_len]);
+ allocator.free(self.kvs.keys[0..self.kvs.len]);
+ allocator.free(self.kvs.values[0..self.kvs.len]);
+ allocator.destroy(self.kvs);
+ }
+
+ const SortContext = struct {
+ keys: [][]const u8,
+ vals: []V,
+
+ pub fn lessThan(ctx: @This(), a: usize, b: usize) bool {
+ return ctx.keys[a].len < ctx.keys[b].len;
+ }
+
+ pub fn swap(ctx: @This(), a: usize, b: usize) void {
+ std.mem.swap([]const u8, &ctx.keys[a], &ctx.keys[b]);
+ std.mem.swap(V, &ctx.vals[a], &ctx.vals[b]);
+ }
+ };
+
+ fn initSortedKVs(
+ self: *Self,
+ kvs_list: anytype,
+ sorted_keys: [][]const u8,
+ sorted_vals: []V,
+ ) void {
+ for (kvs_list, 0..) |kv, i| {
+ sorted_keys[i] = kv.@"0";
+ sorted_vals[i] = if (V == void) {} else kv.@"1";
+ self.min_len = @intCast(@min(self.min_len, kv.@"0".len));
+ self.max_len = @intCast(@max(self.max_len, kv.@"0".len));
+ }
+ mem.sortUnstableContext(0, sorted_keys.len, SortContext{
+ .keys = sorted_keys,
+ .vals = sorted_vals,
+ });
+ }
+
+ fn initLenIndexes(self: Self, len_indexes: []u32) void {
+ var len: usize = 0;
+ var i: u32 = 0;
+ while (len <= self.max_len) : (len += 1) {
+ // find the first keyword len == len
+ while (len > self.kvs.keys[i].len) {
+ i += 1;
+ }
+ len_indexes[len] = i;
+ }
+ }
+
+ /// Checks if the map has a value for the key.
+ pub fn has(self: Self, str: []const u8) bool {
+ return self.get(str) != null;
+ }
+
+ /// Returns the value for the key if any, else null.
+ pub fn get(self: Self, str: []const u8) ?V {
+ if (self.kvs.len == 0)
+ return null;
+
+ return self.kvs.values[self.getIndex(str) orelse return null];
+ }
+
+ pub fn getIndex(self: Self, str: []const u8) ?usize {
+ const kvs = self.kvs.*;
+ if (kvs.len == 0)
+ return null;
+
+ if (str.len < self.min_len or str.len > self.max_len)
+ return null;
+
+ var i = self.len_indexes[str.len];
+ while (true) {
+ const key = kvs.keys[i];
+ if (key.len != str.len)
+ return null;
+ if (eql(key, str))
+ return i;
+ i += 1;
+ if (i >= kvs.len)
+ return null;
+ }
+ }
+
+ /// Returns the longest key, value pair where key is a prefix of `str`
+ /// else null.
+ pub fn getLongestPrefix(self: Self, str: []const u8) ?KV {
+ if (self.kvs.len == 0)
+ return null;
+ const i = self.getLongestPrefixIndex(str) orelse return null;
+ const kvs = self.kvs.*;
+ return .{
+ .key = kvs.keys[i],
+ .value = kvs.values[i],
+ };
+ }
+
+ pub fn getLongestPrefixIndex(self: Self, str: []const u8) ?usize {
+ if (self.kvs.len == 0)
+ return null;
+
+ if (str.len < self.min_len)
+ return null;
+
+ var len = @min(self.max_len, str.len);
+ while (len >= self.min_len) : (len -= 1) {
+ if (self.getIndex(str[0..len])) |i|
+ return i;
+ }
+ return null;
+ }
+
+ pub fn keys(self: Self) []const []const u8 {
+ const kvs = self.kvs.*;
+ return kvs.keys[0..kvs.len];
+ }
+
+ pub fn values(self: Self) []const V {
+ const kvs = self.kvs.*;
+ return kvs.values[0..kvs.len];
+ }
+ };
+}
+
+const TestEnum = enum { A, B, C, D, E };
+const TestMap = StaticStringMap(TestEnum);
+const TestKV = struct { []const u8, TestEnum };
+const TestMapVoid = StaticStringMap(void);
+const TestKVVoid = struct { []const u8 };
+const TestMapWithEql = StaticStringMapWithEql(TestEnum, eqlAsciiIgnoreCase);
+const testing = std.testing;
+const test_alloc = testing.allocator;
+
+test "list literal of list literals" {
+ const slice = [_]TestKV{
+ .{ "these", .D },
+ .{ "have", .A },
+ .{ "nothing", .B },
+ .{ "incommon", .C },
+ .{ "samelen", .E },
+ };
+ const map = TestMap.initComptime(slice);
+ try testMap(map);
+ // Default comparison is case sensitive
+ try testing.expect(null == map.get("NOTHING"));
+
+ // runtime init(), deinit()
+ const map_rt = try TestMap.init(slice, test_alloc);
+ defer map_rt.deinit(test_alloc);
+ try testMap(map_rt);
+ // Default comparison is case sensitive
+ try testing.expect(null == map_rt.get("NOTHING"));
+}
+
+test "array of structs" {
+ const slice = [_]TestKV{
+ .{ "these", .D },
+ .{ "have", .A },
+ .{ "nothing", .B },
+ .{ "incommon", .C },
+ .{ "samelen", .E },
+ };
+
+ try testMap(TestMap.initComptime(slice));
+}
+
+test "slice of structs" {
+ const slice = [_]TestKV{
+ .{ "these", .D },
+ .{ "have", .A },
+ .{ "nothing", .B },
+ .{ "incommon", .C },
+ .{ "samelen", .E },
+ };
+
+ try testMap(TestMap.initComptime(slice));
+}
+
+fn testMap(map: anytype) !void {
+ try testing.expectEqual(TestEnum.A, map.get("have").?);
+ try testing.expectEqual(TestEnum.B, map.get("nothing").?);
+ try testing.expect(null == map.get("missing"));
+ try testing.expectEqual(TestEnum.D, map.get("these").?);
+ try testing.expectEqual(TestEnum.E, map.get("samelen").?);
+
+ try testing.expect(!map.has("missing"));
+ try testing.expect(map.has("these"));
+
+ try testing.expect(null == map.get(""));
+ try testing.expect(null == map.get("averylongstringthathasnomatches"));
+}
+
+test "void value type, slice of structs" {
+ const slice = [_]TestKVVoid{
+ .{"these"},
+ .{"have"},
+ .{"nothing"},
+ .{"incommon"},
+ .{"samelen"},
+ };
+ const map = TestMapVoid.initComptime(slice);
+ try testSet(map);
+ // Default comparison is case sensitive
+ try testing.expect(null == map.get("NOTHING"));
+}
+
+test "void value type, list literal of list literals" {
+ const slice = [_]TestKVVoid{
+ .{"these"},
+ .{"have"},
+ .{"nothing"},
+ .{"incommon"},
+ .{"samelen"},
+ };
+
+ try testSet(TestMapVoid.initComptime(slice));
+}
+
+fn testSet(map: TestMapVoid) !void {
+ try testing.expectEqual({}, map.get("have").?);
+ try testing.expectEqual({}, map.get("nothing").?);
+ try testing.expect(null == map.get("missing"));
+ try testing.expectEqual({}, map.get("these").?);
+ try testing.expectEqual({}, map.get("samelen").?);
+
+ try testing.expect(!map.has("missing"));
+ try testing.expect(map.has("these"));
+
+ try testing.expect(null == map.get(""));
+ try testing.expect(null == map.get("averylongstringthathasnomatches"));
+}
+
+fn testStaticStringMapWithEql(map: TestMapWithEql) !void {
+ try testMap(map);
+ try testing.expectEqual(TestEnum.A, map.get("HAVE").?);
+ try testing.expectEqual(TestEnum.E, map.get("SameLen").?);
+ try testing.expect(null == map.get("SameLength"));
+ try testing.expect(map.has("ThESe"));
+}
+
+test "StaticStringMapWithEql" {
+ const slice = [_]TestKV{
+ .{ "these", .D },
+ .{ "have", .A },
+ .{ "nothing", .B },
+ .{ "incommon", .C },
+ .{ "samelen", .E },
+ };
+
+ try testStaticStringMapWithEql(TestMapWithEql.initComptime(slice));
+}
+
+test "empty" {
+ const m1 = StaticStringMap(usize).initComptime(.{});
+ try testing.expect(null == m1.get("anything"));
+
+ const m2 = StaticStringMapWithEql(usize, eqlAsciiIgnoreCase).initComptime(.{});
+ try testing.expect(null == m2.get("anything"));
+
+ const m3 = try StaticStringMap(usize).init(.{}, test_alloc);
+ try testing.expect(null == m3.get("anything"));
+
+ const m4 = try StaticStringMapWithEql(usize, eqlAsciiIgnoreCase).init(.{}, test_alloc);
+ try testing.expect(null == m4.get("anything"));
+}
+
+test "redundant entries" {
+ const slice = [_]TestKV{
+ .{ "redundant", .D },
+ .{ "theNeedle", .A },
+ .{ "redundant", .B },
+ .{ "re" ++ "dundant", .C },
+ .{ "redun" ++ "dant", .E },
+ };
+ const map = TestMap.initComptime(slice);
+
+ // No promises about which one you get:
+ try testing.expect(null != map.get("redundant"));
+
+ // Default map is not case sensitive:
+ try testing.expect(null == map.get("REDUNDANT"));
+
+ try testing.expectEqual(TestEnum.A, map.get("theNeedle").?);
+}
+
+test "redundant insensitive" {
+ const slice = [_]TestKV{
+ .{ "redundant", .D },
+ .{ "theNeedle", .A },
+ .{ "redundanT", .B },
+ .{ "RE" ++ "dundant", .C },
+ .{ "redun" ++ "DANT", .E },
+ };
+
+ const map = TestMapWithEql.initComptime(slice);
+
+ // No promises about which result you'll get ...
+ try testing.expect(null != map.get("REDUNDANT"));
+ try testing.expect(null != map.get("ReDuNdAnT"));
+ try testing.expectEqual(TestEnum.A, map.get("theNeedle").?);
+}
+
+test "comptime-only value" {
+ const map = StaticStringMap(type).initComptime(.{
+ .{ "a", struct {
+ pub const foo = 1;
+ } },
+ .{ "b", struct {
+ pub const foo = 2;
+ } },
+ .{ "c", struct {
+ pub const foo = 3;
+ } },
+ });
+
+ try testing.expect(map.get("a").?.foo == 1);
+ try testing.expect(map.get("b").?.foo == 2);
+ try testing.expect(map.get("c").?.foo == 3);
+ try testing.expect(map.get("d") == null);
+}
+
+test "getLongestPrefix" {
+ const slice = [_]TestKV{
+ .{ "a", .A },
+ .{ "aa", .B },
+ .{ "aaa", .C },
+ .{ "aaaa", .D },
+ };
+
+ const map = TestMap.initComptime(slice);
+
+ try testing.expectEqual(null, map.getLongestPrefix(""));
+ try testing.expectEqual(null, map.getLongestPrefix("bar"));
+ try testing.expectEqualStrings("aaaa", map.getLongestPrefix("aaaabar").?.key);
+ try testing.expectEqualStrings("aaa", map.getLongestPrefix("aaabar").?.key);
+}
+
+test "getLongestPrefix2" {
+ const slice = [_]struct { []const u8, u8 }{
+ .{ "one", 1 },
+ .{ "two", 2 },
+ .{ "three", 3 },
+ .{ "four", 4 },
+ .{ "five", 5 },
+ .{ "six", 6 },
+ .{ "seven", 7 },
+ .{ "eight", 8 },
+ .{ "nine", 9 },
+ };
+ const map = StaticStringMap(u8).initComptime(slice);
+
+ try testing.expectEqual(1, map.get("one"));
+ try testing.expectEqual(null, map.get("o"));
+ try testing.expectEqual(null, map.get("onexxx"));
+ try testing.expectEqual(9, map.get("nine"));
+ try testing.expectEqual(null, map.get("n"));
+ try testing.expectEqual(null, map.get("ninexxx"));
+ try testing.expectEqual(null, map.get("xxx"));
+
+ try testing.expectEqual(1, map.getLongestPrefix("one").?.value);
+ try testing.expectEqual(1, map.getLongestPrefix("onexxx").?.value);
+ try testing.expectEqual(null, map.getLongestPrefix("o"));
+ try testing.expectEqual(null, map.getLongestPrefix("on"));
+ try testing.expectEqual(9, map.getLongestPrefix("nine").?.value);
+ try testing.expectEqual(9, map.getLongestPrefix("ninexxx").?.value);
+ try testing.expectEqual(null, map.getLongestPrefix("n"));
+ try testing.expectEqual(null, map.getLongestPrefix("xxx"));
+}
+
+test "long kvs_list doesn't exceed @setEvalBranchQuota" {
+ _ = TestMapVoid.initComptime([1]TestKVVoid{.{"x"}} ** 1_000);
+}
diff --git a/lib/std/std.zig b/lib/std/std.zig
index 8aa12ff31a..bd8c6db276 100644
--- a/lib/std/std.zig
+++ b/lib/std/std.zig
@@ -16,8 +16,8 @@ pub const BufMap = @import("buf_map.zig").BufMap;
pub const BufSet = @import("buf_set.zig").BufSet;
/// Deprecated: use `process.Child`.
pub const ChildProcess = @import("child_process.zig").ChildProcess;
-pub const ComptimeStringMap = comptime_string_map.ComptimeStringMap;
-pub const ComptimeStringMapWithEql = comptime_string_map.ComptimeStringMapWithEql;
+pub const StaticStringMap = static_string_map.StaticStringMap;
+pub const StaticStringMapWithEql = static_string_map.StaticStringMapWithEql;
pub const DoublyLinkedList = @import("linked_list.zig").DoublyLinkedList;
pub const DynLib = @import("dynamic_library.zig").DynLib;
pub const DynamicBitSet = bit_set.DynamicBitSet;
@@ -62,7 +62,7 @@ pub const builtin = @import("builtin.zig");
pub const c = @import("c.zig");
pub const coff = @import("coff.zig");
pub const compress = @import("compress.zig");
-pub const comptime_string_map = @import("comptime_string_map.zig");
+pub const static_string_map = @import("static_string_map.zig");
pub const crypto = @import("crypto.zig");
pub const debug = @import("debug.zig");
pub const dwarf = @import("dwarf.zig");
diff --git a/lib/std/zig/AstGen.zig b/lib/std/zig/AstGen.zig
index a52007eabf..9be6a9f605 100644
--- a/lib/std/zig/AstGen.zig
+++ b/lib/std/zig/AstGen.zig
@@ -10125,7 +10125,7 @@ fn calleeExpr(
}
}
-const primitive_instrs = std.ComptimeStringMap(Zir.Inst.Ref, .{
+const primitive_instrs = std.StaticStringMap(Zir.Inst.Ref).initComptime(.{
.{ "anyerror", .anyerror_type },
.{ "anyframe", .anyframe_type },
.{ "anyopaque", .anyopaque_type },
@@ -10173,14 +10173,14 @@ const primitive_instrs = std.ComptimeStringMap(Zir.Inst.Ref, .{
comptime {
// These checks ensure that std.zig.primitives stays in sync with the primitive->Zir map.
const primitives = std.zig.primitives;
- for (primitive_instrs.kvs) |kv| {
- if (!primitives.isPrimitive(kv.key)) {
- @compileError("std.zig.isPrimitive() is not aware of Zir instr '" ++ @tagName(kv.value) ++ "'");
+ for (primitive_instrs.keys(), primitive_instrs.values()) |key, value| {
+ if (!primitives.isPrimitive(key)) {
+ @compileError("std.zig.isPrimitive() is not aware of Zir instr '" ++ @tagName(value) ++ "'");
}
}
- for (primitives.names.kvs) |kv| {
- if (primitive_instrs.get(kv.key) == null) {
- @compileError("std.zig.primitives entry '" ++ kv.key ++ "' does not have a corresponding Zir instr");
+ for (primitives.names.keys()) |key| {
+ if (primitive_instrs.get(key) == null) {
+ @compileError("std.zig.primitives entry '" ++ key ++ "' does not have a corresponding Zir instr");
}
}
}
diff --git a/lib/std/zig/BuiltinFn.zig b/lib/std/zig/BuiltinFn.zig
index 11d6a17303..4bea0278fa 100644
--- a/lib/std/zig/BuiltinFn.zig
+++ b/lib/std/zig/BuiltinFn.zig
@@ -160,7 +160,7 @@ param_count: ?u8,
pub const list = list: {
@setEvalBranchQuota(3000);
- break :list std.ComptimeStringMap(@This(), .{
+ break :list std.StaticStringMap(@This()).initComptime(.{
.{
"@addWithOverflow",
.{
diff --git a/lib/std/zig/LibCInstallation.zig b/lib/std/zig/LibCInstallation.zig
index f8ee9be602..656ea32981 100644
--- a/lib/std/zig/LibCInstallation.zig
+++ b/lib/std/zig/LibCInstallation.zig
@@ -182,18 +182,18 @@ pub fn findNative(args: FindNativeOptions) FindError!LibCInstallation {
});
return self;
} else if (is_windows) {
- var sdk = std.zig.WindowsSdk.find(args.allocator) catch |err| switch (err) {
+ const sdk = std.zig.WindowsSdk.find(args.allocator) catch |err| switch (err) {
error.NotFound => return error.WindowsSdkNotFound,
error.PathTooLong => return error.WindowsSdkNotFound,
error.OutOfMemory => return error.OutOfMemory,
};
defer sdk.free(args.allocator);
- try self.findNativeMsvcIncludeDir(args, &sdk);
- try self.findNativeMsvcLibDir(args, &sdk);
- try self.findNativeKernel32LibDir(args, &sdk);
- try self.findNativeIncludeDirWindows(args, &sdk);
- try self.findNativeCrtDirWindows(args, &sdk);
+ try self.findNativeMsvcIncludeDir(args, sdk);
+ try self.findNativeMsvcLibDir(args, sdk);
+ try self.findNativeKernel32LibDir(args, sdk);
+ try self.findNativeIncludeDirWindows(args, sdk);
+ try self.findNativeCrtDirWindows(args, sdk);
} else if (is_haiku) {
try self.findNativeIncludeDirPosix(args);
try self.findNativeGccDirHaiku(args);
@@ -358,19 +358,19 @@ fn findNativeIncludeDirPosix(self: *LibCInstallation, args: FindNativeOptions) F
fn findNativeIncludeDirWindows(
self: *LibCInstallation,
args: FindNativeOptions,
- sdk: *std.zig.WindowsSdk,
+ sdk: std.zig.WindowsSdk,
) FindError!void {
const allocator = args.allocator;
- var search_buf: [2]Search = undefined;
- const searches = fillSearch(&search_buf, sdk);
+ var install_buf: [2]std.zig.WindowsSdk.Installation = undefined;
+ const installs = fillInstallations(&install_buf, sdk);
var result_buf = std.ArrayList(u8).init(allocator);
defer result_buf.deinit();
- for (searches) |search| {
+ for (installs) |install| {
result_buf.shrinkAndFree(0);
- try result_buf.writer().print("{s}\\Include\\{s}\\ucrt", .{ search.path, search.version });
+ try result_buf.writer().print("{s}\\Include\\{s}\\ucrt", .{ install.path, install.version });
var dir = fs.cwd().openDir(result_buf.items, .{}) catch |err| switch (err) {
error.FileNotFound,
@@ -397,12 +397,12 @@ fn findNativeIncludeDirWindows(
fn findNativeCrtDirWindows(
self: *LibCInstallation,
args: FindNativeOptions,
- sdk: *std.zig.WindowsSdk,
+ sdk: std.zig.WindowsSdk,
) FindError!void {
const allocator = args.allocator;
- var search_buf: [2]Search = undefined;
- const searches = fillSearch(&search_buf, sdk);
+ var install_buf: [2]std.zig.WindowsSdk.Installation = undefined;
+ const installs = fillInstallations(&install_buf, sdk);
var result_buf = std.ArrayList(u8).init(allocator);
defer result_buf.deinit();
@@ -415,9 +415,9 @@ fn findNativeCrtDirWindows(
else => return error.UnsupportedArchitecture,
};
- for (searches) |search| {
+ for (installs) |install| {
result_buf.shrinkAndFree(0);
- try result_buf.writer().print("{s}\\Lib\\{s}\\ucrt\\{s}", .{ search.path, search.version, arch_sub_dir });
+ try result_buf.writer().print("{s}\\Lib\\{s}\\ucrt\\{s}", .{ install.path, install.version, arch_sub_dir });
var dir = fs.cwd().openDir(result_buf.items, .{}) catch |err| switch (err) {
error.FileNotFound,
@@ -464,12 +464,12 @@ fn findNativeGccDirHaiku(self: *LibCInstallation, args: FindNativeOptions) FindE
fn findNativeKernel32LibDir(
self: *LibCInstallation,
args: FindNativeOptions,
- sdk: *std.zig.WindowsSdk,
+ sdk: std.zig.WindowsSdk,
) FindError!void {
const allocator = args.allocator;
- var search_buf: [2]Search = undefined;
- const searches = fillSearch(&search_buf, sdk);
+ var install_buf: [2]std.zig.WindowsSdk.Installation = undefined;
+ const installs = fillInstallations(&install_buf, sdk);
var result_buf = std.ArrayList(u8).init(allocator);
defer result_buf.deinit();
@@ -482,10 +482,10 @@ fn findNativeKernel32LibDir(
else => return error.UnsupportedArchitecture,
};
- for (searches) |search| {
+ for (installs) |install| {
result_buf.shrinkAndFree(0);
const stream = result_buf.writer();
- try stream.print("{s}\\Lib\\{s}\\um\\{s}", .{ search.path, search.version, arch_sub_dir });
+ try stream.print("{s}\\Lib\\{s}\\um\\{s}", .{ install.path, install.version, arch_sub_dir });
var dir = fs.cwd().openDir(result_buf.items, .{}) catch |err| switch (err) {
error.FileNotFound,
@@ -511,7 +511,7 @@ fn findNativeKernel32LibDir(
fn findNativeMsvcIncludeDir(
self: *LibCInstallation,
args: FindNativeOptions,
- sdk: *std.zig.WindowsSdk,
+ sdk: std.zig.WindowsSdk,
) FindError!void {
const allocator = args.allocator;
@@ -543,7 +543,7 @@ fn findNativeMsvcIncludeDir(
fn findNativeMsvcLibDir(
self: *LibCInstallation,
args: FindNativeOptions,
- sdk: *std.zig.WindowsSdk,
+ sdk: std.zig.WindowsSdk,
) FindError!void {
const allocator = args.allocator;
const msvc_lib_dir = sdk.msvc_lib_dir orelse return error.LibCRuntimeNotFound;
@@ -654,28 +654,20 @@ fn printVerboseInvocation(
}
}
-const Search = struct {
- path: []const u8,
- version: []const u8,
-};
-
-fn fillSearch(search_buf: *[2]Search, sdk: *std.zig.WindowsSdk) []Search {
- var search_end: usize = 0;
+fn fillInstallations(
+ installs: *[2]std.zig.WindowsSdk.Installation,
+ sdk: std.zig.WindowsSdk,
+) []std.zig.WindowsSdk.Installation {
+ var installs_len: usize = 0;
if (sdk.windows10sdk) |windows10sdk| {
- search_buf[search_end] = .{
- .path = windows10sdk.path,
- .version = windows10sdk.version,
- };
- search_end += 1;
+ installs[installs_len] = windows10sdk;
+ installs_len += 1;
}
if (sdk.windows81sdk) |windows81sdk| {
- search_buf[search_end] = .{
- .path = windows81sdk.path,
- .version = windows81sdk.version,
- };
- search_end += 1;
+ installs[installs_len] = windows81sdk;
+ installs_len += 1;
}
- return search_buf[0..search_end];
+ return installs[0..installs_len];
}
const inf_loop_env_key = "ZIG_IS_DETECTING_LIBC_PATHS";
diff --git a/lib/std/zig/WindowsSdk.zig b/lib/std/zig/WindowsSdk.zig
index 3b9b788a7b..bebb0fc5a1 100644
--- a/lib/std/zig/WindowsSdk.zig
+++ b/lib/std/zig/WindowsSdk.zig
@@ -1,5 +1,5 @@
-windows10sdk: ?Windows10Sdk,
-windows81sdk: ?Windows81Sdk,
+windows10sdk: ?Installation,
+windows81sdk: ?Installation,
msvc_lib_dir: ?[]const u8,
const WindowsSdk = @This();
@@ -9,7 +9,7 @@ const builtin = @import("builtin");
const windows = std.os.windows;
const RRF = windows.advapi32.RRF;
-const WINDOWS_KIT_REG_KEY = "SOFTWARE\\Microsoft\\Windows Kits\\Installed Roots";
+const windows_kits_reg_key = "SOFTWARE\\Microsoft\\Windows Kits\\Installed Roots";
// https://learn.microsoft.com/en-us/windows/win32/msi/productversion
const version_major_minor_max_length = "255.255".len;
@@ -23,34 +23,24 @@ pub fn find(allocator: std.mem.Allocator) error{ OutOfMemory, NotFound, PathTooL
if (builtin.os.tag != .windows) return error.NotFound;
//note(dimenus): If this key doesn't exist, neither the Win 8 SDK nor the Win 10 SDK is installed
- const roots_key = RegistryWtf8.openKey(windows.HKEY_LOCAL_MACHINE, WINDOWS_KIT_REG_KEY) catch |err| switch (err) {
+ const roots_key = RegistryWtf8.openKey(windows.HKEY_LOCAL_MACHINE, windows_kits_reg_key) catch |err| switch (err) {
error.KeyNotFound => return error.NotFound,
};
defer roots_key.closeKey();
- const windows10sdk: ?Windows10Sdk = blk: {
- const windows10sdk = Windows10Sdk.find(allocator) catch |err| switch (err) {
- error.Windows10SdkNotFound,
- error.PathTooLong,
- error.VersionTooLong,
- => break :blk null,
- error.OutOfMemory => return error.OutOfMemory,
- };
- const is_valid_version = windows10sdk.isValidVersion();
- if (!is_valid_version) break :blk null;
- break :blk windows10sdk;
+ const windows10sdk = Installation.find(allocator, roots_key, "KitsRoot10", "", "v10.0") catch |err| switch (err) {
+ error.InstallationNotFound => null,
+ error.PathTooLong => null,
+ error.VersionTooLong => null,
+ error.OutOfMemory => return error.OutOfMemory,
};
errdefer if (windows10sdk) |*w| w.free(allocator);
- const windows81sdk: ?Windows81Sdk = blk: {
- const windows81sdk = Windows81Sdk.find(allocator, &roots_key) catch |err| switch (err) {
- error.Windows81SdkNotFound => break :blk null,
- error.PathTooLong => break :blk null,
- error.VersionTooLong => break :blk null,
- error.OutOfMemory => return error.OutOfMemory,
- };
- // no check
- break :blk windows81sdk;
+ const windows81sdk = Installation.find(allocator, roots_key, "KitsRoot81", "winver", "v8.1") catch |err| switch (err) {
+ error.InstallationNotFound => null,
+ error.PathTooLong => null,
+ error.VersionTooLong => null,
+ error.OutOfMemory => return error.OutOfMemory,
};
errdefer if (windows81sdk) |*w| w.free(allocator);
@@ -60,96 +50,91 @@ pub fn find(allocator: std.mem.Allocator) error{ OutOfMemory, NotFound, PathTooL
};
errdefer allocator.free(msvc_lib_dir);
- return WindowsSdk{
+ return .{
.windows10sdk = windows10sdk,
.windows81sdk = windows81sdk,
.msvc_lib_dir = msvc_lib_dir,
};
}
-pub fn free(self: *const WindowsSdk, allocator: std.mem.Allocator) void {
- if (self.windows10sdk) |*w10sdk| {
+pub fn free(sdk: WindowsSdk, allocator: std.mem.Allocator) void {
+ if (sdk.windows10sdk) |*w10sdk| {
w10sdk.free(allocator);
}
- if (self.windows81sdk) |*w81sdk| {
+ if (sdk.windows81sdk) |*w81sdk| {
w81sdk.free(allocator);
}
- if (self.msvc_lib_dir) |msvc_lib_dir| {
+ if (sdk.msvc_lib_dir) |msvc_lib_dir| {
allocator.free(msvc_lib_dir);
}
}
-/// Iterates via `iterator` and collects all folders with names starting with `optional_prefix`
-/// and similar to SemVer. Returns slice of folder names sorted in descending order.
+/// Iterates via `iterator` and collects all folders with names starting with `strip_prefix`
+/// and a version. Returns slice of version strings sorted in descending order.
/// Caller owns result.
-fn iterateAndFilterBySemVer(
+fn iterateAndFilterByVersion(
iterator: *std.fs.Dir.Iterator,
allocator: std.mem.Allocator,
- comptime optional_prefix: ?[]const u8,
-) error{ OutOfMemory, VersionNotFound }![][]const u8 {
- var dirs_filtered_list = std.ArrayList([]const u8).init(allocator);
- errdefer {
- for (dirs_filtered_list.items) |filtered_dir| allocator.free(filtered_dir);
- dirs_filtered_list.deinit();
- }
-
- var normalized_name_buf: [std.fs.MAX_NAME_BYTES + ".0+build.0".len]u8 = undefined;
- var normalized_name_fbs = std.io.fixedBufferStream(&normalized_name_buf);
- const normalized_name_w = normalized_name_fbs.writer();
- iterate_folder: while (true) : (normalized_name_fbs.reset()) {
- const maybe_entry = iterator.next() catch continue :iterate_folder;
- const entry = maybe_entry orelse break :iterate_folder;
-
- if (entry.kind != .directory)
- continue :iterate_folder;
-
- // invalidated on next iteration
- const subfolder_name = blk: {
- if (comptime optional_prefix) |prefix| {
- if (!std.mem.startsWith(u8, entry.name, prefix)) continue :iterate_folder;
- break :blk entry.name[prefix.len..];
- } else break :blk entry.name;
- };
-
- { // check if subfolder name looks similar to SemVer
- switch (std.mem.count(u8, subfolder_name, ".")) {
- 0 => normalized_name_w.print("{s}.0.0+build.0", .{subfolder_name}) catch unreachable, // 17 => 17.0.0+build.0
- 1 => if (std.mem.indexOfScalar(u8, subfolder_name, '_')) |underscore_pos| blk: { // 17.0_9e9cbb98 => 17.0.1+build.9e9cbb98
- var subfolder_name_tmp_copy_buf: [std.fs.MAX_NAME_BYTES]u8 = undefined;
- const subfolder_name_tmp_copy = subfolder_name_tmp_copy_buf[0..subfolder_name.len];
- @memcpy(subfolder_name_tmp_copy, subfolder_name);
-
- subfolder_name_tmp_copy[underscore_pos] = '.'; // 17.0_9e9cbb98 => 17.0.9e9cbb98
- var subfolder_name_parts = std.mem.splitScalar(u8, subfolder_name_tmp_copy, '.'); // [ 17, 0, 9e9cbb98 ]
-
- const first = subfolder_name_parts.first(); // 17
- const second = subfolder_name_parts.next().?; // 0
- const third = subfolder_name_parts.rest(); // 9e9cbb98
+ prefix: []const u8,
+) error{OutOfMemory}![][]const u8 {
+ const Version = struct {
+ nums: [4]u32,
+ build: []const u8,
+
+ fn parseNum(num: []const u8) ?u32 {
+ if (num[0] == '0' and num.len > 1) return null;
+ return std.fmt.parseInt(u32, num, 10) catch null;
+ }
- break :blk normalized_name_w.print("{s}.{s}.1+build.{s}", .{ first, second, third }) catch unreachable; // [ 17, 0, 9e9cbb98 ] => 17.0.1+build.9e9cbb98
- } else normalized_name_w.print("{s}.0+build.0", .{subfolder_name}) catch unreachable, // 17.0 => 17.0.0+build.0
- else => normalized_name_w.print("{s}+build.0", .{subfolder_name}) catch unreachable, // 17.0.0 => 17.0.0+build.0
- }
- const subfolder_name_normalized: []const u8 = normalized_name_fbs.getWritten();
- const sem_ver = std.SemanticVersion.parse(subfolder_name_normalized);
- _ = sem_ver catch continue :iterate_folder;
+ fn order(lhs: @This(), rhs: @This()) std.math.Order {
+ return std.mem.order(u32, &lhs.nums, &rhs.nums).differ() orelse
+ std.mem.order(u8, lhs.build, rhs.build);
}
- // entry.name passed check
+ };
+ var versions = std.ArrayList(Version).init(allocator);
+ var dirs = std.ArrayList([]const u8).init(allocator);
+ defer {
+ versions.deinit();
+ for (dirs.items) |filtered_dir| allocator.free(filtered_dir);
+ dirs.deinit();
+ }
+
+ iterate: while (iterator.next() catch null) |entry| {
+ if (entry.kind != .directory) continue;
+ if (!std.mem.startsWith(u8, entry.name, prefix)) continue;
- const subfolder_name_allocated = try allocator.dupe(u8, subfolder_name);
- errdefer allocator.free(subfolder_name_allocated);
- try dirs_filtered_list.append(subfolder_name_allocated);
+ var version: Version = .{
+ .nums = .{0} ** 4,
+ .build = "",
+ };
+ const suffix = entry.name[prefix.len..];
+ const underscore = std.mem.indexOfScalar(u8, entry.name, '_');
+ var num_it = std.mem.splitScalar(u8, suffix[0 .. underscore orelse suffix.len], '.');
+ version.nums[0] = Version.parseNum(num_it.first()) orelse continue;
+ for (version.nums[1..]) |*num|
+ num.* = Version.parseNum(num_it.next() orelse break) orelse continue :iterate
+ else if (num_it.next()) |_| continue;
+
+ const name = try allocator.dupe(u8, suffix);
+ errdefer allocator.free(name);
+ if (underscore) |pos| version.build = name[pos + 1 ..];
+
+ try versions.append(version);
+ try dirs.append(name);
}
- const dirs_filtered_slice = try dirs_filtered_list.toOwnedSlice();
- // Keep in mind that order of these names is not guaranteed by Windows,
- // so we cannot just reverse or "while (popOrNull())" this ArrayList.
- std.mem.sortUnstable([]const u8, dirs_filtered_slice, {}, struct {
- fn desc(_: void, lhs: []const u8, rhs: []const u8) bool {
- return std.mem.order(u8, lhs, rhs) == .gt;
+ std.mem.sortUnstableContext(0, dirs.items.len, struct {
+ versions: []Version,
+ dirs: [][]const u8,
+ pub fn lessThan(context: @This(), lhs: usize, rhs: usize) bool {
+ return context.versions[lhs].order(context.versions[rhs]).compare(.gt);
+ }
+ pub fn swap(context: @This(), lhs: usize, rhs: usize) void {
+ std.mem.swap(Version, &context.versions[lhs], &context.versions[rhs]);
+ std.mem.swap([]const u8, &context.dirs[lhs], &context.dirs[rhs]);
}
- }.desc);
- return dirs_filtered_slice;
+ }{ .versions = versions.items, .dirs = dirs.items });
+ return dirs.toOwnedSlice();
}
const RegistryWtf8 = struct {
@@ -167,12 +152,12 @@ const RegistryWtf8 = struct {
};
const registry_wtf16le = try RegistryWtf16Le.openKey(hkey, key_wtf16le);
- return RegistryWtf8{ .key = registry_wtf16le.key };
+ return .{ .key = registry_wtf16le.key };
}
/// Closes key, after that usage is invalid
- pub fn closeKey(self: *const RegistryWtf8) void {
- const return_code_int: windows.HRESULT = windows.advapi32.RegCloseKey(self.key);
+ pub fn closeKey(reg: RegistryWtf8) void {
+ const return_code_int: windows.HRESULT = windows.advapi32.RegCloseKey(reg.key);
const return_code: windows.Win32Error = @enumFromInt(return_code_int);
switch (return_code) {
.SUCCESS => {},
@@ -182,7 +167,7 @@ const RegistryWtf8 = struct {
/// Get string from registry.
/// Caller owns result.
- pub fn getString(self: *const RegistryWtf8, allocator: std.mem.Allocator, subkey: []const u8, value_name: []const u8) error{ OutOfMemory, ValueNameNotFound, NotAString, StringNotFound }![]u8 {
+ pub fn getString(reg: RegistryWtf8, allocator: std.mem.Allocator, subkey: []const u8, value_name: []const u8) error{ OutOfMemory, ValueNameNotFound, NotAString, StringNotFound }![]u8 {
const subkey_wtf16le: [:0]const u16 = subkey_wtf16le: {
var subkey_wtf16le_buf: [RegistryWtf16Le.key_name_max_len]u16 = undefined;
const subkey_wtf16le_len: usize = std.unicode.wtf8ToWtf16Le(subkey_wtf16le_buf[0..], subkey) catch unreachable;
@@ -197,7 +182,7 @@ const RegistryWtf8 = struct {
break :value_name_wtf16le value_name_wtf16le_buf[0..value_name_wtf16le_len :0];
};
- const registry_wtf16le = RegistryWtf16Le{ .key = self.key };
+ const registry_wtf16le: RegistryWtf16Le = .{ .key = reg.key };
const value_wtf16le = try registry_wtf16le.getString(allocator, subkey_wtf16le, value_name_wtf16le);
defer allocator.free(value_wtf16le);
@@ -208,7 +193,7 @@ const RegistryWtf8 = struct {
}
/// Get DWORD (u32) from registry.
- pub fn getDword(self: *const RegistryWtf8, subkey: []const u8, value_name: []const u8) error{ ValueNameNotFound, NotADword, DwordTooLong, DwordNotFound }!u32 {
+ pub fn getDword(reg: RegistryWtf8, subkey: []const u8, value_name: []const u8) error{ ValueNameNotFound, NotADword, DwordTooLong, DwordNotFound }!u32 {
const subkey_wtf16le: [:0]const u16 = subkey_wtf16le: {
var subkey_wtf16le_buf: [RegistryWtf16Le.key_name_max_len]u16 = undefined;
const subkey_wtf16le_len: usize = std.unicode.wtf8ToWtf16Le(subkey_wtf16le_buf[0..], subkey) catch unreachable;
@@ -223,8 +208,8 @@ const RegistryWtf8 = struct {
break :value_name_wtf16le value_name_wtf16le_buf[0..value_name_wtf16le_len :0];
};
- const registry_wtf16le = RegistryWtf16Le{ .key = self.key };
- return try registry_wtf16le.getDword(subkey_wtf16le, value_name_wtf16le);
+ const registry_wtf16le: RegistryWtf16Le = .{ .key = reg.key };
+ return registry_wtf16le.getDword(subkey_wtf16le, value_name_wtf16le);
}
/// Under private space with flags:
@@ -239,7 +224,7 @@ const RegistryWtf8 = struct {
};
const registry_wtf16le = try RegistryWtf16Le.loadFromPath(absolute_path_wtf16le);
- return RegistryWtf8{ .key = registry_wtf16le.key };
+ return .{ .key = registry_wtf16le.key };
}
};
@@ -272,12 +257,12 @@ const RegistryWtf16Le = struct {
else => return error.KeyNotFound,
}
- return RegistryWtf16Le{ .key = key };
+ return .{ .key = key };
}
/// Closes key, after that usage is invalid
- fn closeKey(self: *const RegistryWtf16Le) void {
- const return_code_int: windows.HRESULT = windows.advapi32.RegCloseKey(self.key);
+ fn closeKey(reg: RegistryWtf16Le) void {
+ const return_code_int: windows.HRESULT = windows.advapi32.RegCloseKey(reg.key);
const return_code: windows.Win32Error = @enumFromInt(return_code_int);
switch (return_code) {
.SUCCESS => {},
@@ -286,13 +271,13 @@ const RegistryWtf16Le = struct {
}
/// Get string ([:0]const u16) from registry.
- fn getString(self: *const RegistryWtf16Le, allocator: std.mem.Allocator, subkey_wtf16le: [:0]const u16, value_name_wtf16le: [:0]const u16) error{ OutOfMemory, ValueNameNotFound, NotAString, StringNotFound }![]const u16 {
+ fn getString(reg: RegistryWtf16Le, allocator: std.mem.Allocator, subkey_wtf16le: [:0]const u16, value_name_wtf16le: [:0]const u16) error{ OutOfMemory, ValueNameNotFound, NotAString, StringNotFound }![]const u16 {
var actual_type: windows.ULONG = undefined;
// Calculating length to allocate
var value_wtf16le_buf_size: u32 = 0; // in bytes, including any terminating NUL character or characters.
var return_code_int: windows.HRESULT = windows.advapi32.RegGetValueW(
- self.key,
+ reg.key,
subkey_wtf16le,
value_name_wtf16le,
RRF.RT_REG_SZ,
@@ -319,7 +304,7 @@ const RegistryWtf16Le = struct {
errdefer allocator.free(value_wtf16le_buf);
return_code_int = windows.advapi32.RegGetValueW(
- self.key,
+ reg.key,
subkey_wtf16le,
value_name_wtf16le,
RRF.RT_REG_SZ,
@@ -355,13 +340,13 @@ const RegistryWtf16Le = struct {
}
/// Get DWORD (u32) from registry.
- fn getDword(self: *const RegistryWtf16Le, subkey_wtf16le: [:0]const u16, value_name_wtf16le: [:0]const u16) error{ ValueNameNotFound, NotADword, DwordTooLong, DwordNotFound }!u32 {
+ fn getDword(reg: RegistryWtf16Le, subkey_wtf16le: [:0]const u16, value_name_wtf16le: [:0]const u16) error{ ValueNameNotFound, NotADword, DwordTooLong, DwordNotFound }!u32 {
var actual_type: windows.ULONG = undefined;
var reg_size: u32 = @sizeOf(u32);
var reg_value: u32 = 0;
const return_code_int: windows.HRESULT = windows.advapi32.RegGetValueW(
- self.key,
+ reg.key,
subkey_wtf16le,
value_name_wtf16le,
RRF.RT_REG_DWORD,
@@ -405,28 +390,118 @@ const RegistryWtf16Le = struct {
else => return error.KeyNotFound,
}
- return RegistryWtf16Le{ .key = key };
+ return .{ .key = key };
}
};
-pub const Windows10Sdk = struct {
+pub const Installation = struct {
path: []const u8,
version: []const u8,
- /// Find path and version of Windows 10 SDK.
+ /// Find path and version of Windows SDK.
/// Caller owns the result's fields.
/// After finishing work, call `free(allocator)`.
- fn find(allocator: std.mem.Allocator) error{ OutOfMemory, Windows10SdkNotFound, PathTooLong, VersionTooLong }!Windows10Sdk {
- const v10_key = RegistryWtf8.openKey(windows.HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Microsoft SDKs\\Windows\\v10.0") catch |err| switch (err) {
- error.KeyNotFound => return error.Windows10SdkNotFound,
+ fn find(
+ allocator: std.mem.Allocator,
+ roots_key: RegistryWtf8,
+ roots_subkey: []const u8,
+ prefix: []const u8,
+ version_key_name: []const u8,
+ ) error{ OutOfMemory, InstallationNotFound, PathTooLong, VersionTooLong }!Installation {
+ roots: {
+ const installation = findFromRoot(allocator, roots_key, roots_subkey, prefix) catch
+ break :roots;
+ if (installation.isValidVersion()) return installation;
+ installation.free(allocator);
+ }
+ {
+ const installation = try findFromInstallationFolder(allocator, version_key_name);
+ if (installation.isValidVersion()) return installation;
+ installation.free(allocator);
+ }
+ return error.InstallationNotFound;
+ }
+
+ fn findFromRoot(
+ allocator: std.mem.Allocator,
+ roots_key: RegistryWtf8,
+ roots_subkey: []const u8,
+ prefix: []const u8,
+ ) error{ OutOfMemory, InstallationNotFound, PathTooLong, VersionTooLong }!Installation {
+ const path = path: {
+ const path_maybe_with_trailing_slash = roots_key.getString(allocator, "", roots_subkey) catch |err| switch (err) {
+ error.NotAString => return error.InstallationNotFound,
+ error.ValueNameNotFound => return error.InstallationNotFound,
+ error.StringNotFound => return error.InstallationNotFound,
+
+ error.OutOfMemory => return error.OutOfMemory,
+ };
+ if (path_maybe_with_trailing_slash.len > std.fs.MAX_PATH_BYTES or !std.fs.path.isAbsolute(path_maybe_with_trailing_slash)) {
+ allocator.free(path_maybe_with_trailing_slash);
+ return error.PathTooLong;
+ }
+
+ var path = std.ArrayList(u8).fromOwnedSlice(allocator, path_maybe_with_trailing_slash);
+ errdefer path.deinit();
+
+ // String might contain trailing slash, so trim it here
+ if (path.items.len > "C:\\".len and path.getLast() == '\\') _ = path.pop();
+ break :path try path.toOwnedSlice();
+ };
+ errdefer allocator.free(path);
+
+ const version = version: {
+ var buf: [std.fs.MAX_PATH_BYTES]u8 = undefined;
+ const sdk_lib_dir_path = std.fmt.bufPrint(buf[0..], "{s}\\Lib\\", .{path}) catch |err| switch (err) {
+ error.NoSpaceLeft => return error.PathTooLong,
+ };
+ if (!std.fs.path.isAbsolute(sdk_lib_dir_path)) return error.InstallationNotFound;
+
+ // enumerate files in sdk path looking for latest version
+ var sdk_lib_dir = std.fs.openDirAbsolute(sdk_lib_dir_path, .{
+ .iterate = true,
+ }) catch |err| switch (err) {
+ error.NameTooLong => return error.PathTooLong,
+ else => return error.InstallationNotFound,
+ };
+ defer sdk_lib_dir.close();
+
+ var iterator = sdk_lib_dir.iterate();
+ const versions = try iterateAndFilterByVersion(&iterator, allocator, prefix);
+ defer {
+ for (versions[1..]) |version| allocator.free(version);
+ allocator.free(versions);
+ }
+ break :version versions[0];
};
- defer v10_key.closeKey();
+ errdefer allocator.free(version);
- const path: []const u8 = path10: {
- const path_maybe_with_trailing_slash = v10_key.getString(allocator, "", "InstallationFolder") catch |err| switch (err) {
- error.NotAString => return error.Windows10SdkNotFound,
- error.ValueNameNotFound => return error.Windows10SdkNotFound,
- error.StringNotFound => return error.Windows10SdkNotFound,
+ return .{ .path = path, .version = version };
+ }
+
+ fn findFromInstallationFolder(
+ allocator: std.mem.Allocator,
+ version_key_name: []const u8,
+ ) error{ OutOfMemory, InstallationNotFound, PathTooLong, VersionTooLong }!Installation {
+ var key_name_buf: [RegistryWtf16Le.key_name_max_len]u8 = undefined;
+ const key = key: for ([_][]const u8{ "\\Wow6432Node", "" }) |wow6432node| {
+ for ([_]windows.HKEY{ windows.HKEY_LOCAL_MACHINE, windows.HKEY_CURRENT_USER }) |hkey| {
+ break :key RegistryWtf8.openKey(hkey, std.fmt.bufPrint(
+ &key_name_buf,
+ "SOFTWARE{s}\\Microsoft\\Microsoft SDKs\\Windows\\{s}",
+ .{ wow6432node, version_key_name },
+ ) catch unreachable) catch |err| switch (err) {
+ error.KeyNotFound => return error.InstallationNotFound,
+ };
+ }
+ } else return error.InstallationNotFound;
+ defer key.closeKey();
+
+ const path: []const u8 = path: {
+ const path_maybe_with_trailing_slash = key.getString(allocator, "", "InstallationFolder") catch |err| switch (err) {
+ error.NotAString => return error.InstallationNotFound,
+ error.ValueNameNotFound => return error.InstallationNotFound,
+ error.StringNotFound => return error.InstallationNotFound,
error.OutOfMemory => return error.OutOfMemory,
};
@@ -443,17 +518,17 @@ pub const Windows10Sdk = struct {
if (path.items.len > "C:\\".len and path.getLast() == '\\') _ = path.pop();
const path_without_trailing_slash = try path.toOwnedSlice();
- break :path10 path_without_trailing_slash;
+ break :path path_without_trailing_slash;
};
errdefer allocator.free(path);
- const version: []const u8 = version10: {
+ const version: []const u8 = version: {
// note(dimenus): Microsoft doesn't include the .0 in the ProductVersion key....
- const version_without_0 = v10_key.getString(allocator, "", "ProductVersion") catch |err| switch (err) {
- error.NotAString => return error.Windows10SdkNotFound,
- error.ValueNameNotFound => return error.Windows10SdkNotFound,
- error.StringNotFound => return error.Windows10SdkNotFound,
+ const version_without_0 = key.getString(allocator, "", "ProductVersion") catch |err| switch (err) {
+ error.NotAString => return error.InstallationNotFound,
+ error.ValueNameNotFound => return error.InstallationNotFound,
+ error.StringNotFound => return error.InstallationNotFound,
error.OutOfMemory => return error.OutOfMemory,
};
@@ -468,21 +543,27 @@ pub const Windows10Sdk = struct {
try version.appendSlice(".0");
const version_with_0 = try version.toOwnedSlice();
- break :version10 version_with_0;
+ break :version version_with_0;
};
errdefer allocator.free(version);
- return Windows10Sdk{ .path = path, .version = version };
+ return .{ .path = path, .version = version };
}
/// Check whether this version is enumerated in registry.
- fn isValidVersion(windows10sdk: *const Windows10Sdk) bool {
+ fn isValidVersion(installation: Installation) bool {
var buf: [std.fs.MAX_PATH_BYTES]u8 = undefined;
- const reg_query_as_wtf8 = std.fmt.bufPrint(buf[0..], "{s}\\{s}\\Installed Options", .{ WINDOWS_KIT_REG_KEY, windows10sdk.version }) catch |err| switch (err) {
+ const reg_query_as_wtf8 = std.fmt.bufPrint(buf[0..], "{s}\\{s}\\Installed Options", .{
+ windows_kits_reg_key,
+ installation.version,
+ }) catch |err| switch (err) {
error.NoSpaceLeft => return false,
};
- const options_key = RegistryWtf8.openKey(windows.HKEY_LOCAL_MACHINE, reg_query_as_wtf8) catch |err| switch (err) {
+ const options_key = RegistryWtf8.openKey(
+ windows.HKEY_LOCAL_MACHINE,
+ reg_query_as_wtf8,
+ ) catch |err| switch (err) {
error.KeyNotFound => return false,
};
defer options_key.closeKey();
@@ -492,87 +573,16 @@ pub const Windows10Sdk = struct {
.aarch64 => "OptionId.DesktopCPParm64",
.x86_64 => "OptionId.DesktopCPPx64",
.x86 => "OptionId.DesktopCPPx86",
- else => |tag| @compileError("Windows 10 SDK cannot be detected on architecture " ++ tag),
+ else => |tag| @compileError("Windows SDK cannot be detected on architecture " ++ tag),
};
const reg_value = options_key.getDword("", option_name) catch return false;
return (reg_value == 1);
}
- fn free(self: *const Windows10Sdk, allocator: std.mem.Allocator) void {
- allocator.free(self.path);
- allocator.free(self.version);
- }
-};
-
-pub const Windows81Sdk = struct {
- path: []const u8,
- version: []const u8,
-
- /// Find path and version of Windows 8.1 SDK.
- /// Caller owns the result's fields.
- /// After finishing work, call `free(allocator)`.
- fn find(allocator: std.mem.Allocator, roots_key: *const RegistryWtf8) error{ OutOfMemory, Windows81SdkNotFound, PathTooLong, VersionTooLong }!Windows81Sdk {
- const path: []const u8 = path81: {
- const path_maybe_with_trailing_slash = roots_key.getString(allocator, "", "KitsRoot81") catch |err| switch (err) {
- error.NotAString => return error.Windows81SdkNotFound,
- error.ValueNameNotFound => return error.Windows81SdkNotFound,
- error.StringNotFound => return error.Windows81SdkNotFound,
-
- error.OutOfMemory => return error.OutOfMemory,
- };
- if (path_maybe_with_trailing_slash.len > std.fs.MAX_PATH_BYTES or !std.fs.path.isAbsolute(path_maybe_with_trailing_slash)) {
- allocator.free(path_maybe_with_trailing_slash);
- return error.PathTooLong;
- }
-
- var path = std.ArrayList(u8).fromOwnedSlice(allocator, path_maybe_with_trailing_slash);
- errdefer path.deinit();
-
- // String might contain trailing slash, so trim it here
- if (path.items.len > "C:\\".len and path.getLast() == '\\') _ = path.pop();
-
- const path_without_trailing_slash = try path.toOwnedSlice();
- break :path81 path_without_trailing_slash;
- };
- errdefer allocator.free(path);
-
- const version: []const u8 = version81: {
- var buf: [std.fs.MAX_PATH_BYTES]u8 = undefined;
- const sdk_lib_dir_path = std.fmt.bufPrint(buf[0..], "{s}\\Lib\\", .{path}) catch |err| switch (err) {
- error.NoSpaceLeft => return error.PathTooLong,
- };
- if (!std.fs.path.isAbsolute(sdk_lib_dir_path)) return error.Windows81SdkNotFound;
-
- // enumerate files in sdk path looking for latest version
- var sdk_lib_dir = std.fs.openDirAbsolute(sdk_lib_dir_path, .{
- .iterate = true,
- }) catch |err| switch (err) {
- error.NameTooLong => return error.PathTooLong,
- else => return error.Windows81SdkNotFound,
- };
- defer sdk_lib_dir.close();
-
- var iterator = sdk_lib_dir.iterate();
- const versions = iterateAndFilterBySemVer(&iterator, allocator, "winv") catch |err| switch (err) {
- error.OutOfMemory => return error.OutOfMemory,
- error.VersionNotFound => return error.Windows81SdkNotFound,
- };
- defer {
- for (versions) |version| allocator.free(version);
- allocator.free(versions);
- }
- const latest_version = try allocator.dupe(u8, versions[0]);
- break :version81 latest_version;
- };
- errdefer allocator.free(version);
-
- return Windows81Sdk{ .path = path, .version = version };
- }
-
- fn free(self: *const Windows81Sdk, allocator: std.mem.Allocator) void {
- allocator.free(self.path);
- allocator.free(self.version);
+ fn free(install: Installation, allocator: std.mem.Allocator) void {
+ allocator.free(install.path);
+ allocator.free(install.version);
}
};
@@ -634,7 +644,7 @@ const MsvcLibDir = struct {
/// Example: 17.4.33205.214 -> 0x0011000481b500d6
fn parseVersionQuad(version: []const u8) error{InvalidVersion}!u64 {
var it = std.mem.splitScalar(u8, version, '.');
- const a = it.next() orelse return error.InvalidVersion;
+ const a = it.first();
const b = it.next() orelse return error.InvalidVersion;
const c = it.next() orelse return error.InvalidVersion;
const d = it.next() orelse return error.InvalidVersion;
@@ -791,11 +801,7 @@ const MsvcLibDir = struct {
defer visualstudio_folder.close();
var iterator = visualstudio_folder.iterate();
- const versions = iterateAndFilterBySemVer(&iterator, allocator, null) catch |err| switch (err) {
- error.OutOfMemory => return error.OutOfMemory,
- error.VersionNotFound => return error.PathNotFound,
- };
- break :vs_versions versions;
+ break :vs_versions try iterateAndFilterByVersion(&iterator, allocator, "");
};
defer {
for (vs_versions) |vs_version| allocator.free(vs_version);
diff --git a/lib/std/zig/primitives.zig b/lib/std/zig/primitives.zig
index e4e96e55ac..ea12268f1f 100644
--- a/lib/std/zig/primitives.zig
+++ b/lib/std/zig/primitives.zig
@@ -2,7 +2,7 @@ const std = @import("std");
/// Set of primitive type and value names.
/// Does not include `_` or integer type names.
-pub const names = std.ComptimeStringMap(void, .{
+pub const names = std.StaticStringMap(void).initComptime(.{
.{"anyerror"},
.{"anyframe"},
.{"anyopaque"},
diff --git a/lib/std/zig/render.zig b/lib/std/zig/render.zig
index 00193c4173..336f394211 100644
--- a/lib/std/zig/render.zig
+++ b/lib/std/zig/render.zig
@@ -2886,11 +2886,11 @@ fn renderIdentifier(r: *Render, token_index: Ast.TokenIndex, space: Space, quote
// If we read the whole thing, we have to do further checks.
const longest_keyword_or_primitive_len = comptime blk: {
var longest = 0;
- for (primitives.names.kvs) |kv| {
- if (kv.key.len > longest) longest = kv.key.len;
+ for (primitives.names.keys()) |key| {
+ if (key.len > longest) longest = key.len;
}
- for (std.zig.Token.keywords.kvs) |kv| {
- if (kv.key.len > longest) longest = kv.key.len;
+ for (std.zig.Token.keywords.keys()) |key| {
+ if (key.len > longest) longest = key.len;
}
break :blk longest;
};
diff --git a/lib/std/zig/system.zig b/lib/std/zig/system.zig
index 8bd7f815fe..83c798c342 100644
--- a/lib/std/zig/system.zig
+++ b/lib/std/zig/system.zig
@@ -983,7 +983,7 @@ fn detectAbiAndDynamicLinker(
// Best case scenario: the executable is dynamically linked, and we can iterate
// over our own shared objects and find a dynamic linker.
- const elf_file = blk: {
+ const elf_file = elf_file: {
// This block looks for a shebang line in /usr/bin/env,
// if it finds one, then instead of using /usr/bin/env as the ELF file to examine, it uses the file it references instead,
// doing the same logic recursively in case it finds another shebang line.
@@ -995,9 +995,22 @@ fn detectAbiAndDynamicLinker(
// Haiku does not have a /usr root directory.
.haiku => "/bin/env",
};
- // #! (2) + 255 (max length of shebang line since Linux 5.1) + \n (1)
- var buffer: [258]u8 = undefined;
+
+ // According to `man 2 execve`:
+ //
+ // The kernel imposes a maximum length on the text
+ // that follows the "#!" characters at the start of a script;
+ // characters beyond the limit are ignored.
+ // Before Linux 5.1, the limit is 127 characters.
+ // Since Linux 5.1, the limit is 255 characters.
+ //
+ // Tests show that bash and zsh consider 255 as total limit,
+ // *including* "#!" characters and ignoring newline.
+ // For safety, we set max length as 255 + \n (1).
+ var buffer: [255 + 1]u8 = undefined;
while (true) {
+ // Interpreter path can be relative on Linux, but
+ // for simplicity we are asserting it is an absolute path.
const file = fs.openFileAbsolute(file_name, .{}) catch |err| switch (err) {
error.NoSpaceLeft => unreachable,
error.NameTooLong => unreachable,
@@ -1027,27 +1040,55 @@ fn detectAbiAndDynamicLinker(
else => |e| return e,
};
- errdefer file.close();
-
- const len = preadAtLeast(file, &buffer, 0, buffer.len) catch |err| switch (err) {
+ var is_elf_file = false;
+ defer if (is_elf_file == false) file.close();
+
+ // Shortest working interpreter path is "#!/i" (4)
+ // (interpreter is "/i", assuming all pathes are absolute, like in above comment).
+ // ELF magic number length is also 4.
+ //
+ // If file is shorter than that, it is definitely not ELF file
+ // nor file with "shebang" line.
+ const min_len: usize = 4;
+
+ const len = preadAtLeast(file, &buffer, 0, min_len) catch |err| switch (err) {
error.UnexpectedEndOfFile,
error.UnableToReadElfFile,
- => break :blk file,
+ => return defaultAbiAndDynamicLinker(cpu, os, query),
else => |e| return e,
};
- const newline = mem.indexOfScalar(u8, buffer[0..len], '\n') orelse break :blk file;
- const line = buffer[0..newline];
- if (!mem.startsWith(u8, line, "#!")) break :blk file;
- var it = mem.tokenizeScalar(u8, line[2..], ' ');
- file_name = it.next() orelse return defaultAbiAndDynamicLinker(cpu, os, query);
- file.close();
+ const content = buffer[0..len];
+
+ if (mem.eql(u8, content[0..4], std.elf.MAGIC)) {
+ // It is very likely ELF file!
+ is_elf_file = true;
+ break :elf_file file;
+ } else if (mem.eql(u8, content[0..2], "#!")) {
+ // We detected shebang, now parse entire line.
+
+ // Trim leading "#!", spaces and tabs.
+ const trimmed_line = mem.trimLeft(u8, content[2..], &.{ ' ', '\t' });
+
+ // This line can have:
+ // * Interpreter path only,
+ // * Interpreter path and arguments, all separated by space, tab or NUL character.
+ // And optionally newline at the end.
+ const path_maybe_args = mem.trimRight(u8, trimmed_line, "\n");
+
+ // Separate path and args.
+ const path_end = mem.indexOfAny(u8, path_maybe_args, &.{ ' ', '\t', 0 }) orelse path_maybe_args.len;
+
+ file_name = path_maybe_args[0..path_end];
+ continue;
+ } else {
+ // Not a ELF file, not a shell script with "shebang line", invalid duck.
+ return defaultAbiAndDynamicLinker(cpu, os, query);
+ }
}
};
defer elf_file.close();
- // If Zig is statically linked, such as via distributed binary static builds, the above
- // trick (block self_exe) won't work. The next thing we fall back to is the same thing, but for elf_file.
// TODO: inline this function and combine the buffer we already read above to find
// the possible shebang line with the buffer we use for the ELF header.
return abiAndDynamicLinkerFromFile(elf_file, cpu, os, ld_info_list, query) catch |err| switch (err) {
@@ -1075,7 +1116,7 @@ fn detectAbiAndDynamicLinker(
};
}
-fn defaultAbiAndDynamicLinker(cpu: Target.Cpu, os: Target.Os, query: Target.Query) !Target {
+fn defaultAbiAndDynamicLinker(cpu: Target.Cpu, os: Target.Os, query: Target.Query) Target {
const abi = query.abi orelse Target.Abi.default(cpu.arch, os);
return .{
.cpu = cpu,
diff --git a/lib/std/zig/system/linux.zig b/lib/std/zig/system/linux.zig
index b05b86995f..2d2c46a54a 100644
--- a/lib/std/zig/system/linux.zig
+++ b/lib/std/zig/system/linux.zig
@@ -374,6 +374,12 @@ fn CpuinfoParser(comptime impl: anytype) type {
};
}
+inline fn getAArch64CpuFeature(comptime feat_reg: []const u8) u64 {
+ return asm ("mrs %[ret], " ++ feat_reg
+ : [ret] "=r" (-> u64),
+ );
+}
+
pub fn detectNativeCpuAndFeatures() ?Target.Cpu {
var f = fs.openFileAbsolute("/proc/cpuinfo", .{}) catch |err| switch (err) {
else => return null,
@@ -382,9 +388,28 @@ pub fn detectNativeCpuAndFeatures() ?Target.Cpu {
const current_arch = builtin.cpu.arch;
switch (current_arch) {
- .arm, .armeb, .thumb, .thumbeb, .aarch64, .aarch64_be, .aarch64_32 => {
+ .arm, .armeb, .thumb, .thumbeb => {
return ArmCpuinfoParser.parse(current_arch, f.reader()) catch null;
},
+ .aarch64, .aarch64_be, .aarch64_32 => {
+ const registers = [12]u64{
+ getAArch64CpuFeature("MIDR_EL1"),
+ getAArch64CpuFeature("ID_AA64PFR0_EL1"),
+ getAArch64CpuFeature("ID_AA64PFR1_EL1"),
+ getAArch64CpuFeature("ID_AA64DFR0_EL1"),
+ getAArch64CpuFeature("ID_AA64DFR1_EL1"),
+ getAArch64CpuFeature("ID_AA64AFR0_EL1"),
+ getAArch64CpuFeature("ID_AA64AFR1_EL1"),
+ getAArch64CpuFeature("ID_AA64ISAR0_EL1"),
+ getAArch64CpuFeature("ID_AA64ISAR1_EL1"),
+ getAArch64CpuFeature("ID_AA64MMFR0_EL1"),
+ getAArch64CpuFeature("ID_AA64MMFR1_EL1"),
+ getAArch64CpuFeature("ID_AA64MMFR2_EL1"),
+ };
+
+ const core = @import("arm.zig").aarch64.detectNativeCpuAndFeatures(current_arch, registers);
+ return core;
+ },
.sparc64 => {
return SparcCpuinfoParser.parse(current_arch, f.reader()) catch null;
},
diff --git a/lib/std/zig/tokenizer.zig b/lib/std/zig/tokenizer.zig
index 72f65afb3a..6f9a232c48 100644
--- a/lib/std/zig/tokenizer.zig
+++ b/lib/std/zig/tokenizer.zig
@@ -9,7 +9,7 @@ pub const Token = struct {
end: usize,
};
- pub const keywords = std.ComptimeStringMap(Tag, .{
+ pub const keywords = std.StaticStringMap(Tag).initComptime(.{
.{ "addrspace", .keyword_addrspace },
.{ "align", .keyword_align },
.{ "allowzero", .keyword_allowzero },