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.
This PR adds a function I've personally missed a lot while working with strings in V. It introduces a way to extract content between two marks in a string in a "balanced" way. It makes it possible to extract "block" or "scope" content in a way that is currently not possible with either of the
find_between
,all_after*
,all_before*
methods defined on thestring
type.My personal use case has been mostly to extract blocks of
enum
orstruct
contents from C (and V) code.E.g. getting the contents between the opening
{
and closing}
of theSDL_RWops
struct in the following was not easy before without extensive parsing (or position/format hacks):It's also good for things like (nested) block comments:
... and for grabbing various parts of V function signatures like:
fn fn_with_anon_fn(i int, callback fn (num int, s string))
<- arguments can be grabbed quickly (including the callback signature) with:arg_str := strings.find_between_pair(signature,'(',')') // i int, callback fn (num int, s string)
I decided to use a sumtype as mark types as both the performance and use-cases differ for the three types I've had use for:
(10000 iterations)
The benchmark I've used is included here: bmark_find_between_pair.zip
I guess it can be discussed what results should be if the input/marks is "unbalanced"/missing marks/uneven/the same:
I.e what should the result be in the following input cases?:
strings.find_between_pair('( blank ( or full ( or half of it','(','') == ??
strings.find_between_pair('* this part * or this *','*','*') == ??
These can be decided later on - right now these will result in blank or "undefined" (untested) return output.