You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
7 months ago | |
|---|---|---|
| .github | 7 months ago | |
| examples/birth-date-finder | 7 months ago | |
| src | 7 months ago | |
| .gitignore | 7 months ago | |
| README.md | 7 months ago | |
| build.zig | 7 months ago | |
| build.zig.zon | 7 months ago | |
| download-pi-data.sh | 7 months ago | |
README.md
Pi Finder Library
A Zig library for searching numeric sequences in the decimal places of Pi.
Features
- Efficient searching of numeric sequences in Pi decimal places
- Automatic memory management with
result.deinit() - Custom JSON serialization using
std.json.stringify - JSON output as standard format
- Configurable context display with delimiters
- Performance metrics included in results
- Reusable module design
- Compatible with Zig 0.14.1
Installation
As a Dependency in a Zig Project
Add this library as a dependency in your build.zig.zon:
.dependencies = .{
.@"pi-finder" = .{
.path = "path/to/pi-finder-lib",
},
},
In your build.zig
const pi_finder_dep = b.dependency("pi-finder", .{
.target = target,
.optimize = optimize,
});
exe.root_module.addImport("pi-finder", pi_finder_dep.module("pi-finder"));
Usage
Basic Usage
const std = @import("std");
const PiFinder = @import("pi-finder");
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const allocator = gpa.allocator();
// Initialize Pi Finder
var pi_finder = try PiFinder.PiFinder.init(allocator, "pi-data.txt");
defer pi_finder.deinit();
// Configure context
const context_config = PiFinder.ContextConfig{
.size = 50,
.position = 25,
.delimiter = "-",
.delimiter_interval = 2,
.prefix = "[",
.suffix = "]",
};
// Search for sequence
const result = try pi_finder.searchSequence("123456", context_config, allocator);
defer result.deinit(); // Automatically frees allocated memory
// Output as JSON using std.json.stringify
var json_string = std.ArrayList(u8).init(allocator);
defer json_string.deinit();
try std.json.stringify(result, .{}, json_string.writer());
std.debug.print("{s}\n", .{json_string.items});
}
Plain Text Output Example
// After getting the search result
if (result.success) {
std.debug.print("Success: {}\n", .{result.success});
std.debug.print("Sequence: {s}\n", .{result.sequence});
if (result.position) |pos| {
std.debug.print("Position: {}\n", .{pos});
}
if (result.context) |context| {
std.debug.print("Context: {s}\n", .{context});
}
} else {
std.debug.print("Search failed: {s}\n", .{result.error_message orelse "Unknown error"});
}
Key Features
Automatic Memory Management
The library uses RAII (Resource Acquisition Is Initialization) pattern:
const result = try pi_finder.searchSequence("123456", context_config, allocator);
defer result.deinit(); // Automatically frees allocated context memory
Custom JSON Serialization
SearchResult implements custom JSON serialization that excludes internal fields:
// The allocator field is automatically excluded from JSON output
try std.json.stringify(result, .{}, writer);
Performance Metrics
Every search result includes detailed performance metrics:
{
"performance": {
"file_load_time_ms": 327.988,
"search_time_ms": 10.766,
"file_size_mb": 953.674,
"load_speed_mbs": 2948.052
}
}
API
PiFinder
init(allocator, file_path)- Initializes the Pi Finderdeinit()- Frees allocated memoryfindSequenceInPi(sequence)- Searches for a sequencegetContext(position, config, sequence, allocator)- Generates contextsearchSequence(sequence, config, allocator)- Comprehensive search with structured result
ContextConfig
Configuration for context display:
size- Size of the contextposition- Position of the sequence within the contextdelimiter- Delimiter character (optional)delimiter_interval- Interval for delimiter insertionprefix- Prefix for the sequencesuffix- Suffix for the sequence
SearchResult
Structured result containing:
success- Search success statussequence- Searched sequenceposition- Position in Pi (if found)context- Context around the positionerror_message- Error message (if any)performance- Performance metricscontext_config- Used context configuration
Methods:
deinit()- Frees any dynamically allocated memory (like context)jsonStringify(writer)- Custom JSON serialization (automatically used bystd.json.stringify)
Examples
See the examples/ directory for complete examples:
birth-date-finder/- Basic example for searching birth datesbirth-date-finder/src/plain_text_example.zig- Example using plain text output format