2 people like it.

Fholm

Fast Concatenated String

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
24: 
25: 
26: 
27: 
28: 
29: 
30: 
31: 
32: 
33: 
34: 
35: 
36: 
37: 
38: 
39: 
40: 
41: 
42: 
43: 
44: 
45: 
46: 
47: 
48: 
49: 
50: 
51: 
52: 
53: 
54: 
55: 
56: 
57: 
58: 
59: 
60: 
61: 
62: 
63: 
64: 
65: 
66: 
67: 
68: 
69: 
70: 
71: 
72: 
73: 
74: 
75: 
76: 
77: 
78: 
79: 
80: 
81: 
82: 
83: 
84: 
open System

///
[<AllowNullLiteral>]
type CString =
  
  ///
  val mutable Builder : Text.StringBuilder

  ///
  val mutable Length : int

  ///
  [<DefaultValue>] 
  val mutable Cached : string

  ///
  new(builder:Text.StringBuilder) = {
    Builder = builder
    Length = builder.Length
  }

  ///
  new() = {
    Builder = new Text.StringBuilder()
    Length = 0
  }

  ///
  new(initial:string) = {
    Builder = new Text.StringBuilder(initial)
    Length = initial.Length
  }

  ///
  new(left:string, right:string) = {
    Builder = (new Text.StringBuilder(left, left.Length + right.Length)).Append(right)
    Length = left.Length + right.Length
  }

  ///
  static member OfArray(values:string array) =
    let mutable length = 0

    for str in values do
      let slength = if Object.ReferenceEquals(str, null) then 0 else str.Length
      length <- length + slength
      
    let cs = CString()
    cs.Builder.EnsureCapacity(length) |> ignore

    for str in values do
      if not <| Object.ReferenceEquals(str, null) then
        cs.Builder.Append(str) |> ignore

    cs

  ///
  member x.Concat(s:string) =
    if x.Length = x.Builder.Length then
      x.Builder.Append(s) |> ignore
      CString(x.Builder)

    else
      let builder = new Text.StringBuilder(x.Builder.ToString(0, x.Length), x.Length + s.Length)
      builder.Append(s) |> ignore
      CString(builder)

  ///
  member x.Concat(cs:CString) =
    x.Concat(cs.ToString())

  ///
  member x.Concat(o:obj) =
    if o :? string then x.Concat(o :?> string)
    elif o :? CString then x.Concat(o :?> CString)
    else x.Concat(string o)

  ///
  override x.ToString() =
    if Object.ReferenceEquals(x.Cached, null) then
      x.Cached <- x.Builder.ToString(0, x.Length)

    x.Cached
namespace System
Multiple items
type AllowNullLiteralAttribute =
  inherit Attribute
  new : unit -> AllowNullLiteralAttribute

Full name: Microsoft.FSharp.Core.AllowNullLiteralAttribute

--------------------
new : unit -> AllowNullLiteralAttribute
Multiple items
type CString =
  new : unit -> CString
  new : builder:StringBuilder -> CString
  new : initial:string -> CString
  new : left:string * right:string -> CString
  val mutable Builder: StringBuilder
  val mutable Length: int
  val mutable Cached: string
  member Concat : s:string -> CString
  member Concat : cs:CString -> CString
  member Concat : o:obj -> CString
  ...

Full name: Script.CString




--------------------
new : unit -> CString



new : initial:string -> CString



new : builder:Text.StringBuilder -> CString



new : left:string * right:string -> CString


CString.Builder: Text.StringBuilder


namespace System.Text
Multiple items
type StringBuilder =
  new : unit -> StringBuilder + 5 overloads
  member Append : value:string -> StringBuilder + 18 overloads
  member AppendFormat : format:string * arg0:obj -> StringBuilder + 4 overloads
  member AppendLine : unit -> StringBuilder + 1 overload
  member Capacity : int with get, set
  member Chars : int -> char with get, set
  member Clear : unit -> StringBuilder
  member CopyTo : sourceIndex:int * destination:char[] * destinationIndex:int * count:int -> unit
  member EnsureCapacity : capacity:int -> int
  member Equals : sb:StringBuilder -> bool
  ...

Full name: System.Text.StringBuilder

--------------------
Text.StringBuilder() : unit
Text.StringBuilder(capacity: int) : unit
Text.StringBuilder(value: string) : unit
Text.StringBuilder(value: string, capacity: int) : unit
Text.StringBuilder(capacity: int, maxCapacity: int) : unit
Text.StringBuilder(value: string, startIndex: int, length: int, capacity: int) : unit
CString.Length: int


Multiple items
val int : value:'T -> int (requires member op_Explicit)

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

--------------------
type int = int32

Full name: Microsoft.FSharp.Core.int

--------------------
type int<'Measure> = int

Full name: Microsoft.FSharp.Core.int<_>
Multiple items
type DefaultValueAttribute =
  inherit Attribute
  new : unit -> DefaultValueAttribute
  new : check:bool -> DefaultValueAttribute
  member Check : bool

Full name: Microsoft.FSharp.Core.DefaultValueAttribute

--------------------
new : unit -> DefaultValueAttribute
new : check:bool -> DefaultValueAttribute
CString.Cached: string


Multiple items
val string : value:'T -> string

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

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

Full name: Microsoft.FSharp.Core.string
val builder : Text.StringBuilder
property Text.StringBuilder.Length: int
val initial : string
property String.Length: int
val left : string
val right : string
static member CString.OfArray : values:string array -> CString

Full name: Script.CString.OfArray


val values : string array
type 'T array = 'T []

Full name: Microsoft.FSharp.Core.array<_>
val mutable length : int
val str : string
val slength : int
Multiple items
type Object =
  new : unit -> obj
  member Equals : obj:obj -> bool
  member GetHashCode : unit -> int
  member GetType : unit -> Type
  member ToString : unit -> string
  static member Equals : objA:obj * objB:obj -> bool
  static member ReferenceEquals : objA:obj * objB:obj -> bool

Full name: System.Object

--------------------
Object() : unit
Object.ReferenceEquals(objA: obj, objB: obj) : bool
val cs : CString
Text.StringBuilder.EnsureCapacity(capacity: int) : int
val ignore : value:'T -> unit

Full name: Microsoft.FSharp.Core.Operators.ignore
val not : value:bool -> bool

Full name: Microsoft.FSharp.Core.Operators.not
Text.StringBuilder.Append(value: char []) : Text.StringBuilder
   (+0 other overloads)
Text.StringBuilder.Append(value: obj) : Text.StringBuilder
   (+0 other overloads)
Text.StringBuilder.Append(value: uint64) : Text.StringBuilder
   (+0 other overloads)
Text.StringBuilder.Append(value: uint32) : Text.StringBuilder
   (+0 other overloads)
Text.StringBuilder.Append(value: uint16) : Text.StringBuilder
   (+0 other overloads)
Text.StringBuilder.Append(value: decimal) : Text.StringBuilder
   (+0 other overloads)
Text.StringBuilder.Append(value: float) : Text.StringBuilder
   (+0 other overloads)
Text.StringBuilder.Append(value: float32) : Text.StringBuilder
   (+0 other overloads)
Text.StringBuilder.Append(value: int64) : Text.StringBuilder
   (+0 other overloads)
Text.StringBuilder.Append(value: int) : Text.StringBuilder
   (+0 other overloads)
val x : CString
member CString.Concat : s:string -> CString

Full name: Script.CString.Concat


val s : string
Text.StringBuilder.ToString() : string
Text.StringBuilder.ToString(startIndex: int, length: int) : string
member CString.Concat : cs:CString -> CString

Full name: Script.CString.Concat


member CString.Concat : s:string -> CString



member CString.Concat : cs:CString -> CString



member CString.Concat : o:obj -> CString


override CString.ToString : unit -> string


member CString.Concat : o:obj -> CString

Full name: Script.CString.Concat


val o : obj
type obj = Object

Full name: Microsoft.FSharp.Core.obj
override CString.ToString : unit -> string

Full name: Script.CString.ToString


Raw view New version

More information

Link:http://fssnip.net/4h
Posted:5 years ago
Author:fholm
Tags: string , concat