Skip to content

Commit 58a4dda

Browse files
committed
feat: optimise format phone number
1 parent e3bfde3 commit 58a4dda

File tree

2 files changed

+8
-10
lines changed

2 files changed

+8
-10
lines changed

csharp/PhoneNumbers.PerformanceTest/README.md

+4
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,7 @@ Below you can see a sample of what the results might look like
1010
| FormatPhoneNumber | .NET 8.0 | .NET 8.0 | 1.141 us | 0.0080 us | 0.0071 us | 0.0076 | 152 B |
1111
| FormatPhoneNumber | .NET Framework 4.8 | .NET Framework 4.8 | 2.746 us | 0.0114 us | 0.0101 us | 0.1335 | 851 B |
1212

13+
## Running performance tests
14+
```bash
15+
dotnet run -c Release --framework net8.0
16+
```

csharp/PhoneNumbers/PhoneNumberUtil.net.cs

+4-10
Original file line numberDiff line numberDiff line change
@@ -687,11 +687,8 @@ private void Format(ref Span<char> span, ref int index, PhoneNumber number, Phon
687687
// Early exit for E164 case since no formatting of the national number needs to be applied.
688688
// Extensions are not formatted.
689689
PrefixNumberWithCountryCallingCode(ref span, ref index, countryCallingCode, PhoneNumberFormat.E164);
690-
for (var i = 0; i < nationalSignificantNumberLength; i++)
691-
{
692-
span[index++] = nationalSignificantNumber[i];
693-
}
694-
690+
nationalSignificantNumber.Slice(0, nationalSignificantNumberLength).CopyTo(span.Slice(index));
691+
index += nationalSignificantNumberLength;
695692
return;
696693
}
697694

@@ -701,11 +698,8 @@ private void Format(ref Span<char> span, ref int index, PhoneNumber number, Phon
701698
var regionCode = GetRegionCodeForCountryCode(countryCallingCode);
702699
if (!HasValidCountryCallingCode(countryCallingCode))
703700
{
704-
for (var i = 0; i < nationalSignificantNumberLength; i++)
705-
{
706-
span[index++] = nationalSignificantNumber[i];
707-
}
708-
701+
nationalSignificantNumber.Slice(0, nationalSignificantNumberLength).CopyTo(span.Slice(index));
702+
index += nationalSignificantNumberLength;
709703
return;
710704
}
711705

0 commit comments

Comments
 (0)