diff options
| author | Eugen Wissner <belka@caraus.de> | 2025-09-03 14:01:58 +0200 |
|---|---|---|
| committer | Eugen Wissner <belka@caraus.de> | 2025-09-03 14:01:58 +0200 |
| commit | 6c38dd2606c66e70b0ab0e0aff7806a8db41ad1d (patch) | |
| tree | 5abecc4633cf37294e334e1258ca4a69df68eed6 /source | |
| parent | 2a67ccd9542d2d61117caefb2f59f5fdf205fbc2 (diff) | |
| download | tanya-6c38dd2606c66e70b0ab0e0aff7806a8db41ad1d.tar.gz | |
Merge math and hash packages
Diffstat (limited to 'source')
| -rw-r--r-- | source/tanya/container/hashtable.d | 2 | ||||
| -rw-r--r-- | source/tanya/container/set.d | 2 | ||||
| -rw-r--r-- | source/tanya/container/string.d | 2 | ||||
| -rw-r--r-- | source/tanya/hash/lookup.d | 342 | ||||
| -rw-r--r-- | source/tanya/hash/package.d | 15 | ||||
| -rw-r--r-- | source/tanya/math/hash.d | 840 | ||||
| -rw-r--r-- | source/tanya/math/random.d | 10 | ||||
| -rw-r--r-- | source/tanya/net/iface.d | 20 | ||||
| -rw-r--r-- | source/tanya/net/inet.d | 11 | ||||
| -rw-r--r-- | source/tanya/net/ip.d | 147 | ||||
| -rw-r--r-- | source/tanya/net/package.d | 1 | ||||
| -rw-r--r-- | source/tanya/net/uri.d | 411 | ||||
| -rw-r--r-- | source/tanya/range/primitive.d | 38 |
13 files changed, 1031 insertions, 810 deletions
diff --git a/source/tanya/container/hashtable.d b/source/tanya/container/hashtable.d index f1f8666..cf9f89b 100644 --- a/source/tanya/container/hashtable.d +++ b/source/tanya/container/hashtable.d @@ -20,7 +20,7 @@ import std.traits; import tanya.algorithm.mutation; import tanya.container.array; import tanya.container.entry; -import tanya.hash.lookup; +import tanya.math.hash; import tanya.memory.allocator; import tanya.memory.lifetime; import tanya.meta; diff --git a/source/tanya/container/set.d b/source/tanya/container/set.d index a8d9572..0c90b37 100644 --- a/source/tanya/container/set.d +++ b/source/tanya/container/set.d @@ -19,7 +19,7 @@ import std.range : isInfinite, isForwardRange; import std.traits; import tanya.container.array; import tanya.container.entry; -import tanya.hash.lookup; +import tanya.math.hash; import tanya.memory.allocator; import tanya.memory.lifetime; import tanya.meta; diff --git a/source/tanya/container/string.d b/source/tanya/container/string.d index 0401881..b94b275 100644 --- a/source/tanya/container/string.d +++ b/source/tanya/container/string.d @@ -31,7 +31,7 @@ import std.algorithm.mutation : bringToFront; import std.range : isInfinite, popFrontN, isInputRange; import std.traits; import tanya.algorithm.mutation; -import tanya.hash.lookup; +import tanya.math.hash; import tanya.memory.allocator; import tanya.memory.lifetime; import tanya.meta; diff --git a/source/tanya/hash/lookup.d b/source/tanya/hash/lookup.d deleted file mode 100644 index 2b5116d..0000000 --- a/source/tanya/hash/lookup.d +++ /dev/null @@ -1,342 +0,0 @@ -/* This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ - -/** - * Non-cryptographic, lookup hash functions. - * - * Copyright: Eugene Wissner 2018-2025. - * License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/, - * Mozilla Public License, v. 2.0). - * Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner) - * Source: $(LINK2 https://github.com/caraus-ecms/tanya/blob/master/source/tanya/hash/lookup.d, - * tanya/hash/lookup.d) - */ -module tanya.hash.lookup; - -import std.range : isInfinite, isInputRange; -import std.traits; -import tanya.meta; -import tanya.range.primitive; - -private struct Hasher -{ - static if (size_t.sizeof == 4) - { - enum uint offsetBasis = 2166136261; - enum uint prime = 16777619; - } - else static if (size_t.sizeof == 8) - { - enum ulong offsetBasis = 14695981039346656037UL; - enum ulong prime = 1099511628211UL; - } - else static if (size_t.sizeof == 16) - { - enum size_t offsetBasis = (size_t(0x6c62272e07bb0142UL) << 64) + 0x62b821756295c58dUL; - enum size_t prime = (size_t(1) << 88) + (1 << 8) + 0x3b; - } - else - { - static assert(false, "FNV requires at least 32-bit hash length"); - } - - size_t hash = offsetBasis; - - void opCall(T)(auto ref T key) - { - static if (is(typeof(key.toHash()) == size_t)) - { - opCall(key.toHash()); // Combine user-defined hashes - } - else static if (isScalarType!T || isPointer!T) - { - // Treat as an array of words - static if (T.sizeof % size_t.sizeof == 0 - && T.alignof >= size_t.alignof) - alias CastT = size_t; - // (64-bit or 128-bit) Treat as an array of ints - else static if (T.sizeof % uint.sizeof == 0 - && T.alignof >= uint.alignof) - alias CastT = uint; - // Treat as an array of bytes - else - alias CastT = ubyte; - add((() @trusted => (cast(const CastT*) &key)[0 .. T.sizeof / CastT.sizeof])()); - } - else static if (isArray!T && isScalarType!(ElementType!T)) - { - // Treat as an array of words - static if (ElementType!T.sizeof % size_t.sizeof == 0 - && ElementType!T.alignof >= size_t.alignof) - alias CastT = size_t; - // (64-bit or 128-bit) Treat as an array of ints - else static if (ElementType!T.sizeof % uint.sizeof == 0 - && ElementType!T.alignof >= uint.alignof) - alias CastT = uint; - // Treat as an array of bytes - else - alias CastT = ubyte; - add(cast(const CastT[]) key); - } - else static if (is(T == typeof(null))) - { - add(key); - } - else static if (isInputRange!T && !isInfinite!T) - { - foreach (e; key) - { - opCall(e); - } - } - else - { - static assert(false, "Hash function is not available"); - } - } - - void add(scope const ubyte[] key) @nogc nothrow pure @safe - { - // FNV-1a - foreach (c; key) - { - this.hash = (this.hash ^ c) * prime; - } - } - - void add(scope const size_t[] key) @nogc nothrow pure @safe - { - static if (size_t.sizeof == 4) - { - // Partial MurmurHash3_x86_32 (no finalization) - enum uint c1 = 0xcc9e2d51; - enum uint c2 = 0x1b873593; - alias h1 = hash; - foreach (x; key) - { - auto k1 = x * c1; - k1 = (k1 << 15) | (k1 >> (32 - 15)); - k1 *= c2; - - h1 ^= k1; - h1 = (h1 << 13) | (h1 >> (32 - 13)); - h1 = h1 * 5 + 0xe6546b64; - } - } - else static if (size_t.sizeof == 8) - { - // Partial 64-bit MurmurHash64A (no finalization) - alias h = hash; - enum ulong m = 0xc6a4a7935bd1e995UL; - foreach (x; key) - { - auto k = x * m; - k ^= k >>> 47; - k *= m; - - h ^= k; - h *= m; - } - } - else static if (size_t.sizeof == 16) - { - // Partial MurmurHash3_x64_128 (no finalization) - // treating each size_t as a pair of ulong. - ulong h1 = cast(ulong) hash; - ulong h2 = cast(ulong) (hash >> 64); - - enum ulong c1 = 0x87c37b91114253d5UL; - enum ulong c2 = 0x4cf5ad432745937fUL; - - foreach (x; key) - { - auto k1 = cast(ulong) x; - auto k2 = cast(ulong) (x >> 64); - - k1 *= c1; k1 = (k1 << 32) | (k1 >> (64 - 31)); k1 *= c2; h1 ^= k1; - h1 = (h1 << 27) | (h1 >> (64 - 27)); h1 += h2; h1 = h1*5+0x52dce729; - k2 *= c2; k2 = (k2 << 33) | (k2 >> (64 - 33)); k2 *= c1; h2 ^= k2; - h2 = (h2 << 31) | (h2 >> (64 - 31)); h2 += h1; h2 = h2*5+0x38495ab5; - } - - hash = cast(size_t) h1 + ((cast(size_t) h2) << 64); - } - else - { - static assert(0, "Hash length must be either 32, 64, or 128 bits."); - } - } - - static if (size_t.sizeof != uint.sizeof) - void add(scope const uint[] key) @nogc nothrow pure @trusted - { - static if (size_t.sizeof == 8) - { - // Partial 32-bit MurmurHash64B (no finalization) - enum uint m = 0x5bd1e995; - enum r = 24; - - uint h1 = cast(uint) hash; - uint h2 = cast(uint) (hash >> 32); - const(uint)* data = key.ptr; - auto len = key.length; - - for (; len >= 2; data += 2, len -= 2) - { - uint k1 = data[0]; - k1 *= m; k1 ^= k1 >> r; k1 *= m; - h1 *= m; h1 ^= k1; - - uint k2 = data[1]; - k2 *= m; k2 ^= k2 >> r; k2 *= m; - h2 *= m; h2 ^= k2; - } - if (len) - { - uint k1 = data[0]; - k1 *= m; k1 ^= k1 >> r; k1 *= m; - h1 *= m; h1 ^= k1; - } - hash = cast(ulong) h1 + ((cast(ulong) h2) << 32); - } - else static if (size_t.sizeof == 16) - { - // Partial MurmurHash3_x86_128 (no finalization) - enum uint c1 = 0x239b961b; - enum uint c2 = 0xab0e9789; - enum uint c3 = 0x38b34ae5; - enum uint c4 = 0xa1e38b93; - - uint h1 = cast(uint) hash; - uint h2 = cast(uint) (hash >> 32); - uint h3 = cast(uint) (hash >> 64); - uint h4 = cast(uint) (hash >> 96); - const(uint)* data = key.ptr; - auto len = key.length; - - for (; len >= 4; data += 4, len -= 4) - { - uint k1 = data[0]; - uint k2 = data[1]; - uint k3 = data[2]; - uint k4 = data[3]; - - h1 = (h1 << 19) | (h1 >> (32 - 19)); h1 += h2; h1 = h1*5+0x561ccd1b; - k2 *= c2; k2 = (k2 << 16) | (k2 >> (32 - 16)); k2 *= c3; h2 ^= k2; - h2 = (h2 << 17) | (h2 >> (32 - 17)); h2 += h3; h2 = h2*5+0x0bcaa747; - k3 *= c3; k3 = (k3 << 17) | (k3 >> (32 - 17)); k3 *= c4; h3 ^= k3; - h3 = (h3 << 15) | (h3 >> (32 - 15)); h3 += h4; h3 = h3*5+0x96cd1c35; - k4 *= c4; k4 = (k4 << 18) | (k4 >> (32 - 18)); k4 *= c1; h4 ^= k4; - h4 = (h4 << 13) | (h4 >> (32 - 13)); h4 += h1; h4 = h4*5+0x32ac3b17; - } - uint k1, k2, k3; - switch (len) // 0, 1, 2, 3 - { - case 3: - k3 = data[2]; - k3 *= c3; k3 = (k3 << 17) | (k3 >> (32 - 17)); k3 *= c4; h3 ^= k3; - goto case; - case 2: - k2 = data[1]; - k2 *= c2; k2 = (k2 << 16) | (k2 >> (32 - 16)); k2 *= c3; h2 ^= k2; - goto case; - case 1: - k1 = data[0]; - k1 *= c1; k1 = (k1 << 15) | (k1 >> (32 - 15)); k1 *= c2; h1 ^= k1; - break; - } - hash = cast(size_t) h1 + - ((cast(size_t) h2) << 32) + - ((cast(size_t) h3) << 64) + - ((cast(size_t) h4) << 96); - } - else - { - static assert(0, "Hash length must be either 32, 64, or 128 bits."); - } - } -} - -/** - * Takes an argument of an arbitrary type $(D_PARAM T) and calculates the hash - * value. - * - * Hash calculation is supported for all scalar types. Aggregate types, like - * $(D_KEYWORD struct)s, should implement `toHash`-function: - * --- - * size_t toHash() const - * { - * return hash; - * } - * --- - * - * For pointers and for scalar types implicitly convertible to `size_t` this - * is an identity operation (i.e. the value is cast to `size_t` and returned - * unaltered). Integer types wider than `size_t` are XOR folded down to - * `size_t`. Other scalar types use an architecture-dependent hash function - * based on their width and alignment. - * If the type provides a `toHash`-function, only `toHash()` is called and its - * result is returned. - * - * This function also accepts input ranges that contain hashable elements. - * Individual values are combined then and the resulting hash is returned. - * - * Params: - * T = Hashable type. - * key = Hashable value. - * - * Returns: Calculated hash value. - * - * See_Also: $(LINK http://www.isthe.com/chongo/tech/comp/fnv/). - */ -size_t hash(T)(auto ref T key) -{ - static if (is(typeof(key.toHash()) == size_t)) - { - return key.toHash(); - } - else static if ((isIntegral!T || isSomeChar!T || isBoolean!T) - && T.sizeof <= size_t.sizeof) - { - return cast(size_t) key; - } - else static if (isIntegral!T && T.sizeof > size_t.sizeof) - { - return cast(size_t) (key ^ (key >>> (size_t.sizeof * 8))); - } - else static if (isPointer!T || is(T : typeof(null))) - { - return (() @trusted => cast(size_t) key)(); - } - else - { - Hasher hasher; - hasher(key); - return hasher.hash; - } -} - -/** - * Determines whether $(D_PARAM hasher) is hash function for $(D_PARAM T), i.e. - * it is callable with a value of type $(D_PARAM T) and returns a - * $(D_PSYMBOL size_t) value. - * - * Params: - * hasher = Hash function candidate. - * T = Type to test the hash function with. - * - * Returns: $(D_KEYWORD true) if $(D_PARAM hasher) is a hash function for - * $(D_PARAM T), $(D_KEYWORD false) otherwise. - */ -template isHashFunction(alias hasher, T) -{ - private alias wrapper = (T x) => hasher(x); - enum bool isHashFunction = is(typeof(wrapper(T.init)) == size_t); -} - -/// -@nogc nothrow pure @safe unittest -{ - static assert(isHashFunction!(hash, int)); -} diff --git a/source/tanya/hash/package.d b/source/tanya/hash/package.d deleted file mode 100644 index 89f6440..0000000 --- a/source/tanya/hash/package.d +++ /dev/null @@ -1,15 +0,0 @@ -/* This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ - -/** - * Copyright: Eugene Wissner 2018-2025. - * License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/, - * Mozilla Public License, v. 2.0). - * Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner) - * Source: $(LINK2 https://github.com/caraus-ecms/tanya/blob/master/source/tanya/hash/package.d, - * tanya/hash/package.d) - */ -module tanya.hash; - -public import tanya.hash.lookup; diff --git a/source/tanya/math/hash.d b/source/tanya/math/hash.d new file mode 100644 index 0000000..feba9c9 --- /dev/null +++ b/source/tanya/math/hash.d @@ -0,0 +1,840 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +/** + * Non-cryptographic, lookup hash functions. + * + * Copyright: Eugene Wissner 2018-2025. + * License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/, + * Mozilla Public License, v. 2.0). + * Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner) + * Source: $(LINK2 https://github.com/caraus-ecms/tanya/blob/master/source/tanya/hash/lookup.d, + * tanya/hash/lookup.d) + */ +module tanya.math.hash; + +import std.range : isInfinite, isInputRange; +import std.traits; +import tanya.meta; +import tanya.range.primitive; + +version (unittest) { + import tanya.test.stub; + + private enum string r10(string x) = x ~ x ~ x ~ x ~ x ~ x ~ x ~ x ~ x ~ x; + private enum string r100(string x) = r10!x ~ r10!x ~ r10!x ~ r10!x ~ r10!x + ~ r10!x ~ r10!x ~ r10!x ~ r10!x ~ r10!x; + private enum string r500(string x) = r100!x ~ r100!x ~ r100!x ~ r100!x ~ r100!x; + + private struct HashRange + { + string fo = "fo"; + + @property ubyte front() const @nogc nothrow pure @safe + { + return this.fo[0]; + } + + void popFront() @nogc nothrow pure @safe + { + this.fo = this.fo[1 .. $]; + } + + @property bool empty() const @nogc nothrow pure @safe + { + return this.fo.length == 0; + } + } + + private struct ToHashRange + { + bool empty_; + + @property Hashable front() const @nogc nothrow pure @safe + { + return Hashable(); + } + + void popFront() @nogc nothrow pure @safe + { + this.empty_ = true; + } + + @property bool empty() const @nogc nothrow pure @safe + { + return this.empty_; + } + } +} + +private struct Hasher +{ + static if (size_t.sizeof == 4) + { + enum uint offsetBasis = 2166136261; + enum uint prime = 16777619; + } + else static if (size_t.sizeof == 8) + { + enum ulong offsetBasis = 14695981039346656037UL; + enum ulong prime = 1099511628211UL; + } + else static if (size_t.sizeof == 16) + { + enum size_t offsetBasis = (size_t(0x6c62272e07bb0142UL) << 64) + 0x62b821756295c58dUL; + enum size_t prime = (size_t(1) << 88) + (1 << 8) + 0x3b; + } + else + { + static assert(false, "FNV requires at least 32-bit hash length"); + } + + size_t hash = offsetBasis; + + void opCall(T)(auto ref T key) + { + static if (is(typeof(key.toHash()) == size_t)) + { + opCall(key.toHash()); // Combine user-defined hashes + } + else static if (isScalarType!T || isPointer!T) + { + // Treat as an array of words + static if (T.sizeof % size_t.sizeof == 0 + && T.alignof >= size_t.alignof) + alias CastT = size_t; + // (64-bit or 128-bit) Treat as an array of ints + else static if (T.sizeof % uint.sizeof == 0 + && T.alignof >= uint.alignof) + alias CastT = uint; + // Treat as an array of bytes + else + alias CastT = ubyte; + add((() @trusted => (cast(const CastT*) &key)[0 .. T.sizeof / CastT.sizeof])()); + } + else static if (isArray!T && isScalarType!(ElementType!T)) + { + // Treat as an array of words + static if (ElementType!T.sizeof % size_t.sizeof == 0 + && ElementType!T.alignof >= size_t.alignof) + alias CastT = size_t; + // (64-bit or 128-bit) Treat as an array of ints + else static if (ElementType!T.sizeof % uint.sizeof == 0 + && ElementType!T.alignof >= uint.alignof) + alias CastT = uint; + // Treat as an array of bytes + else + alias CastT = ubyte; + add(cast(const CastT[]) key); + } + else static if (is(T == typeof(null))) + { + add(key); + } + else static if (isInputRange!T && !isInfinite!T) + { + foreach (e; key) + { + opCall(e); + } + } + else + { + static assert(false, "Hash function is not available"); + } + } + + void add(scope const ubyte[] key) @nogc nothrow pure @safe + { + // FNV-1a + foreach (c; key) + { + this.hash = (this.hash ^ c) * prime; + } + } + + void add(scope const size_t[] key) @nogc nothrow pure @safe + { + static if (size_t.sizeof == 4) + { + // Partial MurmurHash3_x86_32 (no finalization) + enum uint c1 = 0xcc9e2d51; + enum uint c2 = 0x1b873593; + alias h1 = hash; + foreach (x; key) + { + auto k1 = x * c1; + k1 = (k1 << 15) | (k1 >> (32 - 15)); + k1 *= c2; + + h1 ^= k1; + h1 = (h1 << 13) | (h1 >> (32 - 13)); + h1 = h1 * 5 + 0xe6546b64; + } + } + else static if (size_t.sizeof == 8) + { + // Partial 64-bit MurmurHash64A (no finalization) + alias h = hash; + enum ulong m = 0xc6a4a7935bd1e995UL; + foreach (x; key) + { + auto k = x * m; + k ^= k >>> 47; + k *= m; + + h ^= k; + h *= m; + } + } + else static if (size_t.sizeof == 16) + { + // Partial MurmurHash3_x64_128 (no finalization) + // treating each size_t as a pair of ulong. + ulong h1 = cast(ulong) hash; + ulong h2 = cast(ulong) (hash >> 64); + + enum ulong c1 = 0x87c37b91114253d5UL; + enum ulong c2 = 0x4cf5ad432745937fUL; + + foreach (x; key) + { + auto k1 = cast(ulong) x; + auto k2 = cast(ulong) (x >> 64); + + k1 *= c1; k1 = (k1 << 32) | (k1 >> (64 - 31)); k1 *= c2; h1 ^= k1; + h1 = (h1 << 27) | (h1 >> (64 - 27)); h1 += h2; h1 = h1*5+0x52dce729; + k2 *= c2; k2 = (k2 << 33) | (k2 >> (64 - 33)); k2 *= c1; h2 ^= k2; + h2 = (h2 << 31) | (h2 >> (64 - 31)); h2 += h1; h2 = h2*5+0x38495ab5; + } + + hash = cast(size_t) h1 + ((cast(size_t) h2) << 64); + } + else + { + static assert(0, "Hash length must be either 32, 64, or 128 bits."); + } + } + + static if (size_t.sizeof != uint.sizeof) + void add(scope const uint[] key) @nogc nothrow pure @trusted + { + static if (size_t.sizeof == 8) + { + // Partial 32-bit MurmurHash64B (no finalization) + enum uint m = 0x5bd1e995; + enum r = 24; + + uint h1 = cast(uint) hash; + uint h2 = cast(uint) (hash >> 32); + const(uint)* data = key.ptr; + auto len = key.length; + + for (; len >= 2; data += 2, len -= 2) + { + uint k1 = data[0]; + k1 *= m; k1 ^= k1 >> r; k1 *= m; + h1 *= m; h1 ^= k1; + + uint k2 = data[1]; + k2 *= m; k2 ^= k2 >> r; k2 *= m; + h2 *= m; h2 ^= k2; + } + if (len) + { + uint k1 = data[0]; + k1 *= m; k1 ^= k1 >> r; k1 *= m; + h1 *= m; h1 ^= k1; + } + hash = cast(ulong) h1 + ((cast(ulong) h2) << 32); + } + else static if (size_t.sizeof == 16) + { + // Partial MurmurHash3_x86_128 (no finalization) + enum uint c1 = 0x239b961b; + enum uint c2 = 0xab0e9789; + enum uint c3 = 0x38b34ae5; + enum uint c4 = 0xa1e38b93; + + uint h1 = cast(uint) hash; + uint h2 = cast(uint) (hash >> 32); + uint h3 = cast(uint) (hash >> 64); + uint h4 = cast(uint) (hash >> 96); + const(uint)* data = key.ptr; + auto len = key.length; + + for (; len >= 4; data += 4, len -= 4) + { + uint k1 = data[0]; + uint k2 = data[1]; + uint k3 = data[2]; + uint k4 = data[3]; + + h1 = (h1 << 19) | (h1 >> (32 - 19)); h1 += h2; h1 = h1*5+0x561ccd1b; + k2 *= c2; k2 = (k2 << 16) | (k2 >> (32 - 16)); k2 *= c3; h2 ^= k2; + h2 = (h2 << 17) | (h2 >> (32 - 17)); h2 += h3; h2 = h2*5+0x0bcaa747; + k3 *= c3; k3 = (k3 << 17) | (k3 >> (32 - 17)); k3 *= c4; h3 ^= k3; + h3 = (h3 << 15) | (h3 >> (32 - 15)); h3 += h4; h3 = h3*5+0x96cd1c35; + k4 *= c4; k4 = (k4 << 18) | (k4 >> (32 - 18)); k4 *= c1; h4 ^= k4; + h4 = (h4 << 13) | (h4 >> (32 - 13)); h4 += h1; h4 = h4*5+0x32ac3b17; + } + uint k1, k2, k3; + switch (len) // 0, 1, 2, 3 + { + case 3: + k3 = data[2]; + k3 *= c3; k3 = (k3 << 17) | (k3 >> (32 - 17)); k3 *= c4; h3 ^= k3; + goto case; + case 2: + k2 = data[1]; + k2 *= c2; k2 = (k2 << 16) | (k2 >> (32 - 16)); k2 *= c3; h2 ^= k2; + goto case; + case 1: + k1 = data[0]; + k1 *= c1; k1 = (k1 << 15) | (k1 >> (32 - 15)); k1 *= c2; h1 ^= k1; + break; + } + hash = cast(size_t) h1 + + ((cast(size_t) h2) << 32) + + ((cast(size_t) h3) << 64) + + ((cast(size_t) h4) << 96); + } + else + { + static assert(0, "Hash length must be either 32, 64, or 128 bits."); + } + } +} + +/** + * Takes an argument of an arbitrary type $(D_PARAM T) and calculates the hash + * value. + * + * Hash calculation is supported for all scalar types. Aggregate types, like + * $(D_KEYWORD struct)s, should implement `toHash`-function: + * --- + * size_t toHash() const + * { + * return hash; + * } + * --- + * + * For pointers and for scalar types implicitly convertible to `size_t` this + * is an identity operation (i.e. the value is cast to `size_t` and returned + * unaltered). Integer types wider than `size_t` are XOR folded down to + * `size_t`. Other scalar types use an architecture-dependent hash function + * based on their width and alignment. + * If the type provides a `toHash`-function, only `toHash()` is called and its + * result is returned. + * + * This function also accepts input ranges that contain hashable elements. + * Individual values are combined then and the resulting hash is returned. + * + * Params: + * T = Hashable type. + * key = Hashable value. + * + * Returns: Calculated hash value. + * + * See_Also: $(LINK http://www.isthe.com/chongo/tech/comp/fnv/). + */ +size_t hash(T)(auto ref T key) +{ + static if (is(typeof(key.toHash()) == size_t)) + { + return key.toHash(); + } + else static if ((isIntegral!T || isSomeChar!T || isBoolean!T) + && T.sizeof <= size_t.sizeof) + { + return cast(size_t) key; + } + else static if (isIntegral!T && T.sizeof > size_t.sizeof) + { + return cast(size_t) (key ^ (key >>> (size_t.sizeof * 8))); + } + else static if (isPointer!T || is(T : typeof(null))) + { + return (() @trusted => cast(size_t) key)(); + } + else + { + Hasher hasher; + hasher(key); + return hasher.hash; + } +} + +// Tests that work for any hash size +@nogc nothrow pure @safe unittest +{ + assert(hash(null) == 0); + assert(hash(Hashable()) == 0U); + assert(hash('a') == 'a'); +} + +static if (size_t.sizeof == 4) @nogc nothrow pure @safe unittest +{ + assert(hash(HashRange()) == 0x6222e842U); + assert(hash(ToHashRange()) == 3371162643U); +} + +static if (size_t.sizeof == 8) @nogc nothrow pure @safe unittest +{ + assert(hash(HashRange()) == 0x08985907b541d342UL); + assert(hash(ToHashRange()) == 2072958611659694473); +} + +static if (size_t.sizeof == 4) @nogc nothrow pure @system unittest +{ + assert(hash(cast(void*) 0x6e6f6863) == 0x6e6f6863); +} + +static if (size_t.sizeof == 8) @nogc nothrow pure @system unittest +{ + assert(hash(cast(void*) 0x77206f676e6f6863) == 0x77206f676e6f6863); +} + +/* + * These are official FNV-1a test vectors and they are in the public domain. + */ +// FNV-1a 32 bit test vectors +static if (size_t.sizeof == 4) @nogc nothrow pure @safe unittest +{ + assert(hash("") == 0x811c9dc5U); + assert(hash("a") == 0xe40c292cU); + assert(hash("b") == 0xe70c2de5U); + assert(hash("c") == 0xe60c2c52U); + assert(hash("d") == 0xe10c2473U); + assert(hash("e") == 0xe00c22e0U); + assert(hash("f") == 0xe30c2799U); + assert(hash("fo") == 0x6222e842U); + assert(hash("foo") == 0xa9f37ed7U); + assert(hash("foob") == 0x3f5076efU); + assert(hash("fooba") == 0x39aaa18aU); + assert(hash("foobar") == 0xbf9cf968U); + assert(hash("\0") == 0x050c5d1fU); + assert(hash("a\0") == 0x2b24d044U); + assert(hash("b\0") == 0x9d2c3f7fU); + assert(hash("c\0") == 0x7729c516U); + assert(hash("d\0") == 0xb91d6109U); + assert(hash("e\0") == 0x931ae6a0U); + assert(hash("f\0") == 0x052255dbU); + assert(hash("fo\0") == 0xbef39fe6U); + assert(hash("foo\0") == 0x6150ac75U); + assert(hash("foob\0") == 0x9aab3a3dU); + assert(hash("fooba\0") == 0x519c4c3eU); + assert(hash("foobar\0") == 0x0c1c9eb8U); + assert(hash("ch") == 0x5f299f4eU); + assert(hash("cho") == 0xef8580f3U); + assert(hash("chon") == 0xac297727U); + assert(hash("chong") == 0x4546b9c0U); + assert(hash("chongo") == 0xbd564e7dU); + assert(hash("chongo ") == 0x6bdd5c67U); + assert(hash("chongo w") == 0xdd77ed30U); + assert(hash("chongo wa") == 0xf4ca9683U); + assert(hash("chongo was") == 0x4aeb9bd0U); + assert(hash("chongo was ") == 0xe0e67ad0U); + assert(hash("chongo was h") == 0xc2d32fa8U); + assert(hash("chongo was he") == 0x7f743fb7U); + assert(hash("chongo was her") == 0x6900631fU); + assert(hash("chongo was here") == 0xc59c990eU); + assert(hash("chongo was here!") == 0x448524fdU); + assert(hash("chongo was here!\n") == 0xd49930d5U); + assert(hash("ch\0") == 0x1c85c7caU); + assert(hash("cho\0") == 0x0229fe89U); + assert(hash("chon\0") == 0x2c469265U); + assert(hash("chong\0") == 0xce566940U); + assert(hash("chongo\0") == 0x8bdd8ec7U); + assert(hash("chongo \0") == 0x34787625U); + assert(hash("chongo w\0") == 0xd3ca6290U); + assert(hash("chongo wa\0") == 0xddeaf039U); + assert(hash("chongo was\0") == 0xc0e64870U); + assert(hash("chongo was \0") == 0xdad35570U); + assert(hash("chongo was h\0") == 0x5a740578U); + assert(hash("chongo was he\0") == 0x5b004d15U); + assert(hash("chongo was her\0") == 0x6a9c09cdU); + assert(hash("chongo was here\0") == 0x2384f10aU); + assert(hash("chongo was here!\0") == 0xda993a47U); + assert(hash("chongo was here!\n\0") == 0x8227df4fU); + assert(hash("cu") == 0x4c298165U); + assert(hash("cur") == 0xfc563735U); + assert(hash("curd") == 0x8cb91483U); + assert(hash("curds") == 0x775bf5d0U); + assert(hash("curds ") == 0xd5c428d0U); + assert(hash("curds a") == 0x34cc0ea3U); + assert(hash("curds an") == 0xea3b4cb7U); + assert(hash("curds and") == 0x8e59f029U); + assert(hash("curds and ") == 0x2094de2bU); + assert(hash("curds and w") == 0xa65a0ad4U); + assert(hash("curds and wh") == 0x9bbee5f4U); + assert(hash("curds and whe") == 0xbe836343U); + assert(hash("curds and whey") == 0x22d5344eU); + assert(hash("curds and whey\n") == 0x19a1470cU); + assert(hash("cu\0") == 0x4a56b1ffU); + assert(hash("cur\0") == 0x70b8e86fU); + assert(hash("curd\0") == 0x0a5b4a39U); + assert(hash("curds\0") == 0xb5c3f670U); + assert(hash("curds \0") == 0x53cc3f70U); + assert(hash("curds a\0") == 0xc03b0a99U); + assert(hash("curds an\0") == 0x7259c415U); + assert(hash("curds and\0") == 0x4095108bU); + assert(hash("curds and \0") == 0x7559bdb1U); + assert(hash("curds and w\0") == 0xb3bf0bbcU); + assert(hash("curds and wh\0") == 0x2183ff1cU); + assert(hash("curds and whe\0") == 0x2bd54279U); + assert(hash("curds and whey\0") == 0x23a156caU); + assert(hash("curds and whey\n\0") == 0x64e2d7e4U); + assert(hash("hi") == 0x683af69aU); + assert(hash("hi\0") == 0xaed2346eU); + assert(hash("hello") == 0x4f9f2cabU); + assert(hash("hello\0") == 0x02935131U); + assert(hash("\xff\x00\x00\x01") == 0xc48fb86dU); + assert(hash("\x01\x00\x00\xff") == 0x2269f369U); + assert(hash("\xff\x00\x00\x02") == 0xc18fb3b4U); + assert(hash("\x02\x00\x00\xff") == 0x50ef1236U); + assert(hash("\xff\x00\x00\x03") == 0xc28fb547U); + assert(hash("\x03\x00\x00\xff") == 0x96c3bf47U); + assert(hash("\xff\x00\x00\x04") == 0xbf8fb08eU); + assert(hash("\x04\x00\x00\xff") == 0xf3e4d49cU); + assert(hash("\x40\x51\x4e\x44") == 0x32179058U); + assert(hash("\x44\x4e\x51\x40") == 0x280bfee6U); + assert(hash("\x40\x51\x4e\x4a") == 0x30178d32U); + assert(hash("\x4a\x4e\x51\x40") == 0x21addaf8U); + assert(hash("\x40\x51\x4e\x54") == 0x4217a988U); + assert(hash("\x54\x4e\x51\x40") == 0x772633d6U); + assert(hash("127.0.0.1") == 0x08a3d11eU); + assert(hash("127.0.0.1\0") == 0xb7e2323aU); + assert(hash("127.0.0.2") == 0x07a3cf8bU); + assert(hash("127.0.0.2\0") == 0x91dfb7d1U); + assert(hash("127.0.0.3") == 0x06a3cdf8U); + assert(hash("127.0.0.3\0") == 0x6bdd3d68U); + assert(hash("64.81.78.68") == 0x1d5636a7U); + assert(hash("64.81.78.68\0") == 0xd5b808e5U); + assert(hash("64.81.78.74") == 0x1353e852U); + assert(hash("64.81.78.74\0") == 0xbf16b916U); + assert(hash("64.81.78.84") == 0xa55b89edU); + assert(hash("64.81.78.84\0") == 0x3c1a2017U); + assert(hash("feedface") == 0x0588b13cU); + assert(hash("feedface\0") == 0xf22f0174U); + assert(hash("feedfacedaffdeed") == 0xe83641e1U); + assert(hash("feedfacedaffdeed\0") == 0x6e69b533U); + assert(hash("feedfacedeadbeef") == 0xf1760448U); + assert(hash("feedfacedeadbeef\0") == 0x64c8bd58U); + assert(hash("line 1\nline 2\nline 3") == 0x97b4ea23U); + assert(hash("chongo <Landon Curt Noll> /\\../\\") == 0x9a4e92e6U); + assert(hash("chongo <Landon Curt Noll> /\\../\\\0") == 0xcfb14012U); + assert(hash("chongo (Landon Curt Noll) /\\../\\") == 0xf01b2511U); + assert(hash("chongo (Landon Curt Noll) /\\../\\\0") == 0x0bbb59c3U); + assert(hash("http://antwrp.gsfc.nasa.gov/apod/astropix.html") == 0xce524afaU); + assert(hash("http://en.wikipedia.org/wiki/Fowler_Noll_Vo_hash") == 0xdd16ef45U); + assert(hash("http://epod.usra.edu/") == 0x60648bb3U); + assert(hash("http://exoplanet.eu/") == 0x7fa4bcfcU); + assert(hash("http://hvo.wr.usgs.gov/cam3/") == 0x5053ae17U); + assert(hash("http://hvo.wr.usgs.gov/cams/HMcam/") == 0xc9302890U); + assert(hash("http://hvo.wr.usgs.gov/kilauea/update/deformation.html") == 0x956ded32U); + assert(hash("http://hvo.wr.usgs.gov/kilauea/update/images.html") == 0x9136db84U); + assert(hash("http://hvo.wr.usgs.gov/kilauea/update/maps.html") == 0xdf9d3323U); + assert(hash("http://hvo.wr.usgs.gov/volcanowatch/current_issue.html") == 0x32bb6cd0U); + assert(hash("http://neo.jpl.nasa.gov/risk/") == 0xc8f8385bU); + assert(hash("http://norvig.com/21-days.html") == 0xeb08bfbaU); + assert(hash("http://primes.utm.edu/curios/home.php") == 0x62cc8e3dU); + assert(hash("http://slashdot.org/") == 0xc3e20f5cU); + assert(hash("http://tux.wr.usgs.gov/Maps/155.25-19.5.html") == 0x39e97f17U); + assert(hash("http://volcano.wr.usgs.gov/kilaueastatus.php") == 0x7837b203U); + assert(hash("http://www.avo.alaska.edu/activity/Redoubt.php") == 0x319e877bU); + assert(hash("http://www.dilbert.com/fast/") == 0xd3e63f89U); + assert(hash("http://www.fourmilab.ch/gravitation/orbits/") == 0x29b50b38U); + assert(hash("http://www.fpoa.net/") == 0x5ed678b8U); + assert(hash("http://www.ioccc.org/index.html") == 0xb0d5b793U); + assert(hash("http://www.isthe.com/cgi-bin/number.cgi") == 0x52450be5U); + assert(hash("http://www.isthe.com/chongo/bio.html") == 0xfa72d767U); + assert(hash("http://www.isthe.com/chongo/index.html") == 0x95066709U); + assert(hash("http://www.isthe.com/chongo/src/calc/lucas-calc") == 0x7f52e123U); + assert(hash("http://www.isthe.com/chongo/tech/astro/venus2004.html") == 0x76966481U); + assert(hash("http://www.isthe.com/chongo/tech/astro/vita.html") == 0x063258b0U); + assert(hash("http://www.isthe.com/chongo/tech/comp/c/expert.html") == 0x2ded6e8aU); + assert(hash("http://www.isthe.com/chongo/tech/comp/calc/index.html") == 0xb07d7c52U); + assert(hash("http://www.isthe.com/chongo/tech/comp/fnv/index.html") == 0xd0c71b71U); + assert(hash("http://www.isthe.com/chongo/tech/math/number/howhigh.html") == 0xf684f1bdU); + assert(hash("http://www.isthe.com/chongo/tech/math/number/number.html") == 0x868ecfa8U); + assert(hash("http://www.isthe.com/chongo/tech/math/prime/mersenne.html") == 0xf794f684U); + assert(hash("http://www.isthe.com/chongo/tech/math/prime/mersenne.html#largest") == 0xd19701c3U); + assert(hash("http://www.lavarnd.org/cgi-bin/corpspeak.cgi") == 0x346e171eU); + assert(hash("http://www.lavarnd.org/cgi-bin/haiku.cgi") == 0x91f8f676U); + assert(hash("http://www.lavarnd.org/cgi-bin/rand-none.cgi") == 0x0bf58848U); + assert(hash("http://www.lavarnd.org/cgi-bin/randdist.cgi") == 0x6317b6d1U); + assert(hash("http://www.lavarnd.org/index.html") == 0xafad4c54U); + assert(hash("http://www.lavarnd.org/what/nist-test.html") == 0x0f25681eU); + assert(hash("http://www.macosxhints.com/") == 0x91b18d49U); + assert(hash("http://www.mellis.com/") == 0x7d61c12eU); + assert(hash("http://www.nature.nps.gov/air/webcams/parks/havoso2alert/havoalert.cfm") == 0x5147d25cU); + assert(hash("http://www.nature.nps.gov/air/webcams/parks/havoso2alert/timelines_24.cfm") == 0x9a8b6805U); + assert(hash("http://www.paulnoll.com/") == 0x4cd2a447U); + assert(hash("http://www.pepysdiary.com/") == 0x1e549b14U); + assert(hash("http://www.sciencenews.org/index/home/activity/view") == 0x2fe1b574U); + assert(hash("http://www.skyandtelescope.com/") == 0xcf0cd31eU); + assert(hash("http://www.sput.nl/~rob/sirius.html") == 0x6c471669U); + assert(hash("http://www.systemexperts.com/") == 0x0e5eef1eU); + assert(hash("http://www.tq-international.com/phpBB3/index.php") == 0x2bed3602U); + assert(hash("http://www.travelquesttours.com/index.htm") == 0xb26249e0U); + assert(hash("http://www.wunderground.com/global/stations/89606.html") == 0x2c9b86a4U); + assert(hash(r10!"21701") == 0xe415e2bbU); + assert(hash(r10!"M21701") == 0x18a98d1dU); + assert(hash(r10!"2^21701-1") == 0xb7df8b7bU); + assert(hash(r10!"\x54\xc5") == 0x241e9075U); + assert(hash(r10!"\xc5\x54") == 0x063f70ddU); + assert(hash(r10!"23209") == 0x0295aed9U); + assert(hash(r10!"M23209") == 0x56a7f781U); + assert(hash(r10!"2^23209-1") == 0x253bc645U); + assert(hash(r10!"\x5a\xa9") == 0x46610921U); + assert(hash(r10!"\xa9\x5a") == 0x7c1577f9U); + assert(hash(r10!"391581216093") == 0x512b2851U); + assert(hash(r10!"391581*2^216093-1") == 0x76823999U); + assert(hash(r10!"\x05\xf9\x9d\x03\x4c\x81") == 0xc0586935U); + assert(hash(r10!"FEDCBA9876543210") == 0xf3415c85U); + assert(hash(r10!"\xfe\xdc\xba\x98\x76\x54\x32\x10") == 0x0ae4ff65U); + assert(hash(r10!"EFCDAB8967452301") == 0x58b79725U); + assert(hash(r10!"\xef\xcd\xab\x89\x67\x45\x23\x01") == 0xdea43aa5U); + assert(hash(r10!"0123456789ABCDEF") == 0x2bb3be35U); + assert(hash(r10!"\x01\x23\x45\x67\x89\xab\xcd\xef") == 0xea777a45U); + assert(hash(r10!"1032547698BADCFE") == 0x8f21c305U); + assert(hash(r10!"\x10\x32\x54\x76\x98\xba\xdc\xfe") == 0x5c9d0865U); + assert(hash(r500!"\x00") == 0xfa823dd5U); + assert(hash(r500!"\x07") == 0x21a27271U); + assert(hash(r500!"~") == 0x83c5c6d5U); + assert(hash(r500!"\x7f") == 0x813b0881U); +} + +// FNV-1a 64 bit test vectors +static if (size_t.sizeof == 8) @nogc nothrow pure @safe unittest +{ + assert(hash("") == 0xcbf29ce484222325UL); + assert(hash("a") == 0xaf63dc4c8601ec8cUL); + assert(hash("b") == 0xaf63df4c8601f1a5UL); + assert(hash("c") == 0xaf63de4c8601eff2UL); + assert(hash("d") == 0xaf63d94c8601e773UL); + assert(hash("e") == 0xaf63d84c8601e5c0UL); + assert(hash("f") == 0xaf63db4c8601ead9UL); + assert(hash("fo") == 0x08985907b541d342UL); + assert(hash("foo") == 0xdcb27518fed9d577UL); + assert(hash("foob") == 0xdd120e790c2512afUL); + assert(hash("fooba") == 0xcac165afa2fef40aUL); + assert(hash("foobar") == 0x85944171f73967e8UL); + assert(hash("\0") == 0xaf63bd4c8601b7dfUL); + assert(hash("a\0") == 0x089be207b544f1e4UL); + assert(hash("b\0") == 0x08a61407b54d9b5fUL); + assert(hash("c\0") == 0x08a2ae07b54ab836UL); + assert(hash("d\0") == 0x0891b007b53c4869UL); + assert(hash("e\0") == 0x088e4a07b5396540UL); + assert(hash("f\0") == 0x08987c07b5420ebbUL); + assert(hash("fo\0") == 0xdcb28a18fed9f926UL); + assert(hash("foo\0") == 0xdd1270790c25b935UL); + assert(hash("foob\0") == 0xcac146afa2febf5dUL); + assert(hash("fooba\0") == 0x8593d371f738acfeUL); + assert(hash("foobar\0") == 0x34531ca7168b8f38UL); + assert(hash("ch") == 0x08a25607b54a22aeUL); + assert(hash("cho") == 0xf5faf0190cf90df3UL); + assert(hash("chon") == 0xf27397910b3221c7UL); + assert(hash("chong") == 0x2c8c2b76062f22e0UL); + assert(hash("chongo") == 0xe150688c8217b8fdUL); + assert(hash("chongo ") == 0xf35a83c10e4f1f87UL); + assert(hash("chongo w") == 0xd1edd10b507344d0UL); + assert(hash("chongo wa") == 0x2a5ee739b3ddb8c3UL); + assert(hash("chongo was") == 0xdcfb970ca1c0d310UL); + assert(hash("chongo was ") == 0x4054da76daa6da90UL); + assert(hash("chongo was h") == 0xf70a2ff589861368UL); + assert(hash("chongo was he") == 0x4c628b38aed25f17UL); + assert(hash("chongo was her") == 0x9dd1f6510f78189fUL); + assert(hash("chongo was here") == 0xa3de85bd491270ceUL); + assert(hash("chongo was here!") == 0x858e2fa32a55e61dUL); + assert(hash("chongo was here!\n") == 0x46810940eff5f915UL); + assert(hash("ch\0") == 0xf5fadd190cf8edaaUL); + assert(hash("cho\0") == 0xf273ed910b32b3e9UL); + assert(hash("chon\0") == 0x2c8c5276062f6525UL); + assert(hash("chong\0") == 0xe150b98c821842a0UL); + assert(hash("chongo\0") == 0xf35aa3c10e4f55e7UL); + assert(hash("chongo \0") == 0xd1ed680b50729265UL); + assert(hash("chongo w\0") == 0x2a5f0639b3dded70UL); + assert(hash("chongo wa\0") == 0xdcfbaa0ca1c0f359UL); + assert(hash("chongo was\0") == 0x4054ba76daa6a430UL); + assert(hash("chongo was \0") == 0xf709c7f5898562b0UL); + assert(hash("chongo was h\0") == 0x4c62e638aed2f9b8UL); + assert(hash("chongo was he\0") == 0x9dd1a8510f779415UL); + assert(hash("chongo was her\0") == 0xa3de2abd4911d62dUL); + assert(hash("chongo was here\0") == 0x858e0ea32a55ae0aUL); + assert(hash("chongo was here!\0") == 0x46810f40eff60347UL); + assert(hash("chongo was here!\n\0") == 0xc33bce57bef63eafUL); + assert(hash("cu") == 0x08a24307b54a0265UL); + assert(hash("cur") == 0xf5b9fd190cc18d15UL); + assert(hash("curd") == 0x4c968290ace35703UL); + assert(hash("curds") == 0x07174bd5c64d9350UL); + assert(hash("curds ") == 0x5a294c3ff5d18750UL); + assert(hash("curds a") == 0x05b3c1aeb308b843UL); + assert(hash("curds an") == 0xb92a48da37d0f477UL); + assert(hash("curds and") == 0x73cdddccd80ebc49UL); + assert(hash("curds and ") == 0xd58c4c13210a266bUL); + assert(hash("curds and w") == 0xe78b6081243ec194UL); + assert(hash("curds and wh") == 0xb096f77096a39f34UL); + assert(hash("curds and whe") == 0xb425c54ff807b6a3UL); + assert(hash("curds and whey") == 0x23e520e2751bb46eUL); + assert(hash("curds and whey\n") == 0x1a0b44ccfe1385ecUL); + assert(hash("cu\0") == 0xf5ba4b190cc2119fUL); + assert(hash("cur\0") == 0x4c962690ace2baafUL); + assert(hash("curd\0") == 0x0716ded5c64cda19UL); + assert(hash("curds\0") == 0x5a292c3ff5d150f0UL); + assert(hash("curds \0") == 0x05b3e0aeb308ecf0UL); + assert(hash("curds a\0") == 0xb92a5eda37d119d9UL); + assert(hash("curds an\0") == 0x73ce41ccd80f6635UL); + assert(hash("curds and\0") == 0xd58c2c132109f00bUL); + assert(hash("curds and \0") == 0xe78baf81243f47d1UL); + assert(hash("curds and w\0") == 0xb0968f7096a2ee7cUL); + assert(hash("curds and wh\0") == 0xb425a84ff807855cUL); + assert(hash("curds and whe\0") == 0x23e4e9e2751b56f9UL); + assert(hash("curds and whey\0") == 0x1a0b4eccfe1396eaUL); + assert(hash("curds and whey\n\0") == 0x54abd453bb2c9004UL); + assert(hash("hi") == 0x08ba5f07b55ec3daUL); + assert(hash("hi\0") == 0x337354193006cb6eUL); + assert(hash("hello") == 0xa430d84680aabd0bUL); + assert(hash("hello\0") == 0xa9bc8acca21f39b1UL); + assert(hash("\xff\x00\x00\x01") == 0x6961196491cc682dUL); + assert(hash("\x01\x00\x00\xff") == 0xad2bb1774799dfe9UL); + assert(hash("\xff\x00\x00\x02") == 0x6961166491cc6314UL); + assert(hash("\x02\x00\x00\xff") == 0x8d1bb3904a3b1236UL); + assert(hash("\xff\x00\x00\x03") == 0x6961176491cc64c7UL); + assert(hash("\x03\x00\x00\xff") == 0xed205d87f40434c7UL); + assert(hash("\xff\x00\x00\x04") == 0x6961146491cc5faeUL); + assert(hash("\x04\x00\x00\xff") == 0xcd3baf5e44f8ad9cUL); + assert(hash("\x40\x51\x4e\x44") == 0xe3b36596127cd6d8UL); + assert(hash("\x44\x4e\x51\x40") == 0xf77f1072c8e8a646UL); + assert(hash("\x40\x51\x4e\x4a") == 0xe3b36396127cd372UL); + assert(hash("\x4a\x4e\x51\x40") == 0x6067dce9932ad458UL); + assert(hash("\x40\x51\x4e\x54") == 0xe3b37596127cf208UL); + assert(hash("\x54\x4e\x51\x40") == 0x4b7b10fa9fe83936UL); + assert(hash("127.0.0.1") == 0xaabafe7104d914beUL); + assert(hash("127.0.0.1\0") == 0xf4d3180b3cde3edaUL); + assert(hash("127.0.0.2") == 0xaabafd7104d9130bUL); + assert(hash("127.0.0.2\0") == 0xf4cfb20b3cdb5bb1UL); + assert(hash("127.0.0.3") == 0xaabafc7104d91158UL); + assert(hash("127.0.0.3\0") == 0xf4cc4c0b3cd87888UL); + assert(hash("64.81.78.68") == 0xe729bac5d2a8d3a7UL); + assert(hash("64.81.78.68\0") == 0x74bc0524f4dfa4c5UL); + assert(hash("64.81.78.74") == 0xe72630c5d2a5b352UL); + assert(hash("64.81.78.74\0") == 0x6b983224ef8fb456UL); + assert(hash("64.81.78.84") == 0xe73042c5d2ae266dUL); + assert(hash("64.81.78.84\0") == 0x8527e324fdeb4b37UL); + assert(hash("feedface") == 0x0a83c86fee952abcUL); + assert(hash("feedface\0") == 0x7318523267779d74UL); + assert(hash("feedfacedaffdeed") == 0x3e66d3d56b8caca1UL); + assert(hash("feedfacedaffdeed\0") == 0x956694a5c0095593UL); + assert(hash("feedfacedeadbeef") == 0xcac54572bb1a6fc8UL); + assert(hash("feedfacedeadbeef\0") == 0xa7a4c9f3edebf0d8UL); + assert(hash("line 1\nline 2\nline 3") == 0x7829851fac17b143UL); + assert(hash("chongo <Landon Curt Noll> /\\../\\") == 0x2c8f4c9af81bcf06UL); + assert(hash("chongo <Landon Curt Noll> /\\../\\\0") == 0xd34e31539740c732UL); + assert(hash("chongo (Landon Curt Noll) /\\../\\") == 0x3605a2ac253d2db1UL); + assert(hash("chongo (Landon Curt Noll) /\\../\\\0") == 0x08c11b8346f4a3c3UL); + assert(hash("http://antwrp.gsfc.nasa.gov/apod/astropix.html") == 0x6be396289ce8a6daUL); + assert(hash("http://en.wikipedia.org/wiki/Fowler_Noll_Vo_hash") == 0xd9b957fb7fe794c5UL); + assert(hash("http://epod.usra.edu/") == 0x05be33da04560a93UL); + assert(hash("http://exoplanet.eu/") == 0x0957f1577ba9747cUL); + assert(hash("http://hvo.wr.usgs.gov/cam3/") == 0xda2cc3acc24fba57UL); + assert(hash("http://hvo.wr.usgs.gov/cams/HMcam/") == 0x74136f185b29e7f0UL); + assert(hash("http://hvo.wr.usgs.gov/kilauea/update/deformation.html") == 0xb2f2b4590edb93b2UL); + assert(hash("http://hvo.wr.usgs.gov/kilauea/update/images.html") == 0xb3608fce8b86ae04UL); + assert(hash("http://hvo.wr.usgs.gov/kilauea/update/maps.html") == 0x4a3a865079359063UL); + assert(hash("http://hvo.wr.usgs.gov/volcanowatch/current_issue.html") == 0x5b3a7ef496880a50UL); + assert(hash("http://neo.jpl.nasa.gov/risk/") == 0x48fae3163854c23bUL); + assert(hash("http://norvig.com/21-days.html") == 0x07aaa640476e0b9aUL); + assert(hash("http://primes.utm.edu/curios/home.php") == 0x2f653656383a687dUL); + assert(hash("http://slashdot.org/") == 0xa1031f8e7599d79cUL); + assert(hash("http://tux.wr.usgs.gov/Maps/155.25-19.5.html") == 0xa31908178ff92477UL); + assert(hash("http://volcano.wr.usgs.gov/kilaueastatus.php") == 0x097edf3c14c3fb83UL); + assert(hash("http://www.avo.alaska.edu/activity/Redoubt.php") == 0xb51ca83feaa0971bUL); + assert(hash("http://www.dilbert.com/fast/") == 0xdd3c0d96d784f2e9UL); + assert(hash("http://www.fourmilab.ch/gravitation/orbits/") == 0x86cd26a9ea767d78UL); + assert(hash("http://www.fpoa.net/") == 0xe6b215ff54a30c18UL); + assert(hash("http://www.ioccc.org/index.html") == 0xec5b06a1c5531093UL); + assert(hash("http://www.isthe.com/cgi-bin/number.cgi") == 0x45665a929f9ec5e5UL); + assert(hash("http://www.isthe.com/chongo/bio.html") == 0x8c7609b4a9f10907UL); + assert(hash("http://www.isthe.com/chongo/index.html") == 0x89aac3a491f0d729UL); + assert(hash("http://www.isthe.com/chongo/src/calc/lucas-calc") == 0x32ce6b26e0f4a403UL); + assert(hash("http://www.isthe.com/chongo/tech/astro/venus2004.html") == 0x614ab44e02b53e01UL); + assert(hash("http://www.isthe.com/chongo/tech/astro/vita.html") == 0xfa6472eb6eef3290UL); + assert(hash("http://www.isthe.com/chongo/tech/comp/c/expert.html") == 0x9e5d75eb1948eb6aUL); + assert(hash("http://www.isthe.com/chongo/tech/comp/calc/index.html") == 0xb6d12ad4a8671852UL); + assert(hash("http://www.isthe.com/chongo/tech/comp/fnv/index.html") == 0x88826f56eba07af1UL); + assert(hash("http://www.isthe.com/chongo/tech/math/number/howhigh.html") == 0x44535bf2645bc0fdUL); + assert(hash("http://www.isthe.com/chongo/tech/math/number/number.html") == 0x169388ffc21e3728UL); + assert(hash("http://www.isthe.com/chongo/tech/math/prime/mersenne.html") == 0xf68aac9e396d8224UL); + assert(hash("http://www.isthe.com/chongo/tech/math/prime/mersenne.html#largest") == 0x8e87d7e7472b3883UL); + assert(hash("http://www.lavarnd.org/cgi-bin/corpspeak.cgi") == 0x295c26caa8b423deUL); + assert(hash("http://www.lavarnd.org/cgi-bin/haiku.cgi") == 0x322c814292e72176UL); + assert(hash("http://www.lavarnd.org/cgi-bin/rand-none.cgi") == 0x8a06550eb8af7268UL); + assert(hash("http://www.lavarnd.org/cgi-bin/randdist.cgi") == 0xef86d60e661bcf71UL); + assert(hash("http://www.lavarnd.org/index.html") == 0x9e5426c87f30ee54UL); + assert(hash("http://www.lavarnd.org/what/nist-test.html") == 0xf1ea8aa826fd047eUL); + assert(hash("http://www.macosxhints.com/") == 0x0babaf9a642cb769UL); + assert(hash("http://www.mellis.com/") == 0x4b3341d4068d012eUL); + assert(hash("http://www.nature.nps.gov/air/webcams/parks/havoso2alert/havoalert.cfm") == 0xd15605cbc30a335cUL); + assert(hash("http://www.nature.nps.gov/air/webcams/parks/havoso2alert/timelines_24.cfm") == 0x5b21060aed8412e5UL); + assert(hash("http://www.paulnoll.com/") == 0x45e2cda1ce6f4227UL); + assert(hash("http://www.pepysdiary.com/") == 0x50ae3745033ad7d4UL); + assert(hash("http://www.sciencenews.org/index/home/activity/view") == 0xaa4588ced46bf414UL); + assert(hash("http://www.skyandtelescope.com/") == 0xc1b0056c4a95467eUL); + assert(hash("http://www.sput.nl/~rob/sirius.html") == 0x56576a71de8b4089UL); + assert(hash("http://www.systemexperts.com/") == 0xbf20965fa6dc927eUL); + assert(hash("http://www.tq-international.com/phpBB3/index.php") == 0x569f8383c2040882UL); + assert(hash("http://www.travelquesttours.com/index.htm") == 0xe1e772fba08feca0UL); + assert(hash("http://www.wunderground.com/global/stations/89606.html") == 0x4ced94af97138ac4UL); + assert(hash(r10!"21701") == 0xc4112ffb337a82fbUL); + assert(hash(r10!"M21701") == 0xd64a4fd41de38b7dUL); + assert(hash(r10!"2^21701-1") == 0x4cfc32329edebcbbUL); + assert(hash(r10!"\x54\xc5") == 0x0803564445050395UL); + assert(hash(r10!"\xc5\x54") == 0xaa1574ecf4642ffdUL); + assert(hash(r10!"23209") == 0x694bc4e54cc315f9UL); + assert(hash(r10!"M23209") == 0xa3d7cb273b011721UL); + assert(hash(r10!"2^23209-1") == 0x577c2f8b6115bfa5UL); + assert(hash(r10!"\x5a\xa9") == 0xb7ec8c1a769fb4c1UL); + assert(hash(r10!"\xa9\x5a") == 0x5d5cfce63359ab19UL); + assert(hash(r10!"391581216093") == 0x33b96c3cd65b5f71UL); + assert(hash(r10!"391581*2^216093-1") == 0xd845097780602bb9UL); + assert(hash(r10!"\x05\xf9\x9d\x03\x4c\x81") == 0x84d47645d02da3d5UL); + assert(hash(r10!"FEDCBA9876543210") == 0x83544f33b58773a5UL); + assert(hash(r10!"\xfe\xdc\xba\x98\x76\x54\x32\x10") == 0x9175cbb2160836c5UL); + assert(hash(r10!"EFCDAB8967452301") == 0xc71b3bc175e72bc5UL); + assert(hash(r10!"\xef\xcd\xab\x89\x67\x45\x23\x01") == 0x636806ac222ec985UL); + assert(hash(r10!"0123456789ABCDEF") == 0xb6ef0e6950f52ed5UL); + assert(hash(r10!"\x01\x23\x45\x67\x89\xab\xcd\xef") == 0xead3d8a0f3dfdaa5UL); + assert(hash(r10!"1032547698BADCFE") == 0x922908fe9a861ba5UL); + assert(hash(r10!"\x10\x32\x54\x76\x98\xba\xdc\xfe") == 0x6d4821de275fd5c5UL); + assert(hash(r500!"\x00") == 0x1fe3fce62bd816b5UL); + assert(hash(r500!"\x07") == 0xc23e9fccd6f70591UL); + assert(hash(r500!"~") == 0xc1af12bdfe16b5b5UL); + assert(hash(r500!"\x7f") == 0x39e9f18f2f85e221UL); +} + +/** + * Determines whether $(D_PARAM hasher) is hash function for $(D_PARAM T), i.e. + * it is callable with a value of type $(D_PARAM T) and returns a + * $(D_PSYMBOL size_t) value. + * + * Params: + * hasher = Hash function candidate. + * T = Type to test the hash function with. + * + * Returns: $(D_KEYWORD true) if $(D_PARAM hasher) is a hash function for + * $(D_PARAM T), $(D_KEYWORD false) otherwise. + */ +template isHashFunction(alias hasher, T) +{ + private alias wrapper = (T x) => hasher(x); + enum bool isHashFunction = is(typeof(wrapper(T.init)) == size_t); +} + +/// +@nogc nothrow pure @safe unittest +{ + static assert(isHashFunction!(hash, int)); +} diff --git a/source/tanya/math/random.d b/source/tanya/math/random.d index 6146788..41b2806 100644 --- a/source/tanya/math/random.d +++ b/source/tanya/math/random.d @@ -338,3 +338,13 @@ else version (Windows) } } } + +static if (is(PlatformEntropySource)) @nogc @system unittest +{ + import tanya.memory.smartref : unique; + + auto source = defaultAllocator.unique!PlatformEntropySource(); + + assert(source.threshold == 32); + assert(source.strong); +} diff --git a/source/tanya/net/iface.d b/source/tanya/net/iface.d index 6c5a962..55169d2 100644 --- a/source/tanya/net/iface.d +++ b/source/tanya/net/iface.d @@ -158,6 +158,26 @@ String indexToName(uint index) @nogc nothrow @trusted } } +@nogc nothrow @safe unittest +{ + import std.algorithm.comparison; + import std.utf; + + version (linux) + { + assert(equal(indexToName(1)[], "lo".byChar)); + } + else version (Windows) + { + assert(equal(indexToName(1)[], "loopback_0")); + } + else + { + assert(equal(indexToName(1)[], "lo0")); + } + assert(indexToName(uint.max).empty); +} + /** * $(D_PSYMBOL AddressFamily) specifies a communication domain; this selects * the protocol family which will be used for communication. diff --git a/source/tanya/net/inet.d b/source/tanya/net/inet.d index a919c9d..eb2b08b 100644 --- a/source/tanya/net/inet.d +++ b/source/tanya/net/inet.d @@ -190,6 +190,17 @@ if (L > ubyte.sizeof && L <= ulong.sizeof) assert(networkOrder.empty); } +// Static tests +@nogc nothrow pure @safe unittest +{ + import std.range; + + static assert(isBidirectionalRange!(NetworkOrder!4)); + static assert(isBidirectionalRange!(NetworkOrder!8)); + static assert(!is(NetworkOrder!9)); + static assert(!is(NetworkOrder!1)); +} + /** * Converts the $(D_KEYWORD ubyte) input range $(D_PARAM range) to * $(D_PARAM T). diff --git a/source/tanya/net/ip.d b/source/tanya/net/ip.d index 4baa6e2..7e464b1 100644 --- a/source/tanya/net/ip.d +++ b/source/tanya/net/ip.d @@ -319,6 +319,15 @@ struct Address4 } } +// Assignment and comparison works +@nogc nothrow pure @safe unittest +{ + auto address1 = Address4.loopback(); + auto address2 = Address4.any(); + address1 = address2; + assert(address1 == address2); +} + /** * Parses a string containing an IPv4 address in dotted-decimal notation. * @@ -417,6 +426,22 @@ if (isInputRange!R && is(Unqual!(ElementType!R) == ubyte)) } } +// Rejects malformed addresses +@nogc nothrow pure @safe unittest +{ + assert(address4("256.0.0.1").isNull); + assert(address4(".0.0.1").isNull); + assert(address4("0..0.1").isNull); + assert(address4("0.0.0.").isNull); + assert(address4("0.0.").isNull); + assert(address4("").isNull); +} + +@nogc nothrow pure @safe unittest +{ + assert(address4(cast(ubyte[]) []).isNull); +} + /** * IPv6 internet address. */ @@ -748,6 +773,15 @@ struct Address6 } } +// Can assign another address +@nogc nothrow pure @safe unittest +{ + Address actual = Address4.loopback; + Address expected = Address6.loopback; + actual = expected; + assert(actual == expected); +} + private void read2Bytes(R)(ref R range, ubyte[] address) { ushort group = readIntegral!ushort(range, 16); @@ -1057,6 +1091,119 @@ if (isInputRange!R && is(Unqual!(ElementType!R) == ubyte)) } } +@nogc nothrow @safe unittest +{ + char[18] actual; + + address6("ff00:2:3:4:5:6:7:8").get.toString(arrayInserter(actual)); + assert(actual[] == "ff00:2:3:4:5:6:7:8"); +} + +// Skips zero group in the middle +@nogc nothrow @safe unittest +{ + char[12] actual; + + address6("1::4:5:6:7:8").get.toString(arrayInserter(actual)); + assert(actual[] == "1::4:5:6:7:8"); +} + +// Doesn't replace lonely zeroes +@nogc nothrow @safe unittest +{ + char[15] actual; + + address6("0:1:0:2:3:0:4:0").get.toString(arrayInserter(actual)); + assert(actual[] == "0:1:0:2:3:0:4:0"); +} + +// Skips zero group at the beginning +@nogc nothrow @safe unittest +{ + char[13] actual; + + address6("::3:4:5:6:7:8").get.toString(arrayInserter(actual)); + assert(actual[] == "::3:4:5:6:7:8"); +} + +// Skips zero group at the end +@nogc nothrow @safe unittest +{ + char[13] actual; + + address6("1:2:3:4:5:6::").get.toString(arrayInserter(actual)); + assert(actual[] == "1:2:3:4:5:6::"); +} + +@nogc nothrow @safe unittest +{ + ubyte[16] expected = [0, 1, 0, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7, 0, 8]; + auto actual = address6("1:2:3:4:5:6:7:8"); + assert(actual.get.toBytes() == expected); +} + +@nogc nothrow @safe unittest +{ + ubyte[16] expected; + auto actual = address6("::"); + assert(actual.get.toBytes() == expected); +} + +@nogc nothrow @safe unittest +{ + ubyte[16] expected = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]; + auto actual = address6("::1"); + assert(actual.get.toBytes() == expected); +} + +@nogc nothrow @safe unittest +{ + ubyte[16] expected = [0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; + auto actual = address6("1::"); + assert(actual.get.toBytes() == expected); +} + +// Rejects malformed addresses +@nogc nothrow @safe unittest +{ + assert(address6("").isNull); + assert(address6(":").isNull); + assert(address6(":a").isNull); + assert(address6("a:").isNull); + assert(address6("1:2:3:4::6:").isNull); + assert(address6("fe80:2:3:4::6:7:8%").isNull); +} + +// Parses embedded IPv4 address +@nogc nothrow @safe unittest +{ + ubyte[16] expected = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4]; + auto actual = address6("0:0:0:0:0:0:1.2.3.4"); + assert(actual.get.toBytes() == expected); +} + +@nogc nothrow @safe unittest +{ + ubyte[16] expected = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4]; + auto actual = address6("::1.2.3.4"); + assert(actual.get.toBytes() == expected); +} + +@nogc nothrow @safe unittest +{ + ubyte[16] expected = [0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 0, 6, 1, 2, 3, 4]; + auto actual = address6("::5:6:1.2.3.4"); + assert(actual.get.toBytes() == expected); +} + +@nogc nothrow @safe unittest +{ + assert(address6("0:0:0:0:0:0:1.2.3.").isNull); + assert(address6("0:0:0:0:0:0:1.2:3.4").isNull); + assert(address6("0:0:0:0:0:0:1.2.3.4.").isNull); + assert(address6("fe80:0:0:0:0:0:1.2.3.4%1").get.scopeID == 1); +} + /** * Address storage, that can hold either an IPv4 or IPv6 address. */ diff --git a/source/tanya/net/package.d b/source/tanya/net/package.d index f8762a7..f20e069 100644 --- a/source/tanya/net/package.d +++ b/source/tanya/net/package.d @@ -17,4 +17,3 @@ module tanya.net; public import tanya.net.iface; public import tanya.net.inet; public import tanya.net.ip; -public import tanya.net.uri; diff --git a/source/tanya/net/uri.d b/source/tanya/net/uri.d deleted file mode 100644 index d164724..0000000 --- a/source/tanya/net/uri.d +++ /dev/null @@ -1,411 +0,0 @@ -/* This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ - -/** - * URL parser. - * - * Copyright: Eugene Wissner 2017-2025. - * License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/, - * Mozilla Public License, v. 2.0). - * Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner) - * Source: $(LINK2 https://github.com/caraus-ecms/tanya/blob/master/source/tanya/net/uri.d, - * tanya/net/uri.d) - */ -module tanya.net.uri; - -import std.ascii; -import tanya.conv; -import tanya.memory.allocator; - -/** - * Thrown if an invalid URI was specified. - */ -final class URIException : Exception -{ - /** - * Params: - * msg = The message for the exception. - * file = The file where the exception occurred. - * line = The line number where the exception occurred. - * next = The previous exception in the chain of exceptions, if any. - */ - this(string msg, - string file = __FILE__, - size_t line = __LINE__, - Throwable next = null) @nogc nothrow pure @safe - { - super(msg, file, line, next); - } -} - -/** - * A Unique Resource Locator. - */ -struct URL -{ - /// The URL scheme. - const(char)[] scheme; - - /// The username. - const(char)[] user; - - /// The password. - const(char)[] pass; - - /// The hostname. - const(char)[] host; - - /// The port number. - ushort port; - - /// The path. - const(char)[] path; - - /// The query string. - const(char)[] query; - - /// The anchor. - const(char)[] fragment; - - /** - * Attempts to parse an URL from a string. - * Output string data (scheme, user, etc.) are just slices of input string - * (i.e., no memory allocation and copying). - * - * Params: - * source = The string containing the URL. - * - * Throws: $(D_PSYMBOL URIException) if the URL is malformed. - */ - this(const char[] source) @nogc pure - { - ptrdiff_t pos = -1, endPos = source.length, start; - - foreach (i, ref c; source) - { - if (pos == -1 && c == ':') - { - pos = i; - } - if (endPos == source.length && (c == '?' || c == '#')) - { - endPos = i; - } - } - - // Check if the colon is a part of the scheme or the port and parse - // the appropriate part. - if (source.length > 1 && source[0] == '/' && source[1] == '/') - { - // Relative scheme. - start = 2; - } - else if (pos > 0) - { - // Validate scheme: - // [ toLower(alpha) | digit | "+" | "-" | "." ] - foreach (ref c; source[0 .. pos]) - { - if (!c.isAlphaNum && c != '+' && c != '-' && c != '.') - { - goto ParsePath; - } - } - - if (source.length == pos + 1) // only "scheme:" is available. - { - this.scheme = source[0 .. $ - 1]; - return; - } - else if (source.length > pos + 1 && source[pos + 1] == '/') - { - this.scheme = source[0 .. pos]; - - if (source.length > pos + 2 && source[pos + 2] == '/') - { - start = pos + 3; - - if (source.length <= start) - { - // Only "scheme://" is available. - return; - } - if (this.scheme == "file" && source[start] == '/') - { - // Windows drive letters. - if (source.length - start > 2 - && source[start + 2] == ':') - { - ++start; - } - goto ParsePath; - } - } - else - { - start = pos + 1; - goto ParsePath; - } - } - else if (!parsePort(source[pos .. $])) - { - // Schemas like mailto: and zlib: may not have any slash after - // them. - this.scheme = source[0 .. pos]; - start = pos + 1; - goto ParsePath; - } - } - else if (pos == 0 && parsePort(source[pos .. $])) - { - // An URL shouldn't begin with a port number. - throw defaultAllocator.make!URIException("URL begins with port"); - } - else - { - goto ParsePath; - } - - // Parse host. - pos = -1; - for (ptrdiff_t i = start; i < source.length; ++i) - { - if (source[i] == '@') - { - pos = i; - } - else if (source[i] == '/') - { - endPos = i; - break; - } - } - - // Check for login and password. - if (pos != -1) - { - // *( unreserved / pct-encoded / sub-delims / ":" ) - foreach (i, c; source[start .. pos]) - { - if (c == ':') - { - if (this.user is null) - { - this.user = source[start .. start + i]; - this.pass = source[start + i + 1 .. pos]; - } - } - else if (!c.isAlpha() && - !c.isDigit() && - c != '!' && - c != ';' && - c != '=' && - c != '_' && - c != '~' && - !(c >= '$' && c <= '.')) - { - this.scheme = this.user = this.pass = null; - throw make!URIException(defaultAllocator, - "Restricted characters in user information"); - } - } - if (this.user is null) - { - this.user = source[start .. pos]; - } - - start = ++pos; - } - - pos = endPos; - if (endPos <= 1 || source[start] != '[' || source[endPos - 1] != ']') - { - // Short circuit portscan. - // IPv6 embedded address. - for (ptrdiff_t i = endPos - 1; i >= start; --i) - { - if (source[i] == ':') - { - pos = i; - if (this.port == 0 && !parsePort(source[i .. endPos])) - { - this.scheme = this.user = this.pass = null; - throw defaultAllocator.make!URIException("Invalid port"); - } - break; - } - } - } - - // Check if we have a valid host, if we don't reject the string as URL. - if (pos <= start) - { - this.scheme = this.user = this.pass = null; - throw defaultAllocator.make!URIException("Invalid host"); - } - - this.host = source[start .. pos]; - - if (endPos == source.length) - { - return; - } - - start = endPos; - - ParsePath: - endPos = source.length; - pos = -1; - foreach (i, ref c; source[start .. $]) - { - if (c == '?' && pos == -1) - { - pos = start + i; - } - else if (c == '#') - { - endPos = start + i; - break; - } - } - if (pos == -1) - { - pos = endPos; - } - - if (pos > start) - { - this.path = source[start .. pos]; - } - if (endPos >= ++pos) - { - this.query = source[pos .. endPos]; - } - if (++endPos <= source.length) - { - this.fragment = source[endPos .. $]; - } - } - - /* - * Attempts to parse and set the port. - * - * Params: - * port = String beginning with a colon followed by the port number and - * an optional path (query string and/or fragment), like: - * `:12345/some_path` or `:12345`. - * - * Returns: Whether the port could be found. - */ - private bool parsePort(const(char)[] port) @nogc nothrow pure @safe - { - auto unparsed = port[1 .. $]; - auto parsed = readIntegral!ushort(unparsed); - if (unparsed.length == 0 || unparsed[0] == '/') - { - this.port = parsed; - return true; - } - return false; - } -} - -/// -@nogc pure @system unittest -{ - auto u = URL("example.org"); - assert(u.path == "example.org"); - - u = URL("relative/path"); - assert(u.path == "relative/path"); - - // Host and scheme - u = URL("https://example.org"); - assert(u.scheme == "https"); - assert(u.host == "example.org"); - assert(u.path is null); - assert(u.port == 0); - assert(u.fragment is null); - - // With user and port and path - u = URL("https://hilary:putnam@example.org:443/foo/bar"); - assert(u.scheme == "https"); - assert(u.host == "example.org"); - assert(u.path == "/foo/bar"); - assert(u.port == 443); - assert(u.user == "hilary"); - assert(u.pass == "putnam"); - assert(u.fragment is null); - - // With query string - u = URL("https://example.org/?login=true"); - assert(u.scheme == "https"); - assert(u.host == "example.org"); - assert(u.path == "/"); - assert(u.query == "login=true"); - assert(u.fragment is null); - - // With query string and fragment - u = URL("https://example.org/?login=false#label"); - assert(u.scheme == "https"); - assert(u.host == "example.org"); - assert(u.path == "/"); - assert(u.query == "login=false"); - assert(u.fragment == "label"); - - u = URL("redis://root:password@localhost:2201/path?query=value#fragment"); - assert(u.scheme == "redis"); - assert(u.user == "root"); - assert(u.pass == "password"); - assert(u.host == "localhost"); - assert(u.port == 2201); - assert(u.path == "/path"); - assert(u.query == "query=value"); - assert(u.fragment == "fragment"); -} - -/** - * Attempts to parse an URL from a string and returns the specified component - * of the URL or $(D_PSYMBOL URL) if no component is specified. - * - * Params: - * T = "scheme", "host", "port", "user", "pass", "path", "query", - * "fragment". - * source = The string containing the URL. - * - * Returns: Requested URL component. - */ -auto parseURL(string T)(const char[] source) -if (T == "scheme" - || T == "host" - || T == "user" - || T == "pass" - || T == "path" - || T == "query" - || T == "fragment" - || T == "port") -{ - auto ret = URL(source); - return mixin("ret." ~ T); -} - -/// ditto -URL parseURL(const char[] source) @nogc pure -{ - return URL(source); -} - -/// -@nogc pure @system unittest -{ - auto u = parseURL("http://example.org:5326"); - assert(u.scheme == parseURL!"scheme"("http://example.org:5326")); - assert(u.host == parseURL!"host"("http://example.org:5326")); - assert(u.user == parseURL!"user"("http://example.org:5326")); - assert(u.pass == parseURL!"pass"("http://example.org:5326")); - assert(u.path == parseURL!"path"("http://example.org:5326")); - assert(u.query == parseURL!"query"("http://example.org:5326")); - assert(u.fragment == parseURL!"fragment"("http://example.org:5326")); - assert(u.port == parseURL!"port"("http://example.org:5326")); -} diff --git a/source/tanya/range/primitive.d b/source/tanya/range/primitive.d index ec1eb0b..935e887 100644 --- a/source/tanya/range/primitive.d +++ b/source/tanya/range/primitive.d @@ -141,44 +141,6 @@ private struct Primitive(Candidate, string primitive) } /** - * Determines whether `r1.front` and `r2.front` point to the same element. - * - * Params: - * r1 = First range. - * r2 = Second range. - * - * Returns: $(D_KEYWORD true) if $(D_PARAM r1) and $(D_PARAM r2) have the same - * head, $(D_KEYWORD false) otherwise. - */ -bool sameHead(Range)(Range r1, Range r2) @trusted -if (isInputRange!Range && hasLvalueElements!Range) -{ - return &r1.front is &r2.front; -} - -/// -@nogc nothrow pure @safe unittest -{ - const int[2] array; - - auto r1 = array[]; - auto r2 = array[]; - - assert(sameHead(r1, r2)); -} - -/// -@nogc nothrow pure @safe unittest -{ - const int[2] array; - - auto r1 = array[]; - auto r2 = array[1 .. $]; - - assert(!sameHead(r1, r2)); -} - -/** * Returns the first element and advances the range. * * If $(D_PARAM range) has lvalue elements, then $(D_PSYMBOL getAndPopFront) |
