forked from dotnet/fsharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring.fs
More file actions
99 lines (83 loc) · 3.59 KB
/
Copy pathstring.fs
File metadata and controls
99 lines (83 loc) · 3.59 KB
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
// Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
namespace Microsoft.FSharp.Core
open System
open System.Text
open Microsoft.FSharp.Core.LanguagePrimitives.IntrinsicOperators
open Microsoft.FSharp.Core.Operators
open Microsoft.FSharp.Core.Operators.Checked
open Microsoft.FSharp.Collections
[<CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
[<RequireQualifiedAccess>]
module String =
let inline emptyIfNull str =
match str with
| null -> String.Empty
| _ -> str
[<CompiledName("Concat")>]
let concat sep (strings : seq<string>) =
String.Join(sep, Seq.toArray strings)
[<CompiledName("Iterate")>]
let iter (f : (char -> unit)) (str:string) =
let str = emptyIfNull str
for i = 0 to str.Length - 1 do
f str.[i]
[<CompiledName("IterateIndexed")>]
let iteri f (str:string) =
let str = emptyIfNull str
let f = OptimizedClosures.FSharpFunc<_,_,_>.Adapt(f)
for i = 0 to str.Length - 1 do
f.Invoke(i, str.[i])
[<CompiledName("Map")>]
let map (f: char -> char) (str:string) =
let str = emptyIfNull str
let res = StringBuilder str.Length
str |> iter (fun c -> res.Append(f c) |> ignore)
res.ToString()
[<CompiledName("MapIndexed")>]
let mapi (f: int -> char -> char) (str:string) =
let str = emptyIfNull str
let res = StringBuilder str.Length
let f = OptimizedClosures.FSharpFunc<_,_,_>.Adapt(f)
str |> iteri (fun i c -> res.Append(f.Invoke(i, c)) |> ignore)
res.ToString()
[<CompiledName("Filter")>]
let filter (f: char -> bool) (str:string) =
let str = emptyIfNull str
let res = StringBuilder str.Length
str |> iter (fun c -> if f c then res.Append c |> ignore)
res.ToString()
[<CompiledName("Collect")>]
let collect (f: char -> string) (str:string) =
let str = emptyIfNull str
let res = StringBuilder str.Length
str |> iter (fun c -> res.Append(f c) |> ignore)
res.ToString()
[<CompiledName("Initialize")>]
let init (count:int) (initializer: int-> string) =
if count < 0 then invalidArgInputMustBeNonNegative "count" count
let res = StringBuilder count
for i = 0 to count - 1 do
res.Append(initializer i) |> ignore
res.ToString()
[<CompiledName("Replicate")>]
let replicate (count:int) (str:string) =
if count < 0 then invalidArgInputMustBeNonNegative "count" count
let str = emptyIfNull str
let res = StringBuilder str.Length
for i = 0 to count - 1 do
res.Append str |> ignore
res.ToString()
[<CompiledName("ForAll")>]
let forall f (str:string) =
let str = emptyIfNull str
let rec check i = (i >= str.Length) || (f str.[i] && check (i+1))
check 0
[<CompiledName("Exists")>]
let exists f (str:string) =
let str = emptyIfNull str
let rec check i = (i < str.Length) && (f str.[i] || check (i+1))
check 0
[<CompiledName("Length")>]
let length (str:string) =
let str = emptyIfNull str
str.Length