summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEugen Wissner <belka@caraus.de>2018-09-24 06:45:44 +0200
committerEugen Wissner <belka@caraus.de>2018-09-24 06:45:44 +0200
commitfd133554f31a767d27428f18b93d20eee4aa2250 (patch)
tree473e1a2ad043c56056f99b0c01cfb80d038bedb8
parent9ac56c50f16cf884eefb0f3477e657b455ad14f8 (diff)
downloadtanya-fd133554f31a767d27428f18b93d20eee4aa2250.tar.gz
net.ip: Implement opCmp. Fix #63
-rw-r--r--source/tanya/net/ip.d71
1 files changed, 71 insertions, 0 deletions
diff --git a/source/tanya/net/ip.d b/source/tanya/net/ip.d
index a53f3c4..02362af 100644
--- a/source/tanya/net/ip.d
+++ b/source/tanya/net/ip.d
@@ -14,6 +14,7 @@
*/
module tanya.net.ip;
+import tanya.algorithm.comparison;
import tanya.algorithm.mutation;
import tanya.container.string;
import tanya.conv;
@@ -67,6 +68,37 @@ struct Address4
}
/**
+ * Compares two $(D_PARAM Address4) objects.
+ *
+ * Params:
+ * that = Another address.
+ *
+ * Returns: Positive number if $(D_KEYWORD this) is larger than
+ * $(D_PARAM that), negative - if it is smaller, or 0 if they
+ * equal.
+ */
+ int opCmp(ref const Address4 that) const @nogc nothrow pure @safe
+ {
+ const lhs = toUInt();
+ const rhs = that.toUInt();
+ return (rhs < lhs) - (lhs < rhs);
+ }
+
+ /// ditto
+ int opCmp(const Address4 that) const @nogc nothrow pure @safe
+ {
+ return opCmp(that);
+ }
+
+ ///
+ @nogc nothrow pure @safe unittest
+ {
+ assert(address4("127.0.0.1") > address4("126.0.0.0"));
+ assert(address4("127.0.0.1") < address4("127.0.0.2"));
+ assert(address4("127.0.0.1") == address4("127.0.0.1"));
+ }
+
+ /**
* Returns object that represents 127.0.0.1.
*
* Returns: Object that represents the Loopback address.
@@ -424,6 +456,45 @@ struct Address6
}
/**
+ * Compares two $(D_PARAM Address6) objects.
+ *
+ * If $(D_KEYWORD this) and $(D_PARAM that) contain the same address, scope
+ * IDs are compared.
+ *
+ * Params:
+ * that = Another address.
+ *
+ * Returns: Positive number if $(D_KEYWORD this) is larger than
+ * $(D_PARAM that), negative - if it is smaller, or 0 if they
+ * equal.
+ */
+ int opCmp(ref const Address6 that) const @nogc nothrow pure @safe
+ {
+ const diff = compare(this.address[], that.address[]);
+ if (diff == 0)
+ {
+ return (that.scopeID < this.scopeID) - (this.scopeID < that.scopeID);
+ }
+ return diff;
+ }
+
+ /// ditto
+ int opCmp(const Address6 that) const @nogc nothrow pure @safe
+ {
+ return opCmp(that);
+ }
+
+ ///
+ @nogc nothrow @safe unittest
+ {
+ assert(address6("::14") > address6("::1"));
+ assert(address6("::1") < address6("::14"));
+ assert(address6("::1") == address6("::1"));
+ assert(address6("::1%1") < address6("::1%2"));
+ assert(address6("::1%2") > address6("::1%1"));
+ }
+
+ /**
* Returns object that represents ::.
*
* Returns: Object that represents any address.