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
- JSON output as standard
- Optional plain text output format
- Configurable context display with delimiters
- Reusable module design
- Performance metrics
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,
};
// Search for sequence
const result = try pi_finder.searchSequence("123456", context_config, allocator);
// Output as JSON (default format)
const json_output = try PiFinder.formatSearchResultAsJson(allocator, result);
defer allocator.free(json_output);
std.debug.print("{s}\n", .{json_output});
}
Using Plain Text Output Format
// After getting the search result
const plain_text_output = try PiFinder.formatSearchResultAsPlainText(allocator, result);
defer allocator.free(plain_text_output);
std.debug.print("{s}\n", .{plain_text_output});
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
Output Format Functions
formatSearchResultAsJson(allocator, result)- Formats search result as JSONformatSearchResultAsPlainText(allocator, result)- Formats search result as plain text
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
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