2025-08-06 00:30:50 +01:00
|
|
|
structure SearchList =
|
2024-11-11 13:23:37 +00:00
|
|
|
struct
|
2025-08-06 00:42:24 +01:00
|
|
|
type t = int vector
|
2024-11-11 13:23:37 +00:00
|
|
|
|
2025-08-06 00:42:24 +01:00
|
|
|
val empty = Vector.fromList []
|
2025-08-05 13:24:55 +01:00
|
|
|
|
2025-09-06 22:53:07 +01:00
|
|
|
fun backtrackFull (pos, hd, absIdx, tl, acc, searchPos, searchString, prevTl) =
|
2025-09-27 14:47:24 +01:00
|
|
|
if pos < 0 then
|
2025-09-06 22:53:07 +01:00
|
|
|
case prevTl of
|
|
|
|
|
prevHd :: prevTl =>
|
|
|
|
|
let
|
|
|
|
|
val tl = hd :: tl
|
|
|
|
|
in
|
|
|
|
|
backtrackFull
|
|
|
|
|
( String.size prevHd - 1
|
|
|
|
|
, prevHd
|
|
|
|
|
, absIdx
|
|
|
|
|
, tl
|
|
|
|
|
, acc
|
|
|
|
|
, searchPos
|
|
|
|
|
, searchString
|
|
|
|
|
, prevTl
|
|
|
|
|
)
|
|
|
|
|
end
|
|
|
|
|
| [] =>
|
|
|
|
|
(* Should never be called *)
|
|
|
|
|
raise Fail "SearchList.backtrackFull error: line 24\n"
|
2025-09-27 14:47:24 +01:00
|
|
|
else if searchPos <= 1 then
|
|
|
|
|
(* we are trying to backtrack to index 1,
|
|
|
|
|
* and then continue are search from here *)
|
|
|
|
|
loopSearch (pos, hd, absIdx, tl, acc, 0, searchString, prevTl)
|
2025-09-06 22:53:07 +01:00
|
|
|
else
|
|
|
|
|
backtrackFull
|
|
|
|
|
(pos - 1, hd, absIdx - 1, tl, acc, searchPos - 1, searchString, prevTl)
|
|
|
|
|
|
|
|
|
|
and loopSearch (pos, hd, absIdx, tl, acc, searchPos, searchString, prevTl) =
|
2025-08-30 23:05:11 +01:00
|
|
|
if pos = String.size hd then
|
2025-08-06 00:30:50 +01:00
|
|
|
case tl of
|
2025-09-06 22:53:07 +01:00
|
|
|
newHd :: newTl =>
|
|
|
|
|
loopSearch
|
|
|
|
|
( 0
|
|
|
|
|
, newHd
|
|
|
|
|
, absIdx
|
|
|
|
|
, newTl
|
|
|
|
|
, acc
|
|
|
|
|
, searchPos
|
|
|
|
|
, searchString
|
|
|
|
|
, hd :: prevTl
|
|
|
|
|
)
|
2025-08-30 23:31:55 +01:00
|
|
|
| [] => PersistentVector.toVector acc
|
2024-11-16 09:05:47 +00:00
|
|
|
else
|
2025-08-06 00:30:50 +01:00
|
|
|
let
|
|
|
|
|
val bufferChr = String.sub (hd, pos)
|
|
|
|
|
val searchChr = String.sub (searchString, searchPos)
|
|
|
|
|
in
|
|
|
|
|
if bufferChr = searchChr then
|
2025-08-30 23:05:11 +01:00
|
|
|
if searchPos + 1 = String.size searchString then
|
|
|
|
|
(* we fully matched the search string *)
|
|
|
|
|
let
|
|
|
|
|
val foundIdx = absIdx - String.size searchString + 1
|
|
|
|
|
val acc = PersistentVector.append (foundIdx, acc)
|
|
|
|
|
in
|
2025-09-06 22:53:07 +01:00
|
|
|
loopSearch
|
|
|
|
|
(pos + 1, hd, absIdx + 1, tl, acc, 0, searchString, prevTl)
|
2025-08-30 23:05:11 +01:00
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|
loopSearch
|
2025-09-06 22:53:07 +01:00
|
|
|
( pos + 1
|
|
|
|
|
, hd
|
|
|
|
|
, absIdx + 1
|
|
|
|
|
, tl
|
|
|
|
|
, acc
|
|
|
|
|
, searchPos + 1
|
|
|
|
|
, searchString
|
|
|
|
|
, prevTl
|
|
|
|
|
)
|
2025-08-05 13:24:55 +01:00
|
|
|
else
|
2025-08-30 23:31:55 +01:00
|
|
|
(if searchPos = 0 then
|
2025-09-06 22:53:07 +01:00
|
|
|
loopSearch
|
|
|
|
|
(pos + 1, hd, absIdx + 1, tl, acc, 0, searchString, prevTl)
|
2025-08-30 23:31:55 +01:00
|
|
|
else
|
2025-09-06 23:05:11 +01:00
|
|
|
backtrackFull
|
|
|
|
|
(pos, hd, absIdx, tl, acc, searchPos, searchString, prevTl))
|
2025-08-06 00:30:50 +01:00
|
|
|
end
|
2024-11-13 12:54:47 +00:00
|
|
|
|
2025-08-30 23:05:11 +01:00
|
|
|
fun search ({rightStrings, leftStrings, ...}: LineGap.t, searchString) =
|
|
|
|
|
case rightStrings of
|
|
|
|
|
hd :: tl =>
|
2025-09-06 22:53:07 +01:00
|
|
|
loopSearch (0, hd, 0, tl, PersistentVector.empty, 0, searchString, [])
|
2025-08-30 23:05:11 +01:00
|
|
|
| [] => empty
|
2024-11-24 21:38:58 +00:00
|
|
|
|
2025-08-30 23:31:55 +01:00
|
|
|
(* Prerequisite: move buffer/LineGap to start *)
|
2025-08-06 00:30:50 +01:00
|
|
|
fun build (buffer, searchString) =
|
2025-08-07 15:28:29 +01:00
|
|
|
if String.size searchString > 0 then search (buffer, searchString)
|
|
|
|
|
else empty
|
2025-08-07 13:12:04 +01:00
|
|
|
|
2025-09-06 23:05:11 +01:00
|
|
|
fun backtrackRange
|
|
|
|
|
(pos, hd, absIdx, tl, acc, searchPos, searchString, finish, prevTl) =
|
2025-09-27 12:40:28 +01:00
|
|
|
if pos < 0 then
|
2025-09-06 23:05:11 +01:00
|
|
|
case prevTl of
|
|
|
|
|
prevHd :: prevTl =>
|
|
|
|
|
let
|
|
|
|
|
val tl = hd :: tl
|
|
|
|
|
in
|
|
|
|
|
backtrackRange
|
|
|
|
|
( String.size prevHd - 1
|
|
|
|
|
, prevHd
|
|
|
|
|
, absIdx
|
|
|
|
|
, tl
|
|
|
|
|
, acc
|
|
|
|
|
, searchPos
|
|
|
|
|
, searchString
|
|
|
|
|
, finish
|
|
|
|
|
, prevTl
|
|
|
|
|
)
|
|
|
|
|
end
|
|
|
|
|
| [] =>
|
|
|
|
|
(* Should never be called *)
|
|
|
|
|
raise Fail "SearchList.backtrackRange error: line 120\n"
|
2025-09-27 12:40:28 +01:00
|
|
|
else if searchPos <= 1 then
|
|
|
|
|
loopRange (pos, hd, absIdx, tl, acc, 0, searchString, finish, prevTl)
|
2025-09-06 23:05:11 +01:00
|
|
|
else
|
|
|
|
|
backtrackRange
|
|
|
|
|
( pos - 1
|
|
|
|
|
, hd
|
|
|
|
|
, absIdx - 1
|
|
|
|
|
, tl
|
|
|
|
|
, acc
|
|
|
|
|
, searchPos - 1
|
|
|
|
|
, searchString
|
|
|
|
|
, finish
|
|
|
|
|
, prevTl
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
and loopRange
|
|
|
|
|
(pos, hd, absIdx, tl, acc, searchPos, searchString, finish, prevTl) =
|
2025-08-30 23:31:55 +01:00
|
|
|
if pos = String.size hd then
|
|
|
|
|
case tl of
|
2025-09-06 23:05:11 +01:00
|
|
|
newHd :: newTl =>
|
|
|
|
|
let
|
|
|
|
|
val prevTl = hd :: prevTl
|
|
|
|
|
in
|
|
|
|
|
loopRange
|
|
|
|
|
( 0
|
|
|
|
|
, newHd
|
|
|
|
|
, absIdx
|
|
|
|
|
, newTl
|
|
|
|
|
, acc
|
|
|
|
|
, searchPos
|
|
|
|
|
, searchString
|
|
|
|
|
, finish
|
|
|
|
|
, prevTl
|
|
|
|
|
)
|
|
|
|
|
end
|
2025-08-30 23:31:55 +01:00
|
|
|
| [] => PersistentVector.toVector acc
|
|
|
|
|
else if absIdx = finish then
|
|
|
|
|
PersistentVector.toVector acc
|
|
|
|
|
else
|
|
|
|
|
let
|
|
|
|
|
val bufferChr = String.sub (hd, pos)
|
|
|
|
|
val searchChr = String.sub (searchString, searchPos)
|
|
|
|
|
in
|
|
|
|
|
if bufferChr = searchChr then
|
|
|
|
|
if searchPos + 1 = String.size searchString then
|
|
|
|
|
(* full match *)
|
|
|
|
|
let
|
|
|
|
|
val foundIdx = absIdx - String.size searchString + 1
|
|
|
|
|
val acc = PersistentVector.append (foundIdx, acc)
|
|
|
|
|
in
|
|
|
|
|
loopRange
|
2025-09-06 23:05:11 +01:00
|
|
|
( pos + 1
|
|
|
|
|
, hd
|
|
|
|
|
, absIdx + 1
|
|
|
|
|
, tl
|
|
|
|
|
, acc
|
|
|
|
|
, 0
|
|
|
|
|
, searchString
|
|
|
|
|
, finish
|
|
|
|
|
, prevTl
|
|
|
|
|
)
|
2025-08-30 23:31:55 +01:00
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|
loopRange
|
|
|
|
|
( pos + 1
|
|
|
|
|
, hd
|
|
|
|
|
, absIdx + 1
|
|
|
|
|
, tl
|
|
|
|
|
, acc
|
|
|
|
|
, searchPos + 1
|
|
|
|
|
, searchString
|
|
|
|
|
, finish
|
2025-09-06 23:05:11 +01:00
|
|
|
, prevTl
|
2025-08-30 23:31:55 +01:00
|
|
|
)
|
|
|
|
|
else
|
|
|
|
|
((if searchPos = 0 then
|
|
|
|
|
loopRange
|
2025-09-06 23:05:11 +01:00
|
|
|
( pos + 1
|
|
|
|
|
, hd
|
|
|
|
|
, absIdx + 1
|
|
|
|
|
, tl
|
|
|
|
|
, acc
|
|
|
|
|
, 0
|
|
|
|
|
, searchString
|
|
|
|
|
, finish
|
|
|
|
|
, prevTl
|
|
|
|
|
)
|
2025-08-30 23:31:55 +01:00
|
|
|
else
|
2025-09-06 23:05:11 +01:00
|
|
|
backtrackRange
|
|
|
|
|
( pos
|
|
|
|
|
, hd
|
|
|
|
|
, absIdx
|
|
|
|
|
, tl
|
|
|
|
|
, acc
|
|
|
|
|
, searchPos
|
|
|
|
|
, searchString
|
|
|
|
|
, finish
|
|
|
|
|
, prevTl
|
|
|
|
|
)))
|
2025-08-30 23:31:55 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
fun searchRange (buffer: LineGap.t, searchString, finish) =
|
|
|
|
|
let
|
|
|
|
|
val {rightStrings, idx = absIdx, ...} = buffer
|
|
|
|
|
in
|
|
|
|
|
case rightStrings of
|
|
|
|
|
hd :: tl =>
|
|
|
|
|
loopRange
|
2025-09-06 23:05:11 +01:00
|
|
|
( 0
|
|
|
|
|
, hd
|
|
|
|
|
, absIdx
|
|
|
|
|
, tl
|
|
|
|
|
, PersistentVector.empty
|
|
|
|
|
, 0
|
|
|
|
|
, searchString
|
|
|
|
|
, finish
|
|
|
|
|
, []
|
|
|
|
|
)
|
2025-08-30 23:31:55 +01:00
|
|
|
| [] => empty
|
|
|
|
|
end
|
|
|
|
|
|
2025-09-29 13:13:14 +01:00
|
|
|
fun buildRange (buffer, searchString, finishIdx) =
|
2025-08-30 23:31:55 +01:00
|
|
|
if String.size searchString > 0 then
|
2025-09-29 13:13:14 +01:00
|
|
|
let
|
|
|
|
|
val nfa = Nfa.parse searchString
|
|
|
|
|
val startIdx = #idx buffer
|
|
|
|
|
in
|
|
|
|
|
Nfa.getMatchesInRange (startIdx, finishIdx, buffer : LineGap.t, nfa)
|
|
|
|
|
end
|
2025-08-30 23:31:55 +01:00
|
|
|
else
|
|
|
|
|
empty
|
|
|
|
|
|
2025-08-07 13:12:04 +01:00
|
|
|
fun loopNextMatch (pos, searchList, count) =
|
|
|
|
|
if count = 0 then
|
|
|
|
|
Vector.sub (searchList, pos)
|
|
|
|
|
else
|
|
|
|
|
let
|
|
|
|
|
val pos = pos + 1
|
|
|
|
|
val pos = if pos < Vector.length searchList then pos else 0
|
|
|
|
|
val count = count - 1
|
|
|
|
|
in
|
|
|
|
|
loopNextMatch (pos, searchList, count)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
fun nextMatch (cursorIdx, searchList, count) =
|
|
|
|
|
if Vector.length searchList = 0 then
|
|
|
|
|
~1
|
|
|
|
|
else
|
|
|
|
|
let
|
|
|
|
|
val pos = BinSearch.equalOrMore (cursorIdx + 1, searchList)
|
2025-09-13 02:18:14 +01:00
|
|
|
val pos = if pos = ~1 then 0 else pos
|
2025-08-07 13:12:04 +01:00
|
|
|
val count = count - 1
|
|
|
|
|
in
|
|
|
|
|
loopNextMatch (pos, searchList, count)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
fun loopPrevMatch (pos, searchList, count) =
|
|
|
|
|
if count = 0 then
|
|
|
|
|
Vector.sub (searchList, pos)
|
|
|
|
|
else
|
|
|
|
|
let
|
|
|
|
|
val pos = pos - 1
|
|
|
|
|
val pos = if pos < 0 then Vector.length searchList - 1 else pos
|
|
|
|
|
val count = count - 1
|
|
|
|
|
in
|
|
|
|
|
loopPrevMatch (pos, searchList, count)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
fun prevMatch (cursorIdx, searchList, count) =
|
|
|
|
|
if Vector.length searchList = 0 then
|
|
|
|
|
~1
|
|
|
|
|
else
|
|
|
|
|
let
|
|
|
|
|
val pos = BinSearch.equalOrLess (cursorIdx - 1, searchList)
|
2025-09-13 02:18:14 +01:00
|
|
|
val pos = if pos = ~1 then Vector.length searchList - 1 else pos
|
2025-08-07 13:12:04 +01:00
|
|
|
val count = count - 1
|
|
|
|
|
in
|
|
|
|
|
loopPrevMatch (pos, searchList, count)
|
|
|
|
|
end
|
2024-11-11 13:23:37 +00:00
|
|
|
end
|