diff options
Diffstat (limited to 'src/Normalize.zig')
| -rw-r--r-- | src/Normalize.zig | 9 |
1 files changed, 9 insertions, 0 deletions
diff --git a/src/Normalize.zig b/src/Normalize.zig index 7b87406..97c2649 100644 --- a/src/Normalize.zig +++ b/src/Normalize.zig | |||
| @@ -220,10 +220,19 @@ test "decompose" { | |||
| 220 | } | 220 | } |
| 221 | 221 | ||
| 222 | /// Returned from various functions in this namespace. Remember to call `deinit` to free any allocated memory. | 222 | /// Returned from various functions in this namespace. Remember to call `deinit` to free any allocated memory. |
| 223 | /// Note that normalization functions will not copy what they're given if no normalization is needed, if you | ||
| 224 | /// need to ensure that this Result outlasts the given string, call `try result.toOwned(allocator)`. This | ||
| 225 | /// will not make a third copy if the Result is already copied from the input. | ||
| 223 | pub const Result = struct { | 226 | pub const Result = struct { |
| 224 | allocated: bool = false, | 227 | allocated: bool = false, |
| 225 | slice: []const u8, | 228 | slice: []const u8, |
| 226 | 229 | ||
| 230 | /// Ensures that the slice result is a copy of the input, by making a copy if it was not. | ||
| 231 | pub fn toOwned(result: Result, allocator: mem.Allocator) error{OutOfMemory}!Result { | ||
| 232 | if (result.allocatod) return result; | ||
| 233 | return .{ .allocated = true, .slice = try allocator.dupe(u8, result.slice) }; | ||
| 234 | } | ||
| 235 | |||
| 227 | pub fn deinit(self: *const Result, allocator: mem.Allocator) void { | 236 | pub fn deinit(self: *const Result, allocator: mem.Allocator) void { |
| 228 | if (self.allocated) allocator.free(self.slice); | 237 | if (self.allocated) allocator.free(self.slice); |
| 229 | } | 238 | } |