Skip to content

Commit

Permalink
add Stdlib.{concat,chop-prefix,chop-suffix}
Browse files Browse the repository at this point in the history
  • Loading branch information
gfngfn committed Oct 20, 2024
1 parent 52c5c00 commit fcfc3c2
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib-satysfi/packages/stdlib/stdlib.0.0.1/src/stdlib.satyh
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ module Stdlib :> sig
val persistent ~split-into-grapheme-clusters : string -> list string
val persistent ~empty : string
val persistent ~split-into-lines : string -> list (int * string)
val persistent ~concat : string -> list string -> string
val persistent ~chop-prefix : string -> string -> option string
val persistent ~chop-suffix : string -> string -> option string
end
module Ordering : sig
type t = ordering
Expand Down
32 changes: 32 additions & 0 deletions lib-satysfi/packages/stdlib/stdlib.0.0.1/src/string.satyg
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ module String :> sig
val persistent ~split-into-grapheme-clusters : string -> list string
val persistent ~empty : string
val persistent ~split-into-lines : string -> list (int * string)
val persistent ~concat : string -> list string -> string
val persistent ~chop-prefix : string -> string -> option string
val persistent ~chop-suffix : string -> string -> option string
end = struct

type t = string
Expand Down Expand Up @@ -55,4 +58,33 @@ end = struct

val persistent ~split-into-lines = split-into-lines %PRIMITIVE

val persistent ~concat sep elems =
elems |> List.fold-adjacent (fun s elem _ next-opt ->
match next-opt with
| None -> s ^ elem
| Some(_) -> s ^ elem ^ sep
end
) ` `

val persistent ~rec chop-aux chars-prefix chars-target =
match (chars-prefix, chars-target) with
| ([], _) ->
Some(chars-target)
| (prefix-head :: prefix-tail, target-head :: target-tail) ->
if prefix-head == target-head then
chop-aux prefix-tail target-tail
else
None
| (_, []) ->
None
end

val persistent ~chop-prefix prefix target =
Option.map from-scalar-values (chop-aux (to-scalar-values prefix) (to-scalar-values target))

val persistent ~chop-suffix suffix target =
let decompose s = List.reverse (to-scalar-values s) in
let compose chars = from-scalar-values (List.reverse chars) in
Option.map compose (chop-aux (decompose suffix) (decompose target))

end

0 comments on commit fcfc3c2

Please sign in to comment.