Implement string.gmatch iterator - #223
Open
evadne wants to merge 1 commit into
Open
Conversation
string.gmatch(s, pattern) was previously a stub that always raised badarg. This implements it using the existing pattern matching engine. Approach: pre-compute all matches eagerly via gsub_match_loop/6, store the match list in luerl private state under a unique ref, return an erl_func iterator that pops from the match list on each call. The iterator state is cleaned up when the match list is exhausted. Uses luerl:put_private/get_private/delete_private to thread iterator state through the luerl state record rather than the process dictionary, keeping the implementation pure. The eager approach is consistent with gsub, which also pre-computes all matches via gsub_match_loop/6 before processing replacements. Both functions share the same match engine, so a future lazy stepper would benefit both equally. Eager is simpler and makes each iterator call O(1) — the right trade-off given that gmatch typically iterates to completion. Handles: - Basic iteration (word splitting, digit extraction) - Multiple captures (key=value patterns) - Single character matches - Anchored patterns (^) - No-match case (iterator immediately yields nil) - Empty input string - Multiple independent iterators - Typical Lua book pattern: building tables from key=value strings 9 new tests in luerl_str_gmatch_tests.erl.
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.
Implements
string.gmatch(s, pattern).badarggsub_match_loop/6luerlprivate state under a unique referl_funciterator that pops from the match list on each call.luerl:put_private,get_privateanddelete_privateto thread iterator state through theluerlstate record rather than the process dictionary, keeping the implementation pure.gsub, which also pre-computes all matches viagsub_match_loop/6, before processing replacements.Handles
Should fix #150