Skip to content

Commit 7345ef4

Browse files
committed
split files
1 parent a6c08e5 commit 7345ef4

File tree

8 files changed

+334
-273
lines changed

8 files changed

+334
-273
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// SPDX-FileCopyrightText: 2022 smdn <smdn@smdn.jp>
2+
// SPDX-License-Identifier: MIT
3+
using System;
4+
5+
namespace Smdn;
6+
7+
#pragma warning disable IDE0040
8+
partial struct Uuid :
9+
#pragma warning restore IDE0040
10+
IComparable<Uuid>,
11+
IComparable<Guid>,
12+
IComparable
13+
{
14+
public static bool operator <(Uuid x, Uuid y) => x.CompareTo(y) < 0;
15+
public static bool operator <=(Uuid x, Uuid y) => x.CompareTo(y) <= 0;
16+
public static bool operator >(Uuid x, Uuid y) => y < x;
17+
public static bool operator >=(Uuid x, Uuid y) => y <= x;
18+
19+
public int CompareTo(object obj)
20+
{
21+
if (obj == null)
22+
return 1;
23+
else if (obj is Uuid uuid)
24+
return CompareTo(uuid);
25+
else if (obj is Guid guid)
26+
return CompareTo(guid);
27+
else
28+
throw new ArgumentException("obj is not Uuid", nameof(obj));
29+
}
30+
31+
public int CompareTo(Guid other)
32+
=> CompareTo((Uuid)other);
33+
34+
public int CompareTo(Uuid other)
35+
{
36+
int ret;
37+
38+
if ((ret = (int)(this.time_low - other.time_low)) != 0)
39+
return ret;
40+
41+
if ((ret = this.time_mid - other.time_mid) != 0)
42+
return ret;
43+
44+
if ((ret = this.time_hi_and_version - other.time_hi_and_version) != 0)
45+
return ret;
46+
47+
if ((ret = this.clock_seq_hi_and_reserved - other.clock_seq_hi_and_reserved) != 0)
48+
return ret;
49+
50+
if ((ret = this.clock_seq_low - other.clock_seq_low) != 0)
51+
return ret;
52+
53+
return node.CompareTo(other.node);
54+
}
55+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// SPDX-FileCopyrightText: 2022 smdn <smdn@smdn.jp>
2+
// SPDX-License-Identifier: MIT
3+
using System;
4+
5+
namespace Smdn;
6+
7+
#pragma warning disable IDE0040
8+
partial struct Uuid :
9+
#pragma warning restore IDE0040
10+
IEquatable<Uuid>,
11+
IEquatable<Guid>
12+
{
13+
public static bool operator ==(Uuid x, Uuid y) => x.fields_high == y.fields_high && x.fields_low == y.fields_low;
14+
public static bool operator !=(Uuid x, Uuid y) => x.fields_high != y.fields_high || x.fields_low != y.fields_low;
15+
16+
public override bool Equals(object obj)
17+
{
18+
if (obj is Uuid uuid)
19+
return Equals(uuid);
20+
else if (obj is Guid guid)
21+
return Equals(guid);
22+
else
23+
return false;
24+
}
25+
26+
public bool Equals(Guid other) => this == (Uuid)other;
27+
public bool Equals(Uuid other) => this == other;
28+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
// SPDX-FileCopyrightText: 2022 smdn <smdn@smdn.jp>
2+
// SPDX-License-Identifier: MIT
3+
using System;
4+
#if !SYSTEM_FORMATTABLESTRING
5+
using System.Globalization;
6+
#endif
7+
8+
namespace Smdn;
9+
10+
#pragma warning disable IDE0040
11+
partial struct Uuid :
12+
#pragma warning restore IDE0040
13+
IFormattable
14+
{
15+
public string ToString(string format)
16+
=> ToString(format, null);
17+
18+
public string ToString(string format, IFormatProvider formatProvider)
19+
{
20+
if (format == "X") {
21+
const string xopen = "{";
22+
const string xclose = "}";
23+
24+
#if SYSTEM_FORMATTABLESTRING
25+
return FormattableString.Invariant(
26+
$"{xopen}0x{time_low:x8},0x{time_mid:x4},0x{time_hi_and_version:x4},{xopen}0x{clock_seq_hi_and_reserved:x2},0x{clock_seq_low:x2},0x{node.N0:x2},0x{node.N1:x2},0x{node.N2:x2},0x{node.N3:x2},0x{node.N4:x2},0x{node.N5:x2}{xclose}{xclose}"
27+
);
28+
#else
29+
return string.Format(
30+
CultureInfo.InvariantCulture,
31+
"{0}0x{1:x8},0x{2:x4},0x{3:x4},{0}0x{4:x2},0x{5:x2},0x{6:x2},0x{7:x2},0x{8:x2},0x{9:x2},0x{10:x2},0x{11:x2}{12}{12}",
32+
xopen,
33+
time_low,
34+
time_mid,
35+
time_hi_and_version,
36+
clock_seq_hi_and_reserved,
37+
clock_seq_low,
38+
node.N0,
39+
node.N1,
40+
node.N2,
41+
node.N3,
42+
node.N4,
43+
node.N5,
44+
xclose
45+
);
46+
#endif
47+
}
48+
49+
var (open, close, separator) = format switch {
50+
null or "" or "D" => (null, null, "-"),
51+
"B" => ("{", "}", "-"),
52+
"P" => ("(", ")", "-"),
53+
"N" => (null, null, null),
54+
_ => throw new FormatException($"invalid format: {format}"),
55+
};
56+
57+
#if SYSTEM_FORMATTABLESTRING
58+
return FormattableString.Invariant(
59+
$"{open}{time_low:x8}{separator}{time_mid:x4}{separator}{time_hi_and_version:x4}{separator}{clock_seq_hi_and_reserved:x2}{clock_seq_low:x2}{separator}{node.N0:x2}{node.N1:x2}{node.N2:x2}{node.N3:x2}{node.N4:x2}{node.N5:x2}{close}"
60+
);
61+
#else
62+
return string.Format(
63+
CultureInfo.InvariantCulture,
64+
"{0}{3:x8}{1}{4:x4}{1}{5:x4}{1}{6:x2}{7:x2}{1}{8:x2}{9:x2}{10:x2}{11:x2}{12:x2}{13:x2}{2}",
65+
open,
66+
separator,
67+
close,
68+
time_low,
69+
time_mid,
70+
time_hi_and_version,
71+
clock_seq_hi_and_reserved,
72+
clock_seq_low,
73+
node.N0,
74+
node.N1,
75+
node.N2,
76+
node.N3,
77+
node.N4,
78+
node.N5
79+
);
80+
#endif
81+
}
82+
}

src/Smdn.Fundamental.Uuid/Smdn/Uuid.cs

Lines changed: 1 addition & 133 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,7 @@ namespace Smdn;
2121
*/
2222
[System.Runtime.CompilerServices.TypeForwardedFrom("Smdn, Version=3.0.0.0, Culture=neutral, PublicKeyToken=null")]
2323
[StructLayout(LayoutKind.Explicit, Pack = 1)]
24-
public readonly struct Uuid :
25-
IEquatable<Uuid>,
26-
IEquatable<Guid>,
27-
IComparable<Uuid>,
28-
IComparable<Guid>,
29-
IComparable,
30-
IFormattable {
24+
public readonly partial struct Uuid {
3125
#pragma warning disable CA1716
3226
public enum Namespace : int {
3327
#pragma warning restore CA1716
@@ -759,64 +753,6 @@ public Uuid(string uuid)
759753
#endif
760754
}
761755

762-
public static bool operator <(Uuid x, Uuid y) => x.CompareTo(y) < 0;
763-
public static bool operator <=(Uuid x, Uuid y) => x.CompareTo(y) <= 0;
764-
public static bool operator >(Uuid x, Uuid y) => y < x;
765-
public static bool operator >=(Uuid x, Uuid y) => y <= x;
766-
767-
public int CompareTo(object obj)
768-
{
769-
if (obj == null)
770-
return 1;
771-
else if (obj is Uuid uuid)
772-
return CompareTo(uuid);
773-
else if (obj is Guid guid)
774-
return CompareTo(guid);
775-
else
776-
throw new ArgumentException("obj is not Uuid", nameof(obj));
777-
}
778-
779-
public int CompareTo(Guid other)
780-
=> CompareTo((Uuid)other);
781-
782-
public int CompareTo(Uuid other)
783-
{
784-
int ret;
785-
786-
if ((ret = (int)(this.time_low - other.time_low)) != 0)
787-
return ret;
788-
789-
if ((ret = this.time_mid - other.time_mid) != 0)
790-
return ret;
791-
792-
if ((ret = this.time_hi_and_version - other.time_hi_and_version) != 0)
793-
return ret;
794-
795-
if ((ret = this.clock_seq_hi_and_reserved - other.clock_seq_hi_and_reserved) != 0)
796-
return ret;
797-
798-
if ((ret = this.clock_seq_low - other.clock_seq_low) != 0)
799-
return ret;
800-
801-
return node.CompareTo(other.node);
802-
}
803-
804-
public static bool operator ==(Uuid x, Uuid y) => x.fields_high == y.fields_high && x.fields_low == y.fields_low;
805-
public static bool operator !=(Uuid x, Uuid y) => x.fields_high != y.fields_high || x.fields_low != y.fields_low;
806-
807-
public override bool Equals(object obj)
808-
{
809-
if (obj is Uuid uuid)
810-
return Equals(uuid);
811-
else if (obj is Guid guid)
812-
return Equals(guid);
813-
else
814-
return false;
815-
}
816-
817-
public bool Equals(Guid other) => this == (Uuid)other;
818-
public bool Equals(Uuid other) => this == other;
819-
820756
public static explicit operator Guid(Uuid @value) => @value.ToGuid();
821757

822758
public static explicit operator Uuid(Guid @value) => new(@value);
@@ -884,72 +820,4 @@ public override int GetHashCode()
884820

885821
public override string ToString()
886822
=> ToString(null, null);
887-
888-
public string ToString(string format)
889-
=> ToString(format, null);
890-
891-
public string ToString(string format, IFormatProvider formatProvider)
892-
{
893-
if (format == "X") {
894-
const string xopen = "{";
895-
const string xclose = "}";
896-
897-
#if SYSTEM_FORMATTABLESTRING
898-
return FormattableString.Invariant(
899-
$"{xopen}0x{time_low:x8},0x{time_mid:x4},0x{time_hi_and_version:x4},{xopen}0x{clock_seq_hi_and_reserved:x2},0x{clock_seq_low:x2},0x{node.N0:x2},0x{node.N1:x2},0x{node.N2:x2},0x{node.N3:x2},0x{node.N4:x2},0x{node.N5:x2}{xclose}{xclose}"
900-
);
901-
#else
902-
return string.Format(
903-
CultureInfo.InvariantCulture,
904-
"{0}0x{1:x8},0x{2:x4},0x{3:x4},{0}0x{4:x2},0x{5:x2},0x{6:x2},0x{7:x2},0x{8:x2},0x{9:x2},0x{10:x2},0x{11:x2}{12}{12}",
905-
xopen,
906-
time_low,
907-
time_mid,
908-
time_hi_and_version,
909-
clock_seq_hi_and_reserved,
910-
clock_seq_low,
911-
node.N0,
912-
node.N1,
913-
node.N2,
914-
node.N3,
915-
node.N4,
916-
node.N5,
917-
xclose
918-
);
919-
#endif
920-
}
921-
922-
var (open, close, separator) = format switch {
923-
null or "" or "D" => (null, null, "-"),
924-
"B" => ("{", "}", "-"),
925-
"P" => ("(", ")", "-"),
926-
"N" => (null, null, null),
927-
_ => throw new FormatException($"invalid format: {format}"),
928-
};
929-
930-
#if SYSTEM_FORMATTABLESTRING
931-
return FormattableString.Invariant(
932-
$"{open}{time_low:x8}{separator}{time_mid:x4}{separator}{time_hi_and_version:x4}{separator}{clock_seq_hi_and_reserved:x2}{clock_seq_low:x2}{separator}{node.N0:x2}{node.N1:x2}{node.N2:x2}{node.N3:x2}{node.N4:x2}{node.N5:x2}{close}"
933-
);
934-
#else
935-
return string.Format(
936-
CultureInfo.InvariantCulture,
937-
"{0}{3:x8}{1}{4:x4}{1}{5:x4}{1}{6:x2}{7:x2}{1}{8:x2}{9:x2}{10:x2}{11:x2}{12:x2}{13:x2}{2}",
938-
open,
939-
separator,
940-
close,
941-
time_low,
942-
time_mid,
943-
time_hi_and_version,
944-
clock_seq_hi_and_reserved,
945-
clock_seq_low,
946-
node.N0,
947-
node.N1,
948-
node.N2,
949-
node.N3,
950-
node.N4,
951-
node.N5
952-
);
953-
#endif
954-
}
955823
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// SPDX-FileCopyrightText: 2022 smdn <smdn@smdn.jp>
2+
// SPDX-License-Identifier: MIT
3+
using System;
4+
5+
using NUnit.Framework;
6+
7+
namespace Smdn;
8+
9+
partial class UuidTests {
10+
[Test]
11+
public void TestCompareTo()
12+
{
13+
Assert.AreEqual(0, Uuid.RFC4122NamespaceDns.CompareTo(Uuid.RFC4122NamespaceDns));
14+
Assert.AreEqual(1, Uuid.RFC4122NamespaceDns.CompareTo(null));
15+
Assert.AreNotEqual(0, Uuid.RFC4122NamespaceDns.CompareTo(Guid.Empty));
16+
17+
Assert.Throws<ArgumentException>(() => Uuid.RFC4122NamespaceDns.CompareTo(1));
18+
19+
var ux = new Uuid("00000000-0000-0000-0000-000000000000");
20+
var uy = new Uuid("00000001-0000-0000-0000-000000000000");
21+
var gx = new Guid("00000000-0000-0000-0000-000000000000");
22+
var gy = new Guid("00000001-0000-0000-0000-000000000000");
23+
24+
Assert.That(ux.CompareTo(uy) < 0);
25+
Assert.That(ux.CompareTo(gy) < 0);
26+
Assert.That(0 < uy.CompareTo(ux));
27+
Assert.That(0 < uy.CompareTo(gx));
28+
29+
ux = new Uuid("00000000-0000-0000-0000-000000000000");
30+
uy = new Uuid("00000000-0000-0000-0000-000000000001");
31+
gx = new Guid("00000000-0000-0000-0000-000000000000");
32+
gy = new Guid("00000000-0000-0000-0000-000000000001");
33+
34+
Assert.That(ux.CompareTo(uy) < 0);
35+
Assert.That(ux.CompareTo(gy) < 0);
36+
Assert.That(0 < uy.CompareTo(ux));
37+
Assert.That(0 < uy.CompareTo(gx));
38+
}
39+
40+
[Test]
41+
public void TestGreaterThanLessThanOperator()
42+
{
43+
var x = new Uuid("00000000-0000-0000-0000-000000000000");
44+
var y = new Uuid("00000001-0000-0000-0000-000000000000");
45+
46+
Assert.IsTrue(x < y, "x < y");
47+
Assert.IsFalse(x > y, "x > y");
48+
49+
x = new Uuid("00000000-0000-0000-0000-000000000000");
50+
y = new Uuid("00000000-0000-0000-0000-000000000001");
51+
52+
Assert.IsTrue(x < y, "x < y");
53+
Assert.IsFalse(x > y, "x > y");
54+
}
55+
}

0 commit comments

Comments
 (0)