Skip to content

style: idiomatic cleanups in AsYouTypeFormatter and PhoneNumberMatcher - #416

Merged
twcclegg merged 2 commits into
mainfrom
style/matcher-formatter
Aug 26, 2026
Merged

style: idiomatic cleanups in AsYouTypeFormatter and PhoneNumberMatcher#416
twcclegg merged 2 commits into
mainfrom
style/matcher-formatter

Conversation

@twcclegg

Copy link
Copy Markdown
Owner

Summary

Part of an ongoing sweep bringing every C# file up to more idiomatic C# conventions (same spirit as #409), without straying from the Java source's method structure.

  • string.Equals(string) -> ==/!= in both files (the Java source needs .equals() since Java has no operator overloading for strings; the direct port carried that over unnecessarily).
  • AsYouTypeFormatter.MinLeadingDigitsLength: static readonly int -> const int (it mirrors Java's static final int MIN_LEADING_DIGITS_LENGTH = 3, a true compile-time constant, and matches the file's other const fields).
  • AsYouTypeFormatter: made a couple of StringBuilder + string concatenations use explicit .ToString() instead of relying on the compiler's implicit ToString() call via the string operator +(object, string) overload — same behavior, clearer intent.
  • PhoneNumberMatcher: fixed a couple of spots that had drifted to K&R brace style to match the file's dominant Allman style; dropped a stray double blank line in AsYouTypeFormatter.

No behavior changes. Not touching the Java-parity fixes from #408 in these same two files.

Test plan

  • dotnet build csharp/PhoneNumbers.slnx — clean, 0 warnings (TreatWarningsAsErrors)
  • dotnet test csharp/PhoneNumbers.Test/PhoneNumbers.Test.csproj — 414/414 passing on net8.0 and net10.0

- string.Equals(string) -> == / != (idiomatic C#; Java's port required
  .equals() since Java lacks operator overloading for strings)
- MinLeadingDigitsLength: static readonly int -> const int (matches
  Java's static final int, and the file's other true constants)
- Make StringBuilder-to-string concatenation explicit with .ToString()
  instead of relying on the compiler's implicit ToString() via the
  string + object overload
- Match the file's dominant Allman brace style in a couple of spots
  that had drifted to K&R
- Drop a stray double blank line
@codecov

codecov Bot commented Aug 25, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 87.43%. Comparing base (e75ae83) to head (dbde729).
⚠️ Report is 6 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #416      +/-   ##
==========================================
+ Coverage   87.42%   87.43%   +0.01%     
==========================================
  Files          41       41              
  Lines        3856     3852       -4     
  Branches      990      991       +1     
==========================================
- Hits         3371     3368       -3     
+ Misses        283      282       -1     
  Partials      202      202              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Comment thread csharp/PhoneNumbers/AsYouTypeFormatter.cs Fixed
Comment thread csharp/PhoneNumbers/AsYouTypeFormatter.cs Fixed
Comment thread csharp/PhoneNumbers/AsYouTypeFormatter.cs Fixed
@github-actions

github-actions Bot commented Aug 25, 2026

Copy link
Copy Markdown

📊 Benchmark Results

Commit: dbde729 · Full run · Linux ubuntu-24.04-arm

Both sides were measured on the same runner in the same job, so the numbers are
comparable. Treat sub-percent differences as noise.

PR branch

BenchmarkDotNet v0.15.8, Linux Ubuntu 24.04.4 LTS (Noble Numbat)
Neoverse-N2, 4 physical cores
.NET SDK 10.0.400
  [Host]    : .NET 10.0.11 (10.0.11, 10.0.1126.37416), Arm64 RyuJIT armv8.0-a
  .NET 10.0 : .NET 10.0.11 (10.0.11, 10.0.1126.37416), Arm64 RyuJIT armv8.0-a

Job=.NET 10.0  Runtime=.NET 10.0  

Method PhoneNumberCount Mean Error StdDev Gen0 Allocated
InputDigitPerKeystroke 1000 4.384 ms 0.0345 ms 0.0306 ms 54.6875 3.88 MB

BenchmarkDotNet v0.15.8, Linux Ubuntu 24.04.4 LTS (Noble Numbat)
Neoverse-N2, 4 physical cores
.NET SDK 10.0.400
  [Host]     : .NET 10.0.11 (10.0.11, 10.0.1126.37416), Arm64 RyuJIT armv8.0-a
  Job-AMQORM : .NET 10.0.11 (10.0.11, 10.0.1126.37416), Arm64 RyuJIT armv8.0-a

Runtime=.NET 10.0  InvocationCount=1  IterationCount=20  
LaunchCount=1  RunStrategy=ColdStart  UnrollFactor=1  
WarmupCount=1  

Method Mean Error StdDev Allocated
CreateInstance 375.2 μs 97.59 μs 112.4 μs 119.48 KB
CreateInstanceAndLoadAllRegions 7,025.5 μs 358.00 μs 412.3 μs 1620.34 KB
FirstRegionLookup 454.9 μs 138.04 μs 159.0 μs 124.54 KB

BenchmarkDotNet v0.15.8, Linux Ubuntu 24.04.4 LTS (Noble Numbat)
Neoverse-N2, 4 physical cores
.NET SDK 10.0.400
  [Host]    : .NET 10.0.11 (10.0.11, 10.0.1126.37416), Arm64 RyuJIT armv8.0-a
  .NET 10.0 : .NET 10.0.11 (10.0.11, 10.0.1126.37416), Arm64 RyuJIT armv8.0-a

Job=.NET 10.0  Runtime=.NET 10.0  

Method PhoneNumberCount Mean Error StdDev Gen0 Allocated
ExtractPossibleNumber_CleanInput 1000 21.61 μs 0.020 μs 0.017 μs - -
ExtractPossibleNumber_WithLeadingJunk 1000 38.44 μs 0.120 μs 0.113 μs 0.6714 48360 B

BenchmarkDotNet v0.15.8, Linux Ubuntu 24.04.4 LTS (Noble Numbat)
Neoverse-N2, 4 physical cores
.NET SDK 10.0.400
  [Host]    : .NET 10.0.11 (10.0.11, 10.0.1126.37416), Arm64 RyuJIT armv8.0-a
  .NET 10.0 : .NET 10.0.11 (10.0.11, 10.0.1126.37416), Arm64 RyuJIT armv8.0-a

Job=.NET 10.0  Runtime=.NET 10.0  

Method PhoneNumberCount Mean Error StdDev Gen0 Allocated
FindNumbers_Valid 100 143.1 μs 0.35 μs 0.33 μs 0.9766 70.71 KB
FindNumbers_StrictGrouping 100 324.1 μs 6.44 μs 6.89 μs 1.4648 124.84 KB

BenchmarkDotNet v0.15.8, Linux Ubuntu 24.04.4 LTS (Noble Numbat)
Neoverse-N2, 4 physical cores
.NET SDK 10.0.400
  [Host]    : .NET 10.0.11 (10.0.11, 10.0.1126.37416), Arm64 RyuJIT armv8.0-a
  .NET 10.0 : .NET 10.0.11 (10.0.11, 10.0.1126.37416), Arm64 RyuJIT armv8.0-a

Job=.NET 10.0  Runtime=.NET 10.0  

Method PhoneNumberCount Mean Error StdDev Gen0 Allocated
GetDescriptionForNumber 1000 1,420.17 μs 4.616 μs 4.092 μs 1.9531 196.59 KB
GetDisplayCountry 1000 18.59 μs 0.039 μs 0.035 μs 0.0916 7.56 KB

BenchmarkDotNet v0.15.8, Linux Ubuntu 24.04.4 LTS (Noble Numbat)
Neoverse-N2, 4 physical cores
.NET SDK 10.0.400
  [Host]    : .NET 10.0.11 (10.0.11, 10.0.1126.37416), Arm64 RyuJIT armv8.0-a
  .NET 10.0 : .NET 10.0.11 (10.0.11, 10.0.1126.37416), Arm64 RyuJIT armv8.0-a

Job=.NET 10.0  Runtime=.NET 10.0  

Method PhoneNumberCount Mean Error StdDev Gen0 Allocated
ParseValidateAndFormatPhoneNumbers 1000 2,490.8 μs 4.29 μs 3.81 μs 7.8125 577.71 KB
ParseOnly 1000 438.5 μs 0.66 μs 0.62 μs 4.8828 348.13 KB
ParseNationalFormat 1000 772.2 μs 1.45 μs 1.35 μs 5.8594 431.36 KB
ParseWithExtension 1000 1,133.6 μs 5.16 μs 4.83 μs 13.6719 957.38 KB
ValidateOnly 1000 713.3 μs 1.08 μs 0.95 μs - 41.11 KB
FormatOnly 1000 1,059.8 μs 2.68 μs 2.51 μs 1.9531 187.92 KB
PR base

BenchmarkDotNet v0.15.8, Linux Ubuntu 24.04.4 LTS (Noble Numbat)
Neoverse-N2, 4 physical cores
.NET SDK 10.0.400
  [Host]    : .NET 10.0.11 (10.0.11, 10.0.1126.37416), Arm64 RyuJIT armv8.0-a
  .NET 10.0 : .NET 10.0.11 (10.0.11, 10.0.1126.37416), Arm64 RyuJIT armv8.0-a

Job=.NET 10.0  Runtime=.NET 10.0  

Method PhoneNumberCount Mean Error StdDev Gen0 Allocated
InputDigitPerKeystroke 1000 4.380 ms 0.0153 ms 0.0144 ms 54.6875 3.88 MB

BenchmarkDotNet v0.15.8, Linux Ubuntu 24.04.4 LTS (Noble Numbat)
Neoverse-N2, 4 physical cores
.NET SDK 10.0.400
  [Host]     : .NET 10.0.11 (10.0.11, 10.0.1126.37416), Arm64 RyuJIT armv8.0-a
  Job-AMQORM : .NET 10.0.11 (10.0.11, 10.0.1126.37416), Arm64 RyuJIT armv8.0-a

Runtime=.NET 10.0  InvocationCount=1  IterationCount=20  
LaunchCount=1  RunStrategy=ColdStart  UnrollFactor=1  
WarmupCount=1  

Method Mean Error StdDev Allocated
CreateInstance 400.2 μs 114.3 μs 131.6 μs 119.48 KB
CreateInstanceAndLoadAllRegions 7,076.9 μs 402.0 μs 462.9 μs 1620.34 KB
FirstRegionLookup 449.7 μs 144.5 μs 166.4 μs 124.54 KB

BenchmarkDotNet v0.15.8, Linux Ubuntu 24.04.4 LTS (Noble Numbat)
Neoverse-N2, 4 physical cores
.NET SDK 10.0.400
  [Host]    : .NET 10.0.11 (10.0.11, 10.0.1126.37416), Arm64 RyuJIT armv8.0-a
  .NET 10.0 : .NET 10.0.11 (10.0.11, 10.0.1126.37416), Arm64 RyuJIT armv8.0-a

Job=.NET 10.0  Runtime=.NET 10.0  

Method PhoneNumberCount Mean Error StdDev Gen0 Allocated
ExtractPossibleNumber_CleanInput 1000 20.91 μs 0.013 μs 0.011 μs - -
ExtractPossibleNumber_WithLeadingJunk 1000 38.08 μs 0.064 μs 0.060 μs 0.6714 48360 B

BenchmarkDotNet v0.15.8, Linux Ubuntu 24.04.4 LTS (Noble Numbat)
Neoverse-N2, 4 physical cores
.NET SDK 10.0.400
  [Host]    : .NET 10.0.11 (10.0.11, 10.0.1126.37416), Arm64 RyuJIT armv8.0-a
  .NET 10.0 : .NET 10.0.11 (10.0.11, 10.0.1126.37416), Arm64 RyuJIT armv8.0-a

Job=.NET 10.0  Runtime=.NET 10.0  

Method PhoneNumberCount Mean Error StdDev Gen0 Allocated
FindNumbers_Valid 100 142.9 μs 0.32 μs 0.30 μs 0.9766 70.71 KB
FindNumbers_StrictGrouping 100 314.4 μs 0.53 μs 0.47 μs 1.4648 124.84 KB

BenchmarkDotNet v0.15.8, Linux Ubuntu 24.04.4 LTS (Noble Numbat)
Neoverse-N2, 4 physical cores
.NET SDK 10.0.400
  [Host]    : .NET 10.0.11 (10.0.11, 10.0.1126.37416), Arm64 RyuJIT armv8.0-a
  .NET 10.0 : .NET 10.0.11 (10.0.11, 10.0.1126.37416), Arm64 RyuJIT armv8.0-a

Job=.NET 10.0  Runtime=.NET 10.0  

Method PhoneNumberCount Mean Error StdDev Gen0 Allocated
GetDescriptionForNumber 1000 1,481.16 μs 4.221 μs 3.525 μs 1.9531 196.59 KB
GetDisplayCountry 1000 18.60 μs 0.034 μs 0.032 μs 0.0916 7.56 KB

BenchmarkDotNet v0.15.8, Linux Ubuntu 24.04.4 LTS (Noble Numbat)
Neoverse-N2, 4 physical cores
.NET SDK 10.0.400
  [Host]    : .NET 10.0.11 (10.0.11, 10.0.1126.37416), Arm64 RyuJIT armv8.0-a
  .NET 10.0 : .NET 10.0.11 (10.0.11, 10.0.1126.37416), Arm64 RyuJIT armv8.0-a

Job=.NET 10.0  Runtime=.NET 10.0  

Method PhoneNumberCount Mean Error StdDev Gen0 Allocated
ParseValidateAndFormatPhoneNumbers 1000 2,666.8 μs 3.66 μs 3.06 μs 7.8125 577.71 KB
ParseOnly 1000 440.5 μs 0.33 μs 0.29 μs 4.8828 348.13 KB
ParseNationalFormat 1000 777.7 μs 1.58 μs 1.40 μs 5.8594 431.36 KB
ParseWithExtension 1000 1,115.7 μs 2.13 μs 1.89 μs 13.6719 957.38 KB
ValidateOnly 1000 749.9 μs 1.55 μs 1.37 μs - 41.11 KB
FormatOnly 1000 1,084.4 μs 2.56 μs 2.39 μs 1.9531 187.92 KB

CodeQL flagged the two spots where this PR added explicit .ToString()
on prefixBeforeNationalNumber: the other operand in each expression is
already a string, so the compiler's string+object overload calls
ToString() implicitly anyway. Revert those two to the implicit form —
also more consistent with the PR's own idiomatic-cleanup goal.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
@twcclegg
twcclegg merged commit 8bac409 into main Aug 26, 2026
8 checks passed
@twcclegg
twcclegg deleted the style/matcher-formatter branch August 26, 2026 14:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants