From 4801d453472759ef567fde4c48d9860c1965d111 Mon Sep 17 00:00:00 2001 From: "jeffery.thompson" Date: Tue, 18 Jul 2017 09:58:55 -0400 Subject: [PATCH 1/6] Added extensions to byte for getting and setting bits --- ICD.Common.Utils/Extensions/ByteExtensions.cs | 42 +++++++++++++++++++ .../ICD.Common.Utils_SimplSharp.csproj | 1 + 2 files changed, 43 insertions(+) create mode 100644 ICD.Common.Utils/Extensions/ByteExtensions.cs diff --git a/ICD.Common.Utils/Extensions/ByteExtensions.cs b/ICD.Common.Utils/Extensions/ByteExtensions.cs new file mode 100644 index 0000000..42d8787 --- /dev/null +++ b/ICD.Common.Utils/Extensions/ByteExtensions.cs @@ -0,0 +1,42 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using Crestron.SimplSharp; + +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/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 @@ + From 809fe6802b99c3aca85cc1c8e4c94bf9411c1c11 Mon Sep 17 00:00:00 2001 From: "jeffery.thompson" Date: Tue, 18 Jul 2017 10:00:24 -0400 Subject: [PATCH 2/6] Switched to extended ASCII encoding for string <-> byte[] conversion --- ICD.Common.Utils/StringUtils.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ICD.Common.Utils/StringUtils.cs b/ICD.Common.Utils/StringUtils.cs index e339eaf..4a18951 100644 --- a/ICD.Common.Utils/StringUtils.cs +++ b/ICD.Common.Utils/StringUtils.cs @@ -127,7 +127,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); } /// @@ -143,7 +143,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); } /// @@ -154,7 +154,7 @@ namespace ICD.Common.Utils [PublicAPI] public static byte[] ToBytes(string input) { - return Encoding.UTF8.GetBytes(input); + return Encoding.GetEncoding(28591).GetBytes(input); } /// From 01ddf830d640e9fad1d0bf84952d95fa5c1b357a Mon Sep 17 00:00:00 2001 From: "jeffery.thompson" Date: Tue, 18 Jul 2017 10:01:10 -0400 Subject: [PATCH 3/6] string.Split(int) now returns any remaining characters in a new chunk --- ICD.Common.Utils/Extensions/StringExtensions.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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)))); } /// From ff66d635aef9bcf21f30c7185348928d823f3fe9 Mon Sep 17 00:00:00 2001 From: "jeffery.thompson" Date: Tue, 18 Jul 2017 10:01:26 -0400 Subject: [PATCH 4/6] Fixed IcdTextWriter's IDisposable implementation --- ICD.Common.Utils/IO/IcdTextWriter.cs | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) 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; } } } From ea82bcd7ec5e6b3886cb30be85aaddc6c09083bd Mon Sep 17 00:00:00 2001 From: "jeffery.thompson" Date: Tue, 18 Jul 2017 10:01:51 -0400 Subject: [PATCH 5/6] Added GetMilliseconds command for s+ --- ICD.Common.Utils/CrestronUtils.cs | 6 ++++++ 1 file changed, 6 insertions(+) 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"); + } } } From 91534ec0af1cd342c5e82bc7a4ca3c8f016b9796 Mon Sep 17 00:00:00 2001 From: "jeffery.thompson" Date: Tue, 18 Jul 2017 10:39:33 -0400 Subject: [PATCH 6/6] Removing unused namespaces --- ICD.Common.Utils/Extensions/ByteExtensions.cs | 4 ---- 1 file changed, 4 deletions(-) diff --git a/ICD.Common.Utils/Extensions/ByteExtensions.cs b/ICD.Common.Utils/Extensions/ByteExtensions.cs index 42d8787..6a31ef9 100644 --- a/ICD.Common.Utils/Extensions/ByteExtensions.cs +++ b/ICD.Common.Utils/Extensions/ByteExtensions.cs @@ -1,8 +1,4 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using Crestron.SimplSharp; namespace ICD.Common.Utils.Extensions {