diff options
| author | 2024-04-02 13:22:37 -0400 | |
|---|---|---|
| committer | 2024-04-02 13:22:37 -0400 | |
| commit | cc8d110f834d112a230024122fddfcc6e0a67759 (patch) | |
| tree | a69b7ef8fa81f82b9ce9a16ce489b696d237e284 | |
| parent | Updated README with zig fetch (diff) | |
| download | zg-cc8d110f834d112a230024122fddfcc6e0a67759.tar.gz zg-cc8d110f834d112a230024122fddfcc6e0a67759.tar.xz zg-cc8d110f834d112a230024122fddfcc6e0a67759.zip | |
NormData init now takes pointer to uninitialized Self to avoid stack copy issues.
| -rw-r--r-- | README.md | 6 | ||||
| -rw-r--r-- | src/NormData.zig | 5 | ||||
| -rw-r--r-- | src/Normalize.zig | 24 | ||||
| -rw-r--r-- | src/unicode_tests.zig | 5 |
4 files changed, 24 insertions, 16 deletions
| @@ -282,7 +282,8 @@ const Normalize = @import("Normalize"); | |||
| 282 | 282 | ||
| 283 | test "Normalization" { | 283 | test "Normalization" { |
| 284 | // We need lots of Unicode dta for normalization. | 284 | // We need lots of Unicode dta for normalization. |
| 285 | var norm_data = try Normalize.NormData.init(allocator); | 285 | var norm_data: Normalize.NormData = undefined; |
| 286 | try Normalize.NormData.init(&norm_data, allocator); | ||
| 286 | defer norm_data.deinit(); | 287 | defer norm_data.deinit(); |
| 287 | 288 | ||
| 288 | // The `Normalize` structure takes a pointer to the data. | 289 | // The `Normalize` structure takes a pointer to the data. |
| @@ -335,7 +336,8 @@ const CaseFold = @import("CaseFold"); | |||
| 335 | 336 | ||
| 336 | test "Caseless matching" { | 337 | test "Caseless matching" { |
| 337 | // We need to normalize during the matching process. | 338 | // We need to normalize during the matching process. |
| 338 | var norm_data = try Normalize.NormData.init(allocator); | 339 | var norm_data: Normalize.NormData = undefined; |
| 340 | try Normalize.NormData.init(&norm_data, allocator); | ||
| 339 | defer norm_data.deinit(); | 341 | defer norm_data.deinit(); |
| 340 | const n = Normalize{ .norm_data = &norm_data }; | 342 | const n = Normalize{ .norm_data = &norm_data }; |
| 341 | 343 | ||
diff --git a/src/NormData.zig b/src/NormData.zig index 7ffe679..15cc401 100644 --- a/src/NormData.zig +++ b/src/NormData.zig | |||
| @@ -16,8 +16,7 @@ normp_data: NormPropsData = undefined, | |||
| 16 | 16 | ||
| 17 | const Self = @This(); | 17 | const Self = @This(); |
| 18 | 18 | ||
| 19 | pub fn init(allocator: std.mem.Allocator) !Self { | 19 | pub fn init(self: *Self, allocator: std.mem.Allocator) !void { |
| 20 | var self = Self{}; | ||
| 21 | self.canon_data = try CanonData.init(allocator); | 20 | self.canon_data = try CanonData.init(allocator); |
| 22 | errdefer self.canon_data.deinit(); | 21 | errdefer self.canon_data.deinit(); |
| 23 | self.ccc_data = try CccData.init(allocator); | 22 | self.ccc_data = try CccData.init(allocator); |
| @@ -27,8 +26,6 @@ pub fn init(allocator: std.mem.Allocator) !Self { | |||
| 27 | self.hangul_data = try HangulData.init(allocator); | 26 | self.hangul_data = try HangulData.init(allocator); |
| 28 | errdefer self.hangul_data.deinit(); | 27 | errdefer self.hangul_data.deinit(); |
| 29 | self.normp_data = try NormPropsData.init(allocator); | 28 | self.normp_data = try NormPropsData.init(allocator); |
| 30 | |||
| 31 | return self; | ||
| 32 | } | 29 | } |
| 33 | 30 | ||
| 34 | pub fn deinit(self: *Self) void { | 31 | pub fn deinit(self: *Self) void { |
diff --git a/src/Normalize.zig b/src/Normalize.zig index 85e3aa3..f2dbb3e 100644 --- a/src/Normalize.zig +++ b/src/Normalize.zig | |||
| @@ -175,7 +175,8 @@ fn decompose( | |||
| 175 | 175 | ||
| 176 | test "decompose" { | 176 | test "decompose" { |
| 177 | const allocator = testing.allocator; | 177 | const allocator = testing.allocator; |
| 178 | const data = try NormData.init(allocator); | 178 | var data: NormData = undefined; |
| 179 | try NormData.init(&data, allocator); | ||
| 179 | defer data.deinit(); | 180 | defer data.deinit(); |
| 180 | var n = Self{ .norm_data = &data }; | 181 | var n = Self{ .norm_data = &data }; |
| 181 | 182 | ||
| @@ -295,7 +296,8 @@ fn nfxd(self: Self, allocator: mem.Allocator, str: []const u8, form: Form) !Resu | |||
| 295 | 296 | ||
| 296 | test "nfd ASCII / no-alloc" { | 297 | test "nfd ASCII / no-alloc" { |
| 297 | const allocator = testing.allocator; | 298 | const allocator = testing.allocator; |
| 298 | const data = try NormData.init(allocator); | 299 | var data: NormData = undefined; |
| 300 | try NormData.init(&data, allocator); | ||
| 299 | defer data.deinit(); | 301 | defer data.deinit(); |
| 300 | const n = Self{ .norm_data = &data }; | 302 | const n = Self{ .norm_data = &data }; |
| 301 | 303 | ||
| @@ -307,7 +309,8 @@ test "nfd ASCII / no-alloc" { | |||
| 307 | 309 | ||
| 308 | test "nfd !ASCII / alloc" { | 310 | test "nfd !ASCII / alloc" { |
| 309 | const allocator = testing.allocator; | 311 | const allocator = testing.allocator; |
| 310 | const data = try NormData.init(allocator); | 312 | var data: NormData = undefined; |
| 313 | try NormData.init(&data, allocator); | ||
| 311 | defer data.deinit(); | 314 | defer data.deinit(); |
| 312 | const n = Self{ .norm_data = &data }; | 315 | const n = Self{ .norm_data = &data }; |
| 313 | 316 | ||
| @@ -319,7 +322,8 @@ test "nfd !ASCII / alloc" { | |||
| 319 | 322 | ||
| 320 | test "nfkd ASCII / no-alloc" { | 323 | test "nfkd ASCII / no-alloc" { |
| 321 | const allocator = testing.allocator; | 324 | const allocator = testing.allocator; |
| 322 | const data = try NormData.init(allocator); | 325 | var data: NormData = undefined; |
| 326 | try NormData.init(&data, allocator); | ||
| 323 | defer data.deinit(); | 327 | defer data.deinit(); |
| 324 | const n = Self{ .norm_data = &data }; | 328 | const n = Self{ .norm_data = &data }; |
| 325 | 329 | ||
| @@ -331,7 +335,8 @@ test "nfkd ASCII / no-alloc" { | |||
| 331 | 335 | ||
| 332 | test "nfkd !ASCII / alloc" { | 336 | test "nfkd !ASCII / alloc" { |
| 333 | const allocator = testing.allocator; | 337 | const allocator = testing.allocator; |
| 334 | const data = try NormData.init(allocator); | 338 | var data: NormData = undefined; |
| 339 | try NormData.init(&data, allocator); | ||
| 335 | defer data.deinit(); | 340 | defer data.deinit(); |
| 336 | const n = Self{ .norm_data = &data }; | 341 | const n = Self{ .norm_data = &data }; |
| 337 | 342 | ||
| @@ -530,7 +535,8 @@ fn nfxc(self: Self, allocator: mem.Allocator, str: []const u8, form: Form) !Resu | |||
| 530 | 535 | ||
| 531 | test "nfc" { | 536 | test "nfc" { |
| 532 | const allocator = testing.allocator; | 537 | const allocator = testing.allocator; |
| 533 | const data = try NormData.init(allocator); | 538 | var data: NormData = undefined; |
| 539 | try NormData.init(&data, allocator); | ||
| 534 | defer data.deinit(); | 540 | defer data.deinit(); |
| 535 | const n = Self{ .norm_data = &data }; | 541 | const n = Self{ .norm_data = &data }; |
| 536 | 542 | ||
| @@ -542,7 +548,8 @@ test "nfc" { | |||
| 542 | 548 | ||
| 543 | test "nfkc" { | 549 | test "nfkc" { |
| 544 | const allocator = testing.allocator; | 550 | const allocator = testing.allocator; |
| 545 | const data = try NormData.init(allocator); | 551 | var data: NormData = undefined; |
| 552 | try NormData.init(&data, allocator); | ||
| 546 | defer data.deinit(); | 553 | defer data.deinit(); |
| 547 | const n = Self{ .norm_data = &data }; | 554 | const n = Self{ .norm_data = &data }; |
| 548 | 555 | ||
| @@ -564,7 +571,8 @@ pub fn eql(self: Self, allocator: mem.Allocator, a: []const u8, b: []const u8) ! | |||
| 564 | 571 | ||
| 565 | test "eql" { | 572 | test "eql" { |
| 566 | const allocator = testing.allocator; | 573 | const allocator = testing.allocator; |
| 567 | const data = try NormData.init(allocator); | 574 | var data: NormData = undefined; |
| 575 | try NormData.init(&data, allocator); | ||
| 568 | defer data.deinit(); | 576 | defer data.deinit(); |
| 569 | const n = Self{ .norm_data = &data }; | 577 | const n = Self{ .norm_data = &data }; |
| 570 | 578 | ||
diff --git a/src/unicode_tests.zig b/src/unicode_tests.zig index 5442f63..448ce41 100644 --- a/src/unicode_tests.zig +++ b/src/unicode_tests.zig | |||
| @@ -17,8 +17,9 @@ test "Unicode normalization tests" { | |||
| 17 | defer arena.deinit(); | 17 | defer arena.deinit(); |
| 18 | var allocator = arena.allocator(); | 18 | var allocator = arena.allocator(); |
| 19 | 19 | ||
| 20 | const data = try Normalize.NormData.init(allocator); | 20 | var norm_data: Normalize.NormData = undefined; |
| 21 | const n = Normalize{ .norm_data = &data }; | 21 | try Normalize.NormData.init(&norm_data, allocator); |
| 22 | const n = Normalize{ .norm_data = &norm_data }; | ||
| 22 | 23 | ||
| 23 | var file = try fs.cwd().openFile("data/unicode/NormalizationTest.txt", .{}); | 24 | var file = try fs.cwd().openFile("data/unicode/NormalizationTest.txt", .{}); |
| 24 | defer file.close(); | 25 | defer file.close(); |