diff --git a/scripts/UnicodeData.zig b/scripts/UnicodeData.zig index cf61c5bb640cffc7b9c4dca0e96fd8aa7f984d80..c774d5efe1d59089930eb62ad3a05b7ddee048dd 100644 --- a/scripts/UnicodeData.zig +++ b/scripts/UnicodeData.zig @@ -9,6 +9,9 @@ pub usingnamespace common.Main(struct { pub const dest_header = \\const ucd = @import("./lib.zig"); \\const std = @import("std"); + \\const extras = @import("extras"); + \\ + \\pub const data_soa = extras.StaticMultiList(Codepoint).initComptime(&data); \\ \\pub const Codepoint = struct { \\ u21, // U+ code @@ -49,7 +52,7 @@ pub usingnamespace common.Main(struct { \\}; \\ \\pub fn find(cp: u21) Codepoint { - \\ return data[binarySearchClosest(Codepoint, &data, cp, compare)]; + \\ return data[binarySearchClosest(u21, data_soa[0], cp, compare)]; \\} \\ \\fn binarySearchClosest(comptime T: type, items: []const T, context: anytype, comptime compareFn: fn (@TypeOf(context), T) std.math.Order) usize { @@ -67,9 +70,9 @@ pub usingnamespace common.Main(struct { \\ return high; \\} \\ - \\pub fn compare(needle: u21, row: Codepoint) std.math.Order { - \\ if (needle < row[0]) return .lt; - \\ if (needle > row[0]) return .gt; + \\pub fn compare(needle: u21, row: u21) std.math.Order { + \\ if (needle < row) return .lt; + \\ if (needle > row) return .gt; \\ return .eq; \\} \\ diff --git a/src/unicode_data.zig b/src/unicode_data.zig index a36fa34a59fc3a45ab18a47cfa77d4f72680c4a0..272b052a39ea34fdc5585f487cfc9a7e5a530d6e 100644 --- a/src/unicode_data.zig +++ b/src/unicode_data.zig @@ -7,6 +7,9 @@ const ucd = @import("./lib.zig"); const std = @import("std"); +const extras = @import("extras"); + +pub const data_soa = extras.StaticMultiList(Codepoint).initComptime(&data).items; pub const Codepoint = struct { u21, // U+ code @@ -47,7 +50,7 @@ pub const Decomposition = enum { }; pub fn find(cp: u21) Codepoint { - return data[binarySearchClosest(Codepoint, &data, cp, compare)]; + return data[binarySearchClosest(u21, data_soa[0], cp, compare)]; } fn binarySearchClosest(comptime T: type, items: []const T, context: anytype, comptime compareFn: fn (@TypeOf(context), T) std.math.Order) usize { @@ -65,9 +68,9 @@ fn binarySearchClosest(comptime T: type, items: []const T, context: anytype, com return high; } -pub fn compare(needle: u21, row: Codepoint) std.math.Order { - if (needle < row[0]) return .lt; - if (needle > row[0]) return .gt; +pub fn compare(needle: u21, row: u21) std.math.Order { + if (needle < row) return .lt; + if (needle > row) return .gt; return .eq; } diff --git a/zig.mod b/zig.mod index 93a1b6236e6be65226fcca62c3482eda4c4edbc0..c5c9aee3a52e8afd50d5d8af4fd655275a3bdc4d 100644 --- a/zig.mod +++ b/zig.mod @@ -3,6 +3,8 @@ name: unicode-ucd main: src/lib.zig license: MIT description: Zig bindings for the Unicode Character Database +dependencies: + - src: git https://github.com/nektro/zig-extras root_dependencies: - src: git https://github.com/nektro/zfetch - src: git https://github.com/nektro/zig-ansi