implement function which searches for one of two chars (given as arguments) and returns the index, or ~1 if neither char is found

This commit is contained in:
2026-01-02 18:12:59 +00:00
parent 8fdf2411a8
commit 37b6f6ab0a

View File

@@ -209,31 +209,52 @@ struct
val toPrevChr = ToPrevChr.foldPrev val toPrevChr = ToPrevChr.foldPrev
structure ToEitherChrNext =
MakeIfCharFolderNext
(struct
type env = {chr1: char, chr2: char}
fun loop (strPos, str, absIdx, stl, chr1, chr2) =
if strPos = String.size str then
case stl of
str :: stl => loop (0, str, absIdx, stl, chr1, chr2)
| [] => ~1
else
let
val chr = String.sub (str, strPos)
in
if chr = chr1 orelse chr = chr2 then absIdx
else loop (strPos + 1, str, absIdx + 1, stl, chr1, chr2)
end
fun fStart (strPos, str, _, absIdx, stl, _, {chr1, chr2}) =
loop (strPos, str, absIdx, stl, chr1, chr2)
end)
val toEitherChrNext = ToEitherChrNext.foldNext
structure NextPairChr = structure NextPairChr =
MakeIfCharFolderNext MakeIfCharFolderNext
(struct (struct
type env = unit type env = unit
fun isPairChr chr = fun isPairChr chr =
chr = #"(" orelse chr = #")" orelse chr = #"(" orelse chr = #")" orelse chr = #"[" orelse chr = #"]"
chr = #"[" orelse chr = #"]" orelse orelse chr = #"{" orelse chr = #"}" orelse chr = #"<"
chr = #"{" orelse chr = #"}" orelse orelse chr = #">"
chr = #"<" orelse chr = #">"
fun loop (strPos, str, absIdx, stl) = fun loop (strPos, str, absIdx, stl) =
if strPos = String.size str then if strPos = String.size str then
case stl of case stl of
str :: stl => loop (0, str, absIdx, stl) str :: stl => loop (0, str, absIdx, stl)
| [] => ~1 | [] => ~1
else else
let let
val chr = String.sub (str, strPos) val chr = String.sub (str, strPos)
in in
if isPairChr chr then if isPairChr chr then absIdx
absIdx else loop (strPos + 1, str, absIdx + 1, stl)
else end
loop (strPos + 1, str, absIdx + 1, stl)
end
fun fStart (strPos, str, _, absIdx, stl, _, _) = fun fStart (strPos, str, _, absIdx, stl, _, _) =
loop (strPos, str, absIdx, stl) loop (strPos, str, absIdx, stl)
@@ -312,92 +333,28 @@ struct
case String.sub (shd, strIdx) of case String.sub (shd, strIdx) of
#"(" => #"(" =>
helpMatchPairNext helpMatchPairNext
( strIdx + 1 (strIdx + 1, shd, cursorIdx + 1, rightStrings, #"(", 1, #")", 0)
, shd
, cursorIdx + 1
, rightStrings
, #"("
, 1
, #")"
, 0
)
| #")" => | #")" =>
helpMatchPairPrev helpMatchPairPrev
( strIdx - 1 (strIdx - 1, shd, cursorIdx - 1, leftStrings, #"(", 0, #")", 1)
, shd
, cursorIdx - 1
, leftStrings
, #"("
, 0
, #")"
, 1
)
| #"[" => | #"[" =>
helpMatchPairNext helpMatchPairNext
( strIdx + 1 (strIdx + 1, shd, cursorIdx + 1, rightStrings, #"[", 1, #"]", 0)
, shd
, cursorIdx + 1
, rightStrings
, #"["
, 1
, #"]"
, 0
)
| #"]" => | #"]" =>
helpMatchPairPrev helpMatchPairPrev
( strIdx - 1 (strIdx - 1, shd, cursorIdx - 1, leftStrings, #"[", 0, #"]", 1)
, shd
, cursorIdx - 1
, leftStrings
, #"["
, 0
, #"]"
, 1
)
| #"{" => | #"{" =>
helpMatchPairNext helpMatchPairNext
( strIdx + 1 (strIdx + 1, shd, cursorIdx + 1, rightStrings, #"{", 1, #"}", 0)
, shd
, cursorIdx + 1
, rightStrings
, #"{"
, 1
, #"}"
, 0
)
| #"}" => | #"}" =>
helpMatchPairPrev helpMatchPairPrev
( strIdx - 1 (strIdx - 1, shd, cursorIdx - 1, leftStrings, #"{", 0, #"}", 1)
, shd
, cursorIdx - 1
, leftStrings
, #"{"
, 0
, #"}"
, 1
)
| #"<" => | #"<" =>
helpMatchPairNext helpMatchPairNext
( strIdx + 1 (strIdx + 1, shd, cursorIdx + 1, rightStrings, #"<", 1, #">", 0)
, shd
, cursorIdx + 1
, rightStrings
, #"<"
, 1
, #">"
, 0
)
| #">" => | #">" =>
helpMatchPairPrev helpMatchPairPrev
( strIdx - 1 (strIdx - 1, shd, cursorIdx - 1, leftStrings, #"<", 0, #">", 1)
, shd
, cursorIdx - 1
, leftStrings
, #"<"
, 0
, #">"
, 1
)
| _ => ~1 | _ => ~1
fun matchPair (lineGap: LineGap.t, cursorIdx) = fun matchPair (lineGap: LineGap.t, cursorIdx) =