|
| 1 | +namespace Internal.Utilities |
| 2 | + |
| 3 | +#nowarn "1182" |
| 4 | + |
| 5 | +//------------------------------------------------------------------------ |
| 6 | +// shims for things not yet implemented in Fable |
| 7 | +//------------------------------------------------------------------------ |
| 8 | + |
| 9 | +module System = |
| 10 | + |
| 11 | + module Decimal = |
| 12 | + let GetBits(d: decimal): int[] = [| 0; 0; 0; 0 |] //TODO: proper implementation |
| 13 | + |
| 14 | + module Diagnostics = |
| 15 | + type Trace() = |
| 16 | + static member TraceInformation(s) = () //TODO: proper implementation |
| 17 | + |
| 18 | + module Reflection = |
| 19 | + type AssemblyName(assemblyName: string) = |
| 20 | + member x.Name = assemblyName //TODO: proper implementation |
| 21 | + |
| 22 | + type WeakReference<'T>(v: 'T) = |
| 23 | + member x.TryGetTarget () = (true, v) |
| 24 | + |
| 25 | + type StringComparer(comp: System.StringComparison) = |
| 26 | + static member Ordinal = StringComparer(System.StringComparison.Ordinal) |
| 27 | + static member OrdinalIgnoreCase = StringComparer(System.StringComparison.OrdinalIgnoreCase) |
| 28 | + interface System.Collections.Generic.IEqualityComparer<string> with |
| 29 | + member x.Equals(a,b) = System.String.Compare(a, b, comp) = 0 |
| 30 | + member x.GetHashCode(a) = |
| 31 | + match comp with |
| 32 | + | System.StringComparison.Ordinal -> hash a |
| 33 | + | System.StringComparison.OrdinalIgnoreCase -> hash (a.ToLowerInvariant()) |
| 34 | + | _ -> failwithf "Unsupported StringComparison: %A" comp |
| 35 | + interface System.Collections.Generic.IComparer<string> with |
| 36 | + member x.Compare(a,b) = System.String.Compare(a, b, comp) |
| 37 | + |
| 38 | + module Collections = |
| 39 | + module Concurrent = |
| 40 | + open System.Collections.Generic |
| 41 | + |
| 42 | + type ConcurrentDictionary<'TKey, 'TValue when 'TKey: equality>(comparer: IEqualityComparer<'TKey>) = |
| 43 | + inherit Dictionary<'TKey, 'TValue>(comparer) |
| 44 | + new () = ConcurrentDictionary { |
| 45 | + new IEqualityComparer<'TKey> with |
| 46 | + member __.GetHashCode(x) = x.GetHashCode() |
| 47 | + member __.Equals(x, y) = x.Equals(y) } |
| 48 | + member x.TryAdd (key:'TKey, value:'TValue) = x.[key] <- value; true |
| 49 | + member x.GetOrAdd (key:'TKey, valueFactory: 'TKey -> 'TValue): 'TValue = |
| 50 | + match x.TryGetValue key with |
| 51 | + | true, v -> v |
| 52 | + | false, _ -> let v = valueFactory(key) in x.[key] <- v; v |
| 53 | + |
| 54 | + module IO = |
| 55 | + module Directory = |
| 56 | + let GetCurrentDirectory () = "." //TODO: proper xplat implementation |
| 57 | + |
| 58 | + module Path = |
| 59 | + |
| 60 | + let Combine (path1: string, path2: string) = //TODO: proper xplat implementation |
| 61 | + let path1 = |
| 62 | + if (String.length path1) = 0 then path1 |
| 63 | + else (path1.TrimEnd [|'\\';'/'|]) + "/" |
| 64 | + path1 + (path2.TrimStart [|'\\';'/'|]) |
| 65 | + |
| 66 | + let ChangeExtension (path: string, ext: string) = |
| 67 | + let i = path.LastIndexOf(".") |
| 68 | + if i < 0 then path |
| 69 | + else path.Substring(0, i) + ext |
| 70 | + |
| 71 | + let HasExtension (path: string) = |
| 72 | + let i = path.LastIndexOf(".") |
| 73 | + i >= 0 |
| 74 | + |
| 75 | + let GetExtension (path: string) = |
| 76 | + let i = path.LastIndexOf(".") |
| 77 | + if i < 0 then "" |
| 78 | + else path.Substring(i) |
| 79 | + |
| 80 | + let GetInvalidPathChars () = //TODO: proper xplat implementation |
| 81 | + Seq.toArray "<>:\"|?*\b\t" |
| 82 | + |
| 83 | + let GetInvalidFileNameChars () = //TODO: proper xplat implementation |
| 84 | + Seq.toArray "<>:\"|\\/?*\b\t" |
| 85 | + |
| 86 | + let GetFullPath (path: string) = //TODO: proper xplat implementation |
| 87 | + path |
| 88 | + |
| 89 | + let GetFileName (path: string) = |
| 90 | + let normPath = path.Replace("\\", "/").TrimEnd('/') |
| 91 | + let i = normPath.LastIndexOf("/") |
| 92 | + normPath.Substring(i + 1) |
| 93 | + |
| 94 | + let GetFileNameWithoutExtension (path: string) = |
| 95 | + let filename = GetFileName path |
| 96 | + let i = filename.LastIndexOf(".") |
| 97 | + if i < 0 then filename |
| 98 | + else filename.Substring(0, i) |
| 99 | + |
| 100 | + let GetDirectoryName (path: string) = //TODO: proper xplat implementation |
| 101 | + let normPath = path.Replace("\\", "/") |
| 102 | + let i = normPath.LastIndexOf("/") |
| 103 | + if i <= 0 then "" |
| 104 | + else normPath.Substring(0, i) |
| 105 | + |
| 106 | + let IsPathRooted (path: string) = //TODO: proper xplat implementation |
| 107 | + let normPath = path.Replace("\\", "/").TrimEnd('/') |
| 108 | + normPath.StartsWith("/") |
| 109 | + |
| 110 | + |
| 111 | +module Microsoft = |
| 112 | + module FSharp = |
| 113 | + |
| 114 | + //------------------------------------------------------------------------ |
| 115 | + // From reshapedreflection.fs |
| 116 | + //------------------------------------------------------------------------ |
| 117 | + module Core = |
| 118 | + module XmlAdapters = |
| 119 | + let s_escapeChars = [| '<'; '>'; '\"'; '\''; '&' |] |
| 120 | + let getEscapeSequence c = |
| 121 | + match c with |
| 122 | + | '<' -> "<" |
| 123 | + | '>' -> ">" |
| 124 | + | '\"' -> """ |
| 125 | + | '\'' -> "'" |
| 126 | + | '&' -> "&" |
| 127 | + | _ as ch -> ch.ToString() |
| 128 | + let escape str = String.collect getEscapeSequence str |
| 129 | + |
| 130 | + //------------------------------------------------------------------------ |
| 131 | + // From sr.fs |
| 132 | + //------------------------------------------------------------------------ |
| 133 | + module Compiler = |
| 134 | + module SR = |
| 135 | + let GetString(name: string) = |
| 136 | + match SR.Resources.resources.TryGetValue(name) with |
| 137 | + | true, value -> value |
| 138 | + | _ -> "Missing FSStrings error message for: " + name |
| 139 | + |
| 140 | + module DiagnosticMessage = |
| 141 | + type ResourceString<'T>(sfmt: string, fmt: string) = |
| 142 | + member x.Format = |
| 143 | + let a = fmt.Split('%') |
| 144 | + |> Array.filter (fun s -> String.length s > 0) |
| 145 | + |> Array.map (fun s -> box("%" + s)) |
| 146 | + let tmp = System.String.Format(sfmt, a) |
| 147 | + let fmt = Printf.StringFormat<'T>(tmp) |
| 148 | + sprintf fmt |
| 149 | + |
| 150 | + let postProcessString (s: string) = |
| 151 | + s.Replace("\\n","\n").Replace("\\t","\t") |
| 152 | + |
| 153 | + let DeclareResourceString (messageID: string, fmt: string) = |
| 154 | + let messageString = SR.GetString(messageID) |> postProcessString |
| 155 | + ResourceString<'T>(messageString, fmt) |
0 commit comments