Skip to content

Commit

Permalink
Skip builders internally. Reduce allocations.
Browse files Browse the repository at this point in the history
  • Loading branch information
pentp committed Jul 31, 2023
1 parent becdc17 commit 30c1eff
Show file tree
Hide file tree
Showing 20 changed files with 693 additions and 1,057 deletions.
10 changes: 5 additions & 5 deletions csharp/PhoneNumbers.Test/TestAreaCodeMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,25 +159,25 @@ public void TestLookupNumber_CH()
[Fact]
public void TestLookupNumber_IT()
{
var number = new PhoneNumber.Builder().SetCountryCode(39).SetNationalNumber(212345678L).SetItalianLeadingZero(true)
var number = new PhoneNumber.Builder().SetCountryCode(39).SetNationalNumber(212345678L).SetNumberOfLeadingZeros(1)
.Build();
Assert.Equal("Milan", areaCodeMapForIT.Lookup(number));

number = new PhoneNumber.Builder().SetCountryCode(39).SetNationalNumber(612345678L).SetItalianLeadingZero(true)
number = new PhoneNumber.Builder().SetCountryCode(39).SetNationalNumber(612345678L).SetNumberOfLeadingZeros(1)
.Build();
Assert.Equal("Rome", areaCodeMapForIT.Lookup(number));

number = new PhoneNumber.Builder().SetCountryCode(39).SetNationalNumber(3211234L).SetItalianLeadingZero(true)
number = new PhoneNumber.Builder().SetCountryCode(39).SetNationalNumber(3211234L).SetNumberOfLeadingZeros(1)
.Build();
Assert.Equal("Novara", areaCodeMapForIT.Lookup(number));

// A mobile number
number = new PhoneNumber.Builder().SetCountryCode(39).SetNationalNumber(321123456L).SetItalianLeadingZero(false)
number = new PhoneNumber.Builder().SetCountryCode(39).SetNationalNumber(321123456L).ClearNumberOfLeadingZeros()
.Build();
Assert.Null(areaCodeMapForIT.Lookup(number));

// An invalid number (too short)
number = new PhoneNumber.Builder().SetCountryCode(39).SetNationalNumber(321123L).SetItalianLeadingZero(true)
number = new PhoneNumber.Builder().SetCountryCode(39).SetNationalNumber(321123L).SetNumberOfLeadingZeros(1)
.Build();
Assert.Equal("Novara", areaCodeMapForIT.Lookup(number));
}
Expand Down
6 changes: 3 additions & 3 deletions csharp/PhoneNumbers.Test/TestMetadataFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -799,7 +799,7 @@ public void TestFilterMetadata_LiteBuild()
{
var metadata = FakeArmeniaPhoneMetadata();

MetadataFilter.ForLiteBuild().FilterMetadata(metadata);
MetadataFilter.ForLiteBuild().FilterMetadata(metadata.MessageBeingBuilt);

// id, country_code, and international_prefix should never be cleared.
Assert.Equal(ID, metadata.Id);
Expand Down Expand Up @@ -831,7 +831,7 @@ public void TestFilterMetadata_SpecialBuild()
{
var metadata = FakeArmeniaPhoneMetadata();

MetadataFilter.ForSpecialBuild().FilterMetadata(metadata);
MetadataFilter.ForSpecialBuild().FilterMetadata(metadata.MessageBeingBuilt);

// id, country_code, and international_prefix should never be cleared.
Assert.Equal(ID, metadata.Id);
Expand Down Expand Up @@ -877,7 +877,7 @@ public void TestFilterMetadata_EmptyFilter()
{
var metadata = FakeArmeniaPhoneMetadata();

MetadataFilter.EmptyFilter().FilterMetadata(metadata);
MetadataFilter.EmptyFilter().FilterMetadata(metadata.MessageBeingBuilt);

// None of the fields should be cleared.
Assert.Equal(ID, metadata.Id);
Expand Down
61 changes: 11 additions & 50 deletions csharp/PhoneNumbers.Test/TestPhoneNumberUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public TestPhoneNumberUtil()
new PhoneNumber.Builder().SetCountryCode(39).SetNationalNumber(345678901L).Build();
private static readonly PhoneNumber ITNumber =
new PhoneNumber.Builder().SetCountryCode(39).SetNationalNumber(236618300L).
SetItalianLeadingZero(true).Build();
SetNumberOfLeadingZeros(1).Build();
private static readonly PhoneNumber JPStarNumber =
new PhoneNumber.Builder().SetCountryCode(81).SetNationalNumber(2345).Build();
// Numbers to test the formatting rules from Mexico.
Expand Down Expand Up @@ -332,13 +332,9 @@ public void TestGetNationalSignificantNumber_ManyLeadingZeros()
var number = new PhoneNumber.Builder()
.SetCountryCode(1)
.SetNationalNumber(650)
.SetItalianLeadingZero(true)
.SetNumberOfLeadingZeros(2)
.Build();
Assert.Equal("00650", phoneUtil.GetNationalSignificantNumber(number));
// Set a bad value; we shouldn't crash, we shouldn't output any leading zeros at all.
number = Update(number).SetNumberOfLeadingZeros(-3).Build();
Assert.Equal("650", phoneUtil.GetNationalSignificantNumber(number));
}

[Fact]
Expand Down Expand Up @@ -831,7 +827,6 @@ public void TestFormatNumberForMobileDialing()
var auNumber = new PhoneNumber.Builder()
.SetCountryCode(61)
.SetNationalNumber(0L)
.SetItalianLeadingZero(true)
.SetNumberOfLeadingZeros(2)
.Build();
Assert.Equal("000", phoneUtil.FormatNumberForMobileDialing(auNumber, RegionCode.AU, false));
Expand Down Expand Up @@ -1382,9 +1377,9 @@ public void TestTruncateTooLongNumber()

// IT number 022 3456 7890, but entered with 3 extra digits at the end.
tooLongNumber = new PhoneNumber.Builder()
.SetCountryCode(39).SetNationalNumber(2234567890123L).SetItalianLeadingZero(true);
.SetCountryCode(39).SetNationalNumber(2234567890123L).SetNumberOfLeadingZeros(1);
validNumber = new PhoneNumber.Builder()
.SetCountryCode(39).SetNationalNumber(2234567890L).SetItalianLeadingZero(true);
.SetCountryCode(39).SetNationalNumber(2234567890L).SetNumberOfLeadingZeros(1);
Assert.True(phoneUtil.TruncateTooLongNumber(tooLongNumber));
AreEqual(validNumber, tooLongNumber);

Expand Down Expand Up @@ -1792,7 +1787,7 @@ public void TestParseNationalNumber()
// it's not interpreted as national prefix if the remaining number length is local-only in
// terms of length. Example: In GB, length 6-7 are only possible local-only.
shortNumber = new PhoneNumber.Builder().SetCountryCode(44).SetNationalNumber(123456)
.SetItalianLeadingZero(true).Build();
.SetNumberOfLeadingZeros(1).Build();
Assert.Equal(shortNumber, phoneUtil.Parse("0123456", RegionCode.GB));
}

Expand Down Expand Up @@ -2190,15 +2185,14 @@ public void TestParseItalianLeadingZeros()
var oneZero = new PhoneNumber.Builder()
.SetCountryCode(61)
.SetNationalNumber(11L)
.SetItalianLeadingZero(true)
.SetNumberOfLeadingZeros(1)
.Build();
Assert.Equal(oneZero, phoneUtil.Parse("011", RegionCode.AU));

// Test the number "001".
var twoZeros = new PhoneNumber.Builder()
.SetCountryCode(61)
.SetNationalNumber(1)
.SetItalianLeadingZero(true)
.SetNumberOfLeadingZeros(2)
.Build();
Assert.Equal(twoZeros, phoneUtil.Parse("001", RegionCode.AU));
Expand All @@ -2207,7 +2201,6 @@ public void TestParseItalianLeadingZeros()
var stillTwoZeros = new PhoneNumber.Builder()
.SetCountryCode(61)
.SetNationalNumber(0L)
.SetItalianLeadingZero(true)
.SetNumberOfLeadingZeros(2)
.Build();
Assert.Equal(stillTwoZeros, phoneUtil.Parse("000", RegionCode.AU));
Expand All @@ -2216,7 +2209,6 @@ public void TestParseItalianLeadingZeros()
var threeZeros = new PhoneNumber.Builder()
.SetCountryCode(61)
.SetNationalNumber(0L)
.SetItalianLeadingZero(true)
.SetNumberOfLeadingZeros(3)
.Build();
Assert.Equal(threeZeros, phoneUtil.Parse("0000", RegionCode.AU));
Expand Down Expand Up @@ -2305,47 +2297,16 @@ public void TestIsNumberMatchMatches()
public void TestIsNumberMatchShortMatchIfDiffNumLeadingZeros()
{
var nzNumberOne = new PhoneNumber.Builder()
.SetCountryCode(64).SetNationalNumber(33316005L).SetItalianLeadingZero(true).Build();
.SetCountryCode(64).SetNationalNumber(33316005L).SetNumberOfLeadingZeros(1).Build();
var nzNumberTwo = new PhoneNumber.Builder()
.SetCountryCode(64).SetNationalNumber(33316005L).SetItalianLeadingZero(true).SetNumberOfLeadingZeros(2).Build();
.SetCountryCode(64).SetNationalNumber(33316005L).SetNumberOfLeadingZeros(2).Build();
Assert.Equal(PhoneNumberUtil.MatchType.SHORT_NSN_MATCH, phoneUtil.IsNumberMatch(nzNumberOne, nzNumberTwo));

nzNumberOne = Update(nzNumberOne).SetItalianLeadingZero(false).SetNumberOfLeadingZeros(1).Build();
nzNumberTwo = Update(nzNumberTwo).SetItalianLeadingZero(true).SetNumberOfLeadingZeros(1).Build();
// Since one doesn't have the "italian_leading_zero" set to true, we ignore the number of
// leading zeros present (1 is in any case the default value).
nzNumberOne = Update(nzNumberOne).ClearNumberOfLeadingZeros().Build();
nzNumberTwo = Update(nzNumberTwo).SetNumberOfLeadingZeros(1).Build();
Assert.Equal(PhoneNumberUtil.MatchType.SHORT_NSN_MATCH, phoneUtil.IsNumberMatch(nzNumberOne, nzNumberTwo));
}

[Fact]
public void TestIsNumberMatchAcceptsProtoDefaultsAsMatch()
{
var nzNumberOne = new PhoneNumber.Builder()
.SetCountryCode(64).SetNationalNumber(33316005L).SetItalianLeadingZero(true).Build();
// The default for number_of_leading_zeros is 1, so it shouldn't normally be set, however if it
// is it should be considered equivalent.
var nzNumberTwo = new PhoneNumber.Builder()
.SetCountryCode(64).SetNationalNumber(33316005L).SetItalianLeadingZero(true).SetNumberOfLeadingZeros(1).Build();
Assert.Equal(PhoneNumberUtil.MatchType.EXACT_MATCH, phoneUtil.IsNumberMatch(nzNumberOne, nzNumberTwo));
}

[Fact]
public void TestIsNumberMatchMatchesDiffLeadingZerosIfItalianLeadingZeroFalse()
{
var nzNumberOne = new PhoneNumber.Builder()
.SetCountryCode(64).SetNationalNumber(33316005L).Build();
// The default for number_of_leading_zeros is 1, so it shouldn't normally be set, however if it
// is it should be considered equivalent.
var nzNumberTwo = new PhoneNumber.Builder()
.SetCountryCode(64).SetNationalNumber(33316005L).SetNumberOfLeadingZeros(1).Build();
Assert.Equal(PhoneNumberUtil.MatchType.EXACT_MATCH, phoneUtil.IsNumberMatch(nzNumberOne, nzNumberTwo));

// Even if it is set to ten, it is still equivalent because in both cases
// italian_leading_zero is not true.
nzNumberTwo = Update(nzNumberTwo).SetNumberOfLeadingZeros(10).Build();
Assert.Equal(PhoneNumberUtil.MatchType.EXACT_MATCH, phoneUtil.IsNumberMatch(nzNumberOne, nzNumberTwo));
}

[Fact]
public void TestIsNumberMatchNonMatches()
{
Expand Down Expand Up @@ -2457,13 +2418,13 @@ public void TestIsNumberMatchShortNsnMatches()
phoneUtil.IsNumberMatch("+64 3 331-6005", "3 331 6005#1234"));
// One has Italian leading zero, one does not.
var italianNumberOne = new PhoneNumber.Builder()
.SetCountryCode(39).SetNationalNumber(1234L).SetItalianLeadingZero(true).Build();
.SetCountryCode(39).SetNationalNumber(1234L).SetNumberOfLeadingZeros(1).Build();
var italianNumberTwo = new PhoneNumber.Builder()
.SetCountryCode(39).SetNationalNumber(1234L).Build();
Assert.Equal(PhoneNumberUtil.MatchType.SHORT_NSN_MATCH,
phoneUtil.IsNumberMatch(italianNumberOne, italianNumberTwo));
// One has an extension, the other has an extension of "".
italianNumberOne = Update(italianNumberOne).SetExtension("1234").ClearItalianLeadingZero().Build();
italianNumberOne = Update(italianNumberOne).SetExtension("1234").ClearNumberOfLeadingZeros().Build();
italianNumberTwo = Update(italianNumberTwo).SetExtension("").Build();
Assert.Equal(PhoneNumberUtil.MatchType.SHORT_NSN_MATCH,
phoneUtil.IsNumberMatch(italianNumberOne, italianNumberTwo));
Expand Down
2 changes: 1 addition & 1 deletion csharp/PhoneNumbers.Test/TestPhonenumber.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public void TestEqualWithCountryCodeSourceSet()
public void TestNonEqualWithItalianLeadingZeroSetToTrue()
{
var numberA = new PhoneNumber.Builder()
.SetCountryCode(1).SetNationalNumber(6502530000L).SetItalianLeadingZero(true).Build();
.SetCountryCode(1).SetNationalNumber(6502530000L).SetNumberOfLeadingZeros(1).Build();

var numberB = new PhoneNumber.Builder()
.SetCountryCode(1).SetNationalNumber(6502530000L).Build();
Expand Down
13 changes: 5 additions & 8 deletions csharp/PhoneNumbers/AsYouTypeFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,7 @@ public partial class AsYouTypeFormatter
// Character used when appropriate to separate a prefix, such as a long NDD or a country calling
// code, from the national number.
private const char SeparatorBeforeNationalNumber = ' ';
private static readonly PhoneMetadata EmptyMetadata =
new PhoneMetadata.Builder().SetInternationalPrefix("NA").BuildPartial();
private static readonly PhoneMetadata EmptyMetadata = new() { InternationalPrefix = "NA" };
private readonly PhoneMetadata defaultMetaData;
private PhoneMetadata currentMetadata;

Expand Down Expand Up @@ -172,8 +171,8 @@ private void GetAvailableFormats(string leadingDigits)
var isInternationalNumber = isCompleteNumber && extractedNationalPrefix.Length == 0;
var formatList =
isInternationalNumber && currentMetadata.IntlNumberFormatCount > 0
? currentMetadata.IntlNumberFormatList
: currentMetadata.NumberFormatList;
? currentMetadata.intlNumberFormat_
: currentMetadata.numberFormat_;
foreach (var format in formatList)
{
// Discard a few formats that we know are not relevant based on the presence of the national
Expand Down Expand Up @@ -598,12 +597,10 @@ private string RemoveNationalPrefixFromNationalNumber()
}
else if (currentMetadata.HasNationalPrefixForParsing)
{
var nationalPrefixForParsing =
PhoneRegex.Get(currentMetadata.NationalPrefixForParsing);
var m = nationalPrefixForParsing.MatchBeginning(nationalNumber.ToString());
var m = currentMetadata.MatchNationalPrefixForParsing(nationalNumber.ToString());
// Since some national prefix patterns are entirely optional, check that a national prefix
// could actually be extracted.
if (m.Length > 0)
if (m?.Length > 0)
{
// When the national prefix is detected, we use international formatting rules instead of
// national ones, because national formatting rules could contain local formatting rules
Expand Down
Loading

0 comments on commit 30c1eff

Please sign in to comment.