Files
sml-projects/fcore/search-list.sml

68 lines
1.8 KiB
Standard ML
Raw Normal View History

2025-08-06 00:30:50 +01:00
structure SearchList =
struct
type t = int vector
val empty = Vector.fromList []
2025-08-06 00:30:50 +01:00
fun searchStep (pos, hd, absIdx, tl, acc, searchPos, searchString) =
if searchPos < 0 then
(absIdx + 1) :: acc
2025-08-06 00:30:50 +01:00
else if pos < 0 then
case tl of
hd :: tl =>
searchStep
(String.size hd - 1, hd, absIdx, tl, acc, searchPos, searchString)
| [] => acc
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
searchStep
(pos - 1, hd, absIdx - 1, tl, acc, searchPos - 1, searchString)
else
2025-08-06 00:30:50 +01:00
acc
end
2024-11-13 12:54:47 +00:00
2025-08-06 00:30:50 +01:00
fun loopSearch (pos, hd, absIdx, tl, acc, searchString) =
if pos < 0 then
case tl of
hd :: tl =>
loopSearch (String.size hd - 1, hd, absIdx, tl, acc, searchString)
| [] => Vector.fromList acc
2025-08-06 00:30:50 +01:00
else
let
val acc = searchStep
(pos, hd, absIdx, tl, acc, String.size searchString - 1, searchString)
in
loopSearch (pos - 1, hd, absIdx - 1, tl, acc, searchString)
end
2024-11-13 12:54:47 +00:00
2025-08-06 00:30:50 +01:00
(* Prerequisite: move buffer/LineGap to end *)
fun search (buffer: LineGap.t, searchString) =
if String.size searchString = 0 then
empty
else
let
val {leftStrings, idx = absIdx, ...} = buffer
in
case leftStrings of
hd :: tl =>
loopSearch
(String.size hd - 1, hd, absIdx - 1, tl, [], searchString)
| [] => empty
end
2025-08-06 00:30:50 +01:00
fun build (buffer, searchString) =
if String.size searchString > 0 then
let
val buffer = LineGap.goToEnd buffer
val searchList = search (buffer, searchString)
in
(buffer, searchList)
end
else
(buffer, empty)
end