aboutsummaryrefslogtreecommitdiffstats
path: root/src/slskd/Extensions.cs
diff options
context:
space:
mode:
Diffstat (limited to 'src/slskd/Extensions.cs')
-rw-r--r--src/slskd/Extensions.cs80
1 files changed, 0 insertions, 80 deletions
diff --git a/src/slskd/Extensions.cs b/src/slskd/Extensions.cs
deleted file mode 100644
index 44b0f721..00000000
--- a/src/slskd/Extensions.cs
+++ /dev/null
@@ -1,80 +0,0 @@
-namespace slskd
-{
- using System;
- using System.IO;
- using System.Linq;
- using System.Security.Cryptography;
- using System.Text;
-
- /// <summary>
- /// Extensions.
- /// </summary>
- public static class Extensions
- {
- /// <summary>
- /// Converts the given path to the local format (normalizes path separators).
- /// </summary>
- /// <param name="path"></param>
- /// <returns></returns>
- public static string ToLocalOSPath(this string path)
- {
- return path.Replace('\\', Path.DirectorySeparatorChar).Replace('/', Path.DirectorySeparatorChar);
- }
-
- /// <summary>
- /// Returns the directory from the given path, regardless of separator format.
- /// </summary>
- /// <param name="path"></param>
- /// <returns></returns>
- public static string DirectoryName(this string path)
- {
- var separator = path.Contains('\\') ? '\\' : '/';
- var parts = path.Split(separator);
- return string.Join(separator, parts.Take(parts.Length - 1));
- }
-
- /// <summary>
- /// Returns the SHA1 hash of the given string.
- /// </summary>
- /// <param name="str"></param>
- /// <returns></returns>
- public static string Sha1(this string str)
- {
- using var sha1 = new SHA1Managed();
- return BitConverter.ToString(sha1.ComputeHash(Encoding.UTF8.GetBytes(str))).Replace("-", "");
- }
-
- /// <summary>
- /// Formats byte to nearest size (KB, MB, etc.)
- /// </summary>
- /// <param name="value"></param>
- /// <param name="decimalPlaces"></param>
- /// <returns></returns>
- public static string SizeSuffix(this double value, int decimalPlaces = 1)
- {
- string[] SizeSuffixes = { "bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" };
-
- if (value < 0) { return "-" + SizeSuffix(-value); }
- if (value == 0) { return string.Format("{0:n" + decimalPlaces + "} bytes", 0); }
-
- // mag is 0 for bytes, 1 for KB, 2, for MB, etc.
- int mag = (int)Math.Log(value, 1024);
-
- // 1L << (mag * 10) == 2 ^ (10 * mag)
- // [i.e. the number of bytes in the unit corresponding to mag]
- decimal adjustedSize = (decimal)value / (1L << (mag * 10));
-
- // make adjustment when the value is large enough that
- // it would round up to 1000 or more
- if (Math.Round(adjustedSize, decimalPlaces) >= 1000)
- {
- mag += 1;
- adjustedSize /= 1024;
- }
-
- return string.Format("{0:n" + decimalPlaces + "} {1}",
- adjustedSize,
- SizeSuffixes[mag]);
- }
- }
-}