Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 2 additions & 8 deletions csharp/PhoneNumbers/AreaCodeMapStorageStrategy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,19 +60,13 @@ public abstract class AreaCodeMapStorageStrategy
/// The number of entries contained in the area code map.
/// </summary>
/// <returns>The number of entries contained in the area code map.</returns>
public int GetNumOfEntries()
{
return NumOfEntries;
}
public int GetNumOfEntries() => NumOfEntries;

/// <summary>
/// The set containing the possible lengths of prefixes.
/// </summary>
/// <returns>The set containing the possible lengths of prefixes.</returns>
public List<int> GetPossibleLengths()
{
return PossibleLengths;
}
public List<int> GetPossibleLengths() => PossibleLengths;

public override string ToString()
{
Expand Down
15 changes: 3 additions & 12 deletions csharp/PhoneNumbers/DefaultMapStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,11 @@ public class DefaultMapStorage : AreaCodeMapStorageStrategy
private int[] phoneNumberPrefixes;
private string[] descriptions;

public override int GetPrefix(int index)
{
return phoneNumberPrefixes[index];
}
public override int GetPrefix(int index) => phoneNumberPrefixes[index];

public override int GetStorageSize()
{
return phoneNumberPrefixes.Length * sizeof(int) + descriptions.Sum(d => d.Length);
}
public override int GetStorageSize() => phoneNumberPrefixes.Length * sizeof(int) + descriptions.Sum(d => d.Length);

public override string GetDescription(int index)
{
return descriptions[index];
}
public override string GetDescription(int index) => descriptions[index];

public override void ReadFromSortedMap(SortedDictionary<int, string> sortedAreaCodeMap)
{
Expand Down
43 changes: 10 additions & 33 deletions csharp/PhoneNumbers/FlyweightMapStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,10 @@ public class FlyweightMapStorage : AreaCodeMapStorageStrategy
// The number of bytes used to store a phone number prefix.
private int prefixSizeInBytes;

public override int GetPrefix(int index)
{
return ReadWordFromBuffer(phoneNumberPrefixes, prefixSizeInBytes, index);
}
public override int GetPrefix(int index) => ReadWordFromBuffer(phoneNumberPrefixes, prefixSizeInBytes, index);

public override int GetStorageSize()
{
return phoneNumberPrefixes.GetCapacity() + descriptionIndexes.GetCapacity()
+ descriptionPool.Sum(d => d.Length);
}
public override int GetStorageSize() =>
phoneNumberPrefixes.GetCapacity() + descriptionIndexes.GetCapacity() + descriptionPool.Sum(d => d.Length);

/// <summary>
/// This implementation returns the same string (same identity) when called for multiple indexes
Expand Down Expand Up @@ -124,10 +118,8 @@ private void CreateDescriptionPool(HashSet<string> descriptionsSet, SortedDictio
/// <summary>
/// Gets the minimum number of bytes that can be used to store the provided <c>value</c>.
/// </summary>
private static int GetOptimalNumberOfBytesForValue(int value)
{
return value <= short.MaxValue ? ShortNumBytes : IntNumBytes;
}
private static int GetOptimalNumberOfBytesForValue(int value) =>
value <= short.MaxValue ? ShortNumBytes : IntNumBytes;

/// <summary>
/// Stores the provided <c>value</c> to the provided byte <c>buffer</c> at the specified <c>index</c> using the provided <c>wordSize</c> in bytes. Note that only integer and short sizes are
Expand Down Expand Up @@ -176,30 +168,15 @@ public ByteBuffer(int size)
bytes = new byte[size];
}

public void PutShort(int offset, short value)
{
BinaryPrimitives.WriteInt16LittleEndian(bytes.AsSpan(offset), value);
}
public void PutShort(int offset, short value) => BinaryPrimitives.WriteInt16LittleEndian(bytes.AsSpan(offset), value);

public void PutInt(int offset, int value)
{
BinaryPrimitives.WriteInt32LittleEndian(bytes.AsSpan(offset), value);
}
public void PutInt(int offset, int value) => BinaryPrimitives.WriteInt32LittleEndian(bytes.AsSpan(offset), value);

public short GetShort(int offset)
{
return BinaryPrimitives.ReadInt16LittleEndian(bytes.AsSpan(offset));
}
public short GetShort(int offset) => BinaryPrimitives.ReadInt16LittleEndian(bytes.AsSpan(offset));

public int GetInt(int offset)
{
return BinaryPrimitives.ReadInt32LittleEndian(bytes.AsSpan(offset));
}
public int GetInt(int offset) => BinaryPrimitives.ReadInt32LittleEndian(bytes.AsSpan(offset));

public int GetCapacity()
{
return bytes.Length;
}
public int GetCapacity() => bytes.Length;
}
}
}
6 changes: 3 additions & 3 deletions csharp/PhoneNumbers/LeniencyExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using System;
using Leniency=PhoneNumbers.PhoneNumberUtil.Leniency;
using Leniency = PhoneNumbers.PhoneNumberUtil.Leniency;

namespace PhoneNumbers
{
public static class LeniencyExtensions
Expand All @@ -9,7 +9,7 @@ public static bool Verify(
PhoneNumber number,
string candidate,
PhoneNumberUtil util,
PhoneNumberMatcher matcher)=>
PhoneNumberMatcher matcher) =>
util.Verify(leniency, number, candidate, util, matcher);
}
}
2 changes: 1 addition & 1 deletion csharp/PhoneNumbers/LocaleData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ private static ImmutableDictionary<string, ImmutableDictionary<string, string>>
foreach (var country in LocaleNames.SupportedCountries())
{
var names = LocaleNames.ForCountry(country);
if (names != null)
if (names is not null)
builder[country] = names.ToImmutableDictionary();
}
return builder.ToImmutable();
Expand Down
4 changes: 2 additions & 2 deletions csharp/PhoneNumbers/LocaleNames.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,12 @@ internal static class LocaleNames
/// the caller's job, as it was when this data was a single generated dictionary.
/// </summary>
internal static Dictionary<string, string> ForCountry(string country) =>
country == null ? null : Cache.GetOrAdd(country, LoadFactory);
country is null ? null : Cache.GetOrAdd(country, LoadFactory);

private static Dictionary<string, string> Load(string country)
{
using var raw = Assembly.GetManifestResourceStream(ResourcePrefix + country);
if (raw == null)
if (raw is null)
return null;

using var gz = new GZipStream(raw, CompressionMode.Decompress);
Expand Down
2 changes: 1 addition & 1 deletion csharp/PhoneNumbers/MetadataLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public EmbeddedResourceMetadataLoader(Assembly assembly, string resourcePrefix)
// PhoneNumbers.MetadataBuilder). Decompress on the way out so callers see the plain
// bin format they already expect.
var raw = assembly.GetManifestResourceStream(resourcePrefix + fileName);
return raw == null ? null : new GZipStream(raw, CompressionMode.Decompress);
return raw is null ? null : new GZipStream(raw, CompressionMode.Decompress);
}
}
}
2 changes: 1 addition & 1 deletion csharp/PhoneNumbers/MetadataManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ private static MetadataSource CreateDefault(string filePrefix)
/// <param name="loader">Loader to use for both supplementary metadata file types.</param>
public static void SetMetadataLoader(IMetadataLoader loader)
{
if (loader == null) throw new ArgumentNullException(nameof(loader));
if (loader is null) throw new ArgumentNullException(nameof(loader));
alternateFormatsSource = new(loader, AlternateFormatsPrefix);
shortNumberSource = new(loader, ShortNumberMetadataPrefix);
}
Expand Down
2 changes: 1 addition & 1 deletion csharp/PhoneNumbers/MetadataSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public MetadataSource(IMetadataLoader loader, string filePrefix)
private PhoneMetadata? Load(string key)
{
using var stream = loader.LoadMetadata($"{filePrefix}_{key}");
return stream == null ? null : BuildMetadataFromBin.ReadMetadata(stream);
return stream is null ? null : BuildMetadataFromBin.ReadMetadata(stream);
}
}
}
15 changes: 7 additions & 8 deletions csharp/PhoneNumbers/PhoneNumberMatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ public PhoneNumberMatch(int start, string rawString, PhoneNumber number)
{
if (start < 0)
throw new ArgumentException("Start index must be >= 0.", nameof(start));
if (rawString == null)
if (rawString is null)
throw new ArgumentNullException(nameof(rawString));
if (number == null)
if (number is null)
throw new ArgumentNullException(nameof(number));
Start = start;
RawString = rawString;
Expand All @@ -50,8 +50,10 @@ public override bool Equals(object obj)
{
if (this == obj)
return true;
var p = (obj as PhoneNumberMatch);
return p != null && RawString == p.RawString && Start == p.Start && Number.Equals(p.Number);
if (obj is null || GetType() != obj.GetType())
return false;
var p = (PhoneNumberMatch)obj;
return RawString == p.RawString && Start == p.Start && Number.Equals(p.Number);
}

public override int GetHashCode()
Expand All @@ -63,9 +65,6 @@ public override int GetHashCode()
return hash;
}

public override string ToString()
{
return "PhoneNumberMatch [" + Start + "," + Length + ") " + RawString;
}
public override string ToString() => $"PhoneNumberMatch [{Start},{Length}) {RawString}";
}
}
18 changes: 8 additions & 10 deletions csharp/PhoneNumbers/TimezoneMapDataReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ internal static class TimezoneMapDataReader
private static List<string> LineReader(StreamReader reader, char fieldDelimiter = '|')
{
string line;
while (null != (line = reader.ReadLine()))
while ((line = reader.ReadLine()) != null)
{
line = line.Trim();
if (line.Length < 1 || '#' == line[0])
if (line.Length < 1 || line[0] == '#')
continue;

var indexOfDelimiter = line.IndexOf(fieldDelimiter);
Expand All @@ -38,18 +38,16 @@ private static List<string> LineReader(StreamReader reader, char fieldDelimiter
/// <returns></returns>
internal static IDictionary<long, string[]> GetPrefixMap(Stream fp, char[] splitters)
{
if (null == fp)
if (fp is null)
return ImmutableDictionary<long, string[]>.Empty;

var tmpMap = new SortedDictionary<long, string[]>();
using (var lines = new StreamReader(fp, Encoding.UTF8))
using var lines = new StreamReader(fp, Encoding.UTF8);
List<string> line;
while ((line = LineReader(lines)) != null)
{
List<string> line;
while (null != (line = LineReader(lines)))
{
var pnPrefix = line[0];
tmpMap[long.Parse(pnPrefix, CultureInfo.InvariantCulture)] = line[1].Split(splitters, StringSplitOptions.RemoveEmptyEntries);
}
var pnPrefix = line[0];
tmpMap[long.Parse(pnPrefix, CultureInfo.InvariantCulture)] = line[1].Split(splitters, StringSplitOptions.RemoveEmptyEntries);
}

return tmpMap;
Expand Down