Skip to content

Commit

Permalink
Address PR feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
pentp committed Jul 25, 2023
1 parent b740562 commit c7573f3
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 2 deletions.
4 changes: 4 additions & 0 deletions csharp/PhoneNumbers/PhoneNumberUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.IO;
using System.Linq;
Expand Down Expand Up @@ -178,6 +179,9 @@ private static char MapAllPlusNumberGroupingSymbols(char c)
private static Regex SeparatorPattern() => _separatorPattern;
#endif

[Obsolete("This is an internal implementation detail not meant for public use", error: true), EditorBrowsable(EditorBrowsableState.Never)]
public static readonly Regex ValidStartCharPattern;

// We use this pattern to check if the phone number has at least three letters in it - if so, then
// we treat it as a number where some phone-number digits are represented by letters.
private static bool IsValidAlphaPhone(StringBuilder number)
Expand Down
16 changes: 14 additions & 2 deletions csharp/PhoneNumbers/PhoneRegex.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@

using System;
using System.Collections.Concurrent;
using System.ComponentModel;
using System.Text.RegularExpressions;

namespace PhoneNumbers
{
[EditorBrowsable(EditorBrowsableState.Never)]
public sealed class PhoneRegex
{
private readonly string pattern;
Expand All @@ -29,9 +31,9 @@ public sealed class PhoneRegex

private static readonly ConcurrentDictionary<string, PhoneRegex> cache = new();

public static PhoneRegex Get(string regex) => cache.GetOrAdd(regex, k => new PhoneRegex(k));
internal static PhoneRegex Get(string regex) => cache.GetOrAdd(regex, k => new PhoneRegex(k));

private PhoneRegex(string pattern)
public PhoneRegex(string pattern)
{
this.pattern = pattern;

Expand All @@ -40,6 +42,16 @@ private PhoneRegex(string pattern)
beginRegex = new Lazy<Regex>(() => new Regex($"^(?:{this.pattern})", RegexOptions.CultureInvariant), true);
}

[Obsolete("This is an internal implementation detail not meant for public use")]
public PhoneRegex(string pattern, RegexOptions options)
{
this.pattern = pattern;

regex = new Lazy<Regex>(() => new Regex(pattern, options), true);
allRegex = new Lazy<Regex>(() => new Regex($"^(?:{pattern})$", options), true);
beginRegex = new Lazy<Regex>(() => new Regex($"^(?:{pattern})", options), true);
}

public bool IsMatch(string value) => regex.Value.IsMatch(value);
public Match Match(string value) => regex.Value.Match(value);
public string Replace(string value, string replacement) => regex.Value.Replace(value, replacement);
Expand Down
29 changes: 29 additions & 0 deletions csharp/PhoneNumbers/RegexCache.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright (C) 2009 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

using System;
using System.ComponentModel;

namespace PhoneNumbers
{
[Obsolete("This is an internal implementation detail not meant for public use"), EditorBrowsable(EditorBrowsableState.Never)]
public class RegexCache
{
public RegexCache(int size) { }
public PhoneRegex GetPatternForRegex(string regex) => PhoneRegex.Get(regex);
public bool ContainsRegex(string regex) => false;
}
}

0 comments on commit c7573f3

Please sign in to comment.