Skip to content

Instantly share code, notes, and snippets.

@tabakerov
Last active October 16, 2017 19:35
Show Gist options
  • Save tabakerov/37fbd6869773d6116ffa3016a7fb03b6 to your computer and use it in GitHub Desktop.
Save tabakerov/37fbd6869773d6116ffa3016a7fb03b6 to your computer and use it in GitHub Desktop.
split text by limit in bytes
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