mirror of
https://github.com/Ed94/Odin.git
synced 2026-08-06 15:48:51 +00:00
Merge pull request #4697 from JamesDSource/master
Allow captures in gfind and gmatch to be used in-loop
This commit is contained in:
@@ -682,11 +682,14 @@ find_aux :: proc(
|
|||||||
|
|
||||||
// iterative matching which returns the 0th/1st match
|
// iterative matching which returns the 0th/1st match
|
||||||
// rest has to be used from captures
|
// rest has to be used from captures
|
||||||
|
// assumes captures is zeroed on first iteration
|
||||||
|
// resets captures to zero on last iteration
|
||||||
gmatch :: proc(
|
gmatch :: proc(
|
||||||
haystack: ^string,
|
haystack: ^string,
|
||||||
pattern: string,
|
pattern: string,
|
||||||
captures: ^[MAX_CAPTURES]Match,
|
captures: ^[MAX_CAPTURES]Match,
|
||||||
) -> (res: string, ok: bool) {
|
) -> (res: string, ok: bool) {
|
||||||
|
haystack^ = haystack[captures[0].byte_end:]
|
||||||
if len(haystack) > 0 {
|
if len(haystack) > 0 {
|
||||||
length, err := find_aux(haystack^, pattern, 0, false, captures)
|
length, err := find_aux(haystack^, pattern, 0, false, captures)
|
||||||
|
|
||||||
@@ -695,10 +698,11 @@ gmatch :: proc(
|
|||||||
first := length > 1 ? 1 : 0
|
first := length > 1 ? 1 : 0
|
||||||
cap := captures[first]
|
cap := captures[first]
|
||||||
res = haystack[cap.byte_start:cap.byte_end]
|
res = haystack[cap.byte_start:cap.byte_end]
|
||||||
haystack^ = haystack[cap.byte_end:]
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if !ok {
|
||||||
|
captures^ = {}
|
||||||
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -794,11 +798,14 @@ gsub_with :: proc(
|
|||||||
gsub :: proc { gsub_builder, gsub_allocator }
|
gsub :: proc { gsub_builder, gsub_allocator }
|
||||||
|
|
||||||
// iterative find with zeroth capture only
|
// iterative find with zeroth capture only
|
||||||
|
// assumes captures is zeroed on first iteration
|
||||||
|
// resets captures to zero on last iteration
|
||||||
gfind :: proc(
|
gfind :: proc(
|
||||||
haystack: ^string,
|
haystack: ^string,
|
||||||
pattern: string,
|
pattern: string,
|
||||||
captures: ^[MAX_CAPTURES]Match,
|
captures: ^[MAX_CAPTURES]Match,
|
||||||
) -> (res: string, ok: bool) {
|
) -> (res: string, ok: bool) {
|
||||||
|
haystack^ = haystack[captures[0].byte_end:]
|
||||||
if len(haystack) > 0 {
|
if len(haystack) > 0 {
|
||||||
length, err := find_aux(haystack^, pattern, 0, true, captures)
|
length, err := find_aux(haystack^, pattern, 0, true, captures)
|
||||||
|
|
||||||
@@ -806,10 +813,11 @@ gfind :: proc(
|
|||||||
ok = true
|
ok = true
|
||||||
cap := captures[0]
|
cap := captures[0]
|
||||||
res = haystack[cap.byte_start:cap.byte_end]
|
res = haystack[cap.byte_start:cap.byte_end]
|
||||||
haystack^ = haystack[cap.byte_end:]
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if !ok {
|
||||||
|
captures^ = {}
|
||||||
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user