2025-08-06 00:30:50 +01:00
|
|
|
structure SearchList =
|
2024-11-11 13:23:37 +00:00
|
|
|
struct
|
2025-09-29 14:02:07 +01:00
|
|
|
val empty = PersistentVector.empty
|
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-09-29 14:02:07 +01:00
|
|
|
| [] => 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
|
2025-09-29 14:02:07 +01:00
|
|
|
val acc = PersistentVector.append (foundIdx, absIdx, acc)
|
2025-08-30 23:05:11 +01:00
|
|
|
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-09-29 14:55:20 +01:00
|
|
|
| [] => PersistentVector.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)
|
2025-09-29 14:55:20 +01:00
|
|
|
else PersistentVector.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-09-29 14:02:07 +01:00
|
|
|
| [] => acc
|
2025-08-30 23:31:55 +01:00
|
|
|
else if absIdx = finish then
|
2025-09-29 14:02:07 +01:00
|
|
|
acc
|
2025-08-30 23:31:55 +01:00
|
|
|
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
|
2025-09-29 14:02:07 +01:00
|
|
|
val acc = PersistentVector.append (foundIdx, absIdx, acc)
|
2025-08-30 23:31:55 +01:00
|
|
|
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-09-29 14:55:20 +01:00
|
|
|
| [] => PersistentVector.empty
|
2025-08-30 23:31:55 +01:00
|
|
|
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:34:55 +01:00
|
|
|
case Nfa.parse searchString of
|
|
|
|
|
SOME nfa =>
|
2025-09-29 14:02:07 +01:00
|
|
|
Nfa.getMatchesInRange
|
|
|
|
|
(#idx buffer, finishIdx, buffer : LineGap.t, nfa)
|
2025-09-29 14:55:20 +01:00
|
|
|
| NONE => PersistentVector.empty
|
2025-08-30 23:31:55 +01:00
|
|
|
else
|
2025-09-29 14:55:20 +01:00
|
|
|
PersistentVector.empty
|
2025-08-30 23:31:55 +01:00
|
|
|
|
2025-09-29 15:02:40 +01:00
|
|
|
fun nextMatch (cursorIdx, searchList, count) = raise Fail "todo: reimplement"
|
2025-08-07 13:12:04 +01:00
|
|
|
|
2025-09-29 15:02:40 +01:00
|
|
|
fun prevMatch (cursorIdx, searchList, count) = raise Fail "todo: reimplement"
|
2024-11-11 13:23:37 +00:00
|
|
|
end
|