add 'SearchList.exists' function to fcore/search-list.sml function, and make sure we check if mmatchedIdx exists before inserting or appending (so we maintain set-like semantics where each number exists only once)

This commit is contained in:
2024-11-24 21:38:58 +00:00
parent c6343cac40
commit d399016c1a
3 changed files with 120 additions and 44 deletions

View File

@@ -5,8 +5,7 @@ struct
case tl of
tlhd :: tltl =>
helpNextMatch (0, tlhd, tltl, absIdx, searchString, matchedChrs)
| [] =>
NONE
| [] => NONE
else
let
val hdChr = String.sub (hd, idx)
@@ -14,13 +13,11 @@ struct
in
if hdChr = searchChr then
if matchedChrs + 1 = String.size searchString then
let
val matchedIdx = absIdx - String.size searchString + 1
in
SOME matchedIdx
let val matchedIdx = absIdx - String.size searchString + 1
in SOME matchedIdx
end
else
helpNextMatch
helpNextMatch
(idx + 1, hd, tl, absIdx + 1, searchString, matchedChrs + 1)
else
helpNextMatch (idx + 1, hd, tl, absIdx + 1, searchString, 0)
@@ -29,20 +26,18 @@ struct
fun nextMatch (bufferIdx, absIdx, rightStrings, searchString) =
case rightStrings of
hd :: tl =>
let
let
val strIdx = absIdx - bufferIdx
in
if strIdx < String.size hd then
helpNextMatch (strIdx, hd, tl, absIdx, searchString, 0)
else
(case tl of
tlhd :: tltl =>
let
val strIdx = strIdx - String.size hd
in
helpNextMatch (strIdx, tlhd, tltl, absIdx, searchString, 0)
end
| [] => NONE)
tlhd :: tltl =>
let val strIdx = strIdx - String.size hd
in helpNextMatch (strIdx, tlhd, tltl, absIdx, searchString, 0)
end
| [] => NONE)
end
| [] => NONE
@@ -53,12 +48,26 @@ struct
in
case nextMatch (bufferIdx, absIdx, rightStrings, searchString) of
SOME matchedIdx =>
let
val searchList = SearchList.append (matchedIdx, searchList)
in
(* Edge case: we may be searching for a string like "a"
* when the buffer represents "aaa aaa aaa".
* In this case, there will be continual matches that are consecutive
* and we need to check every char in the buffer which is absIdx + 1.
* However, we can skip to matchedIdx + 1 if matchedIdx already exists
* in the searchList because we know the string between
* [absIdx ... matchedIdx - 1] contains no matches.
* This check is important to preserve the set-like semaantics
* of the searchList too: SearchList.append does not check for this.
* *)
if SearchList.exists (matchedIdx, searchList) then
helpFromStart
(app, origIdx, matchedIdx + 1, buffer, searchString, searchList)
end
else
let
val searchList = SearchList.append (matchedIdx, searchList)
in
helpFromStart
(app, origIdx, absIdx + 1, buffer, searchString, searchList)
end
| NONE =>
let
val buffer = LineGap.goToIdx (origIdx, buffer)
@@ -80,38 +89,46 @@ struct
app
(* searches for matchedIdx within a range from the buffer instead of from start *)
fun helpFromRange
fun helpFromRange
(origIdx, curIdx, finishIdx, buffer, searchString, searchList) =
let
val buffer = LineGap.goToIdx (curIdx, buffer)
val {idx = bufferIdx, rightStrings, ...} = buffer
in
case nextMatch (bufferIdx, curIdx, rightStrings, searchString) of
SOME matchedIdx =>
if matchedIdx > finishIdx then
let
val buffer = LineGap.goToIdx (origIdx, buffer)
val searchList = SearchList.goToNum (origIdx, searchList)
in
(buffer, searchList)
end
else
let
val searchList = SearchList.insert (matchedIdx, searchList)
in
helpFromRange
( origIdx, matchedIdx + 1, finishIdx
, buffer, searchString, searchList
)
end
| NONE =>
let
val buffer = LineGap.goToIdx (curIdx, buffer)
val {idx = bufferIdx, rightStrings, ...} = buffer
in
case nextMatch (bufferIdx, curIdx, rightStrings, searchString) of
SOME matchedIdx =>
if matchedIdx > finishIdx then
let
val buffer = LineGap.goToIdx (origIdx, buffer)
val searchList = SearchList.goToNum (origIdx, searchList)
in
(buffer, searchList)
end
end
else
let
val searchList =
if SearchList.exists (matchedIdx, searchList) then
searchList
else
SearchList.insert (matchedIdx, searchList)
in
helpFromRange
( origIdx
, curIdx + 1
, finishIdx
, buffer
, searchString
, searchList
)
end
| NONE =>
let
val buffer = LineGap.goToIdx (origIdx, buffer)
val searchList = SearchList.goToNum (origIdx, searchList)
in
(buffer, searchList)
end
end
fun fromRange (startIdx, length, buffer, searchString, searchList) =
let