Last active
October 16, 2017 19:35
-
-
Save tabakerov/37fbd6869773d6116ffa3016a7fb03b6 to your computer and use it in GitHub Desktop.
split text by limit in bytes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
open System | |
open System.Globalization | |
type ISplitter = | |
abstract member Split: string -> int -> string[] | |
type Splitter () = | |
let folder (lim:int) (acc:string list) (el:string) = | |
match acc with | |
| head::tail -> | |
match head.Length+el.Length>lim/2 with | |
| true -> | |
el::acc | |
| false -> | |
let newHead = head+el | |
newHead::tail | |
| _ -> [|el|] |> Array.toList | |
interface ISplitter with | |
member this.Split str len = | |
let limitedFolder = folder len | |
let strEnum = StringInfo.GetTextElementEnumerator str | |
seq { while strEnum.MoveNext() do yield strEnum.GetTextElement() } | |
|> Seq.fold limitedFolder list.Empty |> Seq.rev |> Seq.toArray |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment