using System.Globalization;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
-
+using System.Runtime.Versioning;
using Internal.Runtime.CompilerServices;
namespace System
// Represents a Globally Unique Identifier.
[StructLayout(LayoutKind.Sequential)]
[Serializable]
- [Runtime.Versioning.NonVersionable] // This only applies to field layout
+ [NonVersionable] // This only applies to field layout
[TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")]
public partial struct Guid : IFormattable, IComparable, IComparable<Guid>, IEquatable<Guid>, ISpanFormattable
{
public static readonly Guid Empty = new Guid();
- ////////////////////////////////////////////////////////////////////////////////
- // Member variables
- ////////////////////////////////////////////////////////////////////////////////
private int _a; // Do not rename (binary serialization)
private short _b; // Do not rename (binary serialization)
private short _c; // Do not rename (binary serialization)
private byte _j; // Do not rename (binary serialization)
private byte _k; // Do not rename (binary serialization)
- ////////////////////////////////////////////////////////////////////////////////
- // Constructors
- ////////////////////////////////////////////////////////////////////////////////
-
// Creates a new guid from an array of bytes.
public Guid(byte[] b) :
this(new ReadOnlySpan<byte>(b ?? throw new ArgumentNullException(nameof(b))))
public Guid(ReadOnlySpan<byte> b)
{
if ((uint)b.Length != 16)
+ {
throw new ArgumentException(SR.Format(SR.Arg_GuidArrayCtor, "16"), nameof(b));
+ }
if (BitConverter.IsLittleEndian)
{
}
// Creates a new GUID initialized to the value represented by the arguments.
- //
public Guid(int a, short b, short c, byte[] d)
{
if (d == null)
+ {
throw new ArgumentNullException(nameof(d));
- // Check that array is not too big
+ }
if (d.Length != 8)
+ {
throw new ArgumentException(SR.Format(SR.Arg_GuidArrayCtor, "8"), nameof(d));
+ }
_a = a;
_b = b;
AllButOverflow = 2
}
- // This will store the result of the parsing. And it will eventually be used to construct a Guid instance.
+ // This will store the result of the parsing. And it will eventually be used to construct a Guid instance.
private struct GuidResult
{
- internal readonly GuidParseThrowStyle _throwStyle;
+ private readonly GuidParseThrowStyle _throwStyle;
internal Guid _parsedGuid;
- private bool _overflow;
- private string _failureMessageID;
- private object _failureMessageFormatArgument;
-
internal GuidResult(GuidParseThrowStyle canThrow) : this()
{
_throwStyle = canThrow;
internal void SetFailure(bool overflow, string failureMessageID)
{
- _overflow = overflow;
- _failureMessageID = failureMessageID;
- if (_throwStyle != GuidParseThrowStyle.None)
+ if (_throwStyle == GuidParseThrowStyle.None)
{
- throw CreateGuidParseException();
+ return;
}
- }
- internal void SetFailure(bool overflow, string failureMessageID, object failureMessageFormatArgument)
- {
- _failureMessageFormatArgument = failureMessageFormatArgument;
- SetFailure(overflow, failureMessageID);
- }
-
- internal Exception CreateGuidParseException()
- {
- if (_overflow)
- {
- return _throwStyle == GuidParseThrowStyle.All ?
- (Exception)new OverflowException(SR.GetResourceString(_failureMessageID)) :
- new FormatException(SR.Format_GuidUnrecognized);
- }
- else
+ if (overflow)
{
- return new FormatException(SR.GetResourceString(_failureMessageID));
+ if (_throwStyle == GuidParseThrowStyle.All)
+ {
+ throw new OverflowException(SR.GetResourceString(failureMessageID));
+ }
+
+ throw new FormatException(SR.Format_GuidUnrecognized);
}
+
+ throw new FormatException(SR.GetResourceString(failureMessageID));
}
}
// 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"
- //
public Guid(string g)
{
if (g == null)
}
var result = new GuidResult(GuidParseThrowStyle.All);
- if (!TryParseGuid(g, ref result))
- {
- throw result.CreateGuidParseException();
- }
+ bool success = TryParseGuid(g, ref result);
+ Debug.Assert(success, "GuidParseThrowStyle.All means throw on all failures");
this = result._parsedGuid;
}
public static Guid Parse(ReadOnlySpan<char> input)
{
var result = new GuidResult(GuidParseThrowStyle.AllButOverflow);
- if (!TryParseGuid(input, ref result))
- {
- throw result.CreateGuidParseException();
- }
+ bool success = TryParseGuid(input, ref result);
+ Debug.Assert(success, "GuidParseThrowStyle.AllButOverflow means throw on all failures");
return result._parsedGuid;
}
throw new FormatException(SR.Format_InvalidGuidFormatSpecification);
}
- if (!success)
- {
- throw result.CreateGuidParseException();
- }
-
+ Debug.Assert(success, "GuidParseThrowStyle.AllButOverflow means throw on all failures");
return result._parsedGuid;
}
// Check for '0x'
if (!IsHexPrefix(guidString, 1))
{
- result.SetFailure(overflow: false, nameof(SR.Format_GuidHexPrefix), "{0xdddddddd, etc}");
+ result.SetFailure(overflow: false, nameof(SR.Format_GuidHexPrefix));
return false;
}
// Check for '0x'
if (!IsHexPrefix(guidString, numStart + numLen + 1))
{
- result.SetFailure(overflow: false, nameof(SR.Format_GuidHexPrefix), "{0xdddddddd, 0xdddd, etc}");
+ result.SetFailure(overflow: false, nameof(SR.Format_GuidHexPrefix));
return false;
}
// +3 to get by ',0x'
// Check for '0x'
if (!IsHexPrefix(guidString, numStart + numLen + 1))
{
- result.SetFailure(overflow: false, nameof(SR.Format_GuidHexPrefix), "{0xdddddddd, 0xdddd, 0xdddd, etc}");
+ result.SetFailure(overflow: false, nameof(SR.Format_GuidHexPrefix));
return false;
}
// +3 to get by ',0x'
// Check for '0x'
if (!IsHexPrefix(guidString, numStart + numLen + 1))
{
- result.SetFailure(overflow: false, nameof(SR.Format_GuidHexPrefix), "{... { ... 0xdd, ...}}");
+ result.SetFailure(overflow: false, nameof(SR.Format_GuidHexPrefix));
return false;
}
}
// Returns the guid in "registry" format.
- public override string ToString()
- {
- return ToString("D", null);
- }
+ public override string ToString() => ToString("D", null);
public override int GetHashCode()
{
Unsafe.Add(ref g._a, 3) == Unsafe.Add(ref _a, 3);
}
- private int GetResult(uint me, uint them)
- {
- if (me < them)
- {
- return -1;
- }
- return 1;
- }
+ private int GetResult(uint me, uint them) => me < them ? -1 : 1;
public int CompareTo(object value)
{
// We currently ignore provider
public string ToString(string format, IFormatProvider provider)
{
- if (format == null || format.Length == 0)
+ if (string.IsNullOrEmpty(format))
+ {
format = "D";
+ }
// all acceptable format strings are of length 1
if (format.Length != 1)
+ {
throw new FormatException(SR.Format_InvalidGuidFormatSpecification);
+ }
int guidSize;
switch (format[0])
public bool TryFormat(Span<char> destination, out int charsWritten, ReadOnlySpan<char> format = default)
{
if (format.Length == 0)
+ {
format = "D";
-
+ }
// all acceptable format strings are of length 1
if (format.Length != 1)
+ {
throw new FormatException(SR.Format_InvalidGuidFormatSpecification);
+ }
bool dash = true;
bool hex = false;