Skip to content

Commit 7e9a5a0

Browse files
Roadrunner67josesimoes
authored andcommitted
TryParseGuidWithDashes added along with string based constructor. (#88)
***NO_CI***
1 parent 6ee34f5 commit 7e9a5a0

File tree

1 file changed

+109
-1
lines changed
  • source/nanoFramework.CoreLibrary/System

1 file changed

+109
-1
lines changed

source/nanoFramework.CoreLibrary/System/Guid.cs

Lines changed: 109 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public struct Guid
1212
////////////////////////////////////////////////////////////////////////////////
1313
// Member variables
1414
////////////////////////////////////////////////////////////////////////////////
15-
private int _a;
15+
private int _a;
1616
private short _b;
1717
private short _c;
1818
private byte _d;
@@ -118,6 +118,25 @@ public Guid(byte[] b)
118118
_k = b[15];
119119
}
120120

121+
/// <summary>
122+
/// Creates a new <see cref="Guid"/> based on the value in the string. The value is made up
123+
/// of hex digits speared by the dash ("-"). The string may begin and end with
124+
/// brackets ("{", "}").
125+
///
126+
/// The string must be of the form dddddddd-dddd-dddd-dddd-dddddddddddd. where
127+
/// d is a hex digit. (That is 8 hex digits, followed by 4, then 4, then 4,
128+
/// then 12) such as: "CA761232-ED42-11CE-BACD-00AA0057B223"
129+
/// </summary>
130+
/// <param name="g">String representation of new <see cref="Guid"/>.</param>
131+
/// <exception cref="ArgumentException"></exception>
132+
public Guid(string g)
133+
{
134+
if (!TryParseGuidWithDashes(g, out this))
135+
{
136+
throw new ArgumentException("Guid string not in expected format: [{]dddddddd-dddd-dddd-dddd-dddddddddddd[}]");
137+
}
138+
}
139+
121140
/// <summary>
122141
/// Compares this instance to a specified object and returns an indication of their relative values.
123142
/// </summary>
@@ -356,5 +375,94 @@ private int GetResult(uint me, uint them)
356375
}
357376
return 1;
358377
}
378+
379+
/// <summary>
380+
/// Creates a new <see cref="Guid"/> based on the value in the string. The value is made up
381+
/// of hex digits speared by the dash ("-"). The string may begin and end with
382+
/// brackets ("{", "}").
383+
///
384+
/// The string must be of the form dddddddd-dddd-dddd-dddd-dddddddddddd. where
385+
/// d is a hex digit. (That is 8 hex digits, followed by 4, then 4, then 4,
386+
/// then 12) such as: "CA761232-ED42-11CE-BACD-00AA0057B223"
387+
/// </summary>
388+
/// <param name="guidString">Guid string to parse.</param>
389+
/// <param name="result">Resulting Guid.</param>
390+
/// <returns></returns>
391+
public static bool TryParseGuidWithDashes(String guidString, out Guid result)
392+
{
393+
int startPos = 0;
394+
int temp;
395+
long templ;
396+
int currentPos = 0;
397+
result = Guid.Empty;
398+
399+
// check to see that it's the proper length
400+
if (guidString[0] == '{')
401+
{
402+
if (guidString.Length != 38 || guidString[37] != '}')
403+
{
404+
return false;
405+
}
406+
startPos = 1;
407+
}
408+
else if (guidString.Length != 36)
409+
{
410+
return false;
411+
}
412+
413+
if (guidString[8 + startPos] != '-' ||
414+
guidString[13 + startPos] != '-' ||
415+
guidString[18 + startPos] != '-' ||
416+
guidString[23 + startPos] != '-')
417+
{
418+
return false;
419+
}
420+
421+
currentPos = startPos;
422+
try
423+
{
424+
result._a = (int)HexStringToLong(guidString, ref currentPos, 8);
425+
++currentPos; // Increment past the '-'
426+
result._b = (short)HexStringToLong(guidString, ref currentPos, 4);
427+
++currentPos; // Increment past the '-'
428+
result._c = (short)HexStringToLong(guidString, ref currentPos, 4);
429+
++currentPos; // Increment past the '-'
430+
temp = (int)HexStringToLong(guidString, ref currentPos, 4);
431+
++currentPos; // Increment past the '-'
432+
templ = HexStringToLong(guidString, ref currentPos, 12);
433+
}
434+
catch
435+
{
436+
result = Guid.Empty;
437+
return false;
438+
}
439+
440+
result._d = (byte)(temp >> 8);
441+
result._e = (byte)(temp);
442+
temp = (int)(templ >> 32);
443+
result._f = (byte)(temp >> 8);
444+
result._g = (byte)(temp);
445+
temp = (int)(templ);
446+
result._h = (byte)(temp >> 24);
447+
result._i = (byte)(temp >> 16);
448+
result._j = (byte)(temp >> 8);
449+
result._k = (byte)(temp);
450+
451+
return true;
452+
}
453+
454+
/// <summary>
455+
/// Converts a hex sub-string to a long, while incrementing the parsePos.
456+
/// </summary>
457+
/// <param name="str">The string containing the hex sub-string.</param>
458+
/// <param name="parsePos">The position of the hex sub-string within str.</param>
459+
/// <param name="requiredLength">The length of the hex sub-string.</param>
460+
/// <returns>False if any character is not a hex digit or string is shorter than needed for the requiredLength. Otherwise true.</returns>
461+
private static long HexStringToLong(String str, ref int parsePos, int requiredLength)
462+
{
463+
long result = Convert.ToInt64(str.Substring(parsePos, requiredLength), 16);
464+
parsePos += requiredLength;
465+
return result;
466+
}
359467
}
360468
}

0 commit comments

Comments
 (0)