Add cleaner API with fte.Encoder class#36
Merged
Merged
Conversation
added 4 commits
January 10, 2026 14:29
New API:
- fte.Encoder(regex, fixed_slice, key) - main class
- fte.encode() / fte.decode() - convenience functions
- regex2dfa is now called internally, users don't need to import it
Before:
import regex2dfa
import fte.encoder
dfa = regex2dfa.regex2dfa('^[a-z]+$')
encoder = fte.encoder.DfaEncoder(dfa, 128)
After:
import fte
encoder = fte.Encoder(regex='^[a-z]+$', fixed_slice=128)
Or even simpler:
ciphertext = fte.encode(b'secret', regex='^[a-z]+$')
Updated all examples and documentation to use new API.
Added example 12 for convenience functions.
Performance improvements: - Use std::unordered_set for final_states (O(1) vs O(n) lookup) - Use std::unordered_map for sigma/sigma_reverse (O(1) vs O(log n)) - Use [] instead of .at() in hot paths after validation - Add string reserve() in unrank to avoid reallocations - Parse DFA string in single pass instead of twice Code quality: - Add const to methods that don't modify state - Use references for large parameters - Fix exception handling (was catching wrong type) - Use std::runtime_error instead of custom exception classes - Use modern C++ style (auto, range-for, emplace_back) - Fix typos in comments - Use <cstdint> instead of <stdint.h> - C++11 compatible (no structured bindings)
- Main example now uses word-like output with spaces - More Examples section shows URL paths, slugs, and alphanumeric tokens - All examples are tested and work correctly - Updated both README.md and README_PYPI.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
New API:
Before:
import regex2dfa import fte.encoder dfa = regex2dfa.regex2dfa('^[a-z]+$') encoder = fte.encoder.DfaEncoder(dfa, 128)
After:
import fte encoder = fte.Encoder(regex='^[a-z]+$', fixed_slice=128)
Or even simpler:
ciphertext = fte.encode(b'secret', regex='^[a-z]+$')
Updated all examples and documentation to use new API. Added example 12 for convenience functions.