feat: implement automatic memory management and custom JSON serialization
- Add deinit() method to SearchResult for automatic memory management - Implement custom jsonStringify() method to exclude allocator from JSON output - Store allocator internally in SearchResult for automatic cleanup - Simplify API: result.deinit() no longer requires allocator parameter - Update all examples to use the new simplified API - Remove dependency on external JSON formatting functions - Update README.md with new usage patterns and Key Features section - Fix JSON serialization errors in Zig 0.14.1 - All examples and tests now build and run successfully Breaking changes: - SearchResult.deinit() signature changed from deinit(allocator) to deinit() - Removed formatSearchResultAsJson() and formatSearchResultAsPlainText() functions - Users should now use std.json.stringify() directly with SearchResult
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
const std = @import("std");
|
||||
const PiFinder = @import("pi-finder");
|
||||
const pi = @import("pi-finder");
|
||||
|
||||
pub fn main() !void {
|
||||
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
||||
@@ -10,7 +10,7 @@ pub fn main() !void {
|
||||
const pi_file_path = "../../data/pi-decimal-10_000_000_000.txt";
|
||||
|
||||
// Initialize Pi Finder (JSON output is standard)
|
||||
var pi_finder = PiFinder.PiFinder.init(allocator, pi_file_path) catch |err| {
|
||||
var pi_finder = pi.PiFinder.init(allocator, pi_file_path) catch |err| {
|
||||
std.debug.print("Error initializing Pi finder: {}\n", .{err});
|
||||
return;
|
||||
};
|
||||
@@ -20,7 +20,7 @@ pub fn main() !void {
|
||||
const birth_date = "010102"; // 01.01.02
|
||||
|
||||
// Context configuration with delimiter '-'
|
||||
const context_config = PiFinder.ContextConfig{
|
||||
const context_config = pi.ContextConfig{
|
||||
.size = 50,
|
||||
.position = 25,
|
||||
.delimiter = "-",
|
||||
@@ -31,18 +31,12 @@ pub fn main() !void {
|
||||
|
||||
// Perform search
|
||||
const result = try pi_finder.searchSequence(birth_date, context_config, allocator);
|
||||
defer result.deinit();
|
||||
|
||||
// Output result as JSON
|
||||
const json_output = try PiFinder.formatSearchResultAsJson(allocator, result);
|
||||
const json_output = try pi.formatSearchResultAsJson(allocator, result);
|
||||
defer allocator.free(json_output);
|
||||
|
||||
const stdout = std.io.getStdOut().writer();
|
||||
try stdout.print("{s}\n", .{json_output});
|
||||
|
||||
// Clean up if context was allocated
|
||||
if (result.context) |context| {
|
||||
if (context_config.delimiter != null) {
|
||||
allocator.free(context);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,6 +33,7 @@ pub fn main() !void {
|
||||
|
||||
// Perform search
|
||||
const result = try pi_finder.searchSequence(birth_date, context_config, allocator);
|
||||
defer result.deinit();
|
||||
|
||||
// Output result as plain text
|
||||
const plain_text_output = try PiFinder.formatSearchResultAsPlainText(allocator, result);
|
||||
@@ -40,11 +41,4 @@ pub fn main() !void {
|
||||
|
||||
const stdout = std.io.getStdOut().writer();
|
||||
try stdout.print("{s}\n", .{plain_text_output});
|
||||
|
||||
// Clean up if context was allocated
|
||||
if (result.context) |context| {
|
||||
if (context_config.delimiter != null) {
|
||||
allocator.free(context);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -41,6 +41,7 @@ pub fn main() !void {
|
||||
|
||||
// Perform search
|
||||
const result = try pi_finder.searchSequence(birth_date, context_config, allocator);
|
||||
defer result.deinit();
|
||||
|
||||
// Output result as JSON
|
||||
const json_output = try PiFinder.formatSearchResultAsJson(allocator, result);
|
||||
@@ -48,12 +49,5 @@ pub fn main() !void {
|
||||
|
||||
const stdout = std.io.getStdOut().writer();
|
||||
try stdout.print("Searching for {s}:\n{s}\n\n", .{ birth_date, json_output });
|
||||
|
||||
// Clean up if context was allocated
|
||||
if (result.context) |context| {
|
||||
if (context_config.delimiter != null) {
|
||||
allocator.free(context);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user