diff --git a/ICD.Common.Utils/CrestronUtils.cs b/ICD.Common.Utils/CrestronUtils.cs index 2a98e8a..308841f 100644 --- a/ICD.Common.Utils/CrestronUtils.cs +++ b/ICD.Common.Utils/CrestronUtils.cs @@ -239,6 +239,12 @@ namespace ICD.Common.Utils } return ramfree; } + + [PublicAPI] + public static string GetMilliseconds() + { + return IcdEnvironment.GetLocalTime().ToString("fff"); + } } } diff --git a/ICD.Common.Utils/Extensions/ByteExtensions.cs b/ICD.Common.Utils/Extensions/ByteExtensions.cs new file mode 100644 index 0000000..6a31ef9 --- /dev/null +++ b/ICD.Common.Utils/Extensions/ByteExtensions.cs @@ -0,0 +1,38 @@ +using System; + +namespace ICD.Common.Utils.Extensions +{ + public static class ByteExtensions + { + public static bool GetBit(this byte b, int n) + { + if (n > 7 || n < 0) + throw new ArgumentException(); + + return (b & (1 << n)) == (1 << n); + } + + public static byte SetBit(this byte b, int n, bool v) + { + if (v) + return SetBitOn(b, n); + return SetBitOff(b, n); + } + + public static byte SetBitOn(this byte b, int n) + { + if (n > 7 || n < 0) + throw new ArgumentException(); + + return (byte)(b | (1 << n)); + } + + public static byte SetBitOff(this byte b, int n) + { + if (n > 7 || n < 0) + throw new ArgumentException(); + + return (byte)(b & ~(1 << n)); + } + } +} \ No newline at end of file diff --git a/ICD.Common.Utils/Extensions/StringExtensions.cs b/ICD.Common.Utils/Extensions/StringExtensions.cs index 88c2030..3e76958 100644 --- a/ICD.Common.Utils/Extensions/StringExtensions.cs +++ b/ICD.Common.Utils/Extensions/StringExtensions.cs @@ -122,8 +122,8 @@ namespace ICD.Common.Utils.Extensions if (chunkSize <= 0) throw new InvalidOperationException("chunkSize must be greater than 0"); - return Enumerable.Range(0, extends.Length / chunkSize) - .Select(i => extends.Substring(i * chunkSize, chunkSize)); + return Enumerable.Range(0, (int)Math.Ceiling(extends.Length / (double)chunkSize)) + .Select(i => extends.Substring(i * chunkSize, Math.Min(chunkSize, extends.Length - (i * chunkSize)))); } /// diff --git a/ICD.Common.Utils/ICD.Common.Utils_SimplSharp.csproj b/ICD.Common.Utils/ICD.Common.Utils_SimplSharp.csproj index d2ae64b..c39b3ae 100644 --- a/ICD.Common.Utils/ICD.Common.Utils_SimplSharp.csproj +++ b/ICD.Common.Utils/ICD.Common.Utils_SimplSharp.csproj @@ -84,6 +84,7 @@ + diff --git a/ICD.Common.Utils/IO/IcdTextWriter.cs b/ICD.Common.Utils/IO/IcdTextWriter.cs index c90eb81..75e5ed3 100644 --- a/ICD.Common.Utils/IO/IcdTextWriter.cs +++ b/ICD.Common.Utils/IO/IcdTextWriter.cs @@ -1,8 +1,10 @@ using System; #if SIMPLSHARP using Crestron.SimplSharp.CrestronIO; +using GC = Crestron.SimplSharp.CrestronEnvironment.GC; #else using System.IO; +using GC = System.GC; #endif namespace ICD.Common.Utils.IO @@ -13,6 +15,8 @@ namespace ICD.Common.Utils.IO public TextWriter WrappedTextWriter { get { return m_TextWriter; } } + private bool disposed = false; + /// /// Constructor. /// @@ -27,12 +31,25 @@ namespace ICD.Common.Utils.IO ~IcdTextWriter() { - Dispose(); + Dispose(false); } public void Dispose() { - m_TextWriter.Dispose(); + Dispose(true); + GC.SuppressFinalize(this); + } + + protected void Dispose(bool disposing) + { + if (disposed) + return; + + if (disposing) + { + m_TextWriter.Dispose(); + } + disposed = true; } } } diff --git a/ICD.Common.Utils/StringUtils.cs b/ICD.Common.Utils/StringUtils.cs index 4acac1c..8a27161 100644 --- a/ICD.Common.Utils/StringUtils.cs +++ b/ICD.Common.Utils/StringUtils.cs @@ -137,7 +137,7 @@ namespace ICD.Common.Utils throw new ArgumentNullException("bytes"); byte[] cast = bytes as byte[] ?? bytes.ToArray(); - return Encoding.UTF8.GetString(cast, 0, cast.Length); + return Encoding.GetEncoding(28591).GetString(cast, 0, cast.Length); } /// @@ -153,7 +153,7 @@ namespace ICD.Common.Utils throw new ArgumentNullException("bytes"); byte[] cast = bytes as byte[] ?? bytes.ToArray(); - return Encoding.UTF8.GetString(cast, 0, length); + return Encoding.GetEncoding(28591).GetString(cast, 0, length); } /// @@ -164,7 +164,7 @@ namespace ICD.Common.Utils [PublicAPI] public static byte[] ToBytes(string input) { - return Encoding.UTF8.GetBytes(input); + return Encoding.GetEncoding(28591).GetBytes(input); } ///