1
1
using System ;
2
2
using System . Runtime . CompilerServices ;
3
3
using System . Globalization ;
4
+ using ServiceStack . Text . Json ;
4
5
#if NETSTANDARD1_1
5
6
using Microsoft . Extensions . Primitives ;
6
7
#endif
@@ -118,17 +119,63 @@ enum ParseState
118
119
TrailingWhite
119
120
}
120
121
121
- public static sbyte ParseSByte ( this StringSegment value ) => ( sbyte ) ParseSignedInteger ( value , sbyte . MaxValue , sbyte . MinValue ) ;
122
- public static byte ParseByte ( this StringSegment value ) => ( byte ) ParseUnsignedInteger ( value , byte . MaxValue ) ;
122
+ public static sbyte ParseSByte ( this StringSegment value )
123
+ {
124
+ return JsConfig . UseSystemParseMethods
125
+ ? sbyte . Parse ( value . Value )
126
+ : ( sbyte ) ParseSignedInteger ( value , sbyte . MaxValue , sbyte . MinValue ) ;
127
+ }
128
+
129
+ public static byte ParseByte ( this StringSegment value )
130
+ {
131
+ return JsConfig . UseSystemParseMethods
132
+ ? byte . Parse ( value . Value )
133
+ : ( byte ) ParseUnsignedInteger ( value , byte . MaxValue ) ;
134
+ }
135
+
136
+ public static short ParseInt16 ( this StringSegment value )
137
+ {
138
+ return JsConfig . UseSystemParseMethods
139
+ ? short . Parse ( value . Value )
140
+ : ( short ) ParseSignedInteger ( value , short . MaxValue , short . MinValue ) ;
141
+ }
142
+
143
+ public static ushort ParseUInt16 ( this StringSegment value )
144
+ {
145
+ return JsConfig . UseSystemParseMethods
146
+ ? ushort . Parse ( value . Value )
147
+ : ( ushort ) ParseUnsignedInteger ( value , ushort . MaxValue ) ;
148
+ }
149
+
150
+
151
+ public static int ParseInt32 ( this StringSegment value )
152
+ {
153
+ return JsConfig . UseSystemParseMethods
154
+ ? int . Parse ( value . Value )
155
+ : ( int ) ParseSignedInteger ( value , Int32 . MaxValue , Int32 . MinValue ) ;
156
+ }
123
157
124
- public static short ParseInt16 ( this StringSegment value ) => ( short ) ParseSignedInteger ( value , short . MaxValue , short . MinValue ) ;
125
- public static ushort ParseUInt16 ( this StringSegment value ) => ( ushort ) ParseUnsignedInteger ( value , ushort . MaxValue ) ;
158
+ public static uint ParseUInt32 ( this StringSegment value )
159
+ {
160
+ return JsConfig . UseSystemParseMethods
161
+ ? uint . Parse ( value . Value )
162
+ : ( uint ) ParseUnsignedInteger ( value , UInt32 . MaxValue ) ;
163
+ }
126
164
127
- public static int ParseInt32 ( this StringSegment value ) => ( int ) ParseSignedInteger ( value , Int32 . MaxValue , Int32 . MinValue ) ;
128
- public static uint ParseUInt32 ( this StringSegment value ) => ( uint ) ParseUnsignedInteger ( value , UInt32 . MaxValue ) ;
165
+ public static long ParseInt64 ( this StringSegment value )
166
+ {
167
+ return JsConfig . UseSystemParseMethods
168
+ ? long . Parse ( value . Value )
169
+ : ParseSignedInteger ( value , Int64 . MaxValue , Int64 . MinValue ) ;
129
170
130
- public static long ParseInt64 ( this StringSegment value ) => ParseSignedInteger ( value , Int64 . MaxValue , Int64 . MinValue ) ;
131
- public static ulong ParseUInt64 ( this StringSegment value ) => ParseUnsignedInteger ( value , UInt64 . MaxValue ) ;
171
+ }
172
+
173
+ public static ulong ParseUInt64 ( this StringSegment value )
174
+ {
175
+ return JsConfig . UseSystemParseMethods
176
+ ? ulong . Parse ( value . Value )
177
+ : ParseUnsignedInteger ( value , UInt64 . MaxValue ) ;
178
+ }
132
179
133
180
private static ulong ParseUnsignedInteger ( StringSegment value , ulong maxValue )
134
181
{
@@ -146,7 +193,7 @@ private static ulong ParseUnsignedInteger(StringSegment value, ulong maxValue)
146
193
switch ( state )
147
194
{
148
195
case ParseState . LeadingWhite :
149
- if ( Char . IsWhiteSpace ( c ) )
196
+ if ( JsonUtils . IsWhiteSpace ( c ) )
150
197
break ;
151
198
if ( c == '0' )
152
199
{
@@ -172,7 +219,7 @@ private static ulong ParseUnsignedInteger(StringSegment value, ulong maxValue)
172
219
if ( result > maxValue ) //check only minvalue, because in absolute value it's greater than maxvalue
173
220
throw new OverflowException ( ) ;
174
221
}
175
- else if ( Char . IsWhiteSpace ( c ) )
222
+ else if ( JsonUtils . IsWhiteSpace ( c ) )
176
223
{
177
224
state = ParseState . TrailingWhite ;
178
225
}
@@ -182,7 +229,7 @@ private static ulong ParseUnsignedInteger(StringSegment value, ulong maxValue)
182
229
}
183
230
break ;
184
231
case ParseState . TrailingWhite :
185
- if ( Char . IsWhiteSpace ( c ) )
232
+ if ( JsonUtils . IsWhiteSpace ( c ) )
186
233
{
187
234
state = ParseState . TrailingWhite ;
188
235
}
@@ -214,7 +261,7 @@ private static long ParseSignedInteger(StringSegment value, long maxValue, long
214
261
switch ( state )
215
262
{
216
263
case ParseState . LeadingWhite :
217
- if ( Char . IsWhiteSpace ( c ) )
264
+ if ( JsonUtils . IsWhiteSpace ( c ) )
218
265
break ;
219
266
220
267
if ( c == '-' )
@@ -256,7 +303,7 @@ private static long ParseSignedInteger(StringSegment value, long maxValue, long
256
303
if ( result < minValue ) //check only minvalue, because in absolute value it's greater than maxvalue
257
304
throw new OverflowException ( ) ;
258
305
}
259
- else if ( Char . IsWhiteSpace ( c ) )
306
+ else if ( JsonUtils . IsWhiteSpace ( c ) )
260
307
{
261
308
state = ParseState . TrailingWhite ;
262
309
} else
@@ -265,7 +312,7 @@ private static long ParseSignedInteger(StringSegment value, long maxValue, long
265
312
}
266
313
break ;
267
314
case ParseState . TrailingWhite :
268
- if ( Char . IsWhiteSpace ( c ) )
315
+ if ( JsonUtils . IsWhiteSpace ( c ) )
269
316
{
270
317
state = ParseState . TrailingWhite ;
271
318
} else
@@ -292,6 +339,13 @@ private static long ParseSignedInteger(StringSegment value, long maxValue, long
292
339
293
340
public static decimal ParseDecimal ( this StringSegment value , bool allowThousands = false )
294
341
{
342
+ if ( JsConfig . UseSystemParseMethods )
343
+ {
344
+ return decimal . Parse ( value . Value ,
345
+ allowThousands ? NumberStyles . Float : ( NumberStyles . Float | NumberStyles . AllowThousands ) ,
346
+ CultureInfo . InvariantCulture ) ;
347
+ }
348
+
295
349
if ( value . Length == 0 )
296
350
throw new FormatException ( BadFormat ) ;
297
351
@@ -312,7 +366,7 @@ public static decimal ParseDecimal(this StringSegment value, bool allowThousands
312
366
switch ( state )
313
367
{
314
368
case ParseState . LeadingWhite :
315
- if ( Char . IsWhiteSpace ( c ) )
369
+ if ( JsonUtils . IsWhiteSpace ( c ) )
316
370
break ;
317
371
318
372
if ( c == '-' )
@@ -389,7 +443,7 @@ public static decimal ParseDecimal(this StringSegment value, bool allowThousands
389
443
}
390
444
}
391
445
}
392
- else if ( Char . IsWhiteSpace ( c ) )
446
+ else if ( JsonUtils . IsWhiteSpace ( c ) )
393
447
{
394
448
state = ParseState . TrailingWhite ;
395
449
}
@@ -411,7 +465,7 @@ public static decimal ParseDecimal(this StringSegment value, bool allowThousands
411
465
}
412
466
break ;
413
467
case ParseState . FractionNumber :
414
- if ( Char . IsWhiteSpace ( c ) )
468
+ if ( JsonUtils . IsWhiteSpace ( c ) )
415
469
{
416
470
if ( noIntegerPart )
417
471
throw new FormatException ( BadFormat ) ;
@@ -484,7 +538,7 @@ public static decimal ParseDecimal(this StringSegment value, bool allowThousands
484
538
}
485
539
break ;
486
540
case ParseState . TrailingWhite :
487
- if ( ! Char . IsWhiteSpace ( c ) )
541
+ if ( ! JsonUtils . IsWhiteSpace ( c ) )
488
542
throw new FormatException ( BadFormat ) ;
489
543
break ;
490
544
}
@@ -534,14 +588,17 @@ public static decimal ParseDecimal(this StringSegment value, bool allowThousands
534
588
535
589
public static Guid ParseGuid ( this StringSegment value )
536
590
{
591
+ if ( JsConfig . UseSystemParseMethods )
592
+ return Guid . Parse ( value . Value ) ;
593
+
537
594
//Guid can be in one of 3 forms:
538
595
//1. General `{dddddddd-dddd-dddd-dddd-dddddddddddd}` or `(dddddddd-dddd-dddd-dddd-dddddddddddd)` 8-4-4-4-12 chars
539
596
//2. Hex `{0xdddddddd,0xdddd,0xdddd,{0xdd,0xdd,0xdd,0xdd,0xdd,0xdd,0xdd,0xdd}}` 8-4-4-8x2 chars
540
597
//3. No style `dddddddddddddddddddddddddddddddd` 32 chars
541
598
542
599
int i = value . Offset ;
543
600
int end = value . Offset + value . Length ;
544
- while ( Char . IsWhiteSpace ( value . Buffer [ i ] ) && i < end ) i ++ ;
601
+ while ( JsonUtils . IsWhiteSpace ( value . Buffer [ i ] ) && i < end ) i ++ ;
545
602
546
603
if ( i == end )
547
604
throw new FormatException ( BadFormat ) ;
@@ -552,7 +609,7 @@ public static Guid ParseGuid(this StringSegment value)
552
609
result = ParseGeneralStyleGuid ( new StringSegment ( value . Buffer , i , end - i ) , out guidLen ) ;
553
610
i += guidLen ;
554
611
555
- while ( i < end && Char . IsWhiteSpace ( value . Buffer [ i ] ) ) i ++ ;
612
+ while ( i < end && JsonUtils . IsWhiteSpace ( value . Buffer [ i ] ) ) i ++ ;
556
613
557
614
if ( i < end )
558
615
throw new FormatException ( BadFormat ) ;
0 commit comments