diff options
| author | Bruce Hill <bruce@bruce-hill.com> | 2024-11-02 13:51:58 -0400 |
|---|---|---|
| committer | Bruce Hill <bruce@bruce-hill.com> | 2024-11-02 13:51:58 -0400 |
| commit | be2673ef2b102c49c4db3e0079b9269b787d821f (patch) | |
| tree | cc6ab972c658c3c06dc234642cb702b243170f41 /stdlib | |
| parent | a60e0e5e9a5fc0e625ece0151328cde6147e8abf (diff) | |
Make Text:find() return an optional int
Diffstat (limited to 'stdlib')
| -rw-r--r-- | stdlib/patterns.c | 6 | ||||
| -rw-r--r-- | stdlib/patterns.h | 2 |
2 files changed, 4 insertions, 4 deletions
diff --git a/stdlib/patterns.c b/stdlib/patterns.c index b8f204f4..fdc7a79f 100644 --- a/stdlib/patterns.c +++ b/stdlib/patterns.c @@ -796,15 +796,15 @@ static int64_t _find(Text_t text, Pattern_t pattern, int64_t first, int64_t last return -1; } -public Int_t Text$find(Text_t text, Pattern_t pattern, Int_t from_index) +public OptionalInt_t Text$find(Text_t text, Pattern_t pattern, Int_t from_index) { int64_t first = Int_to_Int64(from_index, false); if (first == 0) fail("Invalid index: 0"); if (first < 0) first = text.length + first + 1; if (first > text.length || first < 1) - return I(0); + return NULL_INT; int64_t found = _find(text, pattern, first-1, text.length-1, NULL); - return I(found+1); + return found == -1 ? NULL_INT : I(found+1); } PUREFUNC public bool Text$has(Text_t text, Pattern_t pattern) diff --git a/stdlib/patterns.h b/stdlib/patterns.h index 9cfbcd6b..bea58453 100644 --- a/stdlib/patterns.h +++ b/stdlib/patterns.h @@ -18,7 +18,7 @@ Pattern_t Pattern$escape_text(Text_t text); Text_t Text$replace_all(Text_t text, Table_t replacements, Pattern_t backref_pat, bool recursive); Array_t Text$split(Text_t text, Pattern_t pattern); Text_t Text$trim(Text_t text, Pattern_t pattern, bool trim_left, bool trim_right); -Int_t Text$find(Text_t text, Pattern_t pattern, Int_t i); +OptionalInt_t Text$find(Text_t text, Pattern_t pattern, Int_t i); Array_t Text$find_all(Text_t text, Pattern_t pattern); PUREFUNC bool Text$has(Text_t text, Pattern_t pattern); Array_t Text$matches(Text_t text, Pattern_t pattern); |
