Compare commits
28 Commits
Author | SHA1 | Date | |
---|---|---|---|
7e7bf40f70 | |||
642717883e | |||
85be35c5e0 | |||
664298f038 | |||
c199cdd47c | |||
3a24e9e462 | |||
f334e6a1a0 | |||
72d5760589 | |||
b28dde9d8e | |||
b612e978bf | |||
02d1d8218b | |||
fbf6ec5250 | |||
ac317aa9d6 | |||
10022d158c | |||
a38242d0ac | |||
a84c71f26d | |||
7797f0a1fe | |||
4bbc8b510a | |||
87ea1f98dc | |||
9422888b6c | |||
13407fcf8a | |||
e06cc5a071 | |||
12fb9ff9f6 | |||
392cdcf192 | |||
09b6655b9a | |||
7a2768340e | |||
414d7a11a8 | |||
0d69c7fc79 |
@ -7,9 +7,9 @@ os:
|
||||
language: d
|
||||
|
||||
d:
|
||||
- dmd-2.077.0
|
||||
- dmd-2.076.1
|
||||
- dmd-2.075.1
|
||||
- dmd-2.074.1
|
||||
|
||||
env:
|
||||
matrix:
|
||||
@ -22,7 +22,7 @@ addons:
|
||||
- gcc-multilib
|
||||
|
||||
before_script:
|
||||
- if [ "$PS1" = '(dmd-2.076.1)' ]; then
|
||||
- if [ "$PS1" = '(dmd-2.077.0)' ]; then
|
||||
export UNITTEST="unittest-cov";
|
||||
fi
|
||||
|
||||
|
@ -15,7 +15,6 @@ Garbage Collector heap. Everything in the library is usable in @nogc code.
|
||||
Tanya extends Phobos functionality and provides alternative implementations for
|
||||
data structures and utilities that depend on the Garbage Collector in Phobos.
|
||||
|
||||
* [Bug tracker](https://issues.caraus.io/projects/tanya/issues)
|
||||
* [API Documentation](https://docs.caraus.io/tanya)
|
||||
* [Contribution guidelines](CONTRIBUTING.md)
|
||||
|
||||
@ -24,6 +23,7 @@ data structures and utilities that depend on the Garbage Collector in Phobos.
|
||||
|
||||
Tanya consists of the following packages and (top-level) modules:
|
||||
|
||||
* `algorithm`: Collection of generic algorithms.
|
||||
* `async`: Event loop (epoll, kqueue and IOCP).
|
||||
* `container`: Queue, Array, Singly and doubly linked lists, Buffers, UTF-8
|
||||
string, Hash set.
|
||||
@ -149,9 +149,9 @@ There are more containers in the `tanya.container` package.
|
||||
|
||||
| DMD | GCC |
|
||||
|:-------:|:--------------:|
|
||||
| 2.076.1 | *gdc-5* branch |
|
||||
| 2.077.0 | *gdc-5* branch |
|
||||
| 2.076.1 | |
|
||||
| 2.075.1 | |
|
||||
| 2.074.1 | |
|
||||
|
||||
### Current status
|
||||
|
||||
|
12
appveyor.yml
12
appveyor.yml
@ -3,6 +3,12 @@ os: Visual Studio 2015
|
||||
|
||||
environment:
|
||||
matrix:
|
||||
- DC: dmd
|
||||
DVersion: 2.077.0
|
||||
arch: x64
|
||||
- DC: dmd
|
||||
DVersion: 2.077.0
|
||||
arch: x86
|
||||
- DC: dmd
|
||||
DVersion: 2.076.1
|
||||
arch: x64
|
||||
@ -15,12 +21,6 @@ environment:
|
||||
- DC: dmd
|
||||
DVersion: 2.075.1
|
||||
arch: x86
|
||||
- DC: dmd
|
||||
DVersion: 2.074.1
|
||||
arch: x64
|
||||
- DC: dmd
|
||||
DVersion: 2.074.1
|
||||
arch: x86
|
||||
|
||||
skip_tags: true
|
||||
|
||||
|
275
source/tanya/algorithm/mutation.d
Normal file
275
source/tanya/algorithm/mutation.d
Normal file
@ -0,0 +1,275 @@
|
||||
/* 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/. */
|
||||
|
||||
/**
|
||||
* Algorithms that modify its arguments.
|
||||
*
|
||||
* Copyright: Eugene Wissner 2017.
|
||||
* 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/algorithm/mutation.d,
|
||||
* tanya/algorithm/mutation.d)
|
||||
*/
|
||||
module tanya.algorithm.mutation;
|
||||
|
||||
import tanya.memory.op;
|
||||
import tanya.meta.trait;
|
||||
|
||||
private void deinitialize(bool zero, T)(ref T value)
|
||||
{
|
||||
static if (is(T == U[S], U, size_t S))
|
||||
{
|
||||
foreach (ref e; value)
|
||||
{
|
||||
deinitialize!zero(e);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
static if (isNested!T)
|
||||
{
|
||||
// Don't override the context pointer.
|
||||
enum size_t size = T.sizeof - (void*).sizeof;
|
||||
}
|
||||
else
|
||||
{
|
||||
enum size_t size = T.sizeof;
|
||||
}
|
||||
static if (zero)
|
||||
{
|
||||
fill!0((cast(void*) &value)[0 .. size]);
|
||||
}
|
||||
else
|
||||
{
|
||||
copy(typeid(T).initializer()[0 .. size], (&value)[0 .. 1]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Moves $(D_PARAM source) into $(D_PARAM target) assuming that
|
||||
* $(D_PARAM target) isn't initialized.
|
||||
*
|
||||
* Moving the $(D_PARAM source) copies it into the $(D_PARAM target) and places
|
||||
* the $(D_PARAM source) into a valid but unspecified state, which means that
|
||||
* after moving $(D_PARAM source) can be destroyed or assigned a new value, but
|
||||
* accessing it yields an unspecified value. No postblits or destructors are
|
||||
* called. If the $(D_PARAM target) should be destroyed before, use
|
||||
* $(D_PSYMBOL move).
|
||||
*
|
||||
* $(D_PARAM source) and $(D_PARAM target) must be different objects.
|
||||
*
|
||||
* Params:
|
||||
* T = Object type.
|
||||
* source = Source object.
|
||||
* target = Target object.
|
||||
*
|
||||
* See_Also: $(D_PSYMBOL move),
|
||||
* $(D_PSYMBOL hasElaborateCopyConstructor),
|
||||
* $(D_PSYMBOL hasElaborateDestructor).
|
||||
*
|
||||
* Precondition: `&source !is &target`.
|
||||
*/
|
||||
void moveEmplace(T)(ref T source, ref T target) @system
|
||||
in
|
||||
{
|
||||
assert(&source !is &target, "Source and target must be different");
|
||||
}
|
||||
body
|
||||
{
|
||||
static if (is(T == struct) || isStaticArray!T)
|
||||
{
|
||||
copy((&source)[0 .. 1], (&target)[0 .. 1]);
|
||||
|
||||
static if (hasElaborateCopyConstructor!T || hasElaborateDestructor!T)
|
||||
{
|
||||
if (typeid(T).initializer().ptr is null)
|
||||
{
|
||||
deinitialize!true(source);
|
||||
}
|
||||
else
|
||||
{
|
||||
deinitialize!false(source);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
target = source;
|
||||
}
|
||||
}
|
||||
|
||||
///
|
||||
@nogc nothrow pure @system unittest
|
||||
{
|
||||
static struct S
|
||||
{
|
||||
int member = 5;
|
||||
|
||||
this(this) @nogc nothrow pure @safe
|
||||
{
|
||||
assert(false);
|
||||
}
|
||||
}
|
||||
S source, target = void;
|
||||
moveEmplace(source, target);
|
||||
assert(target.member == 5);
|
||||
|
||||
int x1 = 5, x2;
|
||||
moveEmplace(x1, x2);
|
||||
assert(x2 == 5);
|
||||
}
|
||||
|
||||
// Is pure.
|
||||
@nogc nothrow pure @system unittest
|
||||
{
|
||||
struct S
|
||||
{
|
||||
this(this)
|
||||
{
|
||||
}
|
||||
}
|
||||
S source, target = void;
|
||||
static assert(is(typeof({ moveEmplace(source, target); })));
|
||||
}
|
||||
|
||||
// Moves nested.
|
||||
@nogc nothrow pure @system unittest
|
||||
{
|
||||
struct Nested
|
||||
{
|
||||
void method() @nogc nothrow pure @safe
|
||||
{
|
||||
}
|
||||
}
|
||||
Nested source, target = void;
|
||||
moveEmplace(source, target);
|
||||
assert(source == target);
|
||||
}
|
||||
|
||||
// Emplaces static arrays.
|
||||
@nogc nothrow pure @system unittest
|
||||
{
|
||||
static struct S
|
||||
{
|
||||
size_t member;
|
||||
this(size_t i) @nogc nothrow pure @safe
|
||||
{
|
||||
this.member = i;
|
||||
}
|
||||
~this() @nogc nothrow pure @safe
|
||||
{
|
||||
}
|
||||
}
|
||||
S[2] source = [ S(5), S(5) ], target = void;
|
||||
moveEmplace(source, target);
|
||||
assert(source[0].member == 0);
|
||||
assert(target[0].member == 5);
|
||||
assert(source[1].member == 0);
|
||||
assert(target[1].member == 5);
|
||||
}
|
||||
|
||||
/**
|
||||
* Moves $(D_PARAM source) into $(D_PARAM target) assuming that
|
||||
* $(D_PARAM target) isn't initialized.
|
||||
*
|
||||
* Moving the $(D_PARAM source) copies it into the $(D_PARAM target) and places
|
||||
* the $(D_PARAM source) into a valid but unspecified state, which means that
|
||||
* after moving $(D_PARAM source) can be destroyed or assigned a new value, but
|
||||
* accessing it yields an unspecified value. $(D_PARAM target) is destroyed before
|
||||
* the new value is assigned. If $(D_PARAM target) isn't initialized and
|
||||
* therefore shouldn't be destroyed, $(D_PSYMBOL moveEmplace) can be used.
|
||||
*
|
||||
* If $(D_PARAM target) isn't specified, $(D_PSYMBOL move) returns the source
|
||||
* as rvalue without calling its copy constructor or destructor.
|
||||
*
|
||||
* $(D_PARAM source) and $(D_PARAM target) are the same object,
|
||||
* $(D_PSYMBOL move) does nothing.
|
||||
*
|
||||
* Params:
|
||||
* T = Object type.
|
||||
* source = Source object.
|
||||
* target = Target object.
|
||||
*
|
||||
* See_Also: $(D_PSYMBOL moveEmplace).
|
||||
*/
|
||||
void move(T)(ref T source, ref T target)
|
||||
{
|
||||
if ((() @trusted => &source is &target)())
|
||||
{
|
||||
return;
|
||||
}
|
||||
static if (hasElaborateDestructor!T)
|
||||
{
|
||||
target.__xdtor();
|
||||
}
|
||||
(() @trusted => moveEmplace(source, target))();
|
||||
}
|
||||
|
||||
/// ditto
|
||||
T move(T)(ref T source) @trusted
|
||||
{
|
||||
T target = void;
|
||||
moveEmplace(source, target);
|
||||
return target;
|
||||
}
|
||||
|
||||
///
|
||||
@nogc nothrow pure @safe unittest
|
||||
{
|
||||
static struct S
|
||||
{
|
||||
int member = 5;
|
||||
|
||||
this(this) @nogc nothrow pure @safe
|
||||
{
|
||||
assert(false);
|
||||
}
|
||||
}
|
||||
S source, target = void;
|
||||
move(source, target);
|
||||
assert(target.member == 5);
|
||||
assert(move(target).member == 5);
|
||||
|
||||
int x1 = 5, x2;
|
||||
move(x1, x2);
|
||||
assert(x2 == 5);
|
||||
assert(move(x2) == 5);
|
||||
}
|
||||
|
||||
// Moves if source is target.
|
||||
@nogc nothrow pure @safe unittest
|
||||
{
|
||||
int x = 5;
|
||||
move(x, x);
|
||||
assert(x == 5);
|
||||
}
|
||||
|
||||
/**
|
||||
* Exchanges the values of $(D_PARAM a) and $(D_PARAM b).
|
||||
*
|
||||
* $(D_PSYMBOL swap) moves the contents of $(D_PARAM a) and $(D_PARAM b)
|
||||
* without calling its postblits or destructors.
|
||||
*
|
||||
* Params:
|
||||
* a = The first object.
|
||||
* a = The second object.
|
||||
*/
|
||||
void swap(T)(ref T a, ref T b) @trusted
|
||||
{
|
||||
T tmp = void;
|
||||
moveEmplace(a, tmp);
|
||||
moveEmplace(b, a);
|
||||
moveEmplace(tmp, b);
|
||||
}
|
||||
|
||||
///
|
||||
@nogc nothrow pure @safe unittest
|
||||
{
|
||||
int a = 3, b = 5;
|
||||
swap(a, b);
|
||||
assert(a == 5);
|
||||
assert(b == 3);
|
||||
}
|
17
source/tanya/algorithm/package.d
Normal file
17
source/tanya/algorithm/package.d
Normal file
@ -0,0 +1,17 @@
|
||||
/* 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/. */
|
||||
|
||||
/**
|
||||
* Collection of generic algorithms.
|
||||
*
|
||||
* Copyright: Eugene Wissner 2017.
|
||||
* 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/algorithm/package.d,
|
||||
* tanya/algorithm/package.d)
|
||||
*/
|
||||
module tanya.algorithm;
|
||||
|
||||
public import tanya.algorithm.mutation;
|
@ -16,8 +16,13 @@ module tanya.container.array;
|
||||
|
||||
import core.checkedint;
|
||||
import std.algorithm.comparison;
|
||||
import std.algorithm.mutation;
|
||||
import std.algorithm.mutation : bringToFront,
|
||||
copy,
|
||||
fill,
|
||||
initializeAll,
|
||||
uninitializedFill;
|
||||
import std.meta;
|
||||
import tanya.algorithm.mutation;
|
||||
import tanya.exception;
|
||||
import tanya.memory;
|
||||
import tanya.meta.trait;
|
||||
@ -264,7 +269,10 @@ struct Array(T)
|
||||
{
|
||||
// Move each element.
|
||||
reserve(init.length_);
|
||||
moveEmplaceAll(init.data[0 .. init.length_], this.data[0 .. init.length_]);
|
||||
foreach (ref target; this.data[0 .. init.length_])
|
||||
{
|
||||
moveEmplace(*init.data++, target);
|
||||
}
|
||||
this.length_ = init.length_;
|
||||
// Destructor of init should destroy it here.
|
||||
}
|
||||
@ -506,7 +514,6 @@ struct Array(T)
|
||||
{
|
||||
allocator.deallocate(buf);
|
||||
}
|
||||
const T* end = this.data + this.length_;
|
||||
for (T* src = this.data, dest = cast(T*) buf; src != end; ++src, ++dest)
|
||||
{
|
||||
moveEmplace(*src, *dest);
|
||||
@ -632,6 +639,11 @@ struct Array(T)
|
||||
assert(v.removeBack(3) == 0);
|
||||
}
|
||||
|
||||
private @property inout(T)* end() inout
|
||||
{
|
||||
return this.data + this.length_;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove all elements beloning to $(D_PARAM r).
|
||||
*
|
||||
@ -652,8 +664,11 @@ struct Array(T)
|
||||
}
|
||||
body
|
||||
{
|
||||
auto end = this.data + this.length;
|
||||
moveAll(Range(this, r.end, end), Range(this, r.begin, end));
|
||||
auto target = r.begin;
|
||||
for (auto source = r.end; source != end; ++source, ++target)
|
||||
{
|
||||
move(*source, *target);
|
||||
}
|
||||
length = length - r.length;
|
||||
return Range(this, r.begin, this.data + length);
|
||||
}
|
||||
@ -683,7 +698,7 @@ struct Array(T)
|
||||
if (isImplicitlyConvertible!(R, T))
|
||||
{
|
||||
reserve(this.length + 1);
|
||||
moveEmplace(el, *(this.data + this.length_));
|
||||
moveEmplace(el, *end);
|
||||
++this.length_;
|
||||
}
|
||||
|
||||
|
@ -15,11 +15,12 @@
|
||||
module tanya.container.list;
|
||||
|
||||
import std.algorithm.comparison;
|
||||
import std.algorithm.mutation;
|
||||
import std.algorithm.searching;
|
||||
import tanya.algorithm.mutation;
|
||||
import tanya.container.entry;
|
||||
import tanya.memory;
|
||||
import tanya.meta.trait;
|
||||
import tanya.meta.transform;
|
||||
import tanya.range.array;
|
||||
import tanya.range.primitive;
|
||||
|
||||
|
@ -14,7 +14,7 @@
|
||||
*/
|
||||
module tanya.container.queue;
|
||||
|
||||
import std.algorithm.mutation;
|
||||
import tanya.algorithm.mutation;
|
||||
import tanya.container.entry;
|
||||
import tanya.exception;
|
||||
import tanya.memory;
|
||||
|
@ -15,7 +15,7 @@
|
||||
*/
|
||||
module tanya.container.set;
|
||||
|
||||
import std.algorithm.mutation;
|
||||
import tanya.algorithm.mutation;
|
||||
import tanya.container;
|
||||
import tanya.container.entry;
|
||||
import tanya.memory;
|
||||
|
@ -27,9 +27,10 @@
|
||||
module tanya.container.string;
|
||||
|
||||
import std.algorithm.comparison;
|
||||
import std.algorithm.mutation;
|
||||
import std.algorithm.mutation : bringToFront, copy;
|
||||
import std.algorithm.searching;
|
||||
static import std.range;
|
||||
import tanya.algorithm.mutation;
|
||||
import tanya.memory;
|
||||
import tanya.meta.trait;
|
||||
import tanya.meta.transform;
|
||||
|
@ -14,9 +14,17 @@
|
||||
*/
|
||||
module tanya.conv;
|
||||
|
||||
import tanya.container.string;
|
||||
import tanya.format;
|
||||
import tanya.memory;
|
||||
import tanya.memory.op;
|
||||
import tanya.meta.trait;
|
||||
import tanya.meta.transform;
|
||||
|
||||
version (unittest)
|
||||
{
|
||||
import tanya.test.assertion;
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a new object of type $(D_PARAM T) in $(D_PARAM memory) with the
|
||||
@ -233,3 +241,469 @@ body
|
||||
}
|
||||
static assert(is(typeof(emplace!F((void[]).init))));
|
||||
}
|
||||
|
||||
/**
|
||||
* Thrown if a type conversion fails.
|
||||
*/
|
||||
final class ConvException : 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 @safe pure nothrow
|
||||
{
|
||||
super(msg, file, line, next);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* If the source type $(D_PARAM From) and the target type $(D_PARAM To) are
|
||||
* equal, does nothing. If $(D_PARAM From) can be implicitly converted to
|
||||
* $(D_PARAM To), just returns $(D_PARAM from).
|
||||
*
|
||||
* Params:
|
||||
* To = Target type.
|
||||
*
|
||||
* Returns: $(D_PARAM from).
|
||||
*/
|
||||
template to(To)
|
||||
{
|
||||
/**
|
||||
* Params:
|
||||
* From = Source type.
|
||||
* from = Source value.
|
||||
*/
|
||||
ref To to(From)(ref From from)
|
||||
if (is(To == From))
|
||||
{
|
||||
return from;
|
||||
}
|
||||
|
||||
/// ditto
|
||||
To to(From)(From from)
|
||||
if (is(Unqual!To == Unqual!From) || (isNumeric!From && isFloatingPoint!To))
|
||||
{
|
||||
return from;
|
||||
}
|
||||
}
|
||||
|
||||
///
|
||||
@nogc nothrow pure @safe unittest
|
||||
{
|
||||
auto val = 5.to!int();
|
||||
assert(val == 5);
|
||||
static assert(is(typeof(val) == int));
|
||||
}
|
||||
|
||||
@nogc nothrow pure @safe unittest
|
||||
{
|
||||
int val = 5;
|
||||
assert(val.to!int() == 5);
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs checked conversion from an integral type $(D_PARAM From) to an
|
||||
* integral type $(D_PARAM To).
|
||||
*
|
||||
* Params:
|
||||
* From = Source type.
|
||||
* To = Target type.
|
||||
* from = Source value.
|
||||
*
|
||||
* Returns: $(D_PARAM from) converted to $(D_PARAM To).
|
||||
*
|
||||
* Throws: $(D_PSYMBOL ConvException) if $(D_PARAM from) is too small or too
|
||||
* large to be represented by $(D_PARAM To).
|
||||
*/
|
||||
To to(To, From)(From from)
|
||||
if (isIntegral!From
|
||||
&& isIntegral!To
|
||||
&& !is(Unqual!To == Unqual!From)
|
||||
&& !is(To == enum))
|
||||
{
|
||||
static if ((isUnsigned!From && isSigned!To && From.sizeof == To.sizeof)
|
||||
|| From.sizeof > To.sizeof)
|
||||
{
|
||||
if (from > To.max)
|
||||
{
|
||||
throw make!ConvException(defaultAllocator,
|
||||
"Positive integer overflow");
|
||||
}
|
||||
}
|
||||
static if (isSigned!From)
|
||||
{
|
||||
static if (isUnsigned!To)
|
||||
{
|
||||
if (from < 0)
|
||||
{
|
||||
throw make!ConvException(defaultAllocator,
|
||||
"Negative integer overflow");
|
||||
}
|
||||
}
|
||||
else static if (From.sizeof > To.sizeof)
|
||||
{
|
||||
if (from < To.min)
|
||||
{
|
||||
throw make!ConvException(defaultAllocator,
|
||||
"Negative integer overflow");
|
||||
}
|
||||
}
|
||||
}
|
||||
static if (From.sizeof <= To.sizeof)
|
||||
{
|
||||
return from;
|
||||
}
|
||||
else static if (isSigned!To)
|
||||
{
|
||||
return cast(To) from;
|
||||
}
|
||||
else
|
||||
{
|
||||
return from & To.max;
|
||||
}
|
||||
}
|
||||
|
||||
@nogc nothrow pure @safe unittest
|
||||
{
|
||||
// ubyte -> ushort
|
||||
assert((cast(ubyte) 0).to!ushort == 0);
|
||||
assert((cast(ubyte) 1).to!ushort == 1);
|
||||
assert((cast(ubyte) (ubyte.max - 1)).to!ushort == ubyte.max - 1);
|
||||
assert((cast(ubyte) ubyte.max).to!ushort == ubyte.max);
|
||||
|
||||
// ubyte -> short
|
||||
assert((cast(ubyte) 0).to!short == 0);
|
||||
assert((cast(ubyte) 1).to!short == 1);
|
||||
assert((cast(ubyte) (ubyte.max - 1)).to!short == ubyte.max - 1);
|
||||
assert((cast(ubyte) ubyte.max).to!short == ubyte.max);
|
||||
}
|
||||
|
||||
@nogc pure @safe unittest
|
||||
{
|
||||
// ubyte <- ushort
|
||||
assert((cast(ushort) 0).to!ubyte == 0);
|
||||
assert((cast(ushort) 1).to!ubyte == 1);
|
||||
assert((cast(ushort) (ubyte.max - 1)).to!ubyte == ubyte.max - 1);
|
||||
assert((cast(ushort) ubyte.max).to!ubyte == ubyte.max);
|
||||
|
||||
// ubyte <- short
|
||||
assert((cast(short) 0).to!ubyte == 0);
|
||||
assert((cast(short) 1).to!ubyte == 1);
|
||||
assert((cast(short) (ubyte.max - 1)).to!ubyte == ubyte.max - 1);
|
||||
assert((cast(short) ubyte.max).to!ubyte == ubyte.max);
|
||||
|
||||
// short <-> int
|
||||
assert(short.min.to!int == short.min);
|
||||
assert((short.min + 1).to!int == short.min + 1);
|
||||
assert((cast(short) -1).to!int == -1);
|
||||
assert((cast(short) 0).to!int == 0);
|
||||
assert((cast(short) 1).to!int == 1);
|
||||
assert((short.max - 1).to!int == short.max - 1);
|
||||
assert(short.max.to!int == short.max);
|
||||
|
||||
assert((cast(int) short.min).to!short == short.min);
|
||||
assert((cast(int) short.min + 1).to!short == short.min + 1);
|
||||
assert((cast(int) -1).to!short == -1);
|
||||
assert((cast(int) 0).to!short == 0);
|
||||
assert((cast(int) 1).to!short == 1);
|
||||
assert((cast(int) short.max - 1).to!short == short.max - 1);
|
||||
assert((cast(int) short.max).to!short == short.max);
|
||||
|
||||
// uint <-> int
|
||||
assert((cast(uint) 0).to!int == 0);
|
||||
assert((cast(uint) 1).to!int == 1);
|
||||
assert((cast(uint) (int.max - 1)).to!int == int.max - 1);
|
||||
assert((cast(uint) int.max).to!int == int.max);
|
||||
|
||||
assert((cast(int) 0).to!uint == 0);
|
||||
assert((cast(int) 1).to!uint == 1);
|
||||
assert((cast(int) (int.max - 1)).to!uint == int.max - 1);
|
||||
assert((cast(int) int.max).to!uint == int.max);
|
||||
}
|
||||
|
||||
@nogc pure @safe unittest
|
||||
{
|
||||
assertThrown!ConvException(&to!(short, int), int.min);
|
||||
assertThrown!ConvException(&to!(short, int), int.max);
|
||||
assertThrown!ConvException(&to!(ushort, uint), uint.max);
|
||||
assertThrown!ConvException(&to!(uint, int), -1);
|
||||
}
|
||||
|
||||
@nogc nothrow pure @safe unittest
|
||||
{
|
||||
enum Test : int
|
||||
{
|
||||
one,
|
||||
two,
|
||||
}
|
||||
assert(Test.one.to!int == 0);
|
||||
assert(Test.two.to!int == 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a floating point number to an integral type.
|
||||
*
|
||||
* Params:
|
||||
* From = Source type.
|
||||
* To = Target type.
|
||||
* from = Source value.
|
||||
*
|
||||
* Returns: Truncated $(D_PARAM from) (everything after the decimal point is
|
||||
* dropped).
|
||||
*
|
||||
* Throws: $(D_PSYMBOL ConvException) if
|
||||
* $(D_INLINECODE from < To.min || from > To.max).
|
||||
*/
|
||||
To to(To, From)(From from)
|
||||
if (isFloatingPoint!From
|
||||
&& isIntegral!To
|
||||
&& !is(Unqual!To == Unqual!From)
|
||||
&& !is(To == enum))
|
||||
{
|
||||
if (from > To.max)
|
||||
{
|
||||
throw make!ConvException(defaultAllocator,
|
||||
"Positive number overflow");
|
||||
}
|
||||
else if (from < To.min)
|
||||
{
|
||||
throw make!ConvException(defaultAllocator,
|
||||
"Negative number overflow");
|
||||
}
|
||||
return cast(To) from;
|
||||
}
|
||||
|
||||
///
|
||||
@nogc pure @safe unittest
|
||||
{
|
||||
assert(1.5.to!int == 1);
|
||||
assert(2147483646.5.to!int == 2147483646);
|
||||
assert((-2147483647.5).to!int == -2147483647);
|
||||
assert(2147483646.5.to!uint == 2147483646);
|
||||
}
|
||||
|
||||
@nogc pure @safe unittest
|
||||
{
|
||||
assertThrown!ConvException(&to!(int, double), 2147483647.5);
|
||||
assertThrown!ConvException(&to!(int, double), -2147483648.5);
|
||||
assertThrown!ConvException(&to!(uint, double), -21474.5);
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs checked conversion from an integral type $(D_PARAM From) to an
|
||||
* $(D_KEYWORD enum).
|
||||
*
|
||||
* Params:
|
||||
* From = Source type.
|
||||
* To = Target type.
|
||||
* from = Source value.
|
||||
*
|
||||
* Returns: $(D_KEYWORD enum) value.
|
||||
*
|
||||
* Throws: $(D_PSYMBOL ConvException) if $(D_PARAM from) is not a member of
|
||||
* $(D_PSYMBOL To).
|
||||
*/
|
||||
To to(To, From)(From from)
|
||||
if (isIntegral!From && is(To == enum))
|
||||
{
|
||||
foreach (m; EnumMembers!To)
|
||||
{
|
||||
if (from == m)
|
||||
{
|
||||
return m;
|
||||
}
|
||||
}
|
||||
throw make!ConvException(defaultAllocator,
|
||||
"Value not found in enum '" ~ To.stringof ~ "'");
|
||||
}
|
||||
|
||||
///
|
||||
@nogc pure @safe unittest
|
||||
{
|
||||
enum Test : int
|
||||
{
|
||||
one,
|
||||
two,
|
||||
}
|
||||
static assert(is(typeof(1.to!Test) == Test));
|
||||
assert(0.to!Test == Test.one);
|
||||
assert(1.to!Test == Test.two);
|
||||
}
|
||||
|
||||
@nogc pure @safe unittest
|
||||
{
|
||||
enum Test : uint
|
||||
{
|
||||
one,
|
||||
two,
|
||||
}
|
||||
assertThrown!ConvException(&to!(Test, int), 5);
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts $(D_PARAM from) to a boolean.
|
||||
*
|
||||
* If $(D_PARAM From) is a numeric type, then `1` becomes $(D_KEYWORD true),
|
||||
* `0` $(D_KEYWORD false). Otherwise $(D_PSYMBOL ConvException) is thrown.
|
||||
*
|
||||
* If $(D_PARAM To) is a string (built-in string or $(D_PSYMBOL String)),
|
||||
* then `"true"` or `"false"` are converted to the appropriate boolean value.
|
||||
* Otherwise $(D_PSYMBOL ConvException) is thrown.
|
||||
*
|
||||
* Params:
|
||||
* From = Source type.
|
||||
* To = Target type.
|
||||
* from = Source value.
|
||||
*
|
||||
* Returns: $(D_KEYWORD from) converted to a boolean.
|
||||
*
|
||||
* Throws: $(D_PSYMBOL ConvException) if $(D_PARAM from) isn't convertible.
|
||||
*/
|
||||
To to(To, From)(From from)
|
||||
if (isNumeric!From && is(Unqual!To == bool) && !is(Unqual!To == Unqual!From))
|
||||
{
|
||||
if (from == 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
else if (from < 0)
|
||||
{
|
||||
throw make!ConvException(defaultAllocator,
|
||||
"Negative number overflow");
|
||||
}
|
||||
else if (from <= 1)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
throw make!ConvException(defaultAllocator,
|
||||
"Positive number overflow");
|
||||
}
|
||||
|
||||
///
|
||||
@nogc pure @safe unittest
|
||||
{
|
||||
assert(!0.0.to!bool);
|
||||
assert(0.2.to!bool);
|
||||
assert(0.5.to!bool);
|
||||
assert(1.0.to!bool);
|
||||
|
||||
assert(!0.to!bool);
|
||||
assert(1.to!bool);
|
||||
}
|
||||
|
||||
@nogc pure @safe unittest
|
||||
{
|
||||
assertThrown!ConvException(&to!(bool, int), -1);
|
||||
assertThrown!ConvException(&to!(bool, int), 2);
|
||||
}
|
||||
|
||||
/// ditto
|
||||
To to(To, From)(auto ref const From from)
|
||||
if ((is(From == String) || isSomeString!From) && is(Unqual!To == bool))
|
||||
{
|
||||
if (from == "true")
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else if (from == "false")
|
||||
{
|
||||
return false;
|
||||
}
|
||||
throw make!ConvException(defaultAllocator,
|
||||
"String doesn't contain a boolean value");
|
||||
}
|
||||
|
||||
///
|
||||
@nogc pure @safe unittest
|
||||
{
|
||||
assert("true".to!bool);
|
||||
assert(!"false".to!bool);
|
||||
assert(String("true").to!bool);
|
||||
assert(!String("false").to!bool);
|
||||
|
||||
}
|
||||
|
||||
@nogc pure @safe unittest
|
||||
{
|
||||
assertThrown!ConvException(() => "1".to!bool);
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a boolean to $(D_PARAM To).
|
||||
*
|
||||
* If $(D_PARAM To) is a numeric type, then $(D_KEYWORD true) becomes `1`,
|
||||
* $(D_KEYWORD false) `0`.
|
||||
*
|
||||
* If $(D_PARAM To) is a $(D_PSYMBOL String), then `"true"` or `"false"`
|
||||
* is returned.
|
||||
*
|
||||
* Params:
|
||||
* From = Source type.
|
||||
* To = Target type.
|
||||
* from = Source value.
|
||||
*
|
||||
* Returns: $(D_PARAM from) converted to $(D_PARAM To).
|
||||
*/
|
||||
To to(To, From)(From from)
|
||||
if (is(Unqual!From == bool) && isNumeric!To && !is(Unqual!To == Unqual!From))
|
||||
{
|
||||
return from;
|
||||
}
|
||||
|
||||
///
|
||||
@nogc nothrow pure @safe unittest
|
||||
{
|
||||
assert(true.to!float == 1.0);
|
||||
assert(true.to!double == 1.0);
|
||||
assert(true.to!ubyte == 1);
|
||||
assert(true.to!byte == 1);
|
||||
assert(true.to!ushort == 1);
|
||||
assert(true.to!short == 1);
|
||||
assert(true.to!uint == 1);
|
||||
assert(true.to!int == 1);
|
||||
|
||||
assert(false.to!float == 0);
|
||||
assert(false.to!double == 0);
|
||||
assert(false.to!ubyte == 0);
|
||||
assert(false.to!byte == 0);
|
||||
assert(false.to!ushort == 0);
|
||||
assert(false.to!short == 0);
|
||||
assert(false.to!uint == 0);
|
||||
assert(false.to!int == 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts $(D_PARAM From) to a $(D_PSYMBOL String).
|
||||
*
|
||||
* Params:
|
||||
* From = Source type.
|
||||
* To = Target type.
|
||||
* from = Source value.
|
||||
*
|
||||
* Returns: $(D_PARAM from) converted to $(D_PSYMBOL String).
|
||||
*/
|
||||
To to(To, From)(auto ref From from)
|
||||
if (is(Unqual!To == String))
|
||||
{
|
||||
return format!"{}"(from);
|
||||
}
|
||||
|
||||
///
|
||||
@nogc nothrow pure @safe unittest
|
||||
{
|
||||
assert(true.to!String == "true");
|
||||
assert(false.to!String == "false");
|
||||
}
|
||||
|
||||
@nogc nothrow pure @safe unittest
|
||||
{
|
||||
static assert(is(typeof((const String("true")).to!bool)));
|
||||
static assert(is(typeof(false.to!(const String) == "false")));
|
||||
}
|
||||
|
@ -20,6 +20,11 @@ import tanya.memory.op;
|
||||
import tanya.meta.trait;
|
||||
import tanya.meta.transform;
|
||||
|
||||
version (unittest)
|
||||
{
|
||||
import tanya.test.assertion;
|
||||
}
|
||||
|
||||
/**
|
||||
* Thrown if a type conversion fails.
|
||||
*/
|
||||
@ -51,6 +56,7 @@ final class ConvException : Exception
|
||||
*
|
||||
* Returns: $(D_PARAM from).
|
||||
*/
|
||||
deprecated("Use tanya.conv.to instead")
|
||||
template to(To)
|
||||
{
|
||||
/**
|
||||
@ -72,20 +78,6 @@ template to(To)
|
||||
}
|
||||
}
|
||||
|
||||
///
|
||||
pure nothrow @safe @nogc unittest
|
||||
{
|
||||
auto val = 5.to!int();
|
||||
assert(val == 5);
|
||||
static assert(is(typeof(val) == int));
|
||||
}
|
||||
|
||||
private pure nothrow @safe @nogc unittest
|
||||
{
|
||||
int val = 5;
|
||||
assert(val.to!int() == 5);
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs checked conversion from an integral type $(D_PARAM From) to an
|
||||
* integral type $(D_PARAM To).
|
||||
@ -100,6 +92,7 @@ private pure nothrow @safe @nogc unittest
|
||||
* Throws: $(D_PSYMBOL ConvException) if $(D_PARAM from) is too small or too
|
||||
* large to be represented by $(D_PARAM To).
|
||||
*/
|
||||
deprecated("Use tanya.conv.to instead")
|
||||
To to(To, From)(From from)
|
||||
if (isIntegral!From
|
||||
&& isIntegral!To
|
||||
@ -148,135 +141,6 @@ if (isIntegral!From
|
||||
}
|
||||
}
|
||||
|
||||
private pure nothrow @safe @nogc unittest
|
||||
{
|
||||
// ubyte -> ushort
|
||||
assert((cast(ubyte) 0).to!ushort == 0);
|
||||
assert((cast(ubyte) 1).to!ushort == 1);
|
||||
assert((cast(ubyte) (ubyte.max - 1)).to!ushort == ubyte.max - 1);
|
||||
assert((cast(ubyte) ubyte.max).to!ushort == ubyte.max);
|
||||
|
||||
// ubyte -> short
|
||||
assert((cast(ubyte) 0).to!short == 0);
|
||||
assert((cast(ubyte) 1).to!short == 1);
|
||||
assert((cast(ubyte) (ubyte.max - 1)).to!short == ubyte.max - 1);
|
||||
assert((cast(ubyte) ubyte.max).to!short == ubyte.max);
|
||||
}
|
||||
|
||||
private unittest
|
||||
{
|
||||
// ubyte <- ushort
|
||||
assert((cast(ushort) 0).to!ubyte == 0);
|
||||
assert((cast(ushort) 1).to!ubyte == 1);
|
||||
assert((cast(ushort) (ubyte.max - 1)).to!ubyte == ubyte.max - 1);
|
||||
assert((cast(ushort) ubyte.max).to!ubyte == ubyte.max);
|
||||
|
||||
// ubyte <- short
|
||||
assert((cast(short) 0).to!ubyte == 0);
|
||||
assert((cast(short) 1).to!ubyte == 1);
|
||||
assert((cast(short) (ubyte.max - 1)).to!ubyte == ubyte.max - 1);
|
||||
assert((cast(short) ubyte.max).to!ubyte == ubyte.max);
|
||||
|
||||
// short <-> int
|
||||
assert(short.min.to!int == short.min);
|
||||
assert((short.min + 1).to!int == short.min + 1);
|
||||
assert((cast(short) -1).to!int == -1);
|
||||
assert((cast(short) 0).to!int == 0);
|
||||
assert((cast(short) 1).to!int == 1);
|
||||
assert((short.max - 1).to!int == short.max - 1);
|
||||
assert(short.max.to!int == short.max);
|
||||
|
||||
assert((cast(int) short.min).to!short == short.min);
|
||||
assert((cast(int) short.min + 1).to!short == short.min + 1);
|
||||
assert((cast(int) -1).to!short == -1);
|
||||
assert((cast(int) 0).to!short == 0);
|
||||
assert((cast(int) 1).to!short == 1);
|
||||
assert((cast(int) short.max - 1).to!short == short.max - 1);
|
||||
assert((cast(int) short.max).to!short == short.max);
|
||||
|
||||
// uint <-> int
|
||||
assert((cast(uint) 0).to!int == 0);
|
||||
assert((cast(uint) 1).to!int == 1);
|
||||
assert((cast(uint) (int.max - 1)).to!int == int.max - 1);
|
||||
assert((cast(uint) int.max).to!int == int.max);
|
||||
|
||||
assert((cast(int) 0).to!uint == 0);
|
||||
assert((cast(int) 1).to!uint == 1);
|
||||
assert((cast(int) (int.max - 1)).to!uint == int.max - 1);
|
||||
assert((cast(int) int.max).to!uint == int.max);
|
||||
}
|
||||
|
||||
private unittest
|
||||
{
|
||||
ConvException exception;
|
||||
try
|
||||
{
|
||||
assert(int.min.to!short == int.min);
|
||||
}
|
||||
catch (ConvException e)
|
||||
{
|
||||
exception = e;
|
||||
}
|
||||
assert(exception !is null);
|
||||
defaultAllocator.dispose(exception);
|
||||
}
|
||||
|
||||
private unittest
|
||||
{
|
||||
ConvException exception;
|
||||
try
|
||||
{
|
||||
assert(int.max.to!short == int.max);
|
||||
}
|
||||
catch (ConvException e)
|
||||
{
|
||||
exception = e;
|
||||
}
|
||||
assert(exception !is null);
|
||||
defaultAllocator.dispose(exception);
|
||||
}
|
||||
|
||||
private unittest
|
||||
{
|
||||
ConvException exception;
|
||||
try
|
||||
{
|
||||
assert(uint.max.to!ushort == ushort.max);
|
||||
}
|
||||
catch (ConvException e)
|
||||
{
|
||||
exception = e;
|
||||
}
|
||||
assert(exception !is null);
|
||||
defaultAllocator.dispose(exception);
|
||||
}
|
||||
|
||||
private unittest
|
||||
{
|
||||
ConvException exception;
|
||||
try
|
||||
{
|
||||
assert((-1).to!uint == -1);
|
||||
}
|
||||
catch (ConvException e)
|
||||
{
|
||||
exception = e;
|
||||
}
|
||||
assert(exception !is null);
|
||||
defaultAllocator.dispose(exception);
|
||||
}
|
||||
|
||||
private @nogc unittest
|
||||
{
|
||||
enum Test : int
|
||||
{
|
||||
one,
|
||||
two,
|
||||
}
|
||||
assert(Test.one.to!int == 0);
|
||||
assert(Test.two.to!int == 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts $(D_PARAM from) to a boolean.
|
||||
*
|
||||
@ -296,6 +160,7 @@ private @nogc unittest
|
||||
*
|
||||
* Throws: $(D_PSYMBOL ConvException) if $(D_PARAM from) isn't convertible.
|
||||
*/
|
||||
deprecated("Use tanya.conv.to instead")
|
||||
To to(To, From)(From from)
|
||||
if (isNumeric!From && is(Unqual!To == bool) && !is(Unqual!To == Unqual!From))
|
||||
{
|
||||
@ -316,49 +181,8 @@ if (isNumeric!From && is(Unqual!To == bool) && !is(Unqual!To == Unqual!From))
|
||||
"Positive number overflow");
|
||||
}
|
||||
|
||||
///
|
||||
@nogc unittest
|
||||
{
|
||||
assert(!0.0.to!bool);
|
||||
assert(0.2.to!bool);
|
||||
assert(0.5.to!bool);
|
||||
assert(1.0.to!bool);
|
||||
|
||||
assert(!0.to!bool);
|
||||
assert(1.to!bool);
|
||||
}
|
||||
|
||||
private @nogc unittest
|
||||
{
|
||||
ConvException exception;
|
||||
try
|
||||
{
|
||||
assert((-1).to!bool);
|
||||
}
|
||||
catch (ConvException e)
|
||||
{
|
||||
exception = e;
|
||||
}
|
||||
assert(exception !is null);
|
||||
defaultAllocator.dispose(exception);
|
||||
}
|
||||
|
||||
private @nogc unittest
|
||||
{
|
||||
ConvException exception;
|
||||
try
|
||||
{
|
||||
assert(2.to!bool);
|
||||
}
|
||||
catch (ConvException e)
|
||||
{
|
||||
exception = e;
|
||||
}
|
||||
assert(exception !is null);
|
||||
defaultAllocator.dispose(exception);
|
||||
}
|
||||
|
||||
/// ditto
|
||||
deprecated("Use tanya.conv.to instead")
|
||||
To to(To, From)(auto ref const From from)
|
||||
if ((is(From == String) || isSomeString!From) && is(Unqual!To == bool))
|
||||
{
|
||||
@ -374,31 +198,6 @@ if ((is(From == String) || isSomeString!From) && is(Unqual!To == bool))
|
||||
"String doesn't contain a boolean value");
|
||||
}
|
||||
|
||||
///
|
||||
@nogc unittest
|
||||
{
|
||||
assert("true".to!bool);
|
||||
assert(!"false".to!bool);
|
||||
assert(String("true").to!bool);
|
||||
assert(!String("false").to!bool);
|
||||
|
||||
}
|
||||
|
||||
private @nogc unittest
|
||||
{
|
||||
ConvException exception;
|
||||
try
|
||||
{
|
||||
assert("1".to!bool);
|
||||
}
|
||||
catch (ConvException e)
|
||||
{
|
||||
exception = e;
|
||||
}
|
||||
assert(exception !is null);
|
||||
defaultAllocator.dispose(exception);
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a boolean to $(D_PARAM To).
|
||||
*
|
||||
@ -415,54 +214,21 @@ private @nogc unittest
|
||||
*
|
||||
* Returns: $(D_PARAM from) converted to $(D_PARAM To).
|
||||
*/
|
||||
deprecated("Use tanya.conv.to instead")
|
||||
To to(To, From)(const From from)
|
||||
if (is(Unqual!From == bool) && isNumeric!To && !is(Unqual!To == Unqual!From))
|
||||
{
|
||||
return from;
|
||||
}
|
||||
|
||||
///
|
||||
pure nothrow @safe @nogc unittest
|
||||
{
|
||||
assert(true.to!float == 1.0);
|
||||
assert(true.to!double == 1.0);
|
||||
assert(true.to!ubyte == 1);
|
||||
assert(true.to!byte == 1);
|
||||
assert(true.to!ushort == 1);
|
||||
assert(true.to!short == 1);
|
||||
assert(true.to!uint == 1);
|
||||
assert(true.to!int == 1);
|
||||
|
||||
assert(false.to!float == 0);
|
||||
assert(false.to!double == 0);
|
||||
assert(false.to!ubyte == 0);
|
||||
assert(false.to!byte == 0);
|
||||
assert(false.to!ushort == 0);
|
||||
assert(false.to!short == 0);
|
||||
assert(false.to!uint == 0);
|
||||
assert(false.to!int == 0);
|
||||
}
|
||||
|
||||
/// ditto
|
||||
deprecated("Use tanya.conv.to instead")
|
||||
To to(To, From)(const From from)
|
||||
if (is(Unqual!From == bool) && is(Unqual!To == String))
|
||||
{
|
||||
return String(from ? "true" : "false");
|
||||
}
|
||||
|
||||
///
|
||||
@nogc unittest
|
||||
{
|
||||
assert(true.to!String == "true");
|
||||
assert(false.to!String == "false");
|
||||
}
|
||||
|
||||
private @nogc unittest
|
||||
{
|
||||
static assert(is(typeof((const String("true")).to!bool)));
|
||||
static assert(is(typeof(false.to!(const String) == "false")));
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a floating point number to an integral type.
|
||||
*
|
||||
@ -477,6 +243,7 @@ private @nogc unittest
|
||||
* Throws: $(D_PSYMBOL ConvException) if
|
||||
* $(D_INLINECODE from < To.min || from > To.max).
|
||||
*/
|
||||
deprecated("Use tanya.conv.to instead")
|
||||
To to(To, From)(From from)
|
||||
if (isFloatingPoint!From
|
||||
&& isIntegral!To
|
||||
@ -496,60 +263,6 @@ if (isFloatingPoint!From
|
||||
return cast(To) from;
|
||||
}
|
||||
|
||||
///
|
||||
@nogc unittest
|
||||
{
|
||||
assert(1.5.to!int == 1);
|
||||
assert(2147483646.5.to!int == 2147483646);
|
||||
assert((-2147483647.5).to!int == -2147483647);
|
||||
assert(2147483646.5.to!uint == 2147483646);
|
||||
}
|
||||
|
||||
private @nogc unittest
|
||||
{
|
||||
ConvException exception;
|
||||
try
|
||||
{
|
||||
assert(2147483647.5.to!int == 2147483647);
|
||||
}
|
||||
catch (ConvException e)
|
||||
{
|
||||
exception = e;
|
||||
}
|
||||
assert(exception !is null);
|
||||
defaultAllocator.dispose(exception);
|
||||
}
|
||||
|
||||
private @nogc unittest
|
||||
{
|
||||
ConvException exception;
|
||||
try
|
||||
{
|
||||
assert((-2147483648.5).to!int == -2147483648);
|
||||
}
|
||||
catch (ConvException e)
|
||||
{
|
||||
exception = e;
|
||||
}
|
||||
assert(exception !is null);
|
||||
defaultAllocator.dispose(exception);
|
||||
}
|
||||
|
||||
private @nogc unittest
|
||||
{
|
||||
ConvException exception;
|
||||
try
|
||||
{
|
||||
assert((-21474.5).to!uint == -21474);
|
||||
}
|
||||
catch (ConvException e)
|
||||
{
|
||||
exception = e;
|
||||
}
|
||||
assert(exception !is null);
|
||||
defaultAllocator.dispose(exception);
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs checked conversion from an integral type $(D_PARAM From) to an
|
||||
* $(D_KEYWORD enum).
|
||||
@ -564,6 +277,7 @@ private @nogc unittest
|
||||
* Throws: $(D_PSYMBOL ConvException) if $(D_PARAM from) is not a member of
|
||||
* $(D_PSYMBOL To).
|
||||
*/
|
||||
deprecated("Use tanya.conv.to instead")
|
||||
To to(To, From)(From from)
|
||||
if (isIntegral!From && is(To == enum))
|
||||
{
|
||||
@ -577,115 +291,3 @@ if (isIntegral!From && is(To == enum))
|
||||
throw make!ConvException(defaultAllocator,
|
||||
"Value not found in enum '" ~ To.stringof ~ "'");
|
||||
}
|
||||
|
||||
///
|
||||
@nogc unittest
|
||||
{
|
||||
enum Test : int
|
||||
{
|
||||
one,
|
||||
two,
|
||||
}
|
||||
static assert(is(typeof(1.to!Test) == Test));
|
||||
assert(0.to!Test == Test.one);
|
||||
assert(1.to!Test == Test.two);
|
||||
}
|
||||
|
||||
private @nogc unittest
|
||||
{
|
||||
enum Test : uint
|
||||
{
|
||||
one,
|
||||
two,
|
||||
}
|
||||
|
||||
ConvException exception;
|
||||
try
|
||||
{
|
||||
assert(5.to!Test == Test.one);
|
||||
}
|
||||
catch (ConvException e)
|
||||
{
|
||||
exception = e;
|
||||
}
|
||||
assert(exception !is null);
|
||||
defaultAllocator.dispose(exception);
|
||||
}
|
||||
|
||||
// Returns the last part of buffer with converted number.
|
||||
package(tanya) char[] number2String(T)(const T number,
|
||||
return ref char[21] buffer)
|
||||
if (isIntegral!T)
|
||||
{
|
||||
// abs the integer.
|
||||
ulong n64 = number < 0 ? -cast(long) number : number;
|
||||
|
||||
char* start = buffer[].ptr + buffer.sizeof - 1;
|
||||
|
||||
while (true)
|
||||
{
|
||||
// Do in 32-bit chunks (avoid lots of 64-bit divides even with constant
|
||||
// denominators).
|
||||
char* o = start - 8;
|
||||
uint n;
|
||||
if (n64 >= 100000000)
|
||||
{
|
||||
n = n64 % 100000000;
|
||||
n64 /= 100000000;
|
||||
}
|
||||
else
|
||||
{
|
||||
n = cast(uint) n64;
|
||||
n64 = 0;
|
||||
}
|
||||
|
||||
while (n)
|
||||
{
|
||||
*--start = cast(char) (n % 10) + '0';
|
||||
n /= 10;
|
||||
}
|
||||
// Ignore the leading zero if it was the last part of the integer.
|
||||
if (n64 == 0)
|
||||
{
|
||||
if ((start[0] == '0')
|
||||
&& (start != (buffer[].ptr + buffer.sizeof -1)))
|
||||
{
|
||||
++start;
|
||||
}
|
||||
break;
|
||||
}
|
||||
// Copy leading zeros if it wasn't the most significant part of the
|
||||
// integer.
|
||||
while (start != o)
|
||||
{
|
||||
*--start = '0';
|
||||
}
|
||||
}
|
||||
|
||||
// Get the length that we have copied.
|
||||
uint l = cast(uint) ((buffer[].ptr + buffer.sizeof - 1) - start);
|
||||
if (l == 0)
|
||||
{
|
||||
*--start = '0';
|
||||
l = 1;
|
||||
}
|
||||
else if (number < 0) // Set the sign.
|
||||
{
|
||||
*--start = '-';
|
||||
++l;
|
||||
}
|
||||
|
||||
return buffer[$ - l - 1 .. $ - 1];
|
||||
}
|
||||
|
||||
// Converting an integer to string.
|
||||
private pure nothrow @system @nogc unittest
|
||||
{
|
||||
char[21] buf;
|
||||
|
||||
assert(number2String(80, buf) == "80");
|
||||
assert(number2String(-80, buf) == "-80");
|
||||
assert(number2String(0, buf) == "0");
|
||||
assert(number2String(uint.max, buf) == "4294967295");
|
||||
assert(number2String(int.min, buf) == "-2147483648");
|
||||
}
|
||||
|
@ -14,4 +14,878 @@
|
||||
*/
|
||||
module tanya.format;
|
||||
|
||||
public import tanya.format.conv;
|
||||
import tanya.container.string;
|
||||
import tanya.encoding.ascii;
|
||||
public import tanya.format.conv;
|
||||
import tanya.math;
|
||||
import tanya.memory.op;
|
||||
import tanya.meta.metafunction;
|
||||
import tanya.meta.trait;
|
||||
import tanya.meta.transform;
|
||||
import tanya.range.array;
|
||||
import tanya.range.primitive;
|
||||
|
||||
// Integer and floating point to string conversion is based on stb_sprintf
|
||||
// written by Jeff Roberts.
|
||||
|
||||
// Returns the last part of buffer with converted number.
|
||||
package(tanya) char[] integral2String(T)(T number, return ref char[21] buffer)
|
||||
@trusted
|
||||
if (isIntegral!T)
|
||||
{
|
||||
// abs the integer.
|
||||
ulong n64 = number < 0 ? -cast(long) number : number;
|
||||
|
||||
char* start = buffer[].ptr + buffer.sizeof - 1;
|
||||
|
||||
while (true)
|
||||
{
|
||||
// Do in 32-bit chunks (avoid lots of 64-bit divides even with constant
|
||||
// denominators).
|
||||
char* o = start - 8;
|
||||
uint n;
|
||||
if (n64 >= 100000000)
|
||||
{
|
||||
n = n64 % 100000000;
|
||||
n64 /= 100000000;
|
||||
}
|
||||
else
|
||||
{
|
||||
n = cast(uint) n64;
|
||||
n64 = 0;
|
||||
}
|
||||
|
||||
while (n)
|
||||
{
|
||||
*--start = cast(char) (n % 10) + '0';
|
||||
n /= 10;
|
||||
}
|
||||
// Ignore the leading zero if it was the last part of the integer.
|
||||
if (n64 == 0)
|
||||
{
|
||||
if ((start[0] == '0')
|
||||
&& (start != (buffer[].ptr + buffer.sizeof -1)))
|
||||
{
|
||||
++start;
|
||||
}
|
||||
break;
|
||||
}
|
||||
// Copy leading zeros if it wasn't the most significant part of the
|
||||
// integer.
|
||||
while (start != o)
|
||||
{
|
||||
*--start = '0';
|
||||
}
|
||||
}
|
||||
|
||||
// Get the length that we have copied.
|
||||
uint l = cast(uint) ((buffer[].ptr + buffer.sizeof - 1) - start);
|
||||
if (l == 0)
|
||||
{
|
||||
*--start = '0';
|
||||
l = 1;
|
||||
}
|
||||
else if (number < 0) // Set the sign.
|
||||
{
|
||||
*--start = '-';
|
||||
++l;
|
||||
}
|
||||
|
||||
return buffer[$ - l - 1 .. $ - 1];
|
||||
}
|
||||
|
||||
// Converting an integer to string.
|
||||
@nogc nothrow pure @system unittest
|
||||
{
|
||||
char[21] buf;
|
||||
|
||||
assert(integral2String(80, buf) == "80");
|
||||
assert(integral2String(-80, buf) == "-80");
|
||||
assert(integral2String(0, buf) == "0");
|
||||
assert(integral2String(uint.max, buf) == "4294967295");
|
||||
assert(integral2String(int.min, buf) == "-2147483648");
|
||||
}
|
||||
|
||||
/*
|
||||
* Double-double high-precision floating point number.
|
||||
*
|
||||
* The first element is a base value corresponding to the nearest approximation
|
||||
* of the target $(D_PSYMBOL HP) value, and the second element is an offset
|
||||
* value corresponding to the difference between the target value and the base.
|
||||
* Thus, the $(D_PSYMBOL HP) value represented is the sum of the base and the
|
||||
* offset.
|
||||
*/
|
||||
private struct HP
|
||||
{
|
||||
private double base;
|
||||
private double offset = 0.0;
|
||||
|
||||
private void normalize() @nogc nothrow pure @safe
|
||||
{
|
||||
const double target = this.base + this.offset;
|
||||
this.offset -= target - this.base;
|
||||
this.base = target;
|
||||
}
|
||||
|
||||
private void multiply(ref const HP x, ref const HP y)
|
||||
@nogc nothrow pure @safe
|
||||
{
|
||||
HP a, b;
|
||||
long bt;
|
||||
|
||||
this.base = x.base * y.base;
|
||||
copyFp(x.base, bt);
|
||||
bt &= ulong.max << 27;
|
||||
copyFp(bt, a.base);
|
||||
|
||||
a.offset = x.base - a.base;
|
||||
copyFp(y.base, bt);
|
||||
bt &= ulong.max << 27;
|
||||
copyFp(bt, b.base);
|
||||
|
||||
b.offset = y.base - b.base;
|
||||
this.offset = a.base * b.base - this.base
|
||||
+ a.base * b.offset
|
||||
+ a.offset * b.base
|
||||
+ a.offset * b.offset;
|
||||
this.offset += x.base * y.offset + x.offset * y.base;
|
||||
}
|
||||
}
|
||||
|
||||
private enum special = 0x7000;
|
||||
private enum char period = '.';
|
||||
|
||||
private static const ulong[20] powersOf10 = [
|
||||
1,
|
||||
10,
|
||||
100,
|
||||
1000,
|
||||
10000,
|
||||
100000,
|
||||
1000000,
|
||||
10000000,
|
||||
100000000,
|
||||
1000000000,
|
||||
10000000000UL,
|
||||
100000000000UL,
|
||||
1000000000000UL,
|
||||
10000000000000UL,
|
||||
100000000000000UL,
|
||||
1000000000000000UL,
|
||||
10000000000000000UL,
|
||||
100000000000000000UL,
|
||||
1000000000000000000UL,
|
||||
10000000000000000000UL,
|
||||
];
|
||||
|
||||
private static const char[201] digitPairs =
|
||||
"0001020304050607080910111213141516171819202122232425262728293031323334353"
|
||||
~ "6373839404142434445464748495051525354555657585960616263646566676869707172"
|
||||
~ "737475767778798081828384858687888990919293949596979899";
|
||||
|
||||
private static const HP[23] bottom = [
|
||||
HP(1e+000), HP(1e+001), HP(1e+002), HP(1e+003), HP(1e+004), HP(1e+005),
|
||||
HP(1e+006), HP(1e+007), HP(1e+008), HP(1e+009), HP(1e+010), HP(1e+011),
|
||||
HP(1e+012), HP(1e+013), HP(1e+014), HP(1e+015), HP(1e+016), HP(1e+017),
|
||||
HP(1e+018), HP(1e+019), HP(1e+020), HP(1e+021), HP(1e+022),
|
||||
];
|
||||
|
||||
private static const HP[22] negativeBottom = [
|
||||
HP(1e-001, -5.551115123125783e-018),
|
||||
HP(1e-002, -2.0816681711721684e-019),
|
||||
HP(1e-003, -2.0816681711721686e-020),
|
||||
HP(1e-004, -4.7921736023859299e-021),
|
||||
HP(1e-005, -8.1803053914031305e-022),
|
||||
HP(1e-006, 4.5251888174113741e-023),
|
||||
HP(1e-007, 4.5251888174113739e-024),
|
||||
HP(1e-008, -2.0922560830128471e-025),
|
||||
HP(1e-009, -6.2281591457779853e-026),
|
||||
HP(1e-010, -3.6432197315497743e-027),
|
||||
HP(1e-011, 6.0503030718060191e-028),
|
||||
HP(1e-012, 2.0113352370744385e-029),
|
||||
HP(1e-013, -3.0373745563400371e-030),
|
||||
HP(1e-014, 1.1806906454401013e-032),
|
||||
HP(1e-015, -7.7705399876661076e-032),
|
||||
HP(1e-016, 2.0902213275965398e-033),
|
||||
HP(1e-017, -7.1542424054621921e-034),
|
||||
HP(1e-018, -7.1542424054621926e-035),
|
||||
HP(1e-019, 2.4754073164739869e-036),
|
||||
HP(1e-020, 5.4846728545790429e-037),
|
||||
HP(1e-021, 9.2462547772103625e-038),
|
||||
HP(1e-022, -4.8596774326570872e-039),
|
||||
];
|
||||
|
||||
private static const HP[13] top = [
|
||||
HP(1e+023, 8388608),
|
||||
HP(1e+046, 6.8601809640529717e+028),
|
||||
HP(1e+069, -7.253143638152921e+052),
|
||||
HP(1e+092, -4.3377296974619174e+075),
|
||||
HP(1e+115, -1.5559416129466825e+098),
|
||||
HP(1e+138, -3.2841562489204913e+121),
|
||||
HP(1e+161, -3.7745893248228135e+144),
|
||||
HP(1e+184, -1.7356668416969134e+167),
|
||||
HP(1e+207, -3.8893577551088374e+190),
|
||||
HP(1e+230, -9.9566444326005119e+213),
|
||||
HP(1e+253, 6.3641293062232429e+236),
|
||||
HP(1e+276, -5.2069140800249813e+259),
|
||||
HP(1e+299, -5.2504760255204387e+282),
|
||||
];
|
||||
|
||||
private static const HP[13] negativeTop = [
|
||||
HP(1e-023, 3.9565301985100693e-040L),
|
||||
HP(1e-046, -2.299904345391321e-063L),
|
||||
HP(1e-069, 3.6506201437945798e-086L),
|
||||
HP(1e-092, 1.1875228833981544e-109L),
|
||||
HP(1e-115, -5.0644902316928607e-132L),
|
||||
HP(1e-138, -6.7156837247865426e-155L),
|
||||
HP(1e-161, -2.812077463003139e-178L),
|
||||
HP(1e-184, -5.7778912386589953e-201L),
|
||||
HP(1e-207, 7.4997100559334532e-224L),
|
||||
HP(1e-230, -4.6439668915134491e-247L),
|
||||
HP(1e-253, -6.3691100762962136e-270L),
|
||||
HP(1e-276, -9.436808465446358e-293L),
|
||||
HP(1e-299, 8.0970921678014997e-317L),
|
||||
];
|
||||
|
||||
private enum ulong tenTo19th = 1000000000000000000UL;
|
||||
|
||||
// Power can be -323 to +350.
|
||||
private HP raise2Power10(const HP value, int power)
|
||||
@nogc nothrow pure @safe
|
||||
{
|
||||
HP result;
|
||||
if ((power >= 0) && (power <= 22))
|
||||
{
|
||||
result.multiply(value, bottom[power]);
|
||||
}
|
||||
else
|
||||
{
|
||||
HP p2;
|
||||
int e = power;
|
||||
|
||||
if (power < 0)
|
||||
{
|
||||
e = -e;
|
||||
}
|
||||
int et = (e * 0x2c9) >> 14; // % 23
|
||||
if (et > 13)
|
||||
{
|
||||
et = 13;
|
||||
}
|
||||
int eb = e - (et * 23);
|
||||
|
||||
result = value;
|
||||
if (power < 0)
|
||||
{
|
||||
if (eb != 0)
|
||||
{
|
||||
--eb;
|
||||
result.multiply(value, negativeBottom[eb]);
|
||||
}
|
||||
if (et)
|
||||
{
|
||||
result.normalize();
|
||||
--et;
|
||||
p2.multiply(result, negativeTop[et]);
|
||||
result = p2;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (eb != 0)
|
||||
{
|
||||
e = eb;
|
||||
if (eb > 22)
|
||||
{
|
||||
eb = 22;
|
||||
}
|
||||
e -= eb;
|
||||
result.multiply(value, bottom[eb]);
|
||||
if (e)
|
||||
{
|
||||
result.normalize();
|
||||
p2.multiply(result, bottom[e]);
|
||||
result = p2;
|
||||
}
|
||||
}
|
||||
if (et != 0)
|
||||
{
|
||||
result.normalize();
|
||||
--et;
|
||||
p2.multiply(result, top[et]);
|
||||
result = p2;
|
||||
}
|
||||
}
|
||||
}
|
||||
result.normalize();
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/*
|
||||
* Given a float value, returns the significant bits in bits, and the position
|
||||
* of the decimal point in $(D_PARAM exponent). +/-Inf and NaN are specified
|
||||
* by special values returned in the $(D_PARAM exponent). Sing bit is set in
|
||||
* $(D_PARAM sign).
|
||||
*/
|
||||
private const(char)[] real2String(double value,
|
||||
ref char[512] buffer,
|
||||
out int exponent,
|
||||
out bool sign) @nogc nothrow pure @trusted
|
||||
{
|
||||
long bits;
|
||||
copyFp(value, bits);
|
||||
|
||||
exponent = (bits >> 52) & 0x7ff;
|
||||
sign = signBit(value);
|
||||
if (sign)
|
||||
{
|
||||
value = -value;
|
||||
}
|
||||
|
||||
if (exponent == 2047) // Is NaN or Inf?
|
||||
{
|
||||
exponent = special;
|
||||
return (bits & ((1UL << 52) - 1)) != 0 ? "NaN" : "Inf";
|
||||
}
|
||||
|
||||
if (exponent == 0) // Is zero or denormal?
|
||||
{
|
||||
if ((bits << 1) == 0) // Zero.
|
||||
{
|
||||
exponent = 1;
|
||||
buffer[0] = '0';
|
||||
return buffer[0 .. 1];
|
||||
}
|
||||
|
||||
// Find the right exponent for denormals.
|
||||
for (long cursor = 1UL << 51; (bits & cursor) == 0; cursor >>= 1)
|
||||
{
|
||||
--exponent;
|
||||
}
|
||||
}
|
||||
|
||||
// "617 / 2048" and "1233 / 4096" are estimations for the common logarithm
|
||||
// (log10) of 2. Multiplied by a binary number it tells how big the number
|
||||
// is in decimals, so it translates the binary exponent into decimal
|
||||
// format. The estimation is tweaked to hit or undershoot by no more than
|
||||
// 1 of log10 of all exponents 1..2046.
|
||||
int tens = exponent - 1023; // Bias.
|
||||
if (tens < 0)
|
||||
{
|
||||
tens = tens * 617 / 2048;
|
||||
}
|
||||
else
|
||||
{
|
||||
tens = tens * 1233 / 4096 + 1;
|
||||
}
|
||||
|
||||
// Move the significant bits into position and stick them into an int.
|
||||
HP p = raise2Power10(HP(value), 18 - tens);
|
||||
|
||||
// Get full as much precision from double-double as possible.
|
||||
bits = cast(long) p.base;
|
||||
double vh = cast(double) bits;
|
||||
auto a = HP(p.base - vh);
|
||||
double t = a.base - p.base;
|
||||
a.offset = p.base - a.base + t - vh - t;
|
||||
bits += cast(long) (a.base + a.offset + p.offset);
|
||||
|
||||
// Check if we undershot (bits >= 10 ^ 19).
|
||||
if ((cast(ulong) bits) >= 1000000000000000000UL)
|
||||
{
|
||||
++tens;
|
||||
}
|
||||
|
||||
// Now do the rounding in integer land.
|
||||
enum uint fracDigits = 6;
|
||||
|
||||
uint dg = 1;
|
||||
if ((cast(ulong) bits) >= powersOf10[9])
|
||||
{
|
||||
dg = 10;
|
||||
}
|
||||
uint length;
|
||||
while ((cast(ulong) bits) >= powersOf10[dg])
|
||||
{
|
||||
++dg;
|
||||
if (dg == 20)
|
||||
{
|
||||
goto NoRound;
|
||||
}
|
||||
}
|
||||
if (fracDigits < dg)
|
||||
{
|
||||
// Add 0.5 at the right position and round.
|
||||
length = dg - fracDigits;
|
||||
if (length >= 24)
|
||||
{
|
||||
goto NoRound;
|
||||
}
|
||||
ulong r = powersOf10[length];
|
||||
bits = bits + (r / 2);
|
||||
if ((cast(ulong) bits) >= powersOf10[dg])
|
||||
{
|
||||
++tens;
|
||||
}
|
||||
bits /= r;
|
||||
}
|
||||
NoRound:
|
||||
|
||||
// Kill long trailing runs of zeros.
|
||||
if (bits)
|
||||
{
|
||||
while (bits > 0xffffffff)
|
||||
{
|
||||
if (bits % 1000)
|
||||
{
|
||||
goto Zeroed;
|
||||
}
|
||||
bits /= 1000;
|
||||
}
|
||||
auto n = cast(uint) bits;
|
||||
while ((n % 1000) == 0)
|
||||
{
|
||||
n /= 1000;
|
||||
}
|
||||
bits = n;
|
||||
}
|
||||
Zeroed:
|
||||
|
||||
// Convert to string.
|
||||
auto result = buffer.ptr + 64;
|
||||
length = 0;
|
||||
while (true)
|
||||
{
|
||||
uint n;
|
||||
char* o = result - 8;
|
||||
// Do the conversion in chunks of U32s (avoid most 64-bit divides,
|
||||
// worth it, constant denomiators be damned).
|
||||
if (bits >= 100000000)
|
||||
{
|
||||
n = cast(uint) (bits % 100000000);
|
||||
bits /= 100000000;
|
||||
}
|
||||
else
|
||||
{
|
||||
n = cast(uint) bits;
|
||||
bits = 0;
|
||||
}
|
||||
while (n)
|
||||
{
|
||||
result -= 2;
|
||||
*cast(ushort*) result = *cast(ushort*) &digitPairs[(n % 100) * 2];
|
||||
n /= 100;
|
||||
length += 2;
|
||||
}
|
||||
if (bits == 0)
|
||||
{
|
||||
if ((length != 0) && (result[0] == '0'))
|
||||
{
|
||||
++result;
|
||||
--length;
|
||||
}
|
||||
break;
|
||||
}
|
||||
for (; result !is o; ++length, --result)
|
||||
{
|
||||
*result = '0';
|
||||
}
|
||||
}
|
||||
exponent = tens;
|
||||
return result[0 .. length];
|
||||
}
|
||||
|
||||
/*
|
||||
* Copies double into long and back bitwise.
|
||||
*/
|
||||
private void copyFp(T, U)(ref const U src, ref T dest) @trusted
|
||||
if (T.sizeof == U.sizeof)
|
||||
{
|
||||
copy((&src)[0 .. 1], (&dest)[0 .. 1]);
|
||||
}
|
||||
|
||||
package(tanya) String format(string fmt, Args...)(auto ref Args args)
|
||||
{
|
||||
String result;
|
||||
|
||||
static if (is(Unqual!(Args[0]) == typeof(null)))
|
||||
{
|
||||
result.insertBack("null");
|
||||
}
|
||||
else static if(is(Unqual!(Args[0]) == bool))
|
||||
{
|
||||
result.insertBack(args[0] ? "true" : "false");
|
||||
}
|
||||
else static if (isSomeString!(Args[0])) // String
|
||||
{
|
||||
if (args[0] is null)
|
||||
{
|
||||
result.insertBack("null");
|
||||
}
|
||||
else
|
||||
{
|
||||
result.insertBack(args[0]);
|
||||
}
|
||||
}
|
||||
else static if (isSomeChar!(Args[0])) // Char
|
||||
{
|
||||
result.insertBack(args[0]);
|
||||
}
|
||||
else static if (isFloatingPoint!(Args[0])) // Float
|
||||
{
|
||||
char[512] buffer; // Big enough for e+308 or e-307.
|
||||
char[8] tail = 0;
|
||||
char[] bufferSlice = buffer[64 .. $];
|
||||
uint precision = 6;
|
||||
bool negative;
|
||||
int decimalPoint;
|
||||
|
||||
// Read the double into a string.
|
||||
auto realString = real2String(args[0], buffer, decimalPoint, negative);
|
||||
auto length = cast(uint) realString.length;
|
||||
|
||||
// Clamp the precision and delete extra zeros after clamp.
|
||||
uint n = precision;
|
||||
if (length > precision)
|
||||
{
|
||||
length = precision;
|
||||
}
|
||||
while ((length > 1)
|
||||
&& (precision != 0)
|
||||
&& (realString[length - 1] == '0'))
|
||||
{
|
||||
--precision;
|
||||
--length;
|
||||
}
|
||||
|
||||
if (negative)
|
||||
{
|
||||
result.insertBack('-');
|
||||
}
|
||||
if (decimalPoint == special)
|
||||
{
|
||||
result.insertBack(realString);
|
||||
goto ParamEnd;
|
||||
}
|
||||
|
||||
// Should we use sceintific notation?
|
||||
if ((decimalPoint <= -4) || (decimalPoint > cast(int) n))
|
||||
{
|
||||
if (precision > length)
|
||||
{
|
||||
precision = length - 1;
|
||||
}
|
||||
else if (precision > 0)
|
||||
{
|
||||
// When using scientific notation, there is one digit before the
|
||||
// decimal.
|
||||
--precision;
|
||||
}
|
||||
|
||||
// Handle leading chars.
|
||||
bufferSlice.front = realString[0];
|
||||
bufferSlice.popFront();
|
||||
|
||||
if (precision != 0)
|
||||
{
|
||||
bufferSlice.front = period;
|
||||
bufferSlice.popFront();
|
||||
}
|
||||
|
||||
// Handle after decimal.
|
||||
if ((length - 1) > precision)
|
||||
{
|
||||
length = precision + 1;
|
||||
}
|
||||
realString[1 .. length].copy(bufferSlice);
|
||||
bufferSlice.popFrontExactly(length - 1);
|
||||
|
||||
// Dump the exponent.
|
||||
tail[1] = 'e';
|
||||
--decimalPoint;
|
||||
if (decimalPoint < 0)
|
||||
{
|
||||
tail[2] = '-';
|
||||
decimalPoint = -decimalPoint;
|
||||
}
|
||||
else
|
||||
{
|
||||
tail[2] = '+';
|
||||
}
|
||||
|
||||
n = decimalPoint >= 100 ? 5 : 4;
|
||||
|
||||
tail[0] = cast(char) n;
|
||||
while (true)
|
||||
{
|
||||
tail[n] = '0' + decimalPoint % 10;
|
||||
if (n <= 3)
|
||||
{
|
||||
break;
|
||||
}
|
||||
--n;
|
||||
decimalPoint /= 10;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (decimalPoint > 0)
|
||||
{
|
||||
precision = decimalPoint < (cast(int) length)
|
||||
? length - decimalPoint
|
||||
: 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
precision = -decimalPoint
|
||||
+ (precision > length ? length : precision);
|
||||
}
|
||||
|
||||
// Handle the three decimal varieties.
|
||||
if (decimalPoint <= 0)
|
||||
{
|
||||
// Handle 0.000*000xxxx.
|
||||
bufferSlice.front = '0';
|
||||
bufferSlice.popFront();
|
||||
|
||||
if (precision != 0)
|
||||
{
|
||||
bufferSlice.front = period;
|
||||
bufferSlice.popFront();
|
||||
}
|
||||
n = -decimalPoint;
|
||||
if (n > precision)
|
||||
{
|
||||
n = precision;
|
||||
}
|
||||
|
||||
fill!'0'(bufferSlice[0 .. n]);
|
||||
bufferSlice.popFrontExactly(n);
|
||||
|
||||
if ((length + n) > precision)
|
||||
{
|
||||
length = precision - n;
|
||||
}
|
||||
|
||||
realString[0 .. length].copy(bufferSlice);
|
||||
bufferSlice.popFrontExactly(length);
|
||||
}
|
||||
else if (cast(uint) decimalPoint >= length)
|
||||
{
|
||||
// Handle xxxx000*000.0.
|
||||
n = 0;
|
||||
do
|
||||
{
|
||||
bufferSlice.front = realString[n];
|
||||
bufferSlice.popFront();
|
||||
++n;
|
||||
}
|
||||
while (n < length);
|
||||
if (n < cast(uint) decimalPoint)
|
||||
{
|
||||
n = decimalPoint - n;
|
||||
|
||||
fill!'0'(bufferSlice[0 .. n]);
|
||||
bufferSlice.popFrontExactly(n);
|
||||
}
|
||||
if (precision != 0)
|
||||
{
|
||||
bufferSlice.front = period;
|
||||
bufferSlice.popFront();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// Handle xxxxx.xxxx000*000.
|
||||
n = 0;
|
||||
do
|
||||
{
|
||||
bufferSlice.front = realString[n];
|
||||
bufferSlice.popFront();
|
||||
++n;
|
||||
}
|
||||
while (n < cast(uint) decimalPoint);
|
||||
|
||||
if (precision > 0)
|
||||
{
|
||||
bufferSlice.front = period;
|
||||
bufferSlice.popFront();
|
||||
}
|
||||
if ((length - decimalPoint) > precision)
|
||||
{
|
||||
length = precision + decimalPoint;
|
||||
}
|
||||
|
||||
realString[n .. length].copy(bufferSlice);
|
||||
bufferSlice.popFrontExactly(length - n);
|
||||
}
|
||||
}
|
||||
|
||||
// Get the length that we've copied.
|
||||
length = cast(uint) (buffer.length - bufferSlice.length);
|
||||
|
||||
result.insertBack(buffer[64 .. length]); // Number.
|
||||
result.insertBack(tail[1 .. tail[0] + 1]); // Tail.
|
||||
}
|
||||
else static if (isPointer!(Args[0])) // Pointer
|
||||
{
|
||||
char[size_t.sizeof * 2] buffer;
|
||||
size_t position = buffer.length;
|
||||
auto address = cast(size_t) args[0];
|
||||
|
||||
do // Write at least "0" if the pointer is null.
|
||||
{
|
||||
buffer[--position] = lowerHexDigits[cast(size_t) (address & 15)];
|
||||
address >>= 4;
|
||||
}
|
||||
while (address != 0);
|
||||
|
||||
result.insertBack("0x");
|
||||
result.insertBack(buffer[position .. $]);
|
||||
}
|
||||
else static if (isIntegral!(Args[0])) // Integer
|
||||
{
|
||||
char[21] buffer;
|
||||
result.insertBack(integral2String(args[0], buffer));
|
||||
}
|
||||
else
|
||||
{
|
||||
static assert(false);
|
||||
}
|
||||
ParamEnd:
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
// One argument tests.
|
||||
@nogc pure @safe unittest
|
||||
{
|
||||
// Modifiers.
|
||||
assert(format!"{}"(8.5) == "8.5");
|
||||
assert(format!"{}"(8.6) == "8.6");
|
||||
assert(format!"{}"(1000) == "1000");
|
||||
assert(format!"{}"(1) == "1");
|
||||
assert(format!"{}"(10.25) == "10.25");
|
||||
assert(format!"{}"(1) == "1");
|
||||
assert(format!"{}"(0.01) == "0.01");
|
||||
|
||||
// String printing.
|
||||
assert(format!"{}"("Some weired string") == "Some weired string");
|
||||
assert(format!"{}"(cast(string) null) == "null");
|
||||
assert(format!"{}"('c') == "c");
|
||||
|
||||
// Integer.
|
||||
assert(format!"{}"(8) == "8");
|
||||
assert(format!"{}"(8) == "8");
|
||||
assert(format!"{}"(-8) == "-8");
|
||||
assert(format!"{}"(-8L) == "-8");
|
||||
assert(format!"{}"(8) == "8");
|
||||
assert(format!"{}"(100000001) == "100000001");
|
||||
assert(format!"{}"(99999999L) == "99999999");
|
||||
assert(format!"{}"(10) == "10");
|
||||
assert(format!"{}"(10L) == "10");
|
||||
|
||||
// Floating point.
|
||||
assert(format!"{}"(0.1234) == "0.1234");
|
||||
assert(format!"{}"(0.3) == "0.3");
|
||||
assert(format!"{}"(0.333333333333) == "0.333333");
|
||||
assert(format!"{}"(38234.1234) == "38234.1");
|
||||
assert(format!"{}"(-0.3) == "-0.3");
|
||||
assert(format!"{}"(0.000000000000000006) == "6e-18");
|
||||
assert(format!"{}"(0.0) == "0");
|
||||
assert(format!"{}"(double.init) == "NaN");
|
||||
assert(format!"{}"(-double.init) == "-NaN");
|
||||
assert(format!"{}"(double.infinity) == "Inf");
|
||||
assert(format!"{}"(-double.infinity) == "-Inf");
|
||||
assert(format!"{}"(0.000000000000000000000000003) == "3e-27");
|
||||
assert(format!"{}"(0.23432e304) == "2.3432e+303");
|
||||
assert(format!"{}"(-0.23432e8) == "-2.3432e+07");
|
||||
assert(format!"{}"(1e-307) == "1e-307");
|
||||
assert(format!"{}"(1e+8) == "1e+08");
|
||||
assert(format!"{}"(111234.1) == "111234");
|
||||
assert(format!"{}"(0.999) == "0.999");
|
||||
assert(format!"{}"(0x1p-16382L) == "0");
|
||||
assert(format!"{}"(1e+3) == "1000");
|
||||
assert(format!"{}"(38234.1234) == "38234.1");
|
||||
|
||||
// typeof(null).
|
||||
assert(format!"{}"(null) == "null");
|
||||
|
||||
// Boolean.
|
||||
assert(format!"{}"(true) == "true");
|
||||
assert(format!"{}"(false) == "false");
|
||||
}
|
||||
|
||||
// Unsafe tests with pointers.
|
||||
@nogc pure @system unittest
|
||||
{
|
||||
// Pointer convesions
|
||||
assert(format!"{}"(cast(void*) 1) == "0x1");
|
||||
assert(format!"{}"(cast(void*) 20) == "0x14");
|
||||
assert(format!"{}"(cast(void*) null) == "0x0");
|
||||
}
|
||||
|
||||
private struct FormatSpec
|
||||
{
|
||||
}
|
||||
|
||||
// Returns the position of `tag` in `fmt`. If `tag` can't be found, returns the
|
||||
// length of `fmt`.
|
||||
private size_t specPosition(string fmt, char tag)()
|
||||
{
|
||||
foreach (i, c; fmt)
|
||||
{
|
||||
if (c == tag)
|
||||
{
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return fmt.length;
|
||||
}
|
||||
|
||||
private template ParseFmt(string fmt, size_t pos = 0)
|
||||
{
|
||||
static if (fmt.length == 0)
|
||||
{
|
||||
alias ParseFmt = AliasSeq!();
|
||||
}
|
||||
else static if (fmt[0] == '{')
|
||||
{
|
||||
static if (fmt.length > 1 && fmt[1] == '{')
|
||||
{
|
||||
enum size_t pos = specPosition!(fmt[2 .. $], '{') + 2;
|
||||
alias ParseFmt = AliasSeq!(fmt[1 .. pos],
|
||||
ParseFmt!(fmt[pos .. $], pos));
|
||||
}
|
||||
else
|
||||
{
|
||||
enum size_t pos = specPosition!(fmt[1 .. $], '}') + 1;
|
||||
static if (pos < fmt.length)
|
||||
{
|
||||
alias ParseFmt = AliasSeq!(FormatSpec(),
|
||||
ParseFmt!(fmt[pos + 1 .. $], pos + 1));
|
||||
}
|
||||
else
|
||||
{
|
||||
static assert(false, "Enclosing '}' is missing");
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
enum size_t pos = specPosition!(fmt, '{');
|
||||
alias ParseFmt = AliasSeq!(fmt[0 .. pos],
|
||||
ParseFmt!(fmt[pos .. $], pos));
|
||||
}
|
||||
}
|
||||
|
||||
@nogc nothrow pure @safe unittest
|
||||
{
|
||||
static assert(ParseFmt!"".length == 0);
|
||||
|
||||
static assert(ParseFmt!"asdf".length == 1);
|
||||
static assert(ParseFmt!"asdf"[0] == "asdf");
|
||||
|
||||
static assert(ParseFmt!"{}".length == 1);
|
||||
}
|
||||
|
@ -14,11 +14,14 @@
|
||||
*/
|
||||
module tanya.math.mp;
|
||||
|
||||
import std.algorithm;
|
||||
import std.algorithm.comparison;
|
||||
import std.algorithm.mutation : copy, fill, reverse;
|
||||
import std.range;
|
||||
import tanya.algorithm.mutation;
|
||||
import tanya.container.array;
|
||||
import tanya.encoding.ascii;
|
||||
import tanya.memory;
|
||||
static import tanya.memory.op;
|
||||
import tanya.meta.trait;
|
||||
import tanya.meta.transform;
|
||||
|
||||
@ -104,7 +107,7 @@ struct Integer
|
||||
}
|
||||
|
||||
/// ditto
|
||||
this(shared Allocator allocator) pure nothrow @safe @nogc
|
||||
this(shared Allocator allocator) @nogc nothrow pure @safe
|
||||
in
|
||||
{
|
||||
assert(allocator !is null);
|
||||
@ -156,7 +159,7 @@ struct Integer
|
||||
}
|
||||
|
||||
///
|
||||
nothrow @safe @nogc unittest
|
||||
@nogc nothrow pure @safe unittest
|
||||
{
|
||||
ubyte[8] range = [ 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xdd, 0xee ];
|
||||
auto integer = Integer(Sign.positive, range[]);
|
||||
@ -187,7 +190,7 @@ struct Integer
|
||||
}
|
||||
|
||||
///
|
||||
nothrow @safe @nogc unittest
|
||||
@nogc nothrow pure @safe unittest
|
||||
{
|
||||
{
|
||||
ubyte[8] range = [ 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xdd, 0xee ];
|
||||
@ -204,17 +207,17 @@ struct Integer
|
||||
/**
|
||||
* Copies the integer.
|
||||
*/
|
||||
this(this) nothrow @trusted @nogc
|
||||
this(this) @nogc nothrow pure @safe
|
||||
{
|
||||
auto tmp = allocator.resize!digit(null, this.size);
|
||||
this.rep[0 .. this.size].copy(tmp);
|
||||
tanya.memory.op.copy(this.rep[0 .. this.size], tmp);
|
||||
this.rep = tmp;
|
||||
}
|
||||
|
||||
/**
|
||||
* Destroys the integer.
|
||||
*/
|
||||
~this() nothrow @trusted @nogc
|
||||
~this() @nogc nothrow pure @safe
|
||||
{
|
||||
allocator.resize(this.rep, 0);
|
||||
}
|
||||
@ -224,7 +227,7 @@ struct Integer
|
||||
];
|
||||
|
||||
// Counts the number of LSBs before the first non-zero bit.
|
||||
private ptrdiff_t countLSBs() const pure nothrow @safe @nogc
|
||||
private ptrdiff_t countLSBs() const @nogc nothrow pure @safe
|
||||
{
|
||||
if (this.size == 0)
|
||||
{
|
||||
@ -256,7 +259,7 @@ struct Integer
|
||||
/**
|
||||
* Returns: Number of bytes in the two's complement representation.
|
||||
*/
|
||||
@property size_t length() const pure nothrow @safe @nogc
|
||||
@property size_t length() const @nogc nothrow pure @safe
|
||||
{
|
||||
if (this.sign)
|
||||
{
|
||||
@ -280,7 +283,7 @@ struct Integer
|
||||
}
|
||||
|
||||
///
|
||||
private nothrow @safe @nogc unittest
|
||||
@nogc nothrow pure @safe unittest
|
||||
{
|
||||
{
|
||||
Integer i;
|
||||
@ -336,11 +339,12 @@ struct Integer
|
||||
}
|
||||
|
||||
/// ditto
|
||||
ref Integer opAssign(T)(ref T value) @trusted
|
||||
ref Integer opAssign(T)(ref T value)
|
||||
if (is(Unqual!T == Integer))
|
||||
{
|
||||
this.rep = allocator.resize(this.rep, value.size);
|
||||
value.rep[0 .. value.size].copy(this.rep[0 .. value.size]);
|
||||
tanya.memory.op.copy(value.rep[0 .. value.size],
|
||||
this.rep[0 .. value.size]);
|
||||
this.size = value.size;
|
||||
this.sign = value.sign;
|
||||
|
||||
@ -348,7 +352,7 @@ struct Integer
|
||||
}
|
||||
|
||||
/// ditto
|
||||
ref Integer opAssign(T)(T value) nothrow @safe @nogc
|
||||
ref Integer opAssign(T)(T value)
|
||||
if (is(T == Integer))
|
||||
{
|
||||
swap(this.rep, value.rep);
|
||||
@ -393,7 +397,7 @@ struct Integer
|
||||
}
|
||||
|
||||
///
|
||||
@safe @nogc unittest
|
||||
@nogc nothrow pure @safe unittest
|
||||
{
|
||||
auto integer = Integer(79);
|
||||
assert(cast(ushort) integer == 79);
|
||||
@ -424,7 +428,7 @@ struct Integer
|
||||
* Typically very fast. Also fixes the sign if there
|
||||
* are no more leading digits
|
||||
*/
|
||||
void contract() nothrow @safe @nogc
|
||||
void contract() @nogc nothrow pure @safe
|
||||
{
|
||||
/* decrease size while the most significant digit is
|
||||
* zero.
|
||||
@ -441,7 +445,7 @@ struct Integer
|
||||
}
|
||||
}
|
||||
|
||||
private void grow(const size_t size) nothrow @trusted @nogc
|
||||
private void grow(const size_t size) @nogc nothrow pure @safe
|
||||
{
|
||||
if (this.rep.length >= size)
|
||||
{
|
||||
@ -452,7 +456,7 @@ struct Integer
|
||||
this.rep[oldLength .. $].fill(digit.init);
|
||||
}
|
||||
|
||||
private size_t countBits() const pure nothrow @safe @nogc
|
||||
private size_t countBits() const @nogc nothrow pure @safe
|
||||
{
|
||||
if (this.size == 0)
|
||||
{
|
||||
@ -470,7 +474,7 @@ struct Integer
|
||||
}
|
||||
|
||||
private void add(ref const Integer summand, ref Integer sum)
|
||||
const nothrow @safe @nogc
|
||||
const @nogc nothrow pure @safe
|
||||
{
|
||||
const(digit)[] max, min;
|
||||
|
||||
@ -521,7 +525,7 @@ struct Integer
|
||||
}
|
||||
|
||||
private void add(const digit summand, ref Integer sum)
|
||||
const nothrow @safe @nogc
|
||||
const @nogc nothrow pure @safe
|
||||
{
|
||||
sum.grow(this.size + 2);
|
||||
|
||||
@ -547,7 +551,7 @@ struct Integer
|
||||
}
|
||||
|
||||
private void subtract(ref const Integer subtrahend, ref Integer difference)
|
||||
const nothrow @safe @nogc
|
||||
const @nogc nothrow pure @safe
|
||||
{
|
||||
difference.grow(this.size);
|
||||
|
||||
@ -583,7 +587,7 @@ struct Integer
|
||||
}
|
||||
|
||||
private void subtract(const digit subtrahend, ref Integer difference)
|
||||
const nothrow @safe @nogc
|
||||
const @nogc nothrow pure @safe
|
||||
{
|
||||
difference.grow(this.size);
|
||||
|
||||
@ -612,7 +616,7 @@ struct Integer
|
||||
}
|
||||
|
||||
// Compare the magnitude.
|
||||
private int compare(ref const Integer that) const pure nothrow @safe @nogc
|
||||
private int compare(ref const Integer that) const @nogc nothrow pure @safe
|
||||
{
|
||||
if (this.size > that.size)
|
||||
{
|
||||
@ -661,7 +665,7 @@ struct Integer
|
||||
}
|
||||
|
||||
///
|
||||
@safe @nogc unittest
|
||||
@nogc nothrow pure @safe unittest
|
||||
{
|
||||
auto integer1 = Integer(1019);
|
||||
auto integer2 = Integer(1019);
|
||||
@ -707,7 +711,7 @@ struct Integer
|
||||
}
|
||||
|
||||
///
|
||||
@safe @nogc unittest
|
||||
@nogc nothrow pure @safe unittest
|
||||
{
|
||||
auto integer = Integer(1019);
|
||||
|
||||
@ -731,7 +735,7 @@ struct Integer
|
||||
}
|
||||
|
||||
///
|
||||
@safe @nogc unittest
|
||||
@nogc nothrow pure @safe unittest
|
||||
{
|
||||
auto integer = Integer(1019);
|
||||
|
||||
@ -767,7 +771,7 @@ struct Integer
|
||||
}
|
||||
|
||||
///
|
||||
unittest
|
||||
@nogc nothrow pure @safe unittest
|
||||
{
|
||||
{
|
||||
auto h1 = Integer(1019);
|
||||
@ -809,7 +813,7 @@ struct Integer
|
||||
}
|
||||
|
||||
///
|
||||
unittest
|
||||
@nogc nothrow pure @safe unittest
|
||||
{
|
||||
{
|
||||
auto h1 = Integer(3);
|
||||
@ -853,7 +857,7 @@ struct Integer
|
||||
}
|
||||
|
||||
///
|
||||
nothrow @safe @nogc unittest
|
||||
@nogc nothrow pure @safe unittest
|
||||
{
|
||||
auto h1 = Integer(123);
|
||||
auto h2 = Integer(456);
|
||||
@ -885,7 +889,7 @@ struct Integer
|
||||
return this;
|
||||
}
|
||||
|
||||
nothrow @safe @nogc unittest
|
||||
@nogc nothrow pure @safe unittest
|
||||
{
|
||||
auto h1 = Integer(18);
|
||||
auto h2 = Integer(4);
|
||||
@ -937,7 +941,7 @@ struct Integer
|
||||
}
|
||||
|
||||
///
|
||||
nothrow @safe @nogc unittest
|
||||
@nogc nothrow pure @safe unittest
|
||||
{
|
||||
auto integer = Integer(4294967294);
|
||||
integer >>= 10;
|
||||
@ -1005,7 +1009,7 @@ struct Integer
|
||||
}
|
||||
|
||||
///
|
||||
nothrow @safe @nogc unittest
|
||||
@nogc nothrow pure @safe unittest
|
||||
{
|
||||
auto integer = Integer(4294967295);
|
||||
integer <<= 1;
|
||||
@ -1023,7 +1027,10 @@ struct Integer
|
||||
Integer opUnary(string op : "~")() const
|
||||
{
|
||||
auto ret = Integer(this, allocator);
|
||||
ret.rep[0 .. ret.size].each!((ref a) => a = ~a & mask);
|
||||
foreach (ref a; ret.rep[0 .. ret.size])
|
||||
{
|
||||
a = ~a & mask;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
@ -1049,7 +1056,7 @@ struct Integer
|
||||
}
|
||||
|
||||
//
|
||||
nothrow @safe @nogc unittest
|
||||
@nogc nothrow pure @safe unittest
|
||||
{
|
||||
auto h1 = Integer(79);
|
||||
Integer h2;
|
||||
@ -1102,7 +1109,7 @@ struct Integer
|
||||
}
|
||||
|
||||
///
|
||||
nothrow @safe @nogc unittest
|
||||
@nogc nothrow pure @safe unittest
|
||||
{
|
||||
Integer integer;
|
||||
|
||||
@ -1172,7 +1179,7 @@ struct Integer
|
||||
}
|
||||
|
||||
// Shift right a certain amount of digits.
|
||||
private void shiftRight(const size_t operand) nothrow @safe @nogc
|
||||
private void shiftRight(const size_t operand) @nogc nothrow pure @safe
|
||||
{
|
||||
if (operand == 0)
|
||||
{
|
||||
@ -1191,7 +1198,7 @@ struct Integer
|
||||
}
|
||||
|
||||
// Shift left a certain amount of digits.
|
||||
private void shiftLeft(const size_t operand) nothrow @safe @nogc
|
||||
private void shiftLeft(const size_t operand) @nogc nothrow pure @safe
|
||||
{
|
||||
if (operand == 0)
|
||||
{
|
||||
@ -1213,7 +1220,7 @@ struct Integer
|
||||
}
|
||||
|
||||
private void multiply(const digit factor, ref Integer product)
|
||||
const nothrow @safe @nogc
|
||||
const @nogc nothrow pure @safe
|
||||
{
|
||||
product.grow(this.size + 1);
|
||||
product.sign = this.sign;
|
||||
@ -1239,7 +1246,7 @@ struct Integer
|
||||
|
||||
private void multiply(ref const Integer factor,
|
||||
ref Integer product,
|
||||
const size_t digits) const nothrow @safe @nogc
|
||||
const size_t digits) const @nogc nothrow pure @safe
|
||||
{
|
||||
Integer intermediate;
|
||||
intermediate.grow(digits);
|
||||
@ -1270,8 +1277,7 @@ struct Integer
|
||||
|
||||
private void divide(Q, ARGS...)(ref const Integer divisor,
|
||||
auto ref Q quotient,
|
||||
ref ARGS args)
|
||||
const nothrow @safe @nogc
|
||||
ref ARGS args) const
|
||||
if ((is(Q : typeof(null))
|
||||
|| (is(Q : Integer) && __traits(isRef, quotient)))
|
||||
&& (ARGS.length == 0 || (ARGS.length == 1 && is(ARGS[0] : Integer))))
|
||||
@ -1404,7 +1410,7 @@ struct Integer
|
||||
}
|
||||
}
|
||||
|
||||
private Integer square() nothrow @safe @nogc
|
||||
private Integer square() @nogc nothrow pure @safe
|
||||
{
|
||||
Integer result;
|
||||
const resultSize = 2 * this.size + 1;
|
||||
@ -1444,7 +1450,7 @@ struct Integer
|
||||
}
|
||||
|
||||
// Returns 2^^n.
|
||||
private Integer exp2(size_t n) const nothrow @safe @nogc
|
||||
private Integer exp2(size_t n) const @nogc nothrow pure @safe
|
||||
{
|
||||
auto ret = Integer(allocator);
|
||||
const bytes = n / digitBitCount;
|
||||
@ -1459,7 +1465,7 @@ struct Integer
|
||||
/**
|
||||
* Returns: Two's complement representation of the integer.
|
||||
*/
|
||||
Array!ubyte toArray() const nothrow @safe @nogc
|
||||
Array!ubyte toArray() const @nogc nothrow pure @safe
|
||||
out (array)
|
||||
{
|
||||
assert(array.length == length);
|
||||
@ -1512,7 +1518,7 @@ struct Integer
|
||||
}
|
||||
|
||||
///
|
||||
nothrow @safe @nogc unittest
|
||||
@nogc nothrow pure @safe unittest
|
||||
{
|
||||
{
|
||||
auto integer = Integer(0x66778899aabbddee);
|
||||
@ -1521,6 +1527,14 @@ struct Integer
|
||||
auto array = integer.toArray();
|
||||
assert(equal(array[], expected[]));
|
||||
}
|
||||
}
|
||||
|
||||
@nogc nothrow pure @safe unittest
|
||||
{
|
||||
{
|
||||
Integer integer;
|
||||
assert(integer.toArray().length == 0);
|
||||
}
|
||||
{
|
||||
auto integer = Integer(0x03);
|
||||
ubyte[1] expected = [ 0x03 ];
|
||||
|
@ -21,9 +21,11 @@
|
||||
*/
|
||||
module tanya.math;
|
||||
|
||||
import tanya.algorithm.mutation;
|
||||
import tanya.math.mp;
|
||||
import tanya.math.nbtheory;
|
||||
import tanya.meta.trait;
|
||||
import tanya.meta.transform;
|
||||
|
||||
/// Floating-point number precisions according to IEEE-754.
|
||||
enum IEEEPrecision : ubyte
|
||||
@ -79,7 +81,7 @@ if (isFloatingPoint!F)
|
||||
}
|
||||
|
||||
///
|
||||
pure nothrow @safe @nogc unittest
|
||||
@nogc nothrow pure @safe unittest
|
||||
{
|
||||
static assert(ieeePrecision!float == IEEEPrecision.single);
|
||||
static assert(ieeePrecision!double == IEEEPrecision.double_);
|
||||
@ -231,7 +233,7 @@ if (isFloatingPoint!F)
|
||||
}
|
||||
|
||||
///
|
||||
pure nothrow @safe @nogc unittest
|
||||
@nogc nothrow pure @safe unittest
|
||||
{
|
||||
assert(classify(0.0) == FloatingPointClass.zero);
|
||||
assert(classify(double.nan) == FloatingPointClass.nan);
|
||||
@ -253,7 +255,7 @@ pure nothrow @safe @nogc unittest
|
||||
assert(classify(-real.infinity) == FloatingPointClass.infinite);
|
||||
}
|
||||
|
||||
private pure nothrow @nogc @safe unittest
|
||||
@nogc nothrow pure @safe unittest
|
||||
{
|
||||
static if (ieeePrecision!float == IEEEPrecision.doubleExtended)
|
||||
{
|
||||
@ -300,7 +302,7 @@ if (isFloatingPoint!F)
|
||||
}
|
||||
|
||||
///
|
||||
pure nothrow @safe @nogc unittest
|
||||
@nogc nothrow pure @safe unittest
|
||||
{
|
||||
assert(!isFinite(float.infinity));
|
||||
assert(!isFinite(-double.infinity));
|
||||
@ -345,7 +347,7 @@ if (isFloatingPoint!F)
|
||||
}
|
||||
|
||||
///
|
||||
pure nothrow @safe @nogc unittest
|
||||
@nogc nothrow pure @safe unittest
|
||||
{
|
||||
assert(isNaN(float.init));
|
||||
assert(isNaN(double.init));
|
||||
@ -382,7 +384,7 @@ if (isFloatingPoint!F)
|
||||
}
|
||||
|
||||
///
|
||||
pure nothrow @safe @nogc unittest
|
||||
@nogc nothrow pure @safe unittest
|
||||
{
|
||||
assert(isInfinity(float.infinity));
|
||||
assert(isInfinity(-float.infinity));
|
||||
@ -436,7 +438,7 @@ if (isFloatingPoint!F)
|
||||
}
|
||||
|
||||
///
|
||||
pure nothrow @safe @nogc unittest
|
||||
@nogc nothrow pure @safe unittest
|
||||
{
|
||||
assert(!isSubnormal(0.0f));
|
||||
assert(!isSubnormal(float.nan));
|
||||
@ -496,7 +498,7 @@ if (isFloatingPoint!F)
|
||||
}
|
||||
|
||||
///
|
||||
pure nothrow @safe @nogc unittest
|
||||
@nogc nothrow pure @safe unittest
|
||||
{
|
||||
assert(!isNormal(0.0f));
|
||||
assert(!isNormal(float.nan));
|
||||
@ -547,7 +549,7 @@ if (isFloatingPoint!F)
|
||||
}
|
||||
|
||||
///
|
||||
pure nothrow @safe @nogc unittest
|
||||
@nogc nothrow pure @safe unittest
|
||||
{
|
||||
assert(signBit(-1.0f));
|
||||
assert(!signBit(1.0f));
|
||||
@ -659,7 +661,7 @@ body
|
||||
}
|
||||
|
||||
///
|
||||
pure nothrow @safe @nogc unittest
|
||||
@nogc nothrow pure @safe unittest
|
||||
{
|
||||
assert(pow(3, 5, 7) == 5);
|
||||
assert(pow(2, 2, 1) == 0);
|
||||
@ -673,7 +675,7 @@ pure nothrow @safe @nogc unittest
|
||||
}
|
||||
|
||||
///
|
||||
nothrow @safe @nogc unittest
|
||||
@nogc nothrow pure @safe unittest
|
||||
{
|
||||
assert(pow(Integer(3), Integer(5), Integer(7)) == 5);
|
||||
assert(pow(Integer(2), Integer(2), Integer(1)) == 0);
|
||||
@ -695,13 +697,13 @@ nothrow @safe @nogc unittest
|
||||
* Returns: $(D_KEYWORD true) if $(D_PARAM x) is a prime number,
|
||||
* $(D_KEYWORD false) otherwise.
|
||||
*/
|
||||
bool isPseudoprime(ulong x) nothrow pure @safe @nogc
|
||||
bool isPseudoprime(ulong x) @nogc nothrow pure @safe
|
||||
{
|
||||
return pow(2, x - 1, x) == 1;
|
||||
}
|
||||
|
||||
///
|
||||
pure nothrow @safe @nogc unittest
|
||||
@nogc nothrow pure @safe unittest
|
||||
{
|
||||
assert(74623.isPseudoprime);
|
||||
assert(104729.isPseudoprime);
|
||||
@ -709,7 +711,7 @@ pure nothrow @safe @nogc unittest
|
||||
assert(!15485868.isPseudoprime);
|
||||
}
|
||||
|
||||
private pure nothrow @safe @nogc unittest
|
||||
@nogc nothrow pure @safe unittest
|
||||
{
|
||||
assert(74653.isPseudoprime);
|
||||
assert(74687.isPseudoprime);
|
||||
@ -738,3 +740,158 @@ private pure nothrow @safe @nogc unittest
|
||||
assert(899809363.isPseudoprime);
|
||||
assert(982451653.isPseudoprime);
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines minimum of two numbers.
|
||||
*
|
||||
* Params:
|
||||
* x = First number.
|
||||
* y = Second number.
|
||||
*
|
||||
* Returns: $(D_PARAM x) if $(D_PARAM x) is smaller than $(D_PSYMBOL y),
|
||||
* $(D_PARAM y) otherwise.
|
||||
*
|
||||
* See_Also: $(D_PSYMBOL max).
|
||||
*/
|
||||
T min(T)(T x, T y)
|
||||
if (isIntegral!T)
|
||||
{
|
||||
return x < y ? x : y;
|
||||
}
|
||||
|
||||
///
|
||||
@nogc nothrow pure @safe unittest
|
||||
{
|
||||
assert(min(5, 3) == 3);
|
||||
assert(min(4, 4) == 4);
|
||||
}
|
||||
|
||||
/// ditto
|
||||
T min(T)(T x, T y)
|
||||
if (isFloatingPoint!T)
|
||||
{
|
||||
if (isNaN(x))
|
||||
{
|
||||
return y;
|
||||
}
|
||||
if (isNaN(y))
|
||||
{
|
||||
return x;
|
||||
}
|
||||
return x < y ? x : y;
|
||||
}
|
||||
|
||||
///
|
||||
@nogc nothrow pure @safe unittest
|
||||
{
|
||||
assert(min(5.2, 3.0) == 3.0);
|
||||
|
||||
assert(min(5.2, double.nan) == 5.2);
|
||||
assert(min(double.nan, 3.0) == 3.0);
|
||||
assert(isNaN(min(double.nan, double.nan)));
|
||||
}
|
||||
|
||||
/// ditto
|
||||
ref T min(T)(ref T x, ref T y)
|
||||
if (is(Unqual!T == Integer))
|
||||
{
|
||||
return x < y ? x : y;
|
||||
}
|
||||
|
||||
/// ditto
|
||||
T min(T)(T x, T y)
|
||||
if (is(T == Integer))
|
||||
{
|
||||
return x < y ? move(x) : move(y);
|
||||
}
|
||||
|
||||
///
|
||||
@nogc nothrow pure @safe unittest
|
||||
{
|
||||
assert(min(Integer(5), Integer(3)) == 3);
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines maximum of two numbers.
|
||||
*
|
||||
* Params:
|
||||
* x = First number.
|
||||
* y = Second number.
|
||||
*
|
||||
* Returns: $(D_PARAM x) if $(D_PARAM x) is larger than $(D_PSYMBOL y),
|
||||
* $(D_PARAM y) otherwise.
|
||||
*
|
||||
* See_Also: $(D_PSYMBOL min).
|
||||
*/
|
||||
T max(T)(T x, T y)
|
||||
if (isIntegral!T)
|
||||
{
|
||||
return x > y ? x : y;
|
||||
}
|
||||
|
||||
///
|
||||
@nogc nothrow pure @safe unittest
|
||||
{
|
||||
assert(max(5, 3) == 5);
|
||||
assert(max(4, 4) == 4);
|
||||
}
|
||||
|
||||
/// ditto
|
||||
T max(T)(T x, T y)
|
||||
if (isFloatingPoint!T)
|
||||
{
|
||||
if (isNaN(x))
|
||||
{
|
||||
return y;
|
||||
}
|
||||
if (isNaN(y))
|
||||
{
|
||||
return x;
|
||||
}
|
||||
return x > y ? x : y;
|
||||
}
|
||||
|
||||
///
|
||||
@nogc nothrow pure @safe unittest
|
||||
{
|
||||
assert(max(5.2, 3.0) == 5.2);
|
||||
|
||||
assert(max(5.2, double.nan) == 5.2);
|
||||
assert(max(double.nan, 3.0) == 3.0);
|
||||
assert(isNaN(max(double.nan, double.nan)));
|
||||
}
|
||||
|
||||
/// ditto
|
||||
ref T max(T)(ref T x, ref T y)
|
||||
if (is(Unqual!T == Integer))
|
||||
{
|
||||
return x > y ? x : y;
|
||||
}
|
||||
|
||||
/// ditto
|
||||
T max(T)(T x, T y)
|
||||
if (is(T == Integer))
|
||||
{
|
||||
return x > y ? move(x) : move(y);
|
||||
}
|
||||
|
||||
///
|
||||
@nogc nothrow pure @safe unittest
|
||||
{
|
||||
assert(max(Integer(5), Integer(3)) == 5);
|
||||
}
|
||||
|
||||
// min/max accept const and mutable references.
|
||||
@nogc nothrow pure @safe unittest
|
||||
{
|
||||
{
|
||||
Integer i1 = 5, i2 = 3;
|
||||
assert(min(i1, i2) == 3);
|
||||
assert(max(i1, i2) == 5);
|
||||
}
|
||||
{
|
||||
const Integer i1 = 5, i2 = 3;
|
||||
assert(min(i1, i2) == 3);
|
||||
assert(max(i1, i2) == 5);
|
||||
}
|
||||
}
|
||||
|
@ -51,10 +51,12 @@ private enum alignMask = size_t.sizeof - 1;
|
||||
*
|
||||
* Precondition: $(D_INLINECODE source.length <= target.length).
|
||||
*/
|
||||
void copy(const void[] source, void[] target) pure nothrow @trusted @nogc
|
||||
void copy(const void[] source, void[] target) @nogc nothrow pure @trusted
|
||||
in
|
||||
{
|
||||
assert(source.length <= target.length);
|
||||
assert(source.length == 0 || source.ptr !is null);
|
||||
assert(target.length == 0 || target.ptr !is null);
|
||||
}
|
||||
body
|
||||
{
|
||||
@ -69,7 +71,7 @@ body
|
||||
}
|
||||
|
||||
///
|
||||
pure nothrow @safe @nogc unittest
|
||||
@nogc nothrow pure @safe unittest
|
||||
{
|
||||
ubyte[9] source = [1, 2, 3, 4, 5, 6, 7, 8, 9];
|
||||
ubyte[9] target;
|
||||
@ -77,7 +79,7 @@ pure nothrow @safe @nogc unittest
|
||||
assert(cmp(source, target) == 0);
|
||||
}
|
||||
|
||||
private pure nothrow @safe @nogc unittest
|
||||
@nogc nothrow pure @safe unittest
|
||||
{
|
||||
{
|
||||
ubyte[0] source, target;
|
||||
@ -120,6 +122,11 @@ private template filledBytes(ubyte Byte, ubyte I = 0)
|
||||
* memory = Memory block.
|
||||
*/
|
||||
void fill(ubyte c = 0)(void[] memory) @trusted
|
||||
in
|
||||
{
|
||||
assert(memory.length == 0 || memory.ptr !is null);
|
||||
}
|
||||
body
|
||||
{
|
||||
version (TanyaNative)
|
||||
{
|
||||
@ -132,7 +139,7 @@ void fill(ubyte c = 0)(void[] memory) @trusted
|
||||
}
|
||||
|
||||
///
|
||||
pure nothrow @safe @nogc unittest
|
||||
@nogc nothrow pure @safe unittest
|
||||
{
|
||||
ubyte[9] memory = [1, 2, 3, 4, 5, 6, 7, 8, 9];
|
||||
memory.fill!0();
|
||||
@ -144,7 +151,7 @@ pure nothrow @safe @nogc unittest
|
||||
|
||||
// Stress test. Checks that `fill` can handle unaligned pointers and different
|
||||
// lengths.
|
||||
pure nothrow @safe @nogc private unittest
|
||||
@nogc nothrow pure @safe unittest
|
||||
{
|
||||
ubyte[192] memory;
|
||||
|
||||
@ -189,10 +196,12 @@ pure nothrow @safe @nogc private unittest
|
||||
*
|
||||
* Precondition: $(D_INLINECODE source.length <= target.length).
|
||||
*/
|
||||
void copyBackward(const void[] source, void[] target) pure nothrow @trusted @nogc
|
||||
void copyBackward(const void[] source, void[] target) @nogc nothrow pure @trusted
|
||||
in
|
||||
{
|
||||
assert(source.length <= target.length);
|
||||
assert(source.length == 0 || source.ptr !is null);
|
||||
assert(target.length == 0 || target.ptr !is null);
|
||||
}
|
||||
body
|
||||
{
|
||||
@ -207,7 +216,7 @@ body
|
||||
}
|
||||
|
||||
///
|
||||
pure nothrow @safe @nogc unittest
|
||||
@nogc nothrow pure @safe unittest
|
||||
{
|
||||
ubyte[6] mem = [ 'a', 'a', 'b', 'b', 'c', 'c' ];
|
||||
ubyte[6] expected = [ 'a', 'a', 'a', 'a', 'b', 'b' ];
|
||||
@ -216,7 +225,7 @@ pure nothrow @safe @nogc unittest
|
||||
assert(cmp(expected, mem) == 0);
|
||||
}
|
||||
|
||||
private nothrow @safe @nogc unittest
|
||||
@nogc nothrow pure @safe unittest
|
||||
{
|
||||
ubyte[9] r1 = [ 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i' ];
|
||||
ubyte[9] r2;
|
||||
@ -242,7 +251,13 @@ private nothrow @safe @nogc unittest
|
||||
* negative integer if $(D_INLINECODE r2 > r1),
|
||||
* `0` if $(D_INLINECODE r1 == r2).
|
||||
*/
|
||||
int cmp(const void[] r1, const void[] r2) pure nothrow @trusted @nogc
|
||||
int cmp(const void[] r1, const void[] r2) @nogc nothrow pure @trusted
|
||||
in
|
||||
{
|
||||
assert(r1.length == 0 || r1.ptr !is null);
|
||||
assert(r2.length == 0 || r2.ptr !is null);
|
||||
}
|
||||
body
|
||||
{
|
||||
version (TanyaNative)
|
||||
{
|
||||
@ -259,7 +274,7 @@ int cmp(const void[] r1, const void[] r2) pure nothrow @trusted @nogc
|
||||
}
|
||||
|
||||
///
|
||||
pure nothrow @safe @nogc unittest
|
||||
@nogc nothrow pure @safe unittest
|
||||
{
|
||||
ubyte[4] r1 = [ 'a', 'b', 'c', 'd' ];
|
||||
ubyte[3] r2 = [ 'c', 'a', 'b' ];
|
||||
@ -271,7 +286,7 @@ pure nothrow @safe @nogc unittest
|
||||
assert(cmp(r2, r1) < 0);
|
||||
}
|
||||
|
||||
private pure nothrow @safe @nogc unittest
|
||||
@nogc nothrow pure @safe unittest
|
||||
{
|
||||
ubyte[16] r1 = [
|
||||
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
|
||||
@ -301,7 +316,12 @@ private pure nothrow @safe @nogc unittest
|
||||
* couldn't be found, an empty `inout void[]` is returned.
|
||||
*/
|
||||
inout(void[]) find(return inout void[] haystack, const ubyte needle)
|
||||
pure nothrow @trusted @nogc
|
||||
@nogc nothrow pure @trusted
|
||||
in
|
||||
{
|
||||
assert(haystack.length == 0 || haystack.ptr !is null);
|
||||
}
|
||||
body
|
||||
{
|
||||
auto length = haystack.length;
|
||||
const size_t needleWord = size_t.max * needle;
|
||||
@ -348,7 +368,7 @@ pure nothrow @trusted @nogc
|
||||
}
|
||||
|
||||
///
|
||||
pure nothrow @safe @nogc unittest
|
||||
@nogc nothrow pure @safe unittest
|
||||
{
|
||||
const ubyte[9] haystack = ['a', 'b', 'c', 'd', 'e', 'f', 'b', 'g', 'h'];
|
||||
|
||||
|
@ -24,7 +24,7 @@
|
||||
module tanya.memory.smartref;
|
||||
|
||||
import std.algorithm.comparison;
|
||||
import std.algorithm.mutation;
|
||||
import tanya.algorithm.mutation;
|
||||
import tanya.conv;
|
||||
import tanya.exception;
|
||||
import tanya.memory;
|
||||
@ -611,7 +611,7 @@ body
|
||||
|
||||
@nogc @system unittest
|
||||
{
|
||||
static bool destroyed = false;
|
||||
static bool destroyed;
|
||||
|
||||
static struct F
|
||||
{
|
||||
@ -812,7 +812,7 @@ struct Unique(T)
|
||||
///
|
||||
@nogc nothrow @system unittest
|
||||
{
|
||||
static bool destroyed = false;
|
||||
static bool destroyed;
|
||||
|
||||
static struct F
|
||||
{
|
||||
|
@ -1758,6 +1758,10 @@ template isMutable(T)
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines whether $(D_PARAM T) is a nested type, i.e. $(D_KEYWORD class),
|
||||
* $(D_KEYWORD struct) or $(D_KEYWORD union), which internally stores a context
|
||||
* pointer.
|
||||
*
|
||||
* Params:
|
||||
* T = $(D_KEYWORD class), $(D_KEYWORD struct) or $(D_KEYWORD union) type.
|
||||
*
|
||||
@ -1788,6 +1792,8 @@ pure nothrow @safe unittest
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines whether $(D_PARAM T) is a nested function.
|
||||
*
|
||||
* Params:
|
||||
* F = A function.
|
||||
*
|
||||
@ -1809,6 +1815,8 @@ enum bool isNestedFunction(alias F) = __traits(isNested, F);
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines the type of the callable $(D_PARAM F).
|
||||
*
|
||||
* Params:
|
||||
* F = A function.
|
||||
*
|
||||
@ -1898,6 +1906,8 @@ if (isCallable!F)
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines the return type of the callable $(D_PARAM F).
|
||||
*
|
||||
* Params:
|
||||
* F = A callable object.
|
||||
*
|
||||
@ -3055,3 +3065,60 @@ template isInnerClass(T)
|
||||
}
|
||||
static assert(!isInnerClass!(RefCountedStore!int));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the types of all members of $(D_PARAM T).
|
||||
*
|
||||
* If $(D_PARAM T) is a $(D_KEYWORD struct) or $(D_KEYWORD union) or
|
||||
* $(D_KEYWORD class), returns the types of all its fields. It is actually the
|
||||
* same as `T.tupleof`, but the content pointer for the nested type isn't
|
||||
* included.
|
||||
*
|
||||
* If $(D_PARAM T) is neither a $(D_KEYWORD struct) nor $(D_KEYWORD union) nor
|
||||
* $(D_KEYWORD class), $(D_PSYMBOL Fields) returns an $(D_PSYMBOL AliasSeq)
|
||||
* with the single element $(D_PARAM T).
|
||||
*
|
||||
* Params:
|
||||
* T = A type.
|
||||
*
|
||||
* Returns: $(D_PARAM T)'s fields.
|
||||
*/
|
||||
template Fields(T)
|
||||
{
|
||||
static if ((is(T == struct) || is(T == union)) && isNested!T)
|
||||
{
|
||||
// The last element of .tupleof of a nested struct or union is "this",
|
||||
// the context pointer, type "void*".
|
||||
alias Fields = typeof(T.tupleof[0 .. $ - 1]);
|
||||
}
|
||||
else static if (is(T == class) || is(T == struct) || is(T == union))
|
||||
{
|
||||
alias Fields = typeof(T.tupleof);
|
||||
}
|
||||
else
|
||||
{
|
||||
alias Fields = AliasSeq!T;
|
||||
}
|
||||
}
|
||||
|
||||
///
|
||||
@nogc nothrow pure @safe unittest
|
||||
{
|
||||
struct Nested
|
||||
{
|
||||
int i;
|
||||
|
||||
void func()
|
||||
{
|
||||
}
|
||||
}
|
||||
static assert(is(Fields!Nested == AliasSeq!int));
|
||||
|
||||
class C
|
||||
{
|
||||
uint u;
|
||||
}
|
||||
static assert(is(Fields!C == AliasSeq!uint));
|
||||
|
||||
static assert(is(Fields!short == AliasSeq!short));
|
||||
}
|
||||
|
@ -65,7 +65,7 @@ body
|
||||
}
|
||||
|
||||
///
|
||||
pure nothrow @safe @nogc unittest
|
||||
@nogc nothrow pure @safe unittest
|
||||
{
|
||||
string s = "Wenn die Wunde nicht mehr wehtut, schmerzt die Narbe";
|
||||
static assert(is(typeof(s.front) == immutable char));
|
||||
@ -105,7 +105,7 @@ body
|
||||
}
|
||||
|
||||
///
|
||||
pure nothrow @safe @nogc unittest
|
||||
@nogc nothrow pure @safe unittest
|
||||
{
|
||||
string s = "Brecht";
|
||||
static assert(is(typeof(s.back) == immutable char));
|
||||
@ -155,7 +155,7 @@ body
|
||||
}
|
||||
|
||||
///
|
||||
pure nothrow @safe @nogc unittest
|
||||
@nogc nothrow pure @safe unittest
|
||||
{
|
||||
wstring array = "Der finstere Ozean der Metaphysik. Nietzsche";
|
||||
|
||||
@ -184,7 +184,7 @@ pure nothrow @safe @nogc unittest
|
||||
}
|
||||
|
||||
///
|
||||
pure nothrow @safe @nogc unittest
|
||||
@nogc nothrow pure @safe unittest
|
||||
{
|
||||
int[1] array;
|
||||
assert(!array.empty);
|
||||
@ -209,7 +209,7 @@ pure nothrow @safe @nogc unittest
|
||||
}
|
||||
|
||||
///
|
||||
pure nothrow @safe @nogc unittest
|
||||
@nogc nothrow pure @safe unittest
|
||||
{
|
||||
ubyte[8] array;
|
||||
auto slice = array.save;
|
||||
|
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user