From 184f7f236dd6dd7bf01eccba135d24d2e90ad91a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B8ren=20Bech=20Christensen?= Date: Wed, 25 Sep 2019 22:37:34 +0200 Subject: [PATCH 1/4] TryParseGuidWithDashes added along with string based constructor. --- .../nanoFramework.CoreLibrary/System/Guid.cs | 131 +++++++++++++++++- 1 file changed, 130 insertions(+), 1 deletion(-) diff --git a/source/nanoFramework.CoreLibrary/System/Guid.cs b/source/nanoFramework.CoreLibrary/System/Guid.cs index 9839a14e..c8518134 100644 --- a/source/nanoFramework.CoreLibrary/System/Guid.cs +++ b/source/nanoFramework.CoreLibrary/System/Guid.cs @@ -12,7 +12,7 @@ public struct Guid //////////////////////////////////////////////////////////////////////////////// // Member variables //////////////////////////////////////////////////////////////////////////////// - private int _a; + private int _a; private short _b; private short _c; private byte _d; @@ -118,6 +118,22 @@ public Guid(byte[] b) _k = b[15]; } + /// + /// Creates a new based on the value in the string. The value is made up + /// of hex digits speared by the dash ("-"). The string may begin and end with + /// brackets ("{", "}"). + /// + /// The string must be of the form dddddddd-dddd-dddd-dddd-dddddddddddd. where + /// d is a hex digit. (That is 8 hex digits, followed by 4, then 4, then 4, + /// then 12) such as: "CA761232-ED42-11CE-BACD-00AA0057B223" + /// + /// String representation of new . + public Guid(string g) + { + if (!TryParseGuidWithDashes(g, out this)) + throw new ArgumentException(); + } + /// /// Compares this instance to a specified object and returns an indication of their relative values. /// @@ -356,5 +372,118 @@ private int GetResult(uint me, uint them) } return 1; } + + + // Check if it's of the form [{|(]dddddddd-dddd-dddd-dddd-dddddddddddd[}|)] + public static bool TryParseGuidWithDashes(String guidString, out Guid result) + { + int startPos = 0; + int temp; + long templ; + int currentPos = 0; + result = new Guid(); + + // check to see that it's the proper length + if (guidString[0] == '{') + { + if (guidString.Length != 38 || guidString[37] != '}') + return false; + startPos = 1; + } + else if (guidString.Length != 36) + return false; + + if (guidString[8 + startPos] != '-' || + guidString[13 + startPos] != '-' || + guidString[18 + startPos] != '-' || + guidString[23 + startPos] != '-') + return false; + + currentPos = startPos; + if (!StringToInt(guidString, ref currentPos, 8, out temp)) + return false; + result._a = temp; + ++currentPos; //Increment past the '-'; + + if (!StringToInt(guidString, ref currentPos, 4, out temp)) + return false; + result._b = (short)temp; + ++currentPos; //Increment past the '-'; + + if (!StringToInt(guidString, ref currentPos, 4, out temp)) + return false; + result._c = (short)temp; + ++currentPos; //Increment past the '-'; + + if (!StringToInt(guidString, ref currentPos, 4, out temp)) + return false; + ++currentPos; //Increment past the '-'; + startPos = currentPos; + + if (!StringToLong(guidString, ref currentPos, 12, out templ)) + return false; + + result._d = (byte)(temp >> 8); + result._e = (byte)(temp); + temp = (int)(templ >> 32); + result._f = (byte)(temp >> 8); + result._g = (byte)(temp); + temp = (int)(templ); + result._h = (byte)(temp >> 24); + result._i = (byte)(temp >> 16); + result._j = (byte)(temp >> 8); + result._k = (byte)(temp); + + return true; + } + + /// + /// Using StringToLong to convert a hex sub-string to an int, while incrementing the parsePos. + /// + /// The string containing the hex sub-string. + /// The position of the hex sub-string within str. + /// The length of the hex sub-string. + /// The numeric value of the hex string. + /// False if any character is not a hex digit or string is shorter than needed for the requiredLength. Otherwise true. + private static bool StringToInt(String str, ref int parsePos, int requiredLength, out int result) + { + result = 0; + if (StringToLong(str, ref parsePos, requiredLength, out long lresult)) + result = (int)lresult; + else + return false; + return true; + } + + /// + /// Converts a hex sub-string to a long with the most basic tools, while incrementing the parsePos. + /// + /// The string containing the hex sub-string. + /// The position of the hex sub-string within str. + /// The length of the hex sub-string. + /// The numeric value of the hex string. + /// False if any character is not a hex digit or string is shorter than needed for the requiredLength. Otherwise true. + private static bool StringToLong(String str, ref int parsePos, int requiredLength, out long result) + { + result = 0; + + if (str.Length < parsePos + requiredLength) + return false; + while (requiredLength-- > 0) + { + result <<= 4; + char c = str[parsePos++]; + if (c >= '0' && c <= '9') + result += (byte)c - 48; + else if (c >= 'A' && c <= 'F') + result += (byte)c - 55; + else if (c >= 'a' && c <= 'f') + result += (byte)c - 87; + else + return false; + } + return true; + } } } + From 5ee353a7ca449d0377732f54ef894f0b363e6677 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B8ren=20Bech=20Christensen?= Date: Thu, 26 Sep 2019 21:50:42 +0200 Subject: [PATCH 2/4] Tidy up some of the code smells. --- .../nanoFramework.CoreLibrary/System/Guid.cs | 64 +++++++++++++++---- 1 file changed, 53 insertions(+), 11 deletions(-) diff --git a/source/nanoFramework.CoreLibrary/System/Guid.cs b/source/nanoFramework.CoreLibrary/System/Guid.cs index c8518134..ee45370f 100644 --- a/source/nanoFramework.CoreLibrary/System/Guid.cs +++ b/source/nanoFramework.CoreLibrary/System/Guid.cs @@ -12,7 +12,7 @@ public struct Guid //////////////////////////////////////////////////////////////////////////////// // Member variables //////////////////////////////////////////////////////////////////////////////// - private int _a; + private int _a; private short _b; private short _c; private byte _d; @@ -131,7 +131,9 @@ public Guid(byte[] b) public Guid(string g) { if (!TryParseGuidWithDashes(g, out this)) - throw new ArgumentException(); + { + throw new ArgumentException("Guid string not in expected format: [{]dddddddd-dddd-dddd-dddd-dddddddddddd[}]"); + } } /// @@ -373,55 +375,81 @@ private int GetResult(uint me, uint them) return 1; } - - // Check if it's of the form [{|(]dddddddd-dddd-dddd-dddd-dddddddddddd[}|)] + /// + /// Creates a new based on the value in the string. The value is made up + /// of hex digits speared by the dash ("-"). The string may begin and end with + /// brackets ("{", "}"). + /// + /// The string must be of the form dddddddd-dddd-dddd-dddd-dddddddddddd. where + /// d is a hex digit. (That is 8 hex digits, followed by 4, then 4, then 4, + /// then 12) such as: "CA761232-ED42-11CE-BACD-00AA0057B223" + /// + /// Guid string to parse. + /// Resulting Guid. + /// public static bool TryParseGuidWithDashes(String guidString, out Guid result) { int startPos = 0; int temp; long templ; int currentPos = 0; - result = new Guid(); + result = Guid.Empty; // check to see that it's the proper length if (guidString[0] == '{') { if (guidString.Length != 38 || guidString[37] != '}') + { return false; + } startPos = 1; } else if (guidString.Length != 36) + { return false; + } if (guidString[8 + startPos] != '-' || guidString[13 + startPos] != '-' || guidString[18 + startPos] != '-' || guidString[23 + startPos] != '-') + { return false; + } currentPos = startPos; if (!StringToInt(guidString, ref currentPos, 8, out temp)) + { return false; + } result._a = temp; - ++currentPos; //Increment past the '-'; + ++currentPos; // Increment past the '-' if (!StringToInt(guidString, ref currentPos, 4, out temp)) + { return false; + } result._b = (short)temp; - ++currentPos; //Increment past the '-'; + ++currentPos; // Increment past the '-' if (!StringToInt(guidString, ref currentPos, 4, out temp)) + { return false; + } result._c = (short)temp; - ++currentPos; //Increment past the '-'; + ++currentPos; // Increment past the '-' if (!StringToInt(guidString, ref currentPos, 4, out temp)) + { return false; - ++currentPos; //Increment past the '-'; + } + ++currentPos; // Increment past the '-' startPos = currentPos; if (!StringToLong(guidString, ref currentPos, 12, out templ)) + { return false; + } result._d = (byte)(temp >> 8); result._e = (byte)(temp); @@ -447,12 +475,16 @@ public static bool TryParseGuidWithDashes(String guidString, out Guid result) /// False if any character is not a hex digit or string is shorter than needed for the requiredLength. Otherwise true. private static bool StringToInt(String str, ref int parsePos, int requiredLength, out int result) { - result = 0; if (StringToLong(str, ref parsePos, requiredLength, out long lresult)) + { result = (int)lresult; + return true; + } else + { + result = 0; return false; - return true; + } } /// @@ -468,19 +500,29 @@ private static bool StringToLong(String str, ref int parsePos, int requiredLengt result = 0; if (str.Length < parsePos + requiredLength) + { return false; + } while (requiredLength-- > 0) { result <<= 4; char c = str[parsePos++]; if (c >= '0' && c <= '9') + { result += (byte)c - 48; + } else if (c >= 'A' && c <= 'F') + { result += (byte)c - 55; + } else if (c >= 'a' && c <= 'f') + { result += (byte)c - 87; + } else + { return false; + } } return true; } From 8002c1d17db8b8eec5700a94745b7afdb61d42db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B8ren=20Bech=20Christensen?= Date: Thu, 26 Sep 2019 21:58:29 +0200 Subject: [PATCH 3/4] small hygienic improvement --- source/nanoFramework.CoreLibrary/System/Guid.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/source/nanoFramework.CoreLibrary/System/Guid.cs b/source/nanoFramework.CoreLibrary/System/Guid.cs index ee45370f..57c2993c 100644 --- a/source/nanoFramework.CoreLibrary/System/Guid.cs +++ b/source/nanoFramework.CoreLibrary/System/Guid.cs @@ -443,8 +443,7 @@ public static bool TryParseGuidWithDashes(String guidString, out Guid result) { return false; } - ++currentPos; // Increment past the '-' - startPos = currentPos; + ++currentPos; // Increment past the '-' if (!StringToLong(guidString, ref currentPos, 12, out templ)) { From 3151e1811ed86b69900336bb577dc84c04c9f9d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B8ren=20Bech=20Christensen?= Date: Fri, 27 Sep 2019 08:44:29 +0200 Subject: [PATCH 4/4] Using Convert.ToInt64 base 16. --- .../nanoFramework.CoreLibrary/System/Guid.cs | 100 ++++-------------- 1 file changed, 19 insertions(+), 81 deletions(-) diff --git a/source/nanoFramework.CoreLibrary/System/Guid.cs b/source/nanoFramework.CoreLibrary/System/Guid.cs index 57c2993c..7d690ef3 100644 --- a/source/nanoFramework.CoreLibrary/System/Guid.cs +++ b/source/nanoFramework.CoreLibrary/System/Guid.cs @@ -12,7 +12,7 @@ public struct Guid //////////////////////////////////////////////////////////////////////////////// // Member variables //////////////////////////////////////////////////////////////////////////////// - private int _a; + private int _a; private short _b; private short _c; private byte _d; @@ -128,6 +128,7 @@ public Guid(byte[] b) /// then 12) such as: "CA761232-ED42-11CE-BACD-00AA0057B223" /// /// String representation of new . + /// public Guid(string g) { if (!TryParseGuidWithDashes(g, out this)) @@ -418,35 +419,21 @@ public static bool TryParseGuidWithDashes(String guidString, out Guid result) } currentPos = startPos; - if (!StringToInt(guidString, ref currentPos, 8, out temp)) + try { - return false; - } - result._a = temp; - ++currentPos; // Increment past the '-' - - if (!StringToInt(guidString, ref currentPos, 4, out temp)) - { - return false; - } - result._b = (short)temp; - ++currentPos; // Increment past the '-' - - if (!StringToInt(guidString, ref currentPos, 4, out temp)) - { - return false; + result._a = (int)HexStringToLong(guidString, ref currentPos, 8); + ++currentPos; // Increment past the '-' + result._b = (short)HexStringToLong(guidString, ref currentPos, 4); + ++currentPos; // Increment past the '-' + result._c = (short)HexStringToLong(guidString, ref currentPos, 4); + ++currentPos; // Increment past the '-' + temp = (int)HexStringToLong(guidString, ref currentPos, 4); + ++currentPos; // Increment past the '-' + templ = HexStringToLong(guidString, ref currentPos, 12); } - result._c = (short)temp; - ++currentPos; // Increment past the '-' - - if (!StringToInt(guidString, ref currentPos, 4, out temp)) - { - return false; - } - ++currentPos; // Increment past the '-' - - if (!StringToLong(guidString, ref currentPos, 12, out templ)) + catch { + result = Guid.Empty; return false; } @@ -465,66 +452,17 @@ public static bool TryParseGuidWithDashes(String guidString, out Guid result) } /// - /// Using StringToLong to convert a hex sub-string to an int, while incrementing the parsePos. + /// Converts a hex sub-string to a long, while incrementing the parsePos. /// /// The string containing the hex sub-string. /// The position of the hex sub-string within str. /// The length of the hex sub-string. - /// The numeric value of the hex string. /// False if any character is not a hex digit or string is shorter than needed for the requiredLength. Otherwise true. - private static bool StringToInt(String str, ref int parsePos, int requiredLength, out int result) + private static long HexStringToLong(String str, ref int parsePos, int requiredLength) { - if (StringToLong(str, ref parsePos, requiredLength, out long lresult)) - { - result = (int)lresult; - return true; - } - else - { - result = 0; - return false; - } - } - - /// - /// Converts a hex sub-string to a long with the most basic tools, while incrementing the parsePos. - /// - /// The string containing the hex sub-string. - /// The position of the hex sub-string within str. - /// The length of the hex sub-string. - /// The numeric value of the hex string. - /// False if any character is not a hex digit or string is shorter than needed for the requiredLength. Otherwise true. - private static bool StringToLong(String str, ref int parsePos, int requiredLength, out long result) - { - result = 0; - - if (str.Length < parsePos + requiredLength) - { - return false; - } - while (requiredLength-- > 0) - { - result <<= 4; - char c = str[parsePos++]; - if (c >= '0' && c <= '9') - { - result += (byte)c - 48; - } - else if (c >= 'A' && c <= 'F') - { - result += (byte)c - 55; - } - else if (c >= 'a' && c <= 'f') - { - result += (byte)c - 87; - } - else - { - return false; - } - } - return true; + long result = Convert.ToInt64(str.Substring(parsePos, requiredLength), 16); + parsePos += requiredLength; + return result; } } } -