refactor function in gap_buffer.sml to make use of two helper functions when inserting into the left/right
This commit is contained in:
124
gap_buffer.sml
124
gap_buffer.sml
@@ -42,6 +42,20 @@ struct
|
||||
, right = right
|
||||
}
|
||||
|
||||
fun joinEndOfLeft (newString, left) =
|
||||
case left of
|
||||
hd :: tail =>
|
||||
if isLessThanTarget (newString, hd) then (hd ^ newString) :: tail
|
||||
else newString :: left
|
||||
| [] => newString :: left
|
||||
|
||||
fun joinStartOfRight (newString, right) =
|
||||
case right of
|
||||
hd :: tail =>
|
||||
if isLessThanTarget (newString, hd) then (newString ^ hd) :: tail
|
||||
else newString :: right
|
||||
| [] => newString :: right
|
||||
|
||||
fun preferInsertLeft (curIdx, newString, left, right) =
|
||||
case left of
|
||||
hd :: tail =>
|
||||
@@ -60,46 +74,12 @@ struct
|
||||
| [] => consLeft (curIdx, newString, left, right))
|
||||
| [] => consLeft (curIdx, newString, left, right)
|
||||
|
||||
fun joinEndOfLeft (newString, left) =
|
||||
case left of
|
||||
hd :: tail =>
|
||||
if isLessThanTarget (newString, hd) then (hd ^ newString) :: tail
|
||||
else newString :: left
|
||||
| [] => newString :: left
|
||||
|
||||
fun joinStartOfRight (newString, right) =
|
||||
case right of
|
||||
hd :: tail =>
|
||||
if isLessThanTarget (newString, hd) then (newString ^ hd) :: tail
|
||||
else newString :: right
|
||||
| [] => newString :: right
|
||||
|
||||
fun ins (idx, newString, curIdx, left, right) : t =
|
||||
if curIdx = idx then
|
||||
preferInsertLeft (curIdx, newString, left, right)
|
||||
else if idx < curIdx then
|
||||
(* Need to insert on the left. *)
|
||||
case left of
|
||||
[] =>
|
||||
(* If there is no string on the left, then add the new string there. *)
|
||||
{idx = String.size newString, left = [newString], right = right}
|
||||
| hd :: tail =>
|
||||
let
|
||||
val prevIdx = curIdx - String.size hd
|
||||
in
|
||||
if
|
||||
idx < prevIdx
|
||||
then
|
||||
(* The requested index is prior to the string on the left,
|
||||
* so move leftward one string. *)
|
||||
ins (idx, newString, prevIdx, tail, joinStartOfRight (hd, right))
|
||||
else
|
||||
fun insLeftLeaf (prevIdx, idx, newString, curIdx, right, hd, tail) =
|
||||
(* The requested index is either:
|
||||
* - At the start of the left string
|
||||
* - In the middle of the left string
|
||||
* Find out which and split the middle of the string if necessary. *) if
|
||||
idx = prevIdx
|
||||
then
|
||||
* Find out which and split the middle of the string if necessary. *)
|
||||
if idx = prevIdx then
|
||||
(* At start of string. *)
|
||||
{ idx = curIdx + String.size newString
|
||||
, right = right
|
||||
@@ -117,18 +97,15 @@ struct
|
||||
* Using `newString ^ hd` with `newString :: hd` gives
|
||||
* different contents in the case of a zipper.
|
||||
* *)
|
||||
if isLessThanTarget (newString, hd) then
|
||||
(newString ^ hd) :: tail
|
||||
else
|
||||
hd :: newString :: tail
|
||||
if isLessThanTarget (newString, hd) then (newString ^ hd) :: tail
|
||||
else hd :: newString :: tail
|
||||
}
|
||||
else
|
||||
(* In middle of string. *)
|
||||
let
|
||||
val length = idx - prevIdx
|
||||
val sub1 = String.substring (hd, 0, length)
|
||||
val sub2 = String.substring
|
||||
(hd, length, String.size hd - length)
|
||||
val sub2 = String.substring (hd, length, String.size hd - length)
|
||||
in
|
||||
if isThreeLessThanTarget (sub1, newString, sub2) then
|
||||
{ idx = curIdx + String.size newString
|
||||
@@ -142,7 +119,7 @@ struct
|
||||
}
|
||||
else if isLessThanTarget (newString, sub2) then
|
||||
{ idx = prevIdx + String.size sub1
|
||||
, left = sub1 :: tail
|
||||
, left = joinEndOfLeft (sub1, tail)
|
||||
, right = (newString ^ sub2) :: right
|
||||
}
|
||||
else
|
||||
@@ -151,32 +128,21 @@ struct
|
||||
, right = sub1 :: newString :: sub2 :: right
|
||||
}
|
||||
end
|
||||
end
|
||||
else
|
||||
(* Need to insert to the right. *)
|
||||
case right of
|
||||
[] => {idx = curIdx, left = left, right = [newString]}
|
||||
| hd :: tail =>
|
||||
let
|
||||
val nextIdx = String.size hd + curIdx
|
||||
in
|
||||
if idx > nextIdx then
|
||||
ins (idx, newString, nextIdx, joinEndOfLeft (hd, left), tail)
|
||||
else if idx = nextIdx then
|
||||
|
||||
fun insRightLeaf (nextIdx, idx, newString, curIdx, left, hd, tail) =
|
||||
if idx = nextIdx then
|
||||
(* At end of next string. *)
|
||||
if isLessThanTarget (newString, hd) then
|
||||
{idx = curIdx, left = left, right = (hd ^ newString) :: tail}
|
||||
else
|
||||
{ idx = curIdx
|
||||
, left = left
|
||||
, right = hd :: (joinStartOfRight (newString, tail))
|
||||
, right =
|
||||
if isLessThanTarget (newString, hd) then (hd ^ newString) :: tail
|
||||
else hd :: (joinStartOfRight (newString, tail))
|
||||
}
|
||||
else
|
||||
let
|
||||
val length = idx - curIdx
|
||||
val sub1 = String.substring (hd, 0, length)
|
||||
val sub2 = String.substring
|
||||
(hd, length, String.size hd - length)
|
||||
val sub2 = String.substring (hd, length, String.size hd - length)
|
||||
in
|
||||
if isThreeLessThanTarget (sub1, newString, sub2) then
|
||||
{ idx =
|
||||
@@ -201,6 +167,40 @@ struct
|
||||
, right = joinStartOfRight (sub2, tail)
|
||||
}
|
||||
end
|
||||
|
||||
fun ins (idx, newString, curIdx, left, right) : t =
|
||||
if curIdx = idx then
|
||||
preferInsertLeft (curIdx, newString, left, right)
|
||||
else if idx < curIdx then
|
||||
(* Need to insert on the left. *)
|
||||
case left of
|
||||
[] =>
|
||||
(* If there is no string on the left, then add the new string there. *)
|
||||
{idx = String.size newString, left = [newString], right = right}
|
||||
| hd :: tail =>
|
||||
let
|
||||
val prevIdx = curIdx - String.size hd
|
||||
in
|
||||
if idx < prevIdx then
|
||||
(* The requested index is prior to the string on the left,
|
||||
* so move leftward one string. *)
|
||||
ins (idx, newString, prevIdx, tail, joinStartOfRight (hd, right))
|
||||
else
|
||||
(* Call function to insert at the current node in the zipper. *)
|
||||
insLeftLeaf (prevIdx, idx, newString, curIdx, right, hd, tail)
|
||||
end
|
||||
else
|
||||
(* Need to insert to the right. *)
|
||||
case right of
|
||||
[] => {idx = curIdx, left = left, right = [newString]}
|
||||
| hd :: tail =>
|
||||
let
|
||||
val nextIdx = String.size hd + curIdx
|
||||
in
|
||||
if idx > nextIdx then
|
||||
ins (idx, newString, nextIdx, joinEndOfLeft (hd, left), tail)
|
||||
else
|
||||
insRightLeaf (nextIdx, idx, newString, curIdx, right, hd, tail)
|
||||
end
|
||||
|
||||
fun insert (idx, newString, buffer: t) =
|
||||
|
||||
3172
svelte_gap.txt
3172
svelte_gap.txt
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user