Skip to content

Commit 464cf28

Browse files
authored
Fix null handling for ResizeArray.fs && Strings.fs (#590)
This should resolve (at least part) of #395
1 parent dc99f17 commit 464cf28

File tree

3 files changed

+217
-46
lines changed

3 files changed

+217
-46
lines changed

.github/workflows/dotnetcore.yml

+1
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ jobs:
7373
name: nupkg
7474
path: ./bin/nupkg/*.nupkg
7575
- name: Push to GitHub Feed
76+
continue-on-error: true
7677
shell: bash
7778
run: |
7879
for f in ./bin/nupkg/*.nupkg

src/FSharpPlus/Extensions/ResizeArray.fs

+58-15
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
module ResizeArray =
66

77
open System
8+
open FSharpPlus.Internals.Errors
89

910
/// <summary>Builds a new ResizeArray whose elements are the results of applying the given function
1011
/// to each of the elements of the ResizeArray.</summary>
@@ -15,11 +16,14 @@ module ResizeArray =
1516
/// <returns>The result ResizeArray.</returns>
1617
///
1718
/// <exception cref="System.ArgumentNullException">Thrown when the input ResizeArray is null.</exception>
18-
let map (mapping: 'T->'U) (source: ResizeArray<'T>) = ResizeArray (Seq.map mapping source)
19+
let map (mapping: 'T->'U) (source: ResizeArray<'T>) =
20+
raiseIfNull (nameof source) source
21+
22+
ResizeArray (Seq.map mapping source)
1923

2024
/// <summary>Applies a ResizeArray of functions to a ResizeArray of values and concatenates them.</summary>
2125
/// <param name="f">The functions.</param>
22-
/// <param name="x">The values.</param>
26+
/// <param name="ra">The values.</param>
2327
/// <returns>A concatenated list of the resulting ResizeArray after applying each function to each value.</returns>
2428
///
2529
/// <example>
@@ -28,32 +32,57 @@ module ResizeArray =
2832
/// val it : int list = [2; 4; 6; 3; 6; 9]
2933
/// </code>
3034
/// </example>
31-
let apply (f: ResizeArray<'T->'U>) (x: ResizeArray<'T>) = ResizeArray (Seq.apply f x)
35+
let apply (f: ResizeArray<'T->'U>) (ra: ResizeArray<'T>) =
36+
raiseIfNull (nameof ra) ra
37+
38+
ResizeArray (Seq.apply f ra)
3239

3340
/// Combines all values from the first ResizeArray with the second, using the supplied mapping function.
34-
let lift2 mapping (x1: ResizeArray<'T>) (x2: ResizeArray<'U>) = ResizeArray (Seq.lift2 mapping x1 x2)
41+
let lift2 mapping (ra1: ResizeArray<'T>) (ra2: ResizeArray<'U>) =
42+
raiseIfNull (nameof ra1) ra1
43+
raiseIfNull (nameof ra2) ra2
44+
45+
ResizeArray (Seq.lift2 mapping ra1 ra2)
3546

3647
/// <summary>Combines values from three ResizeArrays and calls a mapping function on this combination.</summary>
3748
/// <param name="mapping">Mapping function taking three element combination as input.</param>
38-
/// <param name="x1">First ResizeArray.</param>
39-
/// <param name="x2">Second ResizeArray.</param>
40-
/// <param name="x3">Third ResizeArray.</param>
49+
/// <param name="ra1">First ResizeArray.</param>
50+
/// <param name="ra2">Second ResizeArray.</param>
51+
/// <param name="ra3">Third ResizeArray.</param>
4152
///
4253
/// <returns>ResizeArray with values returned from mapping function.</returns>
43-
let lift3 mapping (x1: ResizeArray<'T>) (x2: ResizeArray<'U>) (x3: ResizeArray<'V>) =
44-
ResizeArray (Seq.lift3 mapping x1 x2 x3)
54+
let lift3 mapping (ra1: ResizeArray<'T>) (ra2: ResizeArray<'U>) (ra3: ResizeArray<'V>) =
55+
raiseIfNull (nameof ra1) ra1
56+
raiseIfNull (nameof ra2) ra2
57+
raiseIfNull (nameof ra3) ra3
58+
59+
ResizeArray (Seq.lift3 mapping ra1 ra2 ra3)
4560

4661
/// Concatenates all elements, using the specified separator between each element.
47-
let intercalate (separator: _ []) (source: seq<_ []>) = source |> Seq.intercalate separator |> Seq.toArray
62+
let intercalate (separator: _ []) (source: seq<_ []>) =
63+
raiseIfNull (nameof separator) separator
64+
raiseIfNull (nameof source) source
65+
66+
source |> Seq.intercalate separator |> Seq.toArray
4867

4968
/// Inserts a separator element between each element in the source ResizeArray.
50-
let intersperse element source = source |> Array.toSeq |> Seq.intersperse element |> Seq.toArray : 'T []
69+
let intersperse element source =
70+
raiseIfNull (nameof element) element
71+
raiseIfNull (nameof source) source
72+
73+
source |> Array.toSeq |> Seq.intersperse element |> Seq.toArray : 'T []
5174

5275
/// Creates a sequence of arrays by splitting the source array on any of the given separators.
53-
let split (separators: seq<_ []>) (source: _ []) = source |> Array.toSeq |> Seq.split separators |> Seq.map Seq.toArray
76+
let split (separators: seq<_ []>) (source: _ []) =
77+
raiseIfNull (nameof separators) separators
78+
raiseIfNull (nameof source) source
79+
source |> Array.toSeq |> Seq.split separators |> Seq.map Seq.toArray
5480

5581
/// Replaces a subsequence of the source array with the given replacement array.
56-
let replace (oldValue: _ []) (newValue: _ []) source = source |> Array.toSeq |> Seq.replace oldValue newValue |> Seq.toArray : 'T []
82+
let replace (oldValue: _ []) (newValue: _ []) source =
83+
raiseIfNull (nameof oldValue) oldValue
84+
raiseIfNull (nameof source) source
85+
source |> Array.toSeq |> Seq.replace oldValue newValue |> Seq.toArray : 'T []
5786

5887
#if !FABLE_COMPILER
5988

@@ -67,6 +96,9 @@ module ResizeArray =
6796
/// The index of the slice.
6897
/// </returns>
6998
let findSliceIndex (slice: _ []) (source: _ []) =
99+
raiseIfNull (nameof slice) slice
100+
raiseIfNull (nameof source) source
101+
70102
let index = Internals.FindSliceIndex.arrayImpl slice source
71103
if index = -1 then
72104
ArgumentException("The specified slice was not found in the sequence.") |> raise
@@ -81,6 +113,9 @@ module ResizeArray =
81113
/// The index of the slice or <c>None</c>.
82114
/// </returns>
83115
let tryFindSliceIndex (slice: _ []) (source: _ []) =
116+
raiseIfNull (nameof slice) slice
117+
raiseIfNull (nameof source) source
118+
84119
let index = Internals.FindSliceIndex.arrayImpl slice source
85120
if index = -1 then None else Some index
86121

@@ -120,6 +155,8 @@ module ResizeArray =
120155
/// A tuple with both resulting arrays.
121156
/// </returns>
122157
let partitionMap (mapper: 'T -> Choice<'T1,'T2>) (source: array<'T>) =
158+
raiseIfNull (nameof source) source
159+
123160
let (x, y) = ResizeArray (), ResizeArray ()
124161
Array.iter (mapper >> function Choice1Of2 e -> x.Add e | Choice2Of2 e -> y.Add e) source
125162
x.ToArray (), y.ToArray ()
@@ -128,10 +165,13 @@ module ResizeArray =
128165
/// to each of the elements of the two ResizeArrays pairwise.</summary>
129166
/// <remark>If one array is shorter, excess elements are discarded from the right end of the longer array.</remark>
130167
let map2Shortest f (a1: ResizeArray<_>) (a2: ResizeArray<_>) =
168+
raiseIfNull (nameof a1) a1
169+
raiseIfNull (nameof a2) a2
170+
131171
let len = min a1.Count a2.Count
132172
let ra = ResizeArray(len)
133173
for i in 0..(len-1) do
134-
ra.Add (f a1.[i] a2.[i])
174+
ra.Add (f a1[i] a2[i])
135175
ra
136176

137177
/// <summary>Safely build a new ResizeArray whose elements are the results of applying the given function
@@ -151,10 +191,13 @@ module ResizeArray =
151191
/// <param name="a2">Second input ResizeArray.</param>
152192
/// <returns>ResizeArray with corresponding pairs of input ResizeArrays.</returns>
153193
let zipShortest (a1: ResizeArray<'T1>) (a2: ResizeArray<'T2>) =
194+
raiseIfNull (nameof a1) a1
195+
raiseIfNull (nameof a2) a2
196+
154197
let len = min a1.Count a2.Count
155198
let ra = ResizeArray(len)
156199
for i in 0..(len-1) do
157-
ra.Add (a1.[i], a2.[i])
200+
ra.Add (a1[i], a2[i])
158201
ra
159202

160203
/// <summary>

0 commit comments

Comments
 (0)