From a3cf2d87a6816e21fb1f7bfd718f498400e5d677 Mon Sep 17 00:00:00 2001 From: Francesco Mecca Date: Fri, 13 Dec 2024 11:04:50 +0100 Subject: [PATCH] more code --- Pentole/pervasives.fs | 8 ++++++++ Pentole/string.fs | 34 ++++++++++++++++++---------------- Pentole/test_extensions.fs | 2 ++ 3 files changed, 28 insertions(+), 16 deletions(-) diff --git a/Pentole/pervasives.fs b/Pentole/pervasives.fs index 380fdea..7756f6f 100644 --- a/Pentole/pervasives.fs +++ b/Pentole/pervasives.fs @@ -1,5 +1,6 @@ namespace Pentole +/// The initially opened module. [] module Pervasives = @@ -7,3 +8,10 @@ module Pervasives = /// The identity function. /// let identity = id + + /// An active pattern that matches strings starting with a specified prefix. + let (|Prefix|_|) (p: string) (s: string) = + if s.StartsWith p then + s.Substring p.Length |> Some + else + None diff --git a/Pentole/string.fs b/Pentole/string.fs index 6cb06fa..fc798c3 100644 --- a/Pentole/string.fs +++ b/Pentole/string.fs @@ -1,18 +1,20 @@ -module Pentole.String +namespace Pentole -let split (separator: string) (target: string) : string list = - if System.String.IsNullOrEmpty separator then - [target] - else - let slen = separator.Length - let rec split (acc: string list) (start_idx: int) = - match target.IndexOf(separator, start_idx) with - | -1 -> - if start_idx < target.Length then - target.Substring(start_idx) :: acc - else acc - | index -> - let part = target.Substring (start_idx, index - start_idx) - split (part :: acc) (index + slen) +module String = - split [] 0 |> List.rev + let split (separator: string) (target: string) : string list = + if System.String.IsNullOrEmpty separator then + [target] + else + let slen = separator.Length + let rec split (acc: string list) (start_idx: int) = + match target.IndexOf(separator, start_idx) with + | -1 -> + if start_idx < target.Length then + target.Substring(start_idx) :: acc + else acc + | index -> + let part = target.Substring (start_idx, index - start_idx) + split (part :: acc) (index + slen) + + split [] 0 |> List.rev diff --git a/Pentole/test_extensions.fs b/Pentole/test_extensions.fs index 1e652e9..fe53c84 100644 --- a/Pentole/test_extensions.fs +++ b/Pentole/test_extensions.fs @@ -2,6 +2,8 @@ namespace Pentole open NUnit.Framework +type TestAttribute = NUnit.Framework.TestAttribute + module Assert = let inline okEquals<'ok, 'err> (expected: 'ok) (got: Result<'ok, 'err>) = match got with