@@ -10,7 +10,8 @@ namespace ServiceStack.Text.Support
10
10
{
11
11
public static class StringSegmentExtensions
12
12
{
13
- const string BadFormat = "Input string was not in a correct format" ;
13
+ const string BadFormat = "Input string was not in a correct format." ;
14
+ const string OverflowMessage = "Value was either too large or too small for an {0}." ;
14
15
15
16
[ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
16
17
public static bool IsNullOrEmpty ( this StringSegment value )
@@ -119,62 +120,119 @@ enum ParseState
119
120
TrailingWhite
120
121
}
121
122
123
+ private static Exception GetOverflowException ( Type type ) => new OverflowException ( String . Format ( OverflowMessage , type . Name ) ) ;
124
+
122
125
public static sbyte ParseSByte ( this StringSegment value )
123
126
{
124
- return JsConfig . UseSystemParseMethods
125
- ? sbyte . Parse ( value . Value , CultureInfo . InvariantCulture )
126
- : ( sbyte ) ParseSignedInteger ( value , sbyte . MaxValue , sbyte . MinValue ) ;
127
+ try
128
+ {
129
+ return JsConfig . UseSystemParseMethods
130
+ ? sbyte . Parse ( value . Value , CultureInfo . InvariantCulture )
131
+ : ( sbyte ) ParseSignedInteger ( value , sbyte . MaxValue , sbyte . MinValue ) ;
132
+ }
133
+ catch ( OverflowException )
134
+ {
135
+ throw GetOverflowException ( typeof ( byte ) ) ;
136
+ }
127
137
}
128
138
129
139
public static byte ParseByte ( this StringSegment value )
130
140
{
131
- return JsConfig . UseSystemParseMethods
132
- ? byte . Parse ( value . Value , CultureInfo . InvariantCulture )
133
- : ( byte ) ParseUnsignedInteger ( value , byte . MaxValue ) ;
141
+ try
142
+ {
143
+ return JsConfig . UseSystemParseMethods
144
+ ? byte . Parse ( value . Value , CultureInfo . InvariantCulture )
145
+ : ( byte ) ParseUnsignedInteger ( value , byte . MaxValue ) ;
146
+ }
147
+ catch ( OverflowException )
148
+ {
149
+ throw GetOverflowException ( typeof ( byte ) ) ;
150
+ }
134
151
}
135
152
136
153
public static short ParseInt16 ( this StringSegment value )
137
154
{
138
- return JsConfig . UseSystemParseMethods
139
- ? short . Parse ( value . Value , CultureInfo . InvariantCulture )
140
- : ( short ) ParseSignedInteger ( value , short . MaxValue , short . MinValue ) ;
155
+ try
156
+ {
157
+ return JsConfig . UseSystemParseMethods
158
+ ? short . Parse ( value . Value , CultureInfo . InvariantCulture )
159
+ : ( short ) ParseSignedInteger ( value , short . MaxValue , short . MinValue ) ;
160
+ }
161
+ catch ( OverflowException )
162
+ {
163
+ throw GetOverflowException ( typeof ( Int16 ) ) ;
164
+ }
141
165
}
142
166
143
167
public static ushort ParseUInt16 ( this StringSegment value )
144
168
{
145
- return JsConfig . UseSystemParseMethods
146
- ? ushort . Parse ( value . Value , CultureInfo . InvariantCulture )
147
- : ( ushort ) ParseUnsignedInteger ( value , ushort . MaxValue ) ;
169
+ try
170
+ {
171
+ return JsConfig . UseSystemParseMethods
172
+ ? ushort . Parse ( value . Value , CultureInfo . InvariantCulture )
173
+ : ( ushort ) ParseUnsignedInteger ( value , ushort . MaxValue ) ;
174
+ }
175
+ catch ( OverflowException )
176
+ {
177
+ throw GetOverflowException ( typeof ( UInt16 ) ) ;
178
+ }
148
179
}
149
180
150
181
151
182
public static int ParseInt32 ( this StringSegment value )
152
183
{
153
- return JsConfig . UseSystemParseMethods
154
- ? int . Parse ( value . Value , CultureInfo . InvariantCulture )
155
- : ( int ) ParseSignedInteger ( value , Int32 . MaxValue , Int32 . MinValue ) ;
184
+ try
185
+ {
186
+ return JsConfig . UseSystemParseMethods
187
+ ? int . Parse ( value . Value , CultureInfo . InvariantCulture )
188
+ : ( int ) ParseSignedInteger ( value , Int32 . MaxValue , Int32 . MinValue ) ;
189
+ }
190
+ catch ( OverflowException )
191
+ {
192
+ throw GetOverflowException ( typeof ( Int32 ) ) ;
193
+ }
156
194
}
157
195
158
196
public static uint ParseUInt32 ( this StringSegment value )
159
197
{
160
- return JsConfig . UseSystemParseMethods
161
- ? uint . Parse ( value . Value , CultureInfo . InvariantCulture )
162
- : ( uint ) ParseUnsignedInteger ( value , UInt32 . MaxValue ) ;
198
+ try
199
+ {
200
+ return JsConfig . UseSystemParseMethods
201
+ ? uint . Parse ( value . Value , CultureInfo . InvariantCulture )
202
+ : ( uint ) ParseUnsignedInteger ( value , UInt32 . MaxValue ) ;
203
+ }
204
+ catch ( OverflowException )
205
+ {
206
+ throw GetOverflowException ( typeof ( UInt32 ) ) ;
207
+ }
163
208
}
164
209
165
210
public static long ParseInt64 ( this StringSegment value )
166
211
{
167
- return JsConfig . UseSystemParseMethods
168
- ? long . Parse ( value . Value , CultureInfo . InvariantCulture )
169
- : ParseSignedInteger ( value , Int64 . MaxValue , Int64 . MinValue ) ;
170
-
212
+ try
213
+ {
214
+ return JsConfig . UseSystemParseMethods
215
+ ? long . Parse ( value . Value , CultureInfo . InvariantCulture )
216
+ : ParseSignedInteger ( value , Int64 . MaxValue , Int64 . MinValue ) ;
217
+ }
218
+ catch ( OverflowException )
219
+ {
220
+ throw GetOverflowException ( typeof ( Int64 ) ) ;
221
+ }
171
222
}
172
223
173
224
public static ulong ParseUInt64 ( this StringSegment value )
174
225
{
175
- return JsConfig . UseSystemParseMethods
176
- ? ulong . Parse ( value . Value , CultureInfo . InvariantCulture )
177
- : ParseUnsignedInteger ( value , UInt64 . MaxValue ) ;
226
+ try
227
+ {
228
+ return JsConfig . UseSystemParseMethods
229
+ ? ulong . Parse ( value . Value , CultureInfo . InvariantCulture )
230
+ : ParseUnsignedInteger ( value , UInt64 . MaxValue ) ;
231
+ }
232
+ catch ( OverflowException )
233
+ {
234
+ throw GetOverflowException ( typeof ( UInt64 ) ) ;
235
+ }
178
236
}
179
237
180
238
private static ulong ParseUnsignedInteger ( StringSegment value , ulong maxValue )
0 commit comments