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
4 changes: 2 additions & 2 deletions src/SIPSorcery/sys/Crypto/Crypto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ private static FileStream GetFileStream(string filepath)
if (!File.Exists(filepath))
{
logger.LogError("Cannot open a non-existent file for a hash operation, {FilePath}.", filepath);
throw new IOException("Cannot open a non-existent file for a hash operation, " + filepath + ".");
throw new IOException($"Cannot open a non-existent file for a hash operation, {filepath}.");
}

// Open the file.
Expand All @@ -303,7 +303,7 @@ private static FileStream GetFileStream(string filepath)
{
inputStream.Close();
logger.LogError("Cannot perform a hash operation on an empty file, {FilePath}.", filepath);
throw new IOException("Cannot perform a hash operation on an empty file, " + filepath + ".");
throw new IOException($"Cannot perform a hash operation on an empty file, {filepath}.");
}

return inputStream;
Expand Down
4 changes: 2 additions & 2 deletions src/SIPSorcery/sys/Crypto/PasswordHash.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,15 @@ public static string GenerateSalt(int? explicitIterations = null)
{
if (explicitIterations.HasValue && explicitIterations.Value < RFC289_MINIMUM_ITERATIONS)
{
throw new ArgumentException("Cannot be less than " + RFC289_MINIMUM_ITERATIONS, "explicitIterations");
throw new ArgumentException($"Cannot be less than {RFC289_MINIMUM_ITERATIONS}", "explicitIterations");
}

byte[] salt = new byte[SALT_SIZE];
_randomProvider.GetBytes(salt);

var iterations = (explicitIterations ?? RFC289_MINIMUM_ITERATIONS).ToString("X");

return iterations + "." + Convert.ToBase64String(salt);
return $"{iterations}.{Convert.ToBase64String(salt)}";
}

/// <summary>
Expand Down
18 changes: 9 additions & 9 deletions src/SIPSorcery/sys/Formatting/NumberFormatting.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,22 @@ public static string ToSIFormat(double number, int decimalPlaces)
if (number > 1000000000000)
{
double teraNumber = Math.Round((double)(number / (double)100000000000), decimalPlaces);
return teraNumber.ToString() + "T";
return $"{teraNumber}T";
}
if (number > 1000000000)
{
double gigaNumber = Math.Round((double)(number / (double)100000000), decimalPlaces);
return gigaNumber.ToString() + "G";
return $"{gigaNumber}G";
}
else if (number > 1000000)
{
double kiloNumber = Math.Round((double)(number / (double)1000000), decimalPlaces);
return kiloNumber.ToString() + "M";
return $"{kiloNumber}M";
}
else if (number > 1000)
{
double kiloNumber = Math.Round((double)(number / (double)1000), decimalPlaces);
return kiloNumber.ToString() + "k";
return $"{kiloNumber}k";
}
else
{
Expand All @@ -53,26 +53,26 @@ private static string ToSIByteFormat(double number, int decimalPlaces, string su
if (number > 1099511627776)
{
double teraNumber = Math.Round((double)(number / (double)1099511627776), decimalPlaces);
return teraNumber.ToString() + "T" + suffix;
return $"{teraNumber}T{suffix}";
}
if (number > 1073741824)
{
double gigaNumber = Math.Round((double)(number / (double)1073741824), decimalPlaces);
return gigaNumber.ToString() + "G" + suffix;
return $"{gigaNumber}G{suffix}";
}
else if (number > 1048576)
{
double kiloNumber = Math.Round((double)(number / (double)1048576), decimalPlaces);
return kiloNumber.ToString() + "M" + suffix;
return $"{kiloNumber}M{suffix}";
}
else if (number > 1024)
{
double kiloNumber = Math.Round((double)(number / (double)1024), decimalPlaces);
return kiloNumber.ToString() + "K" + suffix;
return $"{kiloNumber}K{suffix}";
}
else
{
return Math.Round((double)number, decimalPlaces).ToString() + suffix;
return $"{Math.Round((double)number, decimalPlaces)}{suffix}";
}
}

Expand Down
28 changes: 15 additions & 13 deletions src/SIPSorcery/sys/Net/IPSocket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,10 @@ public class IPSocket
/// </summary>
public static string GetSocketString(IPEndPoint endPoint)
{
string format = (endPoint.Address.AddressFamily == AddressFamily.InterNetworkV6) ? "[{0}]:{1}" : "{0}:{1}";
return string.Format(format, endPoint.Address.ToString(), endPoint.Port.ToString(NumberFormatInfo.InvariantInfo));
var address = endPoint.Address.ToString();
var port = endPoint.Port.ToString(NumberFormatInfo.InvariantInfo);

return endPoint.Address.AddressFamily == AddressFamily.InterNetworkV6 ? $"[{address}]:{port}" : $"{address}:{port}";
}

/// <summary>
Expand Down Expand Up @@ -123,7 +125,7 @@ public static string ParseHostFromSocket(string socket)
{
string host = socket;

if (socket != null && socket.Trim().Length > 0 && socket.IndexOf(':') != -1)
if (!string.IsNullOrWhiteSpace(socket) && socket.IndexOf(':') != -1)
{
host = socket.Substring(0, socket.LastIndexOf(':')).Trim();
}
Expand Down Expand Up @@ -154,14 +156,14 @@ public static int ParsePortFromSocket(string socket)
}
// Look to see if this is IPv4 with a port (IPv6 will have another colon)
// If it's a host name there will also not be another ':'.
else if (socket.Substring(0, lastColonPos).LastIndexOf(':') != -1)
else if (socket.AsSpan(0, lastColonPos).LastIndexOf(':') != -1)
{
// This is an IPv6 address WITHOUT a port.
lastColonPos = -1;
}
}

if (socket != null && socket.Trim().Length > 0 && lastColonPos != -1)
if (!string.IsNullOrWhiteSpace(socket) && lastColonPos != -1)
{
port = Convert.ToInt32(socket.Substring(lastColonPos + 1).Trim());
}
Expand All @@ -176,7 +178,7 @@ public static int ParsePortFromSocket(string socket)
/// <returns>true/false</returns>
public static bool IsIPAddress(string socket)
{
if (socket == null || socket.Trim().Length == 0)
if (string.IsNullOrWhiteSpace(socket))
{
return false;
}
Expand Down Expand Up @@ -310,7 +312,7 @@ public static bool Parse(string endpointstring, out string host, out int port)
}
else
{
throw new FormatException(string.Format("Invalid endpoint ipaddress '{0}'", endpointstring));
throw new FormatException($"Invalid endpoint ipaddress '{endpointstring}'");
}

return rc;
Expand All @@ -327,7 +329,7 @@ public static IPEndPoint Parse(string endpointstring, int defaultport = -1)
(defaultport < IPEndPoint.MinPort
|| defaultport > IPEndPoint.MaxPort))
{
throw new ArgumentException(string.Format("Invalid default port '{0}'", defaultport));
throw new ArgumentException($"Invalid default port '{defaultport}'");
}

string[] values = endpointstring.Split(new char[] { ':' });
Expand Down Expand Up @@ -356,7 +358,7 @@ public static IPEndPoint Parse(string endpointstring, int defaultport = -1)
}
catch
{
throw new FormatException(string.Format("Invalid endpoint ipaddress '{0}'", endpointstring));
throw new FormatException($"Invalid endpoint ipaddress '{endpointstring}'");
}
}
}
Expand All @@ -377,7 +379,7 @@ public static IPEndPoint Parse(string endpointstring, int defaultport = -1)
}
else
{
throw new FormatException(string.Format("Invalid endpoint ipaddress '{0}'", endpointstring));
throw new FormatException($"Invalid endpoint ipaddress '{endpointstring}'");
}

if (port == -1)
Expand All @@ -396,7 +398,7 @@ private static int getPort(string p)
|| port < IPEndPoint.MinPort
|| port > IPEndPoint.MaxPort)
{
throw new FormatException(string.Format("Invalid end point port '{0}'", p));
throw new FormatException($"Invalid end point port '{p}'");
}

return port;
Expand All @@ -410,13 +412,13 @@ private static IPAddress getIPfromHost(string p)

if (hosts == null || hosts.Length == 0)
{
throw new ArgumentException(string.Format("Host not found: {0}", p));
throw new ArgumentException($"Host not found: {p}");
}
return hosts[0];
}
catch
{
throw new ArgumentException(string.Format("Host not found: {0}", p));
throw new ArgumentException($"Host not found: {p}");
}
}

Expand Down
6 changes: 2 additions & 4 deletions src/SIPSorcery/sys/TypeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public static class TypeExtensions
/// </summary>
public static bool IsNullOrBlank(this string s)
{
if (s == null || s.Trim(WhiteSpaceChars).Length == 0)
if (s == null || s.AsSpan().Trim(WhiteSpaceChars).Length == 0)
{
return true;
}
Expand All @@ -65,7 +65,7 @@ public static bool IsNullOrBlank(this string s)

public static bool NotNullOrBlank(this string s)
{
if (s == null || s.Trim(WhiteSpaceChars).Length == 0)
if (s == null || s.AsSpan().Trim(WhiteSpaceChars).Length == 0)
{
return false;
}
Expand Down Expand Up @@ -206,13 +206,11 @@ public static byte[] ParseHexStr(string hexStr)
return buffer.ToArray();
}

//#if NET472 || NETSTANDARD2_0
public static void Deconstruct<T1, T2>(this KeyValuePair<T1, T2> tuple, out T1 key, out T2 value)
{
key = tuple.Key;
value = tuple.Value;
}
//#endif

public static bool IsPrivate(this IPAddress address)
{
Expand Down
Loading