@@ -209,7 +209,7 @@ class _InternetAddress implements InternetAddress {
209
209
_InternetAddress (this .type, this .address, this ._host, this ._in_addr,
210
210
[this ._scope_id = 0 ]);
211
211
212
- factory _InternetAddress . fromString (String address,
212
+ static Object _parseAddressString (String address,
213
213
{InternetAddressType ? type}) {
214
214
if (type == InternetAddressType .unix) {
215
215
var rawAddress = FileSystemEntity ._toUtf8Array (address);
@@ -225,14 +225,14 @@ class _InternetAddress implements InternetAddress {
225
225
}
226
226
var inAddr = _parse (address);
227
227
if (inAddr == null ) {
228
- throw ArgumentError ('Invalid internet address $address ' );
228
+ return ArgumentError ('Invalid internet address $address ' );
229
229
}
230
230
InternetAddressType type = inAddr.length == _IPv4AddrLength
231
231
? InternetAddressType .IPv4
232
232
: InternetAddressType .IPv6 ;
233
233
if (scopeID != null && scopeID.length > 0 ) {
234
234
if (type != InternetAddressType .IPv6 ) {
235
- throw ArgumentError .value (
235
+ return ArgumentError .value (
236
236
address, 'address' , 'IPv4 addresses cannot have a scope ID' );
237
237
}
238
238
@@ -242,14 +242,36 @@ class _InternetAddress implements InternetAddress {
242
242
return _InternetAddress (
243
243
InternetAddressType .IPv6 , originalAddress, null , inAddr, scopeID);
244
244
} else {
245
- throw ArgumentError .value (
245
+ return ArgumentError .value (
246
246
address, 'address' , 'Invalid IPv6 address with scope ID' );
247
247
}
248
248
}
249
249
return _InternetAddress (type, originalAddress, null , inAddr, 0 );
250
250
}
251
251
}
252
252
253
+ factory _InternetAddress .fromString (String address,
254
+ {InternetAddressType ? type}) {
255
+ final parsedAddress = _parseAddressString (address, type: type);
256
+ if (parsedAddress is _InternetAddress ) {
257
+ return parsedAddress;
258
+ } else {
259
+ assert (parsedAddress is ArgumentError );
260
+ throw parsedAddress;
261
+ }
262
+ }
263
+
264
+ static _InternetAddress ? tryParse (String address) {
265
+ checkNotNullable (address, "address" );
266
+ final parsedAddress = _parseAddressString (address);
267
+ if (parsedAddress is _InternetAddress ) {
268
+ return parsedAddress;
269
+ } else {
270
+ assert (parsedAddress is ArgumentError );
271
+ return null ;
272
+ }
273
+ }
274
+
253
275
factory _InternetAddress .fromRawAddress (Uint8List rawAddress,
254
276
{InternetAddressType ? type}) {
255
277
if (type == InternetAddressType .unix) {
@@ -273,15 +295,6 @@ class _InternetAddress implements InternetAddress {
273
295
}
274
296
}
275
297
276
- static _InternetAddress ? tryParse (String address) {
277
- checkNotNullable (address, "address" );
278
- try {
279
- return _InternetAddress .fromString (address);
280
- } on ArgumentError catch (_) {
281
- return null ;
282
- }
283
- }
284
-
285
298
factory _InternetAddress .fixed (int id) {
286
299
switch (id) {
287
300
case _addressLoopbackIPv4:
0 commit comments