ptrelford

ASCII String

Oct 5th, 2014
474
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 3.76 KB | None | 0 0
  1. module Array =
  2.    let tryFindLastIndex predicate (array:'T array) =
  3.      let mutable index = array.Length - 1
  4.      while index >= 0 && not(predicate array .[index]) do index <- index - 1
  5.      if index >= 0 then Some index else None
  6.  
  7. module ASCII =
  8.   module Char =
  9.      let ToLower (c:byte) =
  10.         if c >= 'A'B && c <= 'Z'B then c - 'A'B + 'a'B
  11.         else c
  12.      let ToUpper (c:byte) =
  13.         if c >= 'a'B && c <= 'z'B then c - 'a'B + 'A'B
  14.         else c
  15.      let IsWhiteSpace (c:byte) =
  16.         c = ' 'B || c = '\t'B || c = '\r'B || c = '\n'B
  17.  
  18.   module String =
  19.      let Empty : byte[] = [||]  
  20.      let IsEmpty (str:byte[]) = str.Length = 0
  21.      let IsNullOrWhiteSpace str =
  22.         str = null || Array.forall Char.IsWhiteSpace str
  23.      let Compare (strA:byte[],indexA,strB:byte[],indexB,length) =
  24.         let rec compare n =
  25.            if n = length then 0
  26.            else
  27.               let d = strA.[indexA+n] - strB.[indexB+n]
  28.               if d = 0uy then compare (n+1)
  29.               else int d
  30.         compare 0
  31.  
  32. open System.Runtime.CompilerServices
  33.  
  34. type System.Convert with
  35.   static member ToString(value:byte[]) =
  36.      System.Text.ASCIIEncoding.ASCII.GetString(value)
  37.  
  38. [<Extension>]
  39. type AsciiStringExtensions =
  40.   [<Extension>]
  41.   static member ToLower(str) =
  42.      Array.map ASCII.Char.ToLower str
  43.   [<Extension>]
  44.   static member ToUpper(str) =
  45.      Array.map ASCII.Char.ToUpper str
  46.   [<Extension>]
  47.   static member Substring(str:byte[],index) =
  48.      str.[index..]
  49.   [<Extension>]
  50.   static member Substring(str:byte[],index,length) =
  51.      str.[index..index+length-1]
  52.   [<Extension>]
  53.   static member Remove(str:byte[],startIndex) =
  54.      str.[0..startIndex-1]
  55.   [<Extension>]
  56.   static member Remove(str:byte[],startIndex:int,length:int) =
  57.      let array = System.Array.CreateInstance(typeof<byte>, str.Length-length) :?> byte[]
  58.      System.Array.Copy(str, array, startIndex)
  59.      System.Array.Copy(str, startIndex+length, array, startIndex, str.Length-startIndex-length)
  60.      array
  61.   [<Extension>]
  62.   static member IndexOf(str:byte[], value:byte) =
  63.      System.Array.IndexOf(str, value)
  64.   [<Extension>]
  65.   static member IndexOfAny(str, anyOf:byte[]) =
  66.      let exists c = Array.exists ((=) c) anyOf
  67.      match Array.tryFindIndex exists str with
  68.      | Some index -> index
  69.      | None -> -1
  70.   [<Extension>]
  71.   static member LastIndexOf(str, value:byte) =
  72.      System.Array.LastIndexOf(str, value)
  73.   [<Extension>]
  74.   static member LastIndexOfAny(str, anyOf:byte[]) =
  75.      let exists c = Array.exists ((=) c) anyOf
  76.      match Array.tryFindLastIndex exists str with
  77.      | Some index -> index
  78.      | None -> -1
  79.   [<Extension>]
  80.   static member StartsWith(str:byte[],value:byte[]) =
  81.      value.Length <= str.Length &&
  82.      ASCII.String.Compare(str,0,value,0,value.Length) = 0
  83.   [<Extension>]
  84.   static member EndsWith(str:byte[],value:byte[]) =
  85.      value.Length <= str.Length &&
  86.      ASCII.String.Compare(str, str.Length-value.Length, value, 0, value.Length) = 0
  87.   [<Extension>]
  88.   static member Contains(str:byte[],value:byte[]) =
  89.      let rec contains index =
  90.         if index < str.Length - value.Length then compare index
  91.         else false
  92.      and compare index =
  93.         if ASCII.String.Compare(str,index,value,0,value.Length) <> 0
  94.         then contains (index+1)
  95.         else true
  96.      contains 0
  97.   [<Extension>]
  98.   static member Trim(str:byte[]) =
  99.      let mutable i = 0
  100.      while i < str.Length && ASCII.Char.IsWhiteSpace str.[i] do i <- i + 1
  101.      let mutable j = str.Length - 1
  102.      while j > i && ASCII.Char.IsWhiteSpace str.[j] do j <- j - 1
  103.      if i=0 && j=str.Length-1 then str
  104.      else str.[i..j]
  105.  
  106. "Hello"B.StartsWith("He"B)
Advertisement
Add Comment
Please, Sign In to add comment