do not require pattern matching head when in subRight/subLeft loop, but only require that in some cases

This commit is contained in:
2025-09-29 22:13:03 +01:00
parent 6de33a65c2
commit 863b4ba47b

View File

@@ -2262,26 +2262,28 @@ struct
buffer buffer
end end
fun subRight (findIdx, curIdx, tl) = fun subRight (findIdx, curIdx, hd, tl) =
if findIdx > curIdx then if findIdx > curIdx + String.size hd then
case tl of case tl of
hd :: tl => hd :: tl =>
if findIdx > curIdx + String.size hd then if findIdx > curIdx + String.size hd then
subRight (findIdx, curIdx + String.size hd, tl) subRight (findIdx, curIdx + String.size hd, hd, tl)
else else
let val strIdx = findIdx - curIdx let val strIdx = findIdx - curIdx
in String.sub (hd, strIdx) in String.sub (hd, strIdx)
end end
| [] => raise Fail "not found" | [] => raise Fail "not found"
else else
raise Fail "not found" let val strIdx = findIdx - curIdx
in String.sub (hd, strIdx)
end
fun subLeft (findIdx, curIdx, tl) = fun subLeft (findIdx, curIdx, hd, tl) =
if findIdx < curIdx then if findIdx < curIdx - String.size hd then
case tl of case tl of
hd :: tl => hd :: tl =>
if findIdx < curIdx - String.size hd then if findIdx < curIdx - String.size hd then
subLeft (findIdx, curIdx - String.size hd, tl) subLeft (findIdx, curIdx - String.size hd, hd, tl)
else else
let let
val prevIdx = curIdx - String.size hd val prevIdx = curIdx - String.size hd
@@ -2291,13 +2293,22 @@ struct
end end
| [] => raise Fail "not found" | [] => raise Fail "not found"
else else
raise Fail "not found" let
val prevIdx = curIdx - String.size hd
val strIdx = findIdx - prevIdx
in
String.sub (hd, strIdx)
end
fun sub (findIdx, buffer: t) = fun sub (findIdx, buffer: t) =
if findIdx < #idx buffer then if findIdx < #idx buffer then
subLeft (findIdx, #idx buffer, #leftStrings buffer) case #leftStrings buffer of
hd :: tl => subLeft (findIdx, #idx buffer, hd, tl)
| [] => raise Fail "not found"
else else
subRight (findIdx, #idx buffer, #rightStrings buffer) case #rightStrings buffer of
hd :: tl => subRight (findIdx, #idx buffer, hd, tl)
| [] => raise Fail "not found"
local local
fun consIfNotEmpty (s, acc) = fun consIfNotEmpty (s, acc) =