@ -5,11 +5,13 @@ A Zig library for searching numeric sequences in the decimal places of Pi.
## Features
## Features
- Efficient searching of numeric sequences in Pi decimal places
- Efficient searching of numeric sequences in Pi decimal places
- JSON output as standard
- **Automatic memory management** with `result.deinit()`
- Optional plain text output format
- **Custom JSON serialization** using `std.json.stringify`
- JSON output as standard format
- Configurable context display with delimiters
- Configurable context display with delimiters
- Performance metrics included in results
- Reusable module design
- Reusable module design
- Performance metrics
- Compatible with Zig 0.14.1
## Installation
## Installation
@ -59,27 +61,74 @@ pub fn main() !void {
.position = 25,
.position = 25,
.delimiter = "-",
.delimiter = "-",
.delimiter_interval = 2,
.delimiter_interval = 2,
.prefix = "[",
.suffix = "]",
};
};
// Search for sequence
// Search for sequence
const result = try pi_finder.searchSequence("123456", context_config, allocator);
const result = try pi_finder.searchSequence("123456", context_config, allocator);
defer result.deinit(); // Automatically frees allocated memory
// Output as JSON (default format)
// Output as JSON using std.json.stringify
const json_output = try PiFinder.formatSearchResultAsJson(allocator, result );
var json_string = std.ArrayList(u8).init(allocator );
defer allocator.free(json_output );
defer json_string.deinit( );
std.debug.print("{s}\n", .{json_output});
try std.json.stringify(result, .{}, json_string.writer());
std.debug.print("{s}\n", .{json_string.items});
}
}
```
```
### Using Plain Text Output Format
### Plain Text Output Example
```zig
```zig
// After getting the search result
// After getting the search result
const plain_text_output = try PiFinder.formatSearchResultAsPlainText(allocator, result);
if (result.success) {
defer allocator.free(plain_text_output);
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:
```zig
const result = try pi_finder.searchSequence("123456", context_config, allocator);
defer result.deinit(); // Automatically frees allocated context memory
```
### Custom JSON Serialization
std.debug.print("{s}\n", .{plain_text_output});
SearchResult implements custom JSON serialization that excludes internal fields:
```zig
// The allocator field is automatically excluded from JSON output
try std.json.stringify(result, .{}, writer);
```
### Performance Metrics
Every search result includes detailed performance metrics:
```json
{
"performance": {
"file_load_time_ms": 327.988,
"search_time_ms": 10.766,
"file_size_mb": 953.674,
"load_speed_mbs": 2948.052
}
}
```
```
## API
## API
@ -92,11 +141,6 @@ std.debug.print("{s}\n", .{plain_text_output});
- `getContext(position, config, sequence, allocator)` - Generates context
- `getContext(position, config, sequence, allocator)` - Generates context
- `searchSequence(sequence, config, allocator)` - Comprehensive search with structured result
- `searchSequence(sequence, config, allocator)` - Comprehensive search with structured result
### Output Format Functions
- `formatSearchResultAsJson(allocator, result)` - Formats search result as JSON
- `formatSearchResultAsPlainText(allocator, result)` - Formats search result as plain text
### ContextConfig
### ContextConfig
Configuration for context display:
Configuration for context display:
@ -120,6 +164,11 @@ Structured result containing:
- `performance` - Performance metrics
- `performance` - Performance metrics
- `context_config` - Used context configuration
- `context_config` - Used context configuration
**Methods:**
- `deinit()` - Frees any dynamically allocated memory (like context)
- `jsonStringify(writer)` - Custom JSON serialization (automatically used by `std.json.stringify` )
## Examples
## Examples
See the `examples/` directory for complete examples:
See the `examples/` directory for complete examples: