Skip to content

Commit 0a2d83c

Browse files
committed
9.0.28
1 parent 5d383e0 commit 0a2d83c

19 files changed

Lines changed: 437 additions & 30 deletions

‎CHANGELOG.md‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
1+
## 9.0.28
2+
3+
- Fix multi digit group references (`$10`, `$11`, ...) being corrupted when a
4+
national number is transformed (a `$10` reference used to be partially
5+
replaced by the `$1` reference).
6+
- Throw a `PhoneNumberException` with `Code.inputIsTooLong` when a phone number
7+
contains more digits than the maximum supported length (country calling code
8+
plus national significant number).
9+
- Throw a `PhoneNumberException` with `Code.invalid` instead of `Code.notFound`
10+
when a phone number contains no digit at all.
11+
- Remove the unreachable fallback in the destination metadata lookup.
12+
- Rename the generated `metadataLenghtsByIsoCode` map to
13+
`metadataLengthsByIsoCode` (the misspelled name is kept as a deprecated alias).
14+
- Remove the unused `Constants.slashSeparatedDates`.
15+
- Add a round trip test suite covering every metadata example number, and a test
16+
asserting no country calling code is the prefix of another one.
17+
118
## 9.0.27
219
- Upgrade metadata
320

‎example/lib/main.dart‎

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,27 @@
1+
import 'dart:io';
2+
13
import 'package:phone_numbers_parser/phone_numbers_parser.dart';
24

3-
void main(List<String> arguments) {
5+
void main(List<String> arguments) async {
6+
print('Memory before allocations: ${ProcessInfo.currentRss ~/ 1024} KB');
7+
exampleUsage();
8+
await Future.delayed(Duration(seconds: 1));
9+
await Future.delayed(Duration(seconds: 1));
10+
await Future.delayed(Duration(seconds: 1));
11+
await Future.delayed(Duration(seconds: 1));
12+
await Future.delayed(Duration(seconds: 1));
13+
await Future.delayed(Duration(seconds: 1));
14+
await Future.delayed(Duration(seconds: 1));
15+
await Future.delayed(Duration(seconds: 1));
16+
await Future.delayed(Duration(seconds: 1));
17+
18+
await Future.delayed(Duration(seconds: 100));
19+
stdin.readLineSync();
20+
21+
print('Memory after allocations: ${ProcessInfo.currentRss ~/ 1024} KB');
22+
}
23+
24+
void exampleUsage() {
425
final frPhone0 = PhoneNumber.parse('+33 655 5705 76');
526
final inPhone0 = PhoneNumber.parse('+919955059057');
627
print(inPhone0);
@@ -71,4 +92,6 @@ void main(List<String> arguments) {
7192
two - 1 == one;
7293
final another = one + 2;
7394
print('$another == $three');
95+
96+
print('Memory after allocations: ${ProcessInfo.currentRss ~/ 1024} KB');
7497
}

‎lib/src/metadata/generated/metadata_lengths_by_iso_code.dart‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import "../../iso_codes/iso_code.dart";
22
import "../models/phone_metadata_lengths.dart";
33

4-
const metadataLenghtsByIsoCode = {
4+
const metadataLengthsByIsoCode = {
55
IsoCode.AC: PhoneMetadataLengths(
66
general: [],
77
mobile: [5],
@@ -3188,3 +3188,6 @@ const metadataLenghtsByIsoCode = {
31883188
voiceMail: [],
31893189
),
31903190
};
3191+
// ignore: constant_identifier_names
3192+
@Deprecated('Use metadataLengthsByIsoCode instead')
3193+
const metadataLenghtsByIsoCode = metadataLengthsByIsoCode;

‎lib/src/metadata/metadata_finder.dart‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ abstract class MetadataFinder {
3838
}
3939

4040
static PhoneMetadataLengths findMetadataLengthForIsoCode(IsoCode isoCode) {
41-
final metadata = metadataLenghtsByIsoCode[isoCode];
41+
final metadata = metadataLengthsByIsoCode[isoCode];
4242
if (metadata == null) {
4343
throw PhoneNumberException(
4444
code: Code.invalidIsoCode,

‎lib/src/parsers/_country_code_parser.dart‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ abstract class CountryCodeParser {
99
static (String countryCode, String nsn) extractCountryCode(
1010
String phoneNumber,
1111
) {
12+
if (phoneNumber.isEmpty) {
13+
throw PhoneNumberException(
14+
code: Code.invalid,
15+
description: 'no digits found in the phone number',
16+
);
17+
}
1218
final maxCountryCodeLength = min(
1319
phoneNumber.length,
1420
Constants.maxLengthCountryCallingCode,

‎lib/src/parsers/_national_number_parser.dart‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,19 @@ abstract class NationalNumberParser {
7171
return appliedTo.substring(match.end);
7272
}
7373

74+
// replace the group references from the highest group to the lowest one.
75+
// Doing so prevents a multi digit reference such as `$10` from being
76+
// partially replaced by the `$1` replacement (which would turn `$10` into
77+
// `<group1>0` instead of `<group10>`).
7478
var transformed = transformRule;
75-
bool shouldContinueLoop(int i) =>
76-
match.groupCount >= i && match.group(i) != null;
77-
for (var i = 1; shouldContinueLoop(i); i++) {
79+
var highestGroup = 0;
80+
for (var i = 1; i <= match.groupCount; i++) {
81+
// a group can be null despite the group count being higher,
82+
// in that case the reference is left untouched (as before).
83+
if (match.group(i) == null) break;
84+
highestGroup = i;
85+
}
86+
for (var i = highestGroup; i >= 1; i--) {
7887
transformed = transformed.replaceFirst('\$$i', match.group(i)!);
7988
}
8089
return transformed + appliedTo.substring(match.end);

‎lib/src/parsers/_text_parser.dart‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ abstract class TextParser {
66
///
77
/// It also converts easthern arabic digits to westhern arabic.
88
///
9+
/// Note that any character that is not a digit, a `+` sign or one of the
10+
/// accepted separators (whitespace, parentheses, brackets, dash, dot, slash)
11+
/// is dropped. Passing a phone number containing letters will therefore
12+
/// silently drop those letters, e.g. `1-800-FLOWERS` normalizes to `1800`.
13+
///
914
/// Example:
1015
/// [unformatedPhoneNumber]: (+32) 0489/99.99.99
1116
/// Returns: +320489999999

‎lib/src/parsers/base_phone_parser.dart‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import '_text_parser.dart';
88
/// 478 88 88 88
99
/// {@endtemplate}
1010
11-
/// base class for PhoneParser and LightPhoneParser
11+
/// base class for phone number parsers
1212
abstract class BasePhoneParser {
1313
/// parses a [phoneNumber] given an [isoCode]
1414
///

‎lib/src/parsers/phone_parser.dart‎

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import 'package:phone_numbers_parser/src/validation/validator.dart';
44
import '../../phone_numbers_parser.dart';
55
import '../metadata/metadata_finder.dart';
66
import '../metadata/models/phone_metadata.dart';
7+
import '../regex/constants.dart';
78
import '_country_code_parser.dart';
89
import '_international_prefix_parser.dart';
910
import '_national_number_parser.dart';
@@ -37,6 +38,18 @@ abstract class PhoneParser {
3738
IsoCode? destinationCountry,
3839
}) {
3940
phoneNumber = TextParser.normalizePhoneNumber(phoneNumber);
41+
// a phone number can never be longer than the maximum country calling code
42+
// length plus the maximum national significant number length.
43+
final maxLength =
44+
Constants.maxLengthCountryCallingCode + Constants.maxLengthNsn;
45+
final digitCount = phoneNumber.replaceAll('+', '').length;
46+
if (digitCount > maxLength) {
47+
throw PhoneNumberException(
48+
code: Code.inputIsTooLong,
49+
description:
50+
'phone number has $digitCount digits, maximum is $maxLength',
51+
);
52+
}
4053
final callerMetadata = callerCountry != null
4154
? MetadataFinder.findMetadataForIsoCode(callerCountry)
4255
: null;
@@ -124,13 +137,12 @@ abstract class PhoneParser {
124137
// if no caller was provided we need to make a best guess given the country code
125138
final (countryCode, nsn) =
126139
CountryCodeParser.extractCountryCode(phoneWithoutExitCode);
127-
// multiple countries use the same country code
140+
// extractCountryCode only returns once it has found metadata matching both
141+
// the country code and the national number, so this lookup is guaranteed to
142+
// find metadata as well.
128143
final metadata =
129144
MetadataFinder.findMetadataForCountryCode(countryCode, nsn);
130-
131-
return metadata ??
132-
callerMetadata ??
133-
// default if nothing was found.
134-
MetadataFinder.findMetadataForIsoCode(IsoCode.US);
145+
assert(metadata != null, 'no metadata found for country code $countryCode');
146+
return metadata!;
135147
}
136148
}

‎lib/src/regex/constants.dart‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,6 @@ class Constants {
3131
static final String possiblePhoneNumber =
3232
'[$_plus$_digits](?:[$_punctuation]{0,3}[$_digits]){6,}';
3333

34-
/// Matches strings that look like dates using "/" as a separator. Examples: 3/10/2011, 31/10/96 or
35-
/// 08/31/95.
36-
static final Pattern slashSeparatedDates =
37-
r'(?:(?:[0-3]?\\d/[01]?\\d)|(?:[01]?\\d/[0-3]?\\d))/(?:[12]\\d)?\\d{2}';
3834
// Replace Easthern to Westhern arabic numbers https://en.wikipedia.org/wiki/Eastern_Arabic_numerals
3935
static Map<String, String> allNormalizationMappings = {
4036
'+': '+',

0 commit comments

Comments
 (0)