4 people like it.

Efficient Immutable String Concat

Efficient Immutable String Concat

 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: 
namespace IronJS

open System
open System.Text

///
type [<AllowNullLiteral>] SuffixString() =
  
  [<DefaultValue>] val mutable Builder : StringBuilder
  [<DefaultValue>] val mutable Length : int
  [<DefaultValue>] val mutable Cached : string

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

    x.Cached

  ///
  static member Concat(suffix:SuffixString, o:obj) =
    let s = o.ToString()

    let builder = 
      if suffix.Length = suffix.Builder.Length then 
        suffix.Builder.Append(s)

      else 
        let oldValue = suffix.Builder.ToString(0, suffix.Length)
        let newLength = suffix.Length + s.Length
        (new StringBuilder(oldValue, newLength)).Append(s)

    let suffix = SuffixString()
    suffix.Builder <- builder
    suffix.Length <- builder.Length
    suffix

  ///
  static member Concat(left:obj, right:obj) =
    let left = left.ToString()
    let right = right.ToString()
    let suffix = SuffixString()
    suffix.Builder <- new StringBuilder(left, left.Length + right.Length)
    suffix.Builder.Append(right) |> ignore
    suffix.Length <- suffix.Builder.Length
    suffix

  ///
  static member OfArray(values:string array) =
    let cs = SuffixString()

    for str in values do
      let slength = if Object.ReferenceEquals(str, null) then 0 else str.Length
      cs.Length <- cs.Length + slength
      
    cs.Builder <- new StringBuilder(cs.Length)

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

    cs
namespace System
namespace System.Text
Multiple items
type AllowNullLiteralAttribute =
  inherit Attribute
  new : unit -> AllowNullLiteralAttribute

Full name: Microsoft.FSharp.Core.AllowNullLiteralAttribute

--------------------
new : unit -> AllowNullLiteralAttribute
Multiple items
type SuffixString =
  new : unit -> SuffixString
  val mutable Builder: StringBuilder
  val mutable Length: int
  val mutable Cached: string
  override ToString : unit -> string
  static member Concat : suffix:SuffixString * o:obj -> SuffixString
  static member Concat : left:obj * right:obj -> SuffixString
  static member OfArray : values:string array -> SuffixString

Full name: IronJS.SuffixString




--------------------
new : unit -> SuffixString
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
SuffixString.Builder: StringBuilder
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

--------------------
StringBuilder() : unit
StringBuilder(capacity: int) : unit
StringBuilder(value: string) : unit
StringBuilder(value: string, capacity: int) : unit
StringBuilder(capacity: int, maxCapacity: int) : unit
StringBuilder(value: string, startIndex: int, length: int, capacity: int) : unit
SuffixString.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<_>
SuffixString.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 x : SuffixString
override SuffixString.ToString : unit -> string

Full name: IronJS.SuffixString.ToString


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
StringBuilder.ToString() : string
StringBuilder.ToString(startIndex: int, length: int) : string
static member SuffixString.Concat : suffix:SuffixString * o:obj -> SuffixString

Full name: IronJS.SuffixString.Concat


val suffix : SuffixString
val o : obj
type obj = Object

Full name: Microsoft.FSharp.Core.obj
val s : string
Object.ToString() : string
val builder : StringBuilder
property StringBuilder.Length: int
StringBuilder.Append(value: char []) : StringBuilder
   (+0 other overloads)
StringBuilder.Append(value: obj) : StringBuilder
   (+0 other overloads)
StringBuilder.Append(value: uint64) : StringBuilder
   (+0 other overloads)
StringBuilder.Append(value: uint32) : StringBuilder
   (+0 other overloads)
StringBuilder.Append(value: uint16) : StringBuilder
   (+0 other overloads)
StringBuilder.Append(value: decimal) : StringBuilder
   (+0 other overloads)
StringBuilder.Append(value: float) : StringBuilder
   (+0 other overloads)
StringBuilder.Append(value: float32) : StringBuilder
   (+0 other overloads)
StringBuilder.Append(value: int64) : StringBuilder
   (+0 other overloads)
StringBuilder.Append(value: int) : StringBuilder
   (+0 other overloads)
val oldValue : string
val newLength : int
property String.Length: int
static member SuffixString.Concat : left:obj * right:obj -> SuffixString

Full name: IronJS.SuffixString.Concat


val left : obj
val right : obj
val left : string
String.ToString() : string
String.ToString(provider: IFormatProvider) : string
val right : string
val ignore : value:'T -> unit

Full name: Microsoft.FSharp.Core.Operators.ignore
static member SuffixString.OfArray : values:string array -> SuffixString

Full name: IronJS.SuffixString.OfArray


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

Full name: Microsoft.FSharp.Core.array<_>
val cs : SuffixString
val str : string
val slength : int
val not : value:bool -> bool

Full name: Microsoft.FSharp.Core.Operators.not
Raw view Test code New version

More information

Link:http://fssnip.net/4i
Posted:13 years ago
Author:fholm
Tags: string , concat , fast