3 people like it.

ASCII String

Type extensions for ASCII strings represented as byte arrays. Note: requires the F# 3.1 compiler.

  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: 
 85: 
 86: 
 87: 
 88: 
 89: 
 90: 
 91: 
 92: 
 93: 
 94: 
 95: 
 96: 
 97: 
 98: 
 99: 
100: 
101: 
102: 
103: 
104: 
105: 
106: 
module Array =
   let tryFindLastIndex predicate (array:'T array) =
      let mutable index = array.Length - 1
      while index >= 0 && not(predicate array .[index]) do index <- index - 1 
      if index >= 0 then Some index else None

module ASCII =
   module Char =
      let ToLower (c:byte) =
         if c >= 'A'B && c <= 'Z'B then c - 'A'B + 'a'B
         else c
      let ToUpper (c:byte) =
         if c >= 'a'B && c <= 'z'B then c - 'a'B + 'A'B
         else c
      let IsWhiteSpace (c:byte) =
         c = ' 'B || c = '\t'B || c = '\r'B || c = '\n'B

   module String =
      let Empty : byte[] = [||]   
      let IsEmpty (str:byte[]) = str.Length = 0
      let IsNullOrWhiteSpace str = 
         str = null || Array.forall Char.IsWhiteSpace str
      let Compare (strA:byte[],indexA,strB:byte[],indexB,length) =
         let rec compare n =
            if n = length then 0
            else 
               let d = strA.[indexA+n] - strB.[indexB+n] 
               if d = 0uy then compare (n+1)
               else int d
         compare 0

open System.Runtime.CompilerServices

type System.Convert with
   static member ToString(value:byte[]) =
      System.Text.ASCIIEncoding.ASCII.GetString(value)

[<Extension>]
type AsciiStringExtensions =
   [<Extension>] 
   static member ToLower(str) = 
      Array.map ASCII.Char.ToLower str
   [<Extension>]
   static member ToUpper(str) = 
      Array.map ASCII.Char.ToUpper str
   [<Extension>]
   static member Substring(str:byte[],index) = 
      str.[index..]
   [<Extension>]
   static member Substring(str:byte[],index,length) = 
      str.[index..index+length-1]
   [<Extension>]
   static member Remove(str:byte[],startIndex) =
      str.[0..startIndex-1]
   [<Extension>]
   static member Remove(str:byte[],startIndex:int,length:int) =
      let array = System.Array.CreateInstance(typeof<byte>, str.Length-length) :?> byte[]
      System.Array.Copy(str, array, startIndex)
      System.Array.Copy(str, startIndex+length, array, startIndex, str.Length-startIndex-length)
      array
   [<Extension>]
   static member IndexOf(str:byte[], value:byte) = 
      System.Array.IndexOf(str, value)
   [<Extension>]
   static member IndexOfAny(str, anyOf:byte[]) =
      let exists c = Array.exists ((=) c) anyOf
      match Array.tryFindIndex exists str with
      | Some index -> index
      | None -> -1
   [<Extension>]
   static member LastIndexOf(str, value:byte) = 
      System.Array.LastIndexOf(str, value)
   [<Extension>]
   static member LastIndexOfAny(str, anyOf:byte[]) =
      let exists c = Array.exists ((=) c) anyOf
      match Array.tryFindLastIndex exists str with
      | Some index -> index
      | None -> -1 
   [<Extension>]
   static member StartsWith(str:byte[],value:byte[]) =
      value.Length <= str.Length &&
      ASCII.String.Compare(str,0,value,0,value.Length) = 0
   [<Extension>]
   static member EndsWith(str:byte[],value:byte[]) =
      value.Length <= str.Length &&
      ASCII.String.Compare(str, str.Length-value.Length, value, 0, value.Length) = 0
   [<Extension>]
   static member Contains(str:byte[],value:byte[]) =
      let rec contains index =
         if index < str.Length - value.Length then compare index
         else false
      and compare index =
         if ASCII.String.Compare(str,index,value,0,value.Length) <> 0
         then contains (index+1)
         else true
      contains 0
   [<Extension>]
   static member Trim(str:byte[]) =
      let mutable i = 0
      while i < str.Length && ASCII.Char.IsWhiteSpace str.[i] do i <- i + 1
      let mutable j = str.Length - 1
      while j > i && ASCII.Char.IsWhiteSpace str.[j] do j <- j - 1
      if i=0 && j=str.Length-1 then str 
      else str.[i..j]

"Hello"B.StartsWith("Hell"B)
module Array

from Microsoft.FSharp.Collections
val tryFindLastIndex : predicate:('T -> bool) -> array:'T array -> int option

Full name: Script.Array.tryFindLastIndex
val predicate : ('T -> bool)
Multiple items
val array : 'T array

--------------------
type 'T array = 'T []

Full name: Microsoft.FSharp.Core.array<_>
val mutable index : int
property System.Array.Length: int
val not : value:bool -> bool

Full name: Microsoft.FSharp.Core.Operators.not
union case Option.Some: Value: 'T -> Option<'T>
union case Option.None: Option<'T>
val ToLower : c:byte -> byte

Full name: Script.ASCII.Char.ToLower
val c : byte
Multiple items
val byte : value:'T -> byte (requires member op_Explicit)

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

--------------------
type byte = System.Byte

Full name: Microsoft.FSharp.Core.byte
val ToUpper : c:byte -> byte

Full name: Script.ASCII.Char.ToUpper
val IsWhiteSpace : c:byte -> bool

Full name: Script.ASCII.Char.IsWhiteSpace
Multiple items
module String

from Script.ASCII

--------------------
module String

from Microsoft.FSharp.Core
val Empty : byte []

Full name: Script.ASCII.String.Empty
val IsEmpty : str:byte [] -> bool

Full name: Script.ASCII.String.IsEmpty
val str : byte []
val IsNullOrWhiteSpace : str:byte [] -> bool

Full name: Script.ASCII.String.IsNullOrWhiteSpace
Multiple items
module Array

from Script

--------------------
module Array

from Microsoft.FSharp.Collections
val forall : predicate:('T -> bool) -> array:'T [] -> bool

Full name: Microsoft.FSharp.Collections.Array.forall
module Char

from Script.ASCII
val Compare : strA:byte [] * indexA:int * strB:byte [] * indexB:int * length:int -> int

Full name: Script.ASCII.String.Compare
val strA : byte []
val indexA : int
val strB : byte []
val indexB : int
val length : int
val compare : (int -> int)
val n : int
val d : byte
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<_>
namespace System
namespace System.Runtime
namespace System.Runtime.CompilerServices
type Convert =
  static val DBNull : obj
  static member ChangeType : value:obj * typeCode:TypeCode -> obj + 3 overloads
  static member FromBase64CharArray : inArray:char[] * offset:int * length:int -> byte[]
  static member FromBase64String : s:string -> byte[]
  static member GetTypeCode : value:obj -> TypeCode
  static member IsDBNull : value:obj -> bool
  static member ToBase64CharArray : inArray:byte[] * offsetIn:int * length:int * outArray:char[] * offsetOut:int -> int + 1 overload
  static member ToBase64String : inArray:byte[] -> string + 3 overloads
  static member ToBoolean : value:obj -> bool + 17 overloads
  static member ToByte : value:obj -> byte + 18 overloads
  ...

Full name: System.Convert
static member System.Convert.ToString : value:byte [] -> string

Full name: Script.ToString
val value : byte []
namespace System.Text
Multiple items
type ASCIIEncoding =
  inherit Encoding
  new : unit -> ASCIIEncoding
  member GetByteCount : chars:string -> int + 2 overloads
  member GetBytes : chars:char * charCount:int * bytes:byte * byteCount:int -> int + 2 overloads
  member GetCharCount : bytes:byte * count:int -> int + 1 overload
  member GetChars : bytes:byte * byteCount:int * chars:char * charCount:int -> int + 1 overload
  member GetDecoder : unit -> Decoder
  member GetEncoder : unit -> Encoder
  member GetMaxByteCount : charCount:int -> int
  member GetMaxCharCount : byteCount:int -> int
  member GetString : bytes:byte[] * byteIndex:int * byteCount:int -> string
  ...

Full name: System.Text.ASCIIEncoding

--------------------
System.Text.ASCIIEncoding() : unit
property System.Text.Encoding.ASCII: System.Text.Encoding
System.Text.Encoding.GetString(bytes: byte []) : string
System.Text.Encoding.GetString(bytes: byte [], index: int, count: int) : string
Multiple items
type ExtensionAttribute =
  inherit Attribute
  new : unit -> ExtensionAttribute

Full name: System.Runtime.CompilerServices.ExtensionAttribute

--------------------
ExtensionAttribute() : unit
type AsciiStringExtensions =
  static member Contains : str:byte [] * value:byte [] -> bool
  static member EndsWith : str:byte [] * value:byte [] -> bool
  static member IndexOf : str:byte [] * value:byte -> int
  static member IndexOfAny : str:byte [] * anyOf:byte [] -> int
  static member LastIndexOf : str:byte [] * value:byte -> int
  static member LastIndexOfAny : str:byte array * anyOf:byte [] -> int
  static member Remove : str:byte [] * startIndex:int -> byte []
  static member Remove : str:byte [] * startIndex:int * length:int -> byte []
  static member StartsWith : str:byte [] * value:byte [] -> bool
  static member Substring : str:byte [] * index:int -> byte []
  ...

Full name: Script.AsciiStringExtensions
static member AsciiStringExtensions.ToLower : str:byte [] -> byte []

Full name: Script.AsciiStringExtensions.ToLower
val map : mapping:('T -> 'U) -> array:'T [] -> 'U []

Full name: Microsoft.FSharp.Collections.Array.map
module ASCII

from Script
static member AsciiStringExtensions.ToUpper : str:byte [] -> byte []

Full name: Script.AsciiStringExtensions.ToUpper
static member AsciiStringExtensions.Substring : str:byte [] * index:int -> byte []

Full name: Script.AsciiStringExtensions.Substring
val index : int
static member AsciiStringExtensions.Substring : str:byte [] * index:int * length:int -> byte []

Full name: Script.AsciiStringExtensions.Substring
static member AsciiStringExtensions.Remove : str:byte [] * startIndex:int -> byte []

Full name: Script.AsciiStringExtensions.Remove
val startIndex : int
static member AsciiStringExtensions.Remove : str:byte [] * startIndex:int * length:int -> byte []

Full name: Script.AsciiStringExtensions.Remove
Multiple items
val array : byte []

--------------------
type 'T array = 'T []

Full name: Microsoft.FSharp.Core.array<_>
type Array =
  member Clone : unit -> obj
  member CopyTo : array:Array * index:int -> unit + 1 overload
  member GetEnumerator : unit -> IEnumerator
  member GetLength : dimension:int -> int
  member GetLongLength : dimension:int -> int64
  member GetLowerBound : dimension:int -> int
  member GetUpperBound : dimension:int -> int
  member GetValue : [<ParamArray>] indices:int[] -> obj + 7 overloads
  member Initialize : unit -> unit
  member IsFixedSize : bool
  ...

Full name: System.Array
System.Array.CreateInstance(elementType: System.Type, [<System.ParamArray>] lengths: int64 []) : System.Array
System.Array.CreateInstance(elementType: System.Type, [<System.ParamArray>] lengths: int []) : System.Array
System.Array.CreateInstance(elementType: System.Type, length: int) : System.Array
System.Array.CreateInstance(elementType: System.Type, lengths: int [], lowerBounds: int []) : System.Array
System.Array.CreateInstance(elementType: System.Type, length1: int, length2: int) : System.Array
System.Array.CreateInstance(elementType: System.Type, length1: int, length2: int, length3: int) : System.Array
val typeof<'T> : System.Type

Full name: Microsoft.FSharp.Core.Operators.typeof
System.Array.Copy(sourceArray: System.Array, destinationArray: System.Array, length: int64) : unit
System.Array.Copy(sourceArray: System.Array, destinationArray: System.Array, length: int) : unit
System.Array.Copy(sourceArray: System.Array, sourceIndex: int64, destinationArray: System.Array, destinationIndex: int64, length: int64) : unit
System.Array.Copy(sourceArray: System.Array, sourceIndex: int, destinationArray: System.Array, destinationIndex: int, length: int) : unit
static member AsciiStringExtensions.IndexOf : str:byte [] * value:byte -> int

Full name: Script.AsciiStringExtensions.IndexOf
val value : byte
System.Array.IndexOf<'T>(array: 'T [], value: 'T) : int
System.Array.IndexOf(array: System.Array, value: obj) : int
System.Array.IndexOf<'T>(array: 'T [], value: 'T, startIndex: int) : int
System.Array.IndexOf(array: System.Array, value: obj, startIndex: int) : int
System.Array.IndexOf<'T>(array: 'T [], value: 'T, startIndex: int, count: int) : int
System.Array.IndexOf(array: System.Array, value: obj, startIndex: int, count: int) : int
static member AsciiStringExtensions.IndexOfAny : str:byte [] * anyOf:byte [] -> int

Full name: Script.AsciiStringExtensions.IndexOfAny
val anyOf : byte []
val exists : (byte -> bool)
val exists : predicate:('T -> bool) -> array:'T [] -> bool

Full name: Microsoft.FSharp.Collections.Array.exists
val tryFindIndex : predicate:('T -> bool) -> array:'T [] -> int option

Full name: Microsoft.FSharp.Collections.Array.tryFindIndex
static member AsciiStringExtensions.LastIndexOf : str:byte [] * value:byte -> int

Full name: Script.AsciiStringExtensions.LastIndexOf
System.Array.LastIndexOf<'T>(array: 'T [], value: 'T) : int
System.Array.LastIndexOf(array: System.Array, value: obj) : int
System.Array.LastIndexOf<'T>(array: 'T [], value: 'T, startIndex: int) : int
System.Array.LastIndexOf(array: System.Array, value: obj, startIndex: int) : int
System.Array.LastIndexOf<'T>(array: 'T [], value: 'T, startIndex: int, count: int) : int
System.Array.LastIndexOf(array: System.Array, value: obj, startIndex: int, count: int) : int
static member AsciiStringExtensions.LastIndexOfAny : str:byte array * anyOf:byte [] -> int

Full name: Script.AsciiStringExtensions.LastIndexOfAny
val str : byte array
static member AsciiStringExtensions.StartsWith : str:byte [] * value:byte [] -> bool

Full name: Script.AsciiStringExtensions.StartsWith
module String

from Script.ASCII
static member AsciiStringExtensions.EndsWith : str:byte [] * value:byte [] -> bool

Full name: Script.AsciiStringExtensions.EndsWith
static member AsciiStringExtensions.Contains : str:byte [] * value:byte [] -> bool

Full name: Script.AsciiStringExtensions.Contains
val contains : (int -> bool)
val compare : (int -> bool)
static member AsciiStringExtensions.Trim : str:byte [] -> byte []

Full name: Script.AsciiStringExtensions.Trim
val mutable i : int
val mutable j : int
Raw view Test code New version

More information

Link:http://fssnip.net/o8
Posted:9 years ago
Author:Phillip Trelford
Tags: ascii , string