7 people like it.

Lazily split string

Splits strings lazily, instead of splitting entire string into an array like System.String.Split. Especially useful for very large strings.

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
        open System

        type System.String with 
            member x.LazySplit(separator:string) =
                if String.IsNullOrEmpty(x) || String.IsNullOrEmpty(separator) then 
                    Seq.singleton x
                else
                    let n, j = x.Length, separator.Length
                    let rec loop p = 
                        seq {
                            if p < n then
                                let i = match x.IndexOf(separator, p) with -1 -> n | i' -> i'
                                yield x.Substring(p, i - p)
                                yield! loop (i + j)
                        }
                    loop 0
namespace System
Multiple items
type String =
  new : value:char -> string + 7 overloads
  member Chars : int -> char
  member Clone : unit -> obj
  member CompareTo : value:obj -> int + 1 overload
  member Contains : value:string -> bool
  member CopyTo : sourceIndex:int * destination:char[] * destinationIndex:int * count:int -> unit
  member EndsWith : value:string -> bool + 2 overloads
  member Equals : obj:obj -> bool + 2 overloads
  member GetEnumerator : unit -> CharEnumerator
  member GetHashCode : unit -> int
  ...

Full name: System.String

--------------------
String(value: nativeptr<char>) : unit
String(value: nativeptr<sbyte>) : unit
String(value: char []) : unit
String(c: char, count: int) : unit
String(value: nativeptr<char>, startIndex: int, length: int) : unit
String(value: nativeptr<sbyte>, startIndex: int, length: int) : unit
String(value: char [], startIndex: int, length: int) : unit
String(value: nativeptr<sbyte>, startIndex: int, length: int, enc: Text.Encoding) : unit
val x : String
member String.LazySplit : separator:string -> seq<String>

Full name: Script.LazySplit
val separator : string
Multiple items
val string : value:'T -> string

Full name: Microsoft.FSharp.Core.Operators.string

--------------------
type string = String

Full name: Microsoft.FSharp.Core.string
String.IsNullOrEmpty(value: string) : bool
module Seq

from Microsoft.FSharp.Collections
val singleton : value:'T -> seq<'T>

Full name: Microsoft.FSharp.Collections.Seq.singleton
val n : int
val j : int
property String.Length: int
val loop : (int -> seq<string>)
val p : int
Multiple items
val seq : sequence:seq<'T> -> seq<'T>

Full name: Microsoft.FSharp.Core.Operators.seq

--------------------
type seq<'T> = Collections.Generic.IEnumerable<'T>

Full name: Microsoft.FSharp.Collections.seq<_>
val i : int
String.IndexOf(value: string) : int
String.IndexOf(value: char) : int
String.IndexOf(value: string, comparisonType: StringComparison) : int
String.IndexOf(value: string, startIndex: int) : int
String.IndexOf(value: char, startIndex: int) : int
String.IndexOf(value: string, startIndex: int, comparisonType: StringComparison) : int
String.IndexOf(value: string, startIndex: int, count: int) : int
String.IndexOf(value: char, startIndex: int, count: int) : int
String.IndexOf(value: string, startIndex: int, count: int, comparisonType: StringComparison) : int
val i' : int
String.Substring(startIndex: int) : string
String.Substring(startIndex: int, length: int) : string
Next Version Raw view Test code New version

More information

Link:http://fssnip.net/27
Posted:13 years ago
Author:Daniel Robinson
Tags: parsing , strings