forked from fsharp/fsharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring.fs
More file actions
executable file
·99 lines (82 loc) · 3.71 KB
/
Copy pathstring.fs
File metadata and controls
executable file
·99 lines (82 loc) · 3.71 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
//----------------------------------------------------------------------------
// Copyright (c) 2002-2012 Microsoft Corporation.
//
// This source code is subject to terms and conditions of the Apache License, Version 2.0. A
// copy of the license can be found in the License.html file at the root of this distribution.
// By using this source code in any fashion, you are agreeing to be bound
// by the terms of the Apache License, Version 2.0.
//
// You must not remove this notice, or any other, from this software.
//----------------------------------------------------------------------------
namespace Microsoft.FSharp.Core
open System.Diagnostics
open Microsoft.FSharp.Core
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 =
if str = null then "" else str
[<CompiledName("Concat")>]
let concat sep (strings : seq<string>) =
System.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
for i = 0 to str.Length - 1 do
f i str.[i]
[<CompiledName("Map")>]
let map (f: char -> char) (str:string) =
let str = emptyIfNull str
let res = new System.Text.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 = new System.Text.StringBuilder(str.Length)
str |> iteri (fun i c -> res.Append(f i c) |> ignore);
res.ToString()
[<CompiledName("Collect")>]
let collect (f: char -> string) (str:string) =
let str = emptyIfNull str
let res = new System.Text.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 invalidArg "count" (SR.GetString(SR.inputMustBeNonNegative))
let res = new System.Text.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 invalidArg "count" (SR.GetString(SR.inputMustBeNonNegative))
let str = emptyIfNull str
let res = new System.Text.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