Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 7848223

Browse files
stephentoubsafern
authored andcommitted
Fix TryParse overloads using optional arguments (dotnet/coreclr#14877)
When we originally added Parse and TryParse overloads for `ReadOnlySpan<char>`, we used optional arguments to minimize the number of new overloads needed. This worked decently well for Parse, but for TryParse it necessitated reordering the parameters from the string counterparts, so that the out result argument would come before the optional parameters. This makes it more difficult to port string-based calls to span-based calls, and we agreed to add the missing overloads so that we could order the parameters "correctly" to match the existing string-based overloads. This does so. Signed-off-by: dotnet-bot-corefx-mirror <dotnet-bot@microsoft.com>
1 parent 57858bc commit 7848223

File tree

13 files changed

+83
-17
lines changed

13 files changed

+83
-17
lines changed

src/Common/src/CoreLib/System/Byte.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,11 @@ public static bool TryParse(String s, out Byte result)
132132
return TryParse(s.AsReadOnlySpan(), NumberStyles.Integer, NumberFormatInfo.CurrentInfo, out result);
133133
}
134134

135+
public static bool TryParse(ReadOnlySpan<char> s, out byte result)
136+
{
137+
return TryParse(s, NumberStyles.Integer, NumberFormatInfo.CurrentInfo, out result);
138+
}
139+
135140
public static bool TryParse(String s, NumberStyles style, IFormatProvider provider, out Byte result)
136141
{
137142
NumberFormatInfo.ValidateParseStyleInteger(style);
@@ -145,7 +150,7 @@ public static bool TryParse(String s, NumberStyles style, IFormatProvider provid
145150
return TryParse(s.AsReadOnlySpan(), style, NumberFormatInfo.GetInstance(provider), out result);
146151
}
147152

148-
public static bool TryParse(ReadOnlySpan<char> s, out byte result, NumberStyles style = NumberStyles.Integer, IFormatProvider provider = null)
153+
public static bool TryParse(ReadOnlySpan<char> s, NumberStyles style, IFormatProvider provider, out byte result)
149154
{
150155
NumberFormatInfo.ValidateParseStyleInteger(style);
151156
return TryParse(s, style, NumberFormatInfo.GetInstance(provider), out result);

src/Common/src/CoreLib/System/Double.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,11 @@ public static bool TryParse(String s, out double result)
306306
return TryParse(s.AsReadOnlySpan(), NumberStyles.Float | NumberStyles.AllowThousands, NumberFormatInfo.CurrentInfo, out result);
307307
}
308308

309+
public static bool TryParse(ReadOnlySpan<char> s, out double result)
310+
{
311+
return TryParse(s, NumberStyles.Float | NumberStyles.AllowThousands, NumberFormatInfo.CurrentInfo, out result);
312+
}
313+
309314
public static bool TryParse(String s, NumberStyles style, IFormatProvider provider, out double result)
310315
{
311316
NumberFormatInfo.ValidateParseStyleFloatingPoint(style);
@@ -319,7 +324,7 @@ public static bool TryParse(String s, NumberStyles style, IFormatProvider provid
319324
return TryParse(s.AsReadOnlySpan(), style, NumberFormatInfo.GetInstance(provider), out result);
320325
}
321326

322-
public static bool TryParse(ReadOnlySpan<char> s, out double result, NumberStyles style = NumberStyles.Integer, IFormatProvider provider = null)
327+
public static bool TryParse(ReadOnlySpan<char> s, NumberStyles style, IFormatProvider provider, out double result)
323328
{
324329
NumberFormatInfo.ValidateParseStyleFloatingPoint(style);
325330
return TryParse(s, style, NumberFormatInfo.GetInstance(provider), out result);

src/Common/src/CoreLib/System/Globalization/JapaneseCalendar.Win32.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,9 +160,9 @@ private static EraInfo GetEraFromValue(String value, String data)
160160
int day;
161161

162162
ReadOnlySpan<char> valueSpan = value.AsReadOnlySpan();
163-
if (!Int32.TryParse(valueSpan.Slice(0, 4), out year, style:NumberStyles.None, provider: NumberFormatInfo.InvariantInfo) ||
164-
!Int32.TryParse(valueSpan.Slice(5, 2), out month, style:NumberStyles.None, provider: NumberFormatInfo.InvariantInfo) ||
165-
!Int32.TryParse(valueSpan.Slice(8, 2), out day, style:NumberStyles.None, provider: NumberFormatInfo.InvariantInfo))
163+
if (!Int32.TryParse(valueSpan.Slice(0, 4), NumberStyles.None, NumberFormatInfo.InvariantInfo, out year) ||
164+
!Int32.TryParse(valueSpan.Slice(5, 2), NumberStyles.None, NumberFormatInfo.InvariantInfo, out month) ||
165+
!Int32.TryParse(valueSpan.Slice(8, 2), NumberStyles.None, NumberFormatInfo.InvariantInfo, out day))
166166
{
167167
// Couldn't convert integer, fail
168168
return null;

src/Common/src/CoreLib/System/Int16.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,11 @@ public static bool TryParse(String s, out Int16 result)
167167
return TryParse(s.AsReadOnlySpan(), NumberStyles.Integer, NumberFormatInfo.CurrentInfo, out result);
168168
}
169169

170+
public static bool TryParse(ReadOnlySpan<char> s, out short result)
171+
{
172+
return TryParse(s, NumberStyles.Integer, NumberFormatInfo.CurrentInfo, out result);
173+
}
174+
170175
public static bool TryParse(String s, NumberStyles style, IFormatProvider provider, out Int16 result)
171176
{
172177
NumberFormatInfo.ValidateParseStyleInteger(style);
@@ -180,7 +185,7 @@ public static bool TryParse(String s, NumberStyles style, IFormatProvider provid
180185
return TryParse(s.AsReadOnlySpan(), style, NumberFormatInfo.GetInstance(provider), out result);
181186
}
182187

183-
public static bool TryParse(ReadOnlySpan<char> s, out Int16 result, NumberStyles style = NumberStyles.Integer, IFormatProvider provider = null)
188+
public static bool TryParse(ReadOnlySpan<char> s, NumberStyles style, IFormatProvider provider, out short result)
184189
{
185190
NumberFormatInfo.ValidateParseStyleInteger(style);
186191
return TryParse(s, style, NumberFormatInfo.GetInstance(provider), out result);

src/Common/src/CoreLib/System/Int32.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,11 @@ public static bool TryParse(String s, out Int32 result)
150150
return Number.TryParseInt32(s.AsReadOnlySpan(), NumberStyles.Integer, NumberFormatInfo.CurrentInfo, out result);
151151
}
152152

153+
public static bool TryParse(ReadOnlySpan<char> s, out int result)
154+
{
155+
return Number.TryParseInt32(s, NumberStyles.Integer, NumberFormatInfo.CurrentInfo, out result);
156+
}
157+
153158
// Parses an integer from a String in the given style. Returns false rather
154159
// than throwing exceptin if input is invalid
155160
//
@@ -166,7 +171,7 @@ public static bool TryParse(String s, NumberStyles style, IFormatProvider provid
166171
return Number.TryParseInt32(s.AsReadOnlySpan(), style, NumberFormatInfo.GetInstance(provider), out result);
167172
}
168173

169-
public static bool TryParse(ReadOnlySpan<char> s, out int result, NumberStyles style = NumberStyles.Integer, IFormatProvider provider = null)
174+
public static bool TryParse(ReadOnlySpan<char> s, NumberStyles style, IFormatProvider provider, out int result)
170175
{
171176
NumberFormatInfo.ValidateParseStyleInteger(style);
172177
return Number.TryParseInt32(s, style, NumberFormatInfo.GetInstance(provider), out result);

src/Common/src/CoreLib/System/Int64.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,11 @@ public static Boolean TryParse(String s, out Int64 result)
141141
return Number.TryParseInt64(s.AsReadOnlySpan(), NumberStyles.Integer, NumberFormatInfo.CurrentInfo, out result);
142142
}
143143

144+
public static bool TryParse(ReadOnlySpan<char> s, out long result)
145+
{
146+
return Number.TryParseInt64(s, NumberStyles.Integer, NumberFormatInfo.CurrentInfo, out result);
147+
}
148+
144149
public static Boolean TryParse(String s, NumberStyles style, IFormatProvider provider, out Int64 result)
145150
{
146151
NumberFormatInfo.ValidateParseStyleInteger(style);
@@ -154,7 +159,7 @@ public static Boolean TryParse(String s, NumberStyles style, IFormatProvider pro
154159
return Number.TryParseInt64(s.AsReadOnlySpan(), style, NumberFormatInfo.GetInstance(provider), out result);
155160
}
156161

157-
public static bool TryParse(ReadOnlySpan<char> s, out long result, NumberStyles style = NumberStyles.Integer, IFormatProvider provider = null)
162+
public static bool TryParse(ReadOnlySpan<char> s, NumberStyles style, IFormatProvider provider, out long result)
158163
{
159164
NumberFormatInfo.ValidateParseStyleInteger(style);
160165
return Number.TryParseInt64(s, style, NumberFormatInfo.GetInstance(provider), out result);

src/Common/src/CoreLib/System/SByte.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,12 @@ public static bool TryParse(String s, out SByte result)
185185
return TryParse(s.AsReadOnlySpan(), NumberStyles.Integer, NumberFormatInfo.CurrentInfo, out result);
186186
}
187187

188+
[CLSCompliant(false)]
189+
public static bool TryParse(ReadOnlySpan<char> s, out sbyte result)
190+
{
191+
return TryParse(s, NumberStyles.Integer, NumberFormatInfo.CurrentInfo, out result);
192+
}
193+
188194
[CLSCompliant(false)]
189195
public static bool TryParse(String s, NumberStyles style, IFormatProvider provider, out SByte result)
190196
{
@@ -200,7 +206,7 @@ public static bool TryParse(String s, NumberStyles style, IFormatProvider provid
200206
}
201207

202208
[CLSCompliant(false)]
203-
public static bool TryParse(ReadOnlySpan<char> s, out sbyte result, NumberStyles style = NumberStyles.Integer, IFormatProvider provider = null)
209+
public static bool TryParse(ReadOnlySpan<char> s, NumberStyles style, IFormatProvider provider, out sbyte result)
204210
{
205211
NumberFormatInfo.ValidateParseStyleInteger(style);
206212
return TryParse(s, style, NumberFormatInfo.GetInstance(provider), out result);

src/Common/src/CoreLib/System/Single.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,11 @@ public static Boolean TryParse(String s, out Single result)
296296
return TryParse(s.AsReadOnlySpan(), NumberStyles.Float | NumberStyles.AllowThousands, NumberFormatInfo.CurrentInfo, out result);
297297
}
298298

299+
public static bool TryParse(ReadOnlySpan<char> s, out float result)
300+
{
301+
return TryParse(s, NumberStyles.Float | NumberStyles.AllowThousands, NumberFormatInfo.CurrentInfo, out result);
302+
}
303+
299304
public static Boolean TryParse(String s, NumberStyles style, IFormatProvider provider, out Single result)
300305
{
301306
NumberFormatInfo.ValidateParseStyleFloatingPoint(style);
@@ -309,7 +314,7 @@ public static Boolean TryParse(String s, NumberStyles style, IFormatProvider pro
309314
return TryParse(s.AsReadOnlySpan(), style, NumberFormatInfo.GetInstance(provider), out result);
310315
}
311316

312-
public static Boolean TryParse(ReadOnlySpan<char> s, out Single result, NumberStyles style = NumberStyles.Integer, IFormatProvider provider = null)
317+
public static bool TryParse(ReadOnlySpan<char> s, NumberStyles style, IFormatProvider provider, out float result)
313318
{
314319
NumberFormatInfo.ValidateParseStyleFloatingPoint(style);
315320
return TryParse(s, style, NumberFormatInfo.GetInstance(provider), out result);

src/Common/src/CoreLib/System/TimeSpan.cs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,10 @@ public static Boolean TryParse(String s, out TimeSpan result)
366366
}
367367
return TimeSpanParse.TryParse(s.AsReadOnlySpan(), null, out result);
368368
}
369+
public static bool TryParse(ReadOnlySpan<char> s, out TimeSpan result)
370+
{
371+
return TimeSpanParse.TryParse(s, null, out result);
372+
}
369373
public static Boolean TryParse(String input, IFormatProvider formatProvider, out TimeSpan result)
370374
{
371375
if (input == null)
@@ -375,7 +379,7 @@ public static Boolean TryParse(String input, IFormatProvider formatProvider, out
375379
}
376380
return TimeSpanParse.TryParse(input.AsReadOnlySpan(), formatProvider, out result);
377381
}
378-
public static bool TryParse(ReadOnlySpan<char> input, out TimeSpan result, IFormatProvider formatProvider = null)
382+
public static bool TryParse(ReadOnlySpan<char> input, IFormatProvider formatProvider, out TimeSpan result)
379383
{
380384
return TimeSpanParse.TryParse(input, formatProvider, out result);
381385
}
@@ -388,6 +392,10 @@ public static Boolean TryParseExact(String input, String format, IFormatProvider
388392
}
389393
return TimeSpanParse.TryParseExact(input.AsReadOnlySpan(), format, formatProvider, TimeSpanStyles.None, out result);
390394
}
395+
public static bool TryParseExact(ReadOnlySpan<char> input, string format, IFormatProvider formatProvider, out TimeSpan result)
396+
{
397+
return TimeSpanParse.TryParseExact(input, format, formatProvider, TimeSpanStyles.None, out result);
398+
}
391399
public static Boolean TryParseExact(String input, String[] formats, IFormatProvider formatProvider, out TimeSpan result)
392400
{
393401
if (input == null)
@@ -397,6 +405,10 @@ public static Boolean TryParseExact(String input, String[] formats, IFormatProvi
397405
}
398406
return TimeSpanParse.TryParseExactMultiple(input.AsReadOnlySpan(), formats, formatProvider, TimeSpanStyles.None, out result);
399407
}
408+
public static bool TryParseExact(ReadOnlySpan<char> input, string[] formats, IFormatProvider formatProvider, out TimeSpan result)
409+
{
410+
return TimeSpanParse.TryParseExactMultiple(input, formats, formatProvider, TimeSpanStyles.None, out result);
411+
}
400412
public static Boolean TryParseExact(String input, String format, IFormatProvider formatProvider, TimeSpanStyles styles, out TimeSpan result)
401413
{
402414
ValidateStyles(styles, nameof(styles));
@@ -407,7 +419,7 @@ public static Boolean TryParseExact(String input, String format, IFormatProvider
407419
}
408420
return TimeSpanParse.TryParseExact(input.AsReadOnlySpan(), format, formatProvider, styles, out result);
409421
}
410-
public static bool TryParseExact(ReadOnlySpan<char> input, string format, IFormatProvider formatProvider, out TimeSpan result, TimeSpanStyles styles = TimeSpanStyles.None)
422+
public static bool TryParseExact(ReadOnlySpan<char> input, string format, IFormatProvider formatProvider, TimeSpanStyles styles, out TimeSpan result)
411423
{
412424
ValidateStyles(styles, nameof(styles));
413425
return TimeSpanParse.TryParseExact(input, format, formatProvider, styles, out result);
@@ -422,7 +434,7 @@ public static Boolean TryParseExact(String input, String[] formats, IFormatProvi
422434
}
423435
return TimeSpanParse.TryParseExactMultiple(input.AsReadOnlySpan(), formats, formatProvider, styles, out result);
424436
}
425-
public static bool TryParseExact(ReadOnlySpan<char> input, string[] formats, IFormatProvider formatProvider, out TimeSpan result, TimeSpanStyles styles = TimeSpanStyles.None)
437+
public static bool TryParseExact(ReadOnlySpan<char> input, string[] formats, IFormatProvider formatProvider, TimeSpanStyles styles, out TimeSpan result)
426438
{
427439
ValidateStyles(styles, nameof(styles));
428440
return TimeSpanParse.TryParseExactMultiple(input, formats, formatProvider, styles, out result);

src/Common/src/CoreLib/System/UInt16.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,12 @@ public static bool TryParse(String s, out UInt16 result)
154154
return TryParse(s.AsReadOnlySpan(), NumberStyles.Integer, NumberFormatInfo.CurrentInfo, out result);
155155
}
156156

157+
[CLSCompliant(false)]
158+
public static bool TryParse(ReadOnlySpan<char> s, out ushort result)
159+
{
160+
return TryParse(s, NumberStyles.Integer, NumberFormatInfo.CurrentInfo, out result);
161+
}
162+
157163
[CLSCompliant(false)]
158164
public static bool TryParse(String s, NumberStyles style, IFormatProvider provider, out UInt16 result)
159165
{
@@ -169,7 +175,7 @@ public static bool TryParse(String s, NumberStyles style, IFormatProvider provid
169175
}
170176

171177
[CLSCompliant(false)]
172-
public static bool TryParse(ReadOnlySpan<char> s, out ushort result, NumberStyles style = NumberStyles.Integer, IFormatProvider provider = null)
178+
public static bool TryParse(ReadOnlySpan<char> s, NumberStyles style, IFormatProvider provider, out ushort result)
173179
{
174180
NumberFormatInfo.ValidateParseStyleInteger(style);
175181
return TryParse(s, style, NumberFormatInfo.GetInstance(provider), out result);

0 commit comments

Comments
 (0)