Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit a8cdcc4

Browse files
committed
DynamicNumber fixes
1 parent ac9541f commit a8cdcc4

File tree

3 files changed

+42
-14
lines changed

3 files changed

+42
-14
lines changed

src/ServiceStack.Text/Common/DeserializeType.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -339,15 +339,16 @@ public static bool Has(this ParseAsType flags, ParseAsType flag)
339339
return (flag & flags) != 0;
340340
}
341341

342-
public static object ParseNumber(this StringSegment value)
342+
public static object ParseNumber(this StringSegment value) => ParseNumber(value, JsConfig.TryParseIntoBestFit);
343+
public static object ParseNumber(this StringSegment value, bool bestFit)
343344
{
344345
if (value.Length == 1)
345346
{
346347
int singleDigit = value.GetChar(0);
347348
if (singleDigit >= 48 || singleDigit <= 57) // 0 - 9
348349
{
349350
var result = singleDigit - 48;
350-
if (JsConfig.TryParseIntoBestFit)
351+
if (bestFit)
351352
return (byte) result;
352353
return result;
353354
}

src/ServiceStack.Text/DynamicNumber.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public class DynamicSByte : IDynamicNumber
2929
public Type Type => typeof(sbyte);
3030

3131
[MethodImpl(MethodImplOptions.AggressiveInlining)]
32-
public sbyte Convert(object value) => System.Convert.ToSByte(value);
32+
public sbyte Convert(object value) => System.Convert.ToSByte(this.ParseString(value) ?? value);
3333
[MethodImpl(MethodImplOptions.AggressiveInlining)]
3434
public object ConvertFrom(object value) => this.ParseString(value)
3535
?? System.Convert.ToSByte(value);
@@ -59,7 +59,7 @@ public class DynamicByte : IDynamicNumber
5959
public Type Type => typeof(byte);
6060

6161
[MethodImpl(MethodImplOptions.AggressiveInlining)]
62-
public byte Convert(object value) => System.Convert.ToByte(value);
62+
public byte Convert(object value) => System.Convert.ToByte(this.ParseString(value) ?? value);
6363
[MethodImpl(MethodImplOptions.AggressiveInlining)]
6464
public object ConvertFrom(object value) => this.ParseString(value)
6565
?? System.Convert.ToByte(value);
@@ -89,7 +89,7 @@ public class DynamicShort : IDynamicNumber
8989
public Type Type => typeof(short);
9090

9191
[MethodImpl(MethodImplOptions.AggressiveInlining)]
92-
public short Convert(object value) => System.Convert.ToInt16(value);
92+
public short Convert(object value) => System.Convert.ToInt16(this.ParseString(value) ?? value);
9393
[MethodImpl(MethodImplOptions.AggressiveInlining)]
9494
public object ConvertFrom(object value) => this.ParseString(value)
9595
?? System.Convert.ToInt16(value);
@@ -119,7 +119,7 @@ public class DynamicUShort : IDynamicNumber
119119
public Type Type => typeof(ushort);
120120

121121
[MethodImpl(MethodImplOptions.AggressiveInlining)]
122-
public ushort Convert(object value) => System.Convert.ToUInt16(value);
122+
public ushort Convert(object value) => System.Convert.ToUInt16(this.ParseString(value) ?? value);
123123
[MethodImpl(MethodImplOptions.AggressiveInlining)]
124124
public object ConvertFrom(object value) => this.ParseString(value)
125125
?? System.Convert.ToUInt16(value);
@@ -149,7 +149,7 @@ public class DynamicInt : IDynamicNumber
149149
public Type Type => typeof(int);
150150

151151
[MethodImpl(MethodImplOptions.AggressiveInlining)]
152-
public int Convert(object value) => System.Convert.ToInt32(value);
152+
public int Convert(object value) => System.Convert.ToInt32(this.ParseString(value) ?? value);
153153
[MethodImpl(MethodImplOptions.AggressiveInlining)]
154154
public object ConvertFrom(object value) => this.ParseString(value)
155155
?? System.Convert.ToInt32(value);
@@ -179,7 +179,7 @@ public class DynamicUInt : IDynamicNumber
179179
public Type Type => typeof(uint);
180180

181181
[MethodImpl(MethodImplOptions.AggressiveInlining)]
182-
public uint Convert(object value) => System.Convert.ToUInt32(value);
182+
public uint Convert(object value) => System.Convert.ToUInt32(this.ParseString(value) ?? value);
183183
[MethodImpl(MethodImplOptions.AggressiveInlining)]
184184
public object ConvertFrom(object value) => this.ParseString(value)
185185
?? System.Convert.ToUInt32(value);
@@ -209,7 +209,7 @@ public class DynamicLong : IDynamicNumber
209209
public Type Type => typeof(long);
210210

211211
[MethodImpl(MethodImplOptions.AggressiveInlining)]
212-
public long Convert(object value) => System.Convert.ToInt64(value);
212+
public long Convert(object value) => System.Convert.ToInt64(this.ParseString(value) ?? value);
213213
[MethodImpl(MethodImplOptions.AggressiveInlining)]
214214
public object ConvertFrom(object value) => this.ParseString(value)
215215
?? System.Convert.ToInt64(value);
@@ -239,7 +239,7 @@ public class DynamicULong : IDynamicNumber
239239
public Type Type => typeof(ulong);
240240

241241
[MethodImpl(MethodImplOptions.AggressiveInlining)]
242-
public ulong Convert(object value) => System.Convert.ToUInt64(value);
242+
public ulong Convert(object value) => System.Convert.ToUInt64(this.ParseString(value) ?? value);
243243
[MethodImpl(MethodImplOptions.AggressiveInlining)]
244244
public object ConvertFrom(object value) => this.ParseString(value)
245245
?? System.Convert.ToUInt64(value);
@@ -269,7 +269,7 @@ public class DynamicFloat : IDynamicNumber
269269
public Type Type => typeof(float);
270270

271271
[MethodImpl(MethodImplOptions.AggressiveInlining)]
272-
public float Convert(object value) => System.Convert.ToSingle(value);
272+
public float Convert(object value) => System.Convert.ToSingle(this.ParseString(value) ?? value);
273273
[MethodImpl(MethodImplOptions.AggressiveInlining)]
274274
public object ConvertFrom(object value) => this.ParseString(value)
275275
?? System.Convert.ToSingle(value);
@@ -299,7 +299,7 @@ public class DynamicDouble : IDynamicNumber
299299
public Type Type => typeof(double);
300300

301301
[MethodImpl(MethodImplOptions.AggressiveInlining)]
302-
public double Convert(object value) => System.Convert.ToDouble(value);
302+
public double Convert(object value) => System.Convert.ToDouble(this.ParseString(value) ?? value);
303303
[MethodImpl(MethodImplOptions.AggressiveInlining)]
304304
public object ConvertFrom(object value) => this.ParseString(value)
305305
?? System.Convert.ToDouble(value);
@@ -329,7 +329,7 @@ public class DynamicDecimal : IDynamicNumber
329329
public Type Type => typeof(decimal);
330330

331331
[MethodImpl(MethodImplOptions.AggressiveInlining)]
332-
public decimal Convert(object value) => System.Convert.ToDecimal(value);
332+
public decimal Convert(object value) => System.Convert.ToDecimal(this.ParseString(value) ?? value);
333333
[MethodImpl(MethodImplOptions.AggressiveInlining)]
334334
public object ConvertFrom(object value) => this.ParseString(value)
335335
?? System.Convert.ToDecimal(value);
@@ -540,7 +540,7 @@ public static bool TryParseIntoBestFit(string strValue, out object result)
540540
return false;
541541

542542
var segValue = new StringSegment(strValue);
543-
result = segValue.ParseNumber();
543+
result = segValue.ParseNumber(bestFit:true);
544544
return result != null;
545545
}
546546
}

tests/ServiceStack.Text.Tests/DynamicNumberTests.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,5 +172,32 @@ public void Can_apply_operations_to_strings_containing_numbers()
172172
Assert.That(result, Is.EqualTo(2));
173173
}
174174

175+
[Test]
176+
public void Dynamic_number_examples()
177+
{
178+
object objInt = 1;
179+
object objDouble = 1.1;
180+
181+
Assert.That(DynamicNumber.Add(objInt, objDouble) is double d1 && d1 == 2.1);
182+
Assert.That(DynamicNumber.Multiply('2', "1.1") is double d2 && d2 == 2.2);
183+
Assert.That(DynamicNumber.TryParseIntoBestFit("1", out object result) && result is byte b && b == 1);
184+
}
185+
186+
[Test]
187+
public void TryParseIntoBestFit_tests()
188+
{
189+
JsConfig.TryParseIntoBestFit = true;
190+
191+
Assert.That(DynamicNumber.TryParse("1", out object result) && result is byte b && b == 1);
192+
193+
JsConfig.TryParseIntoBestFit = false;
194+
var pos = 0;
195+
var buf = new StringSegment("");
196+
while (buf.TryReadLine(out StringSegment line, ref pos))
197+
{
198+
// line
199+
}
200+
}
201+
175202
}
176203
}

0 commit comments

Comments
 (0)