Compare commits
60 Commits
Author | SHA1 | Date | |
---|---|---|---|
3567a6608e | |||
520bd399a3 | |||
d38e33593e | |||
34b79ad46e | |||
515bf619e8 | |||
617eaab9a2 | |||
d946b598fd | |||
e9d7e9eb73 | |||
4dbfbe9874 | |||
25d59ffdda | |||
2c064eb05b | |||
c9a4a2f651 | |||
0e99effaeb | |||
0f1e53b4b9 | |||
666d59c231 | |||
ce90b4865b | |||
beb5d6963b | |||
a188f8b6e2 | |||
9355c54163 | |||
e8dd6e3217 | |||
94a7fdbb91 | |||
afd3c42c5f | |||
1d91bb4df9 | |||
a5026e48d8 | |||
64f2295d1a | |||
dea0eb9a37 | |||
7c2abadb90 | |||
e6b28468ca | |||
2934bb16d7 | |||
ed92e3993e | |||
1a4d1238a1 | |||
04864559e2 | |||
40e43c1465 | |||
5d145f524c | |||
51ade45108 | |||
3afb40e09e | |||
a9cc135318 | |||
1389b03842 | |||
a37c9b162e | |||
cde492c279 | |||
922c8bf7a3 | |||
a0a28c76f7 | |||
a1f4d2bc1c | |||
e5fb95ceb0 | |||
9ef5986288 | |||
42146c5e8a | |||
e6b91f70cb | |||
657f4a60d5 | |||
839c740cb1 | |||
2bd612fd19 | |||
fc53779d3f | |||
7bdc778390 | |||
97358ebc6c | |||
4834b36271 | |||
53df12897b | |||
4ac890d7d3 | |||
b79657f0d2 | |||
9429e7bb14 | |||
4fd37e84f8 | |||
e46e45ad5a |
2
.gitignore
vendored
2
.gitignore
vendored
@ -6,7 +6,7 @@
|
|||||||
.dub
|
.dub
|
||||||
__test__*__
|
__test__*__
|
||||||
__test__*__.core
|
__test__*__.core
|
||||||
/tanya-test-library
|
/tanya-test-*
|
||||||
|
|
||||||
/docs/
|
/docs/
|
||||||
/docs.json
|
/docs.json
|
||||||
|
@ -7,10 +7,10 @@ os:
|
|||||||
language: d
|
language: d
|
||||||
|
|
||||||
d:
|
d:
|
||||||
|
- dmd-2.076.0
|
||||||
|
- dmd-2.075.1
|
||||||
- dmd-2.074.1
|
- dmd-2.074.1
|
||||||
- dmd-2.073.2
|
- dmd-2.073.2
|
||||||
- dmd-2.072.2
|
|
||||||
- dmd-2.071.2
|
|
||||||
|
|
||||||
env:
|
env:
|
||||||
matrix:
|
matrix:
|
||||||
@ -23,7 +23,7 @@ addons:
|
|||||||
- gcc-multilib
|
- gcc-multilib
|
||||||
|
|
||||||
before_script:
|
before_script:
|
||||||
- if [ "$PS1" = '(dmd-2.074.1)' ]; then
|
- if [ "$PS1" = '(dmd-2.076.0)' ]; then
|
||||||
export UNITTEST="unittest-cov";
|
export UNITTEST="unittest-cov";
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
@ -15,18 +15,21 @@ can work on.
|
|||||||
writing new ones, or providing usage examples.
|
writing new ones, or providing usage examples.
|
||||||
* **Testing**: Test coverage is important for a library. Writing tests is not only helpful, but is also a great way
|
* **Testing**: Test coverage is important for a library. Writing tests is not only helpful, but is also a great way
|
||||||
to get a feel for how tanya works.
|
to get a feel for how tanya works.
|
||||||
|
* **Adding new features**: Tanya is a growing library. If you think some feature is missing, you can suggest
|
||||||
|
and implement this.
|
||||||
|
|
||||||
|
|
||||||
## Opening an issue
|
## Opening an issue
|
||||||
|
|
||||||
If you have found a bug, an error, have some question, or suggestion, open in issue. Tanya uses an external
|
If you have found a bug, an error, have some question, or suggestion, open in issue. I'll try to answer as soon
|
||||||
|
as I can. Tanya uses an external
|
||||||
[bug tracker](https://issues.caraus.io/projects/tanya/issues). You should
|
[bug tracker](https://issues.caraus.io/projects/tanya/issues). You should
|
||||||
[register](https://issues.caraus.io/account/register) before you can report your issue. There is also a list
|
[register](https://issues.caraus.io/account/register) before you can report your issue. There is also a list
|
||||||
of open issues that mirror the current development process and progress. If you're looking for a challenge, just
|
of open issues that mirror the current development process and progress. If you're looking for a challenge, just
|
||||||
pick an issue you are interested in and start working on it. Fill free to comment on the issue to get more
|
pick an issue you are interested in and start working on it. Fill free to comment on the issue to get more
|
||||||
information.
|
information.
|
||||||
|
|
||||||
Some issues have a category assigned to it. Such issues belongs mostly to a larger part of the library that is
|
Some issues have a category assigned to them. Such issues belong mostly to a larger part of the library that is
|
||||||
currently in development. The category specifies then the git branch development happens on. The remaining issues
|
currently in development. The category specifies then the git branch development happens on. The remaining issues
|
||||||
can be fixed directly in master.
|
can be fixed directly in master.
|
||||||
|
|
||||||
@ -35,9 +38,11 @@ to be fixed till a specific release. Version numbers refer to the versions in th
|
|||||||
[git repository](https://github.com/caraus-ecms/tanya/releases).
|
[git repository](https://github.com/caraus-ecms/tanya/releases).
|
||||||
|
|
||||||
|
|
||||||
## Creating a pull request
|
## Contribution process
|
||||||
|
|
||||||
I accept GitHub pull requests. Creating a pull request is like sending a patch with a suggested change.
|
### Creating a pull request
|
||||||
|
|
||||||
|
I accept GitHub pull requests. Creating a pull request is like sending a patch with the suggested change.
|
||||||
First you have to [fork](https://guides.github.com/activities/forking/) the repository. Clone your fork locally
|
First you have to [fork](https://guides.github.com/activities/forking/) the repository. Clone your fork locally
|
||||||
with `git clone` and create a new branch where you want to work, for example:
|
with `git clone` and create a new branch where you want to work, for example:
|
||||||
|
|
||||||
@ -47,16 +52,39 @@ git checkout -b bugfix-x
|
|||||||
Commit your changes to your fork:
|
Commit your changes to your fork:
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
git commit -m "Fix Bug X"
|
git commit -m "Fix X"
|
||||||
git push -u origin bugfix-x
|
git push -u origin bugfix-x
|
||||||
```
|
```
|
||||||
After that if you visit your fork on GitHub, GitHub will suggest to create pull request. Just follow the steps
|
|
||||||
described on GitHub to finish the process.
|
|
||||||
|
|
||||||
Please ensure that you fork is even with the upstream (original) repository. If not, you have to rebase your branch
|
After that if you visit your fork on GitHub, GitHub will suggest to create pull request. Just follow the steps
|
||||||
on upstream master before submitting a pull request. See https://help.github.com/articles/syncing-a-fork/ for a
|
described on GitHub to finish the process. See
|
||||||
|
[Using Pull Requests](https://help.github.com/articles/about-pull-requests/) for more information.
|
||||||
|
|
||||||
|
Please ensure that your fork is even with the upstream (original) repository. If not, you have to rebase your branch
|
||||||
|
on upstream/master before submitting a pull request. See https://help.github.com/articles/syncing-a-fork/ for a
|
||||||
step-by-step guide.
|
step-by-step guide.
|
||||||
|
|
||||||
|
### Fixing a bug
|
||||||
|
|
||||||
|
Add an unittest that demonstrates the bug along with a short description:
|
||||||
|
|
||||||
|
```d
|
||||||
|
// Issue ###: https://issues.caraus.io/issues/###.
|
||||||
|
private unittest
|
||||||
|
{
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Adding new features
|
||||||
|
|
||||||
|
* Use Ddoc to document the feature.
|
||||||
|
* Add some unittests that prevent new bugs and demonstrate how the feature is supposed to work.
|
||||||
|
|
||||||
|
### Style guide
|
||||||
|
|
||||||
|
Make sure your changes follow [The D Style](https://dlang.org/dstyle.html) (including
|
||||||
|
[Additional Requirements for Phobos](https://dlang.org/dstyle.html#phobos).
|
||||||
|
|
||||||
|
|
||||||
## Questions and suggestions
|
## Questions and suggestions
|
||||||
|
|
||||||
|
31
README.md
31
README.md
@ -16,12 +16,13 @@ Tanya extends Phobos functionality and provides alternative implementations for
|
|||||||
data structures and utilities that depend on the Garbage Collector in Phobos.
|
data structures and utilities that depend on the Garbage Collector in Phobos.
|
||||||
|
|
||||||
* [Bug tracker](https://issues.caraus.io/projects/tanya/issues)
|
* [Bug tracker](https://issues.caraus.io/projects/tanya/issues)
|
||||||
* [Documentation](https://docs.caraus.io/tanya)
|
* [API Documentation](https://docs.caraus.io/tanya)
|
||||||
* [Contribution guidelines](CONTRIBUTING.md)
|
* [Contribution guidelines](CONTRIBUTING.md)
|
||||||
|
|
||||||
|
|
||||||
## Overview
|
## Overview
|
||||||
|
|
||||||
Tanya consists of the following packages:
|
Tanya consists of the following packages and (top-level) modules:
|
||||||
|
|
||||||
* `async`: Event loop (epoll, kqueue and IOCP).
|
* `async`: Event loop (epoll, kqueue and IOCP).
|
||||||
* `container`: Queue, Array, Singly and doubly linked lists, Buffers, UTF-8
|
* `container`: Queue, Array, Singly and doubly linked lists, Buffers, UTF-8
|
||||||
@ -29,8 +30,19 @@ string, Hash set.
|
|||||||
* `format`: Formatting and conversion functions.
|
* `format`: Formatting and conversion functions.
|
||||||
* `math`: Arbitrary precision integer and a set of functions.
|
* `math`: Arbitrary precision integer and a set of functions.
|
||||||
* `memory`: Tools for manual memory management (allocators, smart pointers).
|
* `memory`: Tools for manual memory management (allocators, smart pointers).
|
||||||
* `network`: URL-Parsing, sockets, utilities.
|
* `meta`: Template metaprogramming. This package contains utilities to acquire
|
||||||
|
type information at compile-time, to transform from one type to another. It has
|
||||||
|
also different algorithms for iterating, searching and modifying template
|
||||||
|
arguments.
|
||||||
|
* `net`: URL-Parsing, network programming.
|
||||||
|
* `network`: Socket implementation. `network` is currently under rework.
|
||||||
|
After finishing the new socket implementation will land in the `net` package and
|
||||||
|
`network` will be deprecated.
|
||||||
* `os`: Platform-independent interfaces to operating system functionality.
|
* `os`: Platform-independent interfaces to operating system functionality.
|
||||||
|
* `range`: Generic functions and templates for D ranges.
|
||||||
|
* `typecons`: Templates that allow to build new types based on the available
|
||||||
|
ones.
|
||||||
|
|
||||||
|
|
||||||
## Basic usage
|
## Basic usage
|
||||||
|
|
||||||
@ -120,21 +132,22 @@ foreach (el; arr[0 .. 5])
|
|||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
int i = arr[7]; // Access 7th element.
|
int i = arr[7]; // Access 8th element.
|
||||||
```
|
```
|
||||||
|
|
||||||
There are more containers in the `tanya.container` package.
|
There are more containers in the `tanya.container` package.
|
||||||
|
|
||||||
|
|
||||||
## Development
|
## Development
|
||||||
|
|
||||||
### Supported compilers
|
### Supported compilers
|
||||||
|
|
||||||
| dmd |
|
| dmd |
|
||||||
|:-------:|
|
|:-------:|
|
||||||
|
| 2.076.0 |
|
||||||
|
| 2.075.1 |
|
||||||
| 2.074.1 |
|
| 2.074.1 |
|
||||||
| 2.073.2 |
|
| 2.073.2 |
|
||||||
| 2.072.2 |
|
|
||||||
| 2.071.2 |
|
|
||||||
|
|
||||||
### Current status
|
### Current status
|
||||||
|
|
||||||
@ -160,3 +173,9 @@ Deprecated features are removed after one release (in approximately 6 weeks afte
|
|||||||
is being tested on Windows and FreeBSD as well.
|
is being tested on Windows and FreeBSD as well.
|
||||||
|
|
||||||
* The library isn't thread-safe yet.
|
* The library isn't thread-safe yet.
|
||||||
|
|
||||||
|
|
||||||
|
## Feedback
|
||||||
|
|
||||||
|
Any feedback about your experience with tanya would be greatly appreciated. Feel free to
|
||||||
|
[contact me](mailto:info@caraus.de).
|
||||||
|
30
appveyor.yml
30
appveyor.yml
@ -3,6 +3,18 @@ os: Visual Studio 2015
|
|||||||
|
|
||||||
environment:
|
environment:
|
||||||
matrix:
|
matrix:
|
||||||
|
- DC: dmd
|
||||||
|
DVersion: 2.076.0
|
||||||
|
arch: x64
|
||||||
|
- DC: dmd
|
||||||
|
DVersion: 2.076.0
|
||||||
|
arch: x86
|
||||||
|
- DC: dmd
|
||||||
|
DVersion: 2.075.1
|
||||||
|
arch: x64
|
||||||
|
- DC: dmd
|
||||||
|
DVersion: 2.075.1
|
||||||
|
arch: x86
|
||||||
- DC: dmd
|
- DC: dmd
|
||||||
DVersion: 2.074.1
|
DVersion: 2.074.1
|
||||||
arch: x64
|
arch: x64
|
||||||
@ -15,18 +27,6 @@ environment:
|
|||||||
- DC: dmd
|
- DC: dmd
|
||||||
DVersion: 2.073.2
|
DVersion: 2.073.2
|
||||||
arch: x86
|
arch: x86
|
||||||
- DC: dmd
|
|
||||||
DVersion: 2.072.2
|
|
||||||
arch: x64
|
|
||||||
- DC: dmd
|
|
||||||
DVersion: 2.072.2
|
|
||||||
arch: x86
|
|
||||||
- DC: dmd
|
|
||||||
DVersion: 2.071.2
|
|
||||||
arch: x64
|
|
||||||
- DC: dmd
|
|
||||||
DVersion: 2.071.2
|
|
||||||
arch: x86
|
|
||||||
|
|
||||||
skip_tags: true
|
skip_tags: true
|
||||||
|
|
||||||
@ -43,12 +43,6 @@ install:
|
|||||||
}
|
}
|
||||||
- ps: SetUpDCompiler
|
- ps: SetUpDCompiler
|
||||||
|
|
||||||
- ps: if($env:DVersion -eq "2.071.2"){
|
|
||||||
Invoke-WebRequest "http://code.dlang.org/files/dub-1.2.1-windows-x86.zip" -OutFile "dub.zip";
|
|
||||||
7z x dub.zip -odub > $null;
|
|
||||||
Move-Item "dub/dub.exe" "C:\dmd2\windows\bin"
|
|
||||||
}
|
|
||||||
|
|
||||||
before_build:
|
before_build:
|
||||||
- ps: if($env:arch -eq "x86"){
|
- ps: if($env:arch -eq "x86"){
|
||||||
$env:compilersetupargs = "x86";
|
$env:compilersetupargs = "x86";
|
||||||
|
@ -1,4 +1,3 @@
|
|||||||
ignore:
|
ignore:
|
||||||
- "source/tanya/async/event/iocp.d"
|
- "source/tanya/async/event/iocp.d"
|
||||||
- "source/tanya/async/iocp.d"
|
- "source/tanya/async/iocp.d"
|
||||||
- "source/tanya/memory/types.d"
|
|
||||||
|
81
dscanner.ini
Normal file
81
dscanner.ini
Normal file
@ -0,0 +1,81 @@
|
|||||||
|
; Configure which static analysis checks are skip-unittest
|
||||||
|
[analysis.config.StaticAnalysisConfig]
|
||||||
|
; Check variable, class, struct, interface, union, and function names against t
|
||||||
|
; he Phobos style guide
|
||||||
|
style_check="disabled"
|
||||||
|
; Check for array literals that cause unnecessary allocation
|
||||||
|
enum_array_literal_check="skip-unittest"
|
||||||
|
; Check for poor exception handling practices
|
||||||
|
exception_check="skip-unittest"
|
||||||
|
; Check for use of the deprecated 'delete' keyword
|
||||||
|
delete_check="skip-unittest"
|
||||||
|
; Check for use of the deprecated floating point operators
|
||||||
|
float_operator_check="skip-unittest"
|
||||||
|
; Check number literals for readability
|
||||||
|
number_style_check="disabled"
|
||||||
|
; Checks that opEquals, opCmp, toHash, and toString are either const, immutable
|
||||||
|
; , or inout.
|
||||||
|
object_const_check="disabled"
|
||||||
|
; Checks for .. expressions where the left side is larger than the right.
|
||||||
|
backwards_range_check="skip-unittest"
|
||||||
|
; Checks for if statements whose 'then' block is the same as the 'else' block
|
||||||
|
if_else_same_check="skip-unittest"
|
||||||
|
; Checks for some problems with constructors
|
||||||
|
constructor_check="skip-unittest"
|
||||||
|
; Checks for unused variables and function parameters
|
||||||
|
unused_variable_check="disabled"
|
||||||
|
; Checks for unused labels
|
||||||
|
unused_label_check="skip-unittest"
|
||||||
|
; Checks for duplicate attributes
|
||||||
|
duplicate_attribute="skip-unittest"
|
||||||
|
; Checks that opEquals and toHash are both defined or neither are defined
|
||||||
|
opequals_tohash_check="disabled"
|
||||||
|
; Checks for subtraction from .length properties
|
||||||
|
length_subtraction_check="disabled"
|
||||||
|
; Checks for methods or properties whose names conflict with built-in propertie
|
||||||
|
; s
|
||||||
|
builtin_property_names_check="skip-unittest"
|
||||||
|
; Checks for confusing code in inline asm statements
|
||||||
|
asm_style_check="skip-unittest"
|
||||||
|
; Checks for confusing logical operator precedence
|
||||||
|
logical_precedence_check="skip-unittest"
|
||||||
|
; Checks for undocumented public declarations
|
||||||
|
undocumented_declaration_check="disabled"
|
||||||
|
; Checks for poor placement of function attributes
|
||||||
|
function_attribute_check="skip-unittest"
|
||||||
|
; Checks for use of the comma operator
|
||||||
|
comma_expression_check="skip-unittest"
|
||||||
|
; Checks for local imports that are too broad
|
||||||
|
local_import_check="disabled"
|
||||||
|
; Checks for variables that could be declared immutable
|
||||||
|
could_be_immutable_check="disabled"
|
||||||
|
; Checks for redundant expressions in if statements
|
||||||
|
redundant_if_check="skip-unittest"
|
||||||
|
; Checks for redundant parenthesis
|
||||||
|
redundant_parens_check="skip-unittest"
|
||||||
|
; Checks for mismatched argument and parameter names
|
||||||
|
mismatched_args_check="skip-unittest"
|
||||||
|
; Checks for labels with the same name as variables
|
||||||
|
label_var_same_name_check="disabled"
|
||||||
|
; Checks for lines longer than 120 characters
|
||||||
|
long_line_check="skip-unittest"
|
||||||
|
; Checks for assignment to auto-ref function parameters
|
||||||
|
auto_ref_assignment_check="disabled"
|
||||||
|
; Checks for incorrect infinite range definitions
|
||||||
|
incorrect_infinite_range_check="skip-unittest"
|
||||||
|
; Checks for asserts that are always true
|
||||||
|
useless_assert_check="skip-unittest"
|
||||||
|
; Check for uses of the old-style alias syntax
|
||||||
|
alias_syntax_check="disabled"
|
||||||
|
; Checks for else if that should be else static if
|
||||||
|
static_if_else_check="skip-unittest"
|
||||||
|
; Check for unclear lambda syntax
|
||||||
|
lambda_return_check="skip-unittest"
|
||||||
|
; Check for auto function without return statement
|
||||||
|
auto_function_check="skip-unittest"
|
||||||
|
; Check for sortedness of imports
|
||||||
|
imports_sortedness="disabled"
|
||||||
|
; Check for explicitly annotated unittests
|
||||||
|
explicitly_annotated_unittests="disabled"
|
||||||
|
; Check for useless usage of the final attribute
|
||||||
|
final_attribute_check="skip-unittest"
|
11
dub.json
11
dub.json
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "tanya",
|
"name": "tanya",
|
||||||
"description": "General purpose, @nogc library. Containers, networking, memory management, utilities",
|
"description": "General purpose, @nogc library. Containers, networking, metaprogramming, memory management, utilities",
|
||||||
"license": "MPL-2.0",
|
"license": "MPL-2.0",
|
||||||
"copyright": "(c) Eugene Wissner <info@caraus.de>",
|
"copyright": "(c) Eugene Wissner <info@caraus.de>",
|
||||||
"authors": [
|
"authors": [
|
||||||
@ -11,7 +11,14 @@
|
|||||||
|
|
||||||
"configurations": [
|
"configurations": [
|
||||||
{
|
{
|
||||||
"name": "library"
|
"name": "library",
|
||||||
|
"targetType": "library",
|
||||||
|
"versions": ["TanyaPhobos"]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "native",
|
||||||
|
"targetType": "library",
|
||||||
|
"platforms": ["linux-x86_64"]
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
@ -2,13 +2,15 @@
|
|||||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
* 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/. */
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||||
|
|
||||||
/*
|
/**
|
||||||
* Event loop implementation for Linux.
|
* Event loop implementation for Linux.
|
||||||
*
|
*
|
||||||
* Copyright: Eugene Wissner 2016-2017.
|
* Copyright: Eugene Wissner 2016-2017.
|
||||||
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
||||||
* Mozilla Public License, v. 2.0).
|
* Mozilla Public License, v. 2.0).
|
||||||
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
||||||
|
* Source: $(LINK2 https://github.com/caraus-ecms/tanya/blob/master/source/tanya/async/event/epoll.d,
|
||||||
|
* tanya/async/event/epoll.d)
|
||||||
*/
|
*/
|
||||||
module tanya.async.event.epoll;
|
module tanya.async.event.epoll;
|
||||||
|
|
||||||
|
@ -2,13 +2,15 @@
|
|||||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
* 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/. */
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||||
|
|
||||||
/*
|
/**
|
||||||
* Event loop implementation for Windows.
|
* Event loop implementation for Windows.
|
||||||
*
|
*
|
||||||
* Copyright: Eugene Wissner 2016-2017.
|
* Copyright: Eugene Wissner 2016-2017.
|
||||||
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
||||||
* Mozilla Public License, v. 2.0).
|
* Mozilla Public License, v. 2.0).
|
||||||
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
||||||
|
* Source: $(LINK2 https://github.com/caraus-ecms/tanya/blob/master/source/tanya/async/event/iocp.d,
|
||||||
|
* tanya/async/event/iocp.d)
|
||||||
*/
|
*/
|
||||||
module tanya.async.event.iocp;
|
module tanya.async.event.iocp;
|
||||||
|
|
||||||
@ -231,8 +233,8 @@ final class IOCPLoop : Loop
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!(oldEvents & Event.read) && (events & Event.read)
|
if ((!(oldEvents & Event.read) && (events & Event.read))
|
||||||
|| !(oldEvents & Event.write) && (events & Event.write))
|
|| (!(oldEvents & Event.write) && (events & Event.write)))
|
||||||
{
|
{
|
||||||
auto transport = cast(StreamTransport) watcher;
|
auto transport = cast(StreamTransport) watcher;
|
||||||
assert(transport !is null);
|
assert(transport !is null);
|
||||||
|
@ -9,6 +9,8 @@
|
|||||||
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
||||||
* Mozilla Public License, v. 2.0).
|
* Mozilla Public License, v. 2.0).
|
||||||
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
||||||
|
* Source: $(LINK2 https://github.com/caraus-ecms/tanya/blob/master/source/tanya/async/event/kqueue.d,
|
||||||
|
* tanya/async/event/kqueue.d)
|
||||||
*/
|
*/
|
||||||
module tanya.async.event.kqueue;
|
module tanya.async.event.kqueue;
|
||||||
|
|
||||||
|
@ -9,6 +9,8 @@
|
|||||||
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
||||||
* Mozilla Public License, v. 2.0).
|
* Mozilla Public License, v. 2.0).
|
||||||
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
||||||
|
* Source: $(LINK2 https://github.com/caraus-ecms/tanya/blob/master/source/tanya/async/event/selector.d,
|
||||||
|
* tanya/async/event/selector.d)
|
||||||
*/
|
*/
|
||||||
module tanya.async.event.selector;
|
module tanya.async.event.selector;
|
||||||
|
|
||||||
|
@ -11,6 +11,8 @@
|
|||||||
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
||||||
* Mozilla Public License, v. 2.0).
|
* Mozilla Public License, v. 2.0).
|
||||||
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
||||||
|
* Source: $(LINK2 https://github.com/caraus-ecms/tanya/blob/master/source/tanya/async/iocp.d,
|
||||||
|
* tanya/async/iocp.d)
|
||||||
*/
|
*/
|
||||||
module tanya.async.iocp;
|
module tanya.async.iocp;
|
||||||
|
|
||||||
|
@ -64,6 +64,8 @@
|
|||||||
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
||||||
* Mozilla Public License, v. 2.0).
|
* Mozilla Public License, v. 2.0).
|
||||||
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
||||||
|
* Source: $(LINK2 https://github.com/caraus-ecms/tanya/blob/master/source/tanya/async/loop.d,
|
||||||
|
* tanya/async/loop.d)
|
||||||
*/
|
*/
|
||||||
module tanya.async.loop;
|
module tanya.async.loop;
|
||||||
|
|
||||||
|
@ -9,6 +9,8 @@
|
|||||||
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
||||||
* Mozilla Public License, v. 2.0).
|
* Mozilla Public License, v. 2.0).
|
||||||
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
||||||
|
* Source: $(LINK2 https://github.com/caraus-ecms/tanya/blob/master/source/tanya/async/package.d,
|
||||||
|
* tanya/async/package.d)
|
||||||
*/
|
*/
|
||||||
module tanya.async;
|
module tanya.async;
|
||||||
|
|
||||||
|
@ -13,6 +13,8 @@
|
|||||||
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
||||||
* Mozilla Public License, v. 2.0).
|
* Mozilla Public License, v. 2.0).
|
||||||
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
||||||
|
* Source: $(LINK2 https://github.com/caraus-ecms/tanya/blob/master/source/tanya/async/protocol.d,
|
||||||
|
* tanya/async/protocol.d)
|
||||||
*/
|
*/
|
||||||
module tanya.async.protocol;
|
module tanya.async.protocol;
|
||||||
|
|
||||||
|
@ -10,6 +10,8 @@
|
|||||||
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
||||||
* Mozilla Public License, v. 2.0).
|
* Mozilla Public License, v. 2.0).
|
||||||
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
||||||
|
* Source: $(LINK2 https://github.com/caraus-ecms/tanya/blob/master/source/tanya/async/transport.d,
|
||||||
|
* tanya/async/transport.d)
|
||||||
*/
|
*/
|
||||||
module tanya.async.transport;
|
module tanya.async.transport;
|
||||||
|
|
||||||
|
@ -9,6 +9,8 @@
|
|||||||
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
||||||
* Mozilla Public License, v. 2.0).
|
* Mozilla Public License, v. 2.0).
|
||||||
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
||||||
|
* Source: $(LINK2 https://github.com/caraus-ecms/tanya/blob/master/source/tanya/async/watcher.d,
|
||||||
|
* tanya/async/watcher.d)
|
||||||
*/
|
*/
|
||||||
module tanya.async.watcher;
|
module tanya.async.watcher;
|
||||||
|
|
||||||
|
@ -9,6 +9,8 @@
|
|||||||
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
||||||
* Mozilla Public License, v. 2.0).
|
* Mozilla Public License, v. 2.0).
|
||||||
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
||||||
|
* Source: $(LINK2 https://github.com/caraus-ecms/tanya/blob/master/source/tanya/container/array.d,
|
||||||
|
* tanya/container/array.d)
|
||||||
*/
|
*/
|
||||||
module tanya.container.array;
|
module tanya.container.array;
|
||||||
|
|
||||||
@ -19,20 +21,21 @@ import std.algorithm.mutation;
|
|||||||
import std.conv;
|
import std.conv;
|
||||||
import std.range.primitives;
|
import std.range.primitives;
|
||||||
import std.meta;
|
import std.meta;
|
||||||
import std.traits;
|
|
||||||
import tanya.memory;
|
import tanya.memory;
|
||||||
|
import tanya.meta.trait;
|
||||||
|
import tanya.meta.transform;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Random-access range for the $(D_PSYMBOL Array).
|
* Random-access range for the $(D_PSYMBOL Array).
|
||||||
*
|
*
|
||||||
* Params:
|
* Params:
|
||||||
* E = Element type.
|
* A = Array type.
|
||||||
*/
|
*/
|
||||||
struct Range(E)
|
struct Range(A)
|
||||||
{
|
{
|
||||||
|
private alias E = PointerTarget!(typeof(A.data));
|
||||||
private E* begin, end;
|
private E* begin, end;
|
||||||
private alias ContainerType = CopyConstness!(E, Array!(Unqual!E));
|
private A* container;
|
||||||
private ContainerType* container;
|
|
||||||
|
|
||||||
invariant
|
invariant
|
||||||
{
|
{
|
||||||
@ -42,7 +45,7 @@ struct Range(E)
|
|||||||
assert(this.end <= this.container.data + this.container.length);
|
assert(this.end <= this.container.data + this.container.length);
|
||||||
}
|
}
|
||||||
|
|
||||||
private this(ref ContainerType container, E* begin, E* end) @trusted
|
private this(ref A container, E* begin, E* end) @trusted
|
||||||
in
|
in
|
||||||
{
|
{
|
||||||
assert(begin <= end);
|
assert(begin <= end);
|
||||||
@ -130,7 +133,7 @@ struct Range(E)
|
|||||||
return typeof(return)(*this.container, this.begin, this.end);
|
return typeof(return)(*this.container, this.begin, this.end);
|
||||||
}
|
}
|
||||||
|
|
||||||
Range!(const E) opIndex() const
|
A.ConstRange opIndex() const
|
||||||
{
|
{
|
||||||
return typeof(return)(*this.container, this.begin, this.end);
|
return typeof(return)(*this.container, this.begin, this.end);
|
||||||
}
|
}
|
||||||
@ -146,7 +149,7 @@ struct Range(E)
|
|||||||
return typeof(return)(*this.container, this.begin + i, this.begin + j);
|
return typeof(return)(*this.container, this.begin + i, this.begin + j);
|
||||||
}
|
}
|
||||||
|
|
||||||
Range!(const E) opSlice(const size_t i, const size_t j) const @trusted
|
A.ConstRange opSlice(const size_t i, const size_t j) const @trusted
|
||||||
in
|
in
|
||||||
{
|
{
|
||||||
assert(i <= j);
|
assert(i <= j);
|
||||||
@ -172,10 +175,10 @@ struct Range(E)
|
|||||||
struct Array(T)
|
struct Array(T)
|
||||||
{
|
{
|
||||||
/// The range types for $(D_PSYMBOL Array).
|
/// The range types for $(D_PSYMBOL Array).
|
||||||
alias Range = .Range!T;
|
alias Range = .Range!Array;
|
||||||
|
|
||||||
/// Ditto.
|
/// Ditto.
|
||||||
alias ConstRange = .Range!(const T);
|
alias ConstRange = .Range!(const Array);
|
||||||
|
|
||||||
private size_t length_;
|
private size_t length_;
|
||||||
private T* data;
|
private T* data;
|
||||||
@ -651,9 +654,9 @@ struct Array(T)
|
|||||||
body
|
body
|
||||||
{
|
{
|
||||||
auto end = this.data + this.length;
|
auto end = this.data + this.length;
|
||||||
moveAll(.Range!T(this, r.end, end), .Range!T(this, r.begin, end));
|
moveAll(Range(this, r.end, end), Range(this, r.begin, end));
|
||||||
length = length - r.length;
|
length = length - r.length;
|
||||||
return .Range!T(this, r.begin, this.data + length);
|
return Range(this, r.begin, this.data + length);
|
||||||
}
|
}
|
||||||
|
|
||||||
///
|
///
|
||||||
@ -863,7 +866,7 @@ struct Array(T)
|
|||||||
}
|
}
|
||||||
body
|
body
|
||||||
{
|
{
|
||||||
return insertAfter(.Range!T(this, this.data, r.begin), el);
|
return insertAfter(Range(this, this.data, r.begin), el);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Ditto.
|
/// Ditto.
|
||||||
@ -1617,6 +1620,9 @@ private unittest
|
|||||||
}
|
}
|
||||||
A a1, a2;
|
A a1, a2;
|
||||||
auto v1 = Array!A([a1, a2]);
|
auto v1 = Array!A([a1, a2]);
|
||||||
|
|
||||||
|
// Issue 232: https://issues.caraus.io/issues/232.
|
||||||
|
static assert(is(Array!(A*)));
|
||||||
}
|
}
|
||||||
|
|
||||||
private @safe @nogc unittest
|
private @safe @nogc unittest
|
||||||
|
@ -9,11 +9,13 @@
|
|||||||
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
||||||
* Mozilla Public License, v. 2.0).
|
* Mozilla Public License, v. 2.0).
|
||||||
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
||||||
|
* Source: $(LINK2 https://github.com/caraus-ecms/tanya/blob/master/source/tanya/container/buffer.d,
|
||||||
|
* tanya/container/buffer.d)
|
||||||
*/
|
*/
|
||||||
module tanya.container.buffer;
|
module tanya.container.buffer;
|
||||||
|
|
||||||
import std.traits;
|
|
||||||
import tanya.memory;
|
import tanya.memory;
|
||||||
|
import tanya.meta.trait;
|
||||||
|
|
||||||
version (unittest)
|
version (unittest)
|
||||||
{
|
{
|
||||||
|
@ -9,10 +9,12 @@
|
|||||||
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
||||||
* Mozilla Public License, v. 2.0).
|
* Mozilla Public License, v. 2.0).
|
||||||
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
||||||
|
* Source: $(LINK2 https://github.com/caraus-ecms/tanya/blob/master/source/tanya/container/entry.d,
|
||||||
|
* tanya/container/entry.d)
|
||||||
*/
|
*/
|
||||||
module tanya.container.entry;
|
module tanya.container.entry;
|
||||||
|
|
||||||
import std.traits;
|
import tanya.meta.trait;
|
||||||
import tanya.typecons;
|
import tanya.typecons;
|
||||||
|
|
||||||
package struct SEntry(T)
|
package struct SEntry(T)
|
||||||
@ -53,11 +55,6 @@ package enum BucketStatus : byte
|
|||||||
|
|
||||||
package struct Bucket(T)
|
package struct Bucket(T)
|
||||||
{
|
{
|
||||||
this(ref T content)
|
|
||||||
{
|
|
||||||
this.content = content;
|
|
||||||
}
|
|
||||||
|
|
||||||
@property void content(ref T content)
|
@property void content(ref T content)
|
||||||
{
|
{
|
||||||
this.content_ = content;
|
this.content_ = content;
|
||||||
@ -78,7 +75,7 @@ package struct Bucket(T)
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool opEquals(ref T content) const
|
bool opEquals(ref const T content) const
|
||||||
{
|
{
|
||||||
if (this.status == BucketStatus.used && this.content == content)
|
if (this.status == BucketStatus.used && this.content == content)
|
||||||
{
|
{
|
||||||
|
@ -9,6 +9,8 @@
|
|||||||
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
||||||
* Mozilla Public License, v. 2.0).
|
* Mozilla Public License, v. 2.0).
|
||||||
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
||||||
|
* Source: $(LINK2 https://github.com/caraus-ecms/tanya/blob/master/source/tanya/container/list.d,
|
||||||
|
* tanya/container/list.d)
|
||||||
*/
|
*/
|
||||||
module tanya.container.list;
|
module tanya.container.list;
|
||||||
|
|
||||||
@ -16,19 +18,20 @@ import std.algorithm.comparison;
|
|||||||
import std.algorithm.mutation;
|
import std.algorithm.mutation;
|
||||||
import std.algorithm.searching;
|
import std.algorithm.searching;
|
||||||
import std.range.primitives;
|
import std.range.primitives;
|
||||||
import std.traits;
|
|
||||||
import tanya.container.entry;
|
import tanya.container.entry;
|
||||||
import tanya.memory;
|
import tanya.memory;
|
||||||
|
import tanya.meta.trait;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Forward range for the $(D_PSYMBOL SList).
|
* Forward range for the $(D_PSYMBOL SList).
|
||||||
*
|
*
|
||||||
* Params:
|
* Params:
|
||||||
* E = Element type.
|
* L = List type.
|
||||||
*/
|
*/
|
||||||
struct SRange(E)
|
struct SRange(L)
|
||||||
{
|
{
|
||||||
private alias EntryPointer = CopyConstness!(E, SEntry!(Unqual!E)*);
|
private alias EntryPointer = typeof(L.head);
|
||||||
|
private alias E = typeof(EntryPointer.content);
|
||||||
|
|
||||||
private EntryPointer* head;
|
private EntryPointer* head;
|
||||||
|
|
||||||
@ -79,7 +82,7 @@ struct SRange(E)
|
|||||||
return typeof(return)(*this.head);
|
return typeof(return)(*this.head);
|
||||||
}
|
}
|
||||||
|
|
||||||
SRange!(const E) opIndex() const
|
L.ConstRange opIndex() const
|
||||||
{
|
{
|
||||||
return typeof(return)(*this.head);
|
return typeof(return)(*this.head);
|
||||||
}
|
}
|
||||||
@ -94,10 +97,10 @@ struct SRange(E)
|
|||||||
struct SList(T)
|
struct SList(T)
|
||||||
{
|
{
|
||||||
/// The range types for $(D_PSYMBOL SList).
|
/// The range types for $(D_PSYMBOL SList).
|
||||||
alias Range = SRange!T;
|
alias Range = SRange!SList;
|
||||||
|
|
||||||
/// Ditto.
|
/// Ditto.
|
||||||
alias ConstRange = SRange!(const T);
|
alias ConstRange = SRange!(const SList);
|
||||||
|
|
||||||
private alias Entry = SEntry!T;
|
private alias Entry = SEntry!T;
|
||||||
|
|
||||||
@ -434,7 +437,7 @@ struct SList(T)
|
|||||||
|
|
||||||
version (assert)
|
version (assert)
|
||||||
{
|
{
|
||||||
private bool checkRangeBelonging(ref SRange!T r) const
|
private bool checkRangeBelonging(ref Range r) const
|
||||||
{
|
{
|
||||||
const(Entry*)* pos = &this.head;
|
const(Entry*)* pos = &this.head;
|
||||||
for (; pos != r.head && *pos !is null; pos = &(*pos).next)
|
for (; pos != r.head && *pos !is null; pos = &(*pos).next)
|
||||||
@ -456,7 +459,7 @@ struct SList(T)
|
|||||||
*
|
*
|
||||||
* Precondition: $(D_PARAM r) is extracted from this list.
|
* Precondition: $(D_PARAM r) is extracted from this list.
|
||||||
*/
|
*/
|
||||||
size_t insertBefore(R)(SRange!T r, R el)
|
size_t insertBefore(R)(Range r, R el)
|
||||||
if (isImplicitlyConvertible!(R, T))
|
if (isImplicitlyConvertible!(R, T))
|
||||||
in
|
in
|
||||||
{
|
{
|
||||||
@ -477,7 +480,7 @@ struct SList(T)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Ditto.
|
/// Ditto.
|
||||||
size_t insertBefore(R)(SRange!T r, R el)
|
size_t insertBefore(R)(Range r, R el)
|
||||||
if (!isInfinite!R
|
if (!isInfinite!R
|
||||||
&& isInputRange!R
|
&& isInputRange!R
|
||||||
&& isImplicitlyConvertible!(ElementType!R, T))
|
&& isImplicitlyConvertible!(ElementType!R, T))
|
||||||
@ -544,7 +547,18 @@ struct SList(T)
|
|||||||
*/
|
*/
|
||||||
size_t insertBefore(size_t R)(Range r, T[R] el)
|
size_t insertBefore(size_t R)(Range r, T[R] el)
|
||||||
{
|
{
|
||||||
return insertFront!(T[])(el[]);
|
return insertBefore!(T[])(r, el[]);
|
||||||
|
}
|
||||||
|
|
||||||
|
///
|
||||||
|
@safe @nogc unittest
|
||||||
|
{
|
||||||
|
auto l1 = SList!int([5, 234, 30, 1]);
|
||||||
|
auto l2 = SList!int([5, 1]);
|
||||||
|
auto r = l2[];
|
||||||
|
r.popFront();
|
||||||
|
l2.insertBefore(r, [234, 30]);
|
||||||
|
assert(l1 == l2);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -812,7 +826,7 @@ struct SList(T)
|
|||||||
}
|
}
|
||||||
next = &(*next).next;
|
next = &(*next).next;
|
||||||
}
|
}
|
||||||
remove(SRange!T(*next));
|
remove(Range(*next));
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
@ -906,28 +920,28 @@ private @nogc @safe unittest
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Forward range for the $(D_PSYMBOL SList).
|
* Forward range for the $(D_PSYMBOL DList).
|
||||||
*
|
*
|
||||||
* Params:
|
* Params:
|
||||||
* E = Element type.
|
* L = List type.
|
||||||
*/
|
*/
|
||||||
struct DRange(E)
|
struct DRange(L)
|
||||||
{
|
{
|
||||||
private alias EntryPointer = CopyConstness!(E, DEntry!(Unqual!E)*);
|
private alias E = typeof(L.head.content);
|
||||||
private alias TailPointer = CopyConstness!(E, DEntry!(Unqual!E))*;
|
private alias EntryPointer = typeof(L.head);
|
||||||
|
|
||||||
private EntryPointer* head;
|
private EntryPointer* head;
|
||||||
private TailPointer tail;
|
private EntryPointer* tail;
|
||||||
|
|
||||||
invariant
|
invariant
|
||||||
{
|
{
|
||||||
assert(this.head !is null);
|
assert(this.head !is null);
|
||||||
}
|
}
|
||||||
|
|
||||||
private this(ref EntryPointer head, TailPointer tail) @trusted
|
private this(ref EntryPointer head, ref EntryPointer tail) @trusted
|
||||||
{
|
{
|
||||||
this.head = &head;
|
this.head = &head;
|
||||||
this.tail = tail;
|
this.tail = &tail;
|
||||||
}
|
}
|
||||||
|
|
||||||
@disable this();
|
@disable this();
|
||||||
@ -939,7 +953,7 @@ struct DRange(E)
|
|||||||
|
|
||||||
@property bool empty() const
|
@property bool empty() const
|
||||||
{
|
{
|
||||||
return *this.head is null || *this.head is this.tail.next;
|
return *this.head is null || *this.head is (*this.tail).next;
|
||||||
}
|
}
|
||||||
|
|
||||||
@property ref inout(E) front() inout
|
@property ref inout(E) front() inout
|
||||||
@ -959,7 +973,7 @@ struct DRange(E)
|
|||||||
}
|
}
|
||||||
body
|
body
|
||||||
{
|
{
|
||||||
return this.tail.content;
|
return (*this.tail).content;
|
||||||
}
|
}
|
||||||
|
|
||||||
void popFront() @trusted
|
void popFront() @trusted
|
||||||
@ -979,17 +993,17 @@ struct DRange(E)
|
|||||||
}
|
}
|
||||||
body
|
body
|
||||||
{
|
{
|
||||||
this.tail = this.tail.prev;
|
this.tail = &(*this.tail).prev;
|
||||||
}
|
}
|
||||||
|
|
||||||
DRange opIndex()
|
DRange opIndex()
|
||||||
{
|
{
|
||||||
return typeof(return)(*this.head, this.tail);
|
return typeof(return)(*this.head, *this.tail);
|
||||||
}
|
}
|
||||||
|
|
||||||
DRange!(const E) opIndex() const
|
L.ConstRange opIndex() const
|
||||||
{
|
{
|
||||||
return typeof(return)(*this.head, this.tail);
|
return typeof(return)(*this.head, *this.tail);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1002,10 +1016,10 @@ struct DRange(E)
|
|||||||
struct DList(T)
|
struct DList(T)
|
||||||
{
|
{
|
||||||
/// The range types for $(D_PSYMBOL DList).
|
/// The range types for $(D_PSYMBOL DList).
|
||||||
alias Range = DRange!T;
|
alias Range = DRange!DList;
|
||||||
|
|
||||||
/// Ditto.
|
/// Ditto.
|
||||||
alias ConstRange = DRange!(const T);
|
alias ConstRange = DRange!(const DList);
|
||||||
|
|
||||||
private alias Entry = DEntry!T;
|
private alias Entry = DEntry!T;
|
||||||
|
|
||||||
@ -1017,6 +1031,7 @@ struct DList(T)
|
|||||||
assert((this.tail is null && this.head is null)
|
assert((this.tail is null && this.head is null)
|
||||||
|| (this.tail !is null && this.head !is null));
|
|| (this.tail !is null && this.head !is null));
|
||||||
assert(this.tail is null || this.tail.next is null);
|
assert(this.tail is null || this.tail.next is null);
|
||||||
|
assert(this.head is null || this.head.prev is null);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -1272,20 +1287,21 @@ struct DList(T)
|
|||||||
assert(l.back == 25);
|
assert(l.back == 25);
|
||||||
}
|
}
|
||||||
|
|
||||||
private size_t moveEntry(R)(ref Entry* head, ref R el) @trusted
|
private size_t moveFront(R)(ref Entry* head, ref R el) @trusted
|
||||||
if (isImplicitlyConvertible!(R, T))
|
if (isImplicitlyConvertible!(R, T))
|
||||||
{
|
{
|
||||||
auto temp = cast(Entry*) allocator.allocate(Entry.sizeof);
|
auto temp = cast(Entry*) allocator.allocate(Entry.sizeof);
|
||||||
|
|
||||||
el.moveEmplace(temp.content);
|
el.moveEmplace(temp.content);
|
||||||
temp.next = head;
|
temp.next = head;
|
||||||
temp.prev = null;
|
|
||||||
if (this.tail is null)
|
if (this.tail is null)
|
||||||
{
|
{
|
||||||
|
temp.prev = null;
|
||||||
this.tail = temp;
|
this.tail = temp;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
temp.prev = head.prev;
|
||||||
head.prev = temp;
|
head.prev = temp;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1293,6 +1309,34 @@ struct DList(T)
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Creates a lsit of linked entries from a range.
|
||||||
|
// Returns count of the elements in the list.
|
||||||
|
private size_t makeList(R)(ref R el, out Entry* head, out Entry* tail) @trusted
|
||||||
|
out (retLength)
|
||||||
|
{
|
||||||
|
assert((retLength == 0 && head is null && tail is null)
|
||||||
|
|| (retLength > 0 && head !is null && tail !is null));
|
||||||
|
}
|
||||||
|
body
|
||||||
|
{
|
||||||
|
size_t retLength;
|
||||||
|
|
||||||
|
if (!el.empty)
|
||||||
|
{
|
||||||
|
head = tail = allocator.make!Entry(el.front);
|
||||||
|
el.popFront();
|
||||||
|
retLength = 1;
|
||||||
|
}
|
||||||
|
foreach (ref e; el)
|
||||||
|
{
|
||||||
|
tail.next = allocator.make!Entry(e);
|
||||||
|
tail.next.prev = tail;
|
||||||
|
tail = tail.next;
|
||||||
|
++retLength;
|
||||||
|
}
|
||||||
|
return retLength;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Inserts a new element at the beginning.
|
* Inserts a new element at the beginning.
|
||||||
*
|
*
|
||||||
@ -1305,7 +1349,7 @@ struct DList(T)
|
|||||||
size_t insertFront(R)(R el)
|
size_t insertFront(R)(R el)
|
||||||
if (isImplicitlyConvertible!(R, T))
|
if (isImplicitlyConvertible!(R, T))
|
||||||
{
|
{
|
||||||
return moveEntry(this.head, el);
|
return moveFront(this.head, el);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Ditto.
|
/// Ditto.
|
||||||
@ -1341,38 +1385,31 @@ struct DList(T)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Ditto.
|
/// Ditto.
|
||||||
size_t insertFront(R)(R el) @trusted
|
size_t insertFront(R)(R el)
|
||||||
if (!isInfinite!R
|
if (!isInfinite!R
|
||||||
&& isInputRange!R
|
&& isInputRange!R
|
||||||
&& isImplicitlyConvertible!(ElementType!R, T))
|
&& isImplicitlyConvertible!(ElementType!R, T))
|
||||||
{
|
{
|
||||||
size_t retLength;
|
Entry* begin, end;
|
||||||
Entry* next, newHead;
|
const inserted = makeList(el, begin, end);
|
||||||
|
|
||||||
if (!el.empty)
|
|
||||||
{
|
|
||||||
next = allocator.make!Entry(el.front);
|
|
||||||
newHead = next;
|
|
||||||
el.popFront();
|
|
||||||
retLength = 1;
|
|
||||||
}
|
|
||||||
foreach (ref e; el)
|
|
||||||
{
|
|
||||||
next.next = allocator.make!Entry(e);
|
|
||||||
next = next.next;
|
|
||||||
++retLength;
|
|
||||||
}
|
|
||||||
if (this.head is null)
|
if (this.head is null)
|
||||||
{
|
{
|
||||||
this.tail = next;
|
this.tail = end;
|
||||||
}
|
}
|
||||||
if (newHead !is null)
|
if (begin !is null)
|
||||||
{
|
{
|
||||||
next.next = this.head;
|
end.next = this.head;
|
||||||
this.head = newHead;
|
this.head = begin;
|
||||||
}
|
}
|
||||||
|
|
||||||
return retLength;
|
return inserted;
|
||||||
|
}
|
||||||
|
|
||||||
|
private @safe @nogc unittest
|
||||||
|
{
|
||||||
|
auto l1 = DList!int([5, 234]);
|
||||||
|
assert(l1.head is l1.head.next.prev);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Ditto.
|
/// Ditto.
|
||||||
@ -1381,9 +1418,6 @@ struct DList(T)
|
|||||||
return insertFront!(T[])(el[]);
|
return insertFront!(T[])(el[]);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Ditto.
|
|
||||||
alias insert = insertBack;
|
|
||||||
|
|
||||||
///
|
///
|
||||||
@safe @nogc unittest
|
@safe @nogc unittest
|
||||||
{
|
{
|
||||||
@ -1407,6 +1441,28 @@ struct DList(T)
|
|||||||
assert(l2.back == 15);
|
assert(l2.back == 15);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private size_t moveBack(R)(ref Entry* tail, ref R el) @trusted
|
||||||
|
if (isImplicitlyConvertible!(R, T))
|
||||||
|
{
|
||||||
|
auto temp = cast(Entry*) allocator.allocate(Entry.sizeof);
|
||||||
|
|
||||||
|
el.moveEmplace(temp.content);
|
||||||
|
temp.prev = tail;
|
||||||
|
if (this.head is null)
|
||||||
|
{
|
||||||
|
temp.next = null;
|
||||||
|
this.head = this.tail = temp;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
temp.next = tail.next;
|
||||||
|
tail.next = temp;
|
||||||
|
}
|
||||||
|
|
||||||
|
tail = temp;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Inserts a new element at the end.
|
* Inserts a new element at the end.
|
||||||
*
|
*
|
||||||
@ -1419,22 +1475,7 @@ struct DList(T)
|
|||||||
size_t insertBack(R)(R el) @trusted
|
size_t insertBack(R)(R el) @trusted
|
||||||
if (isImplicitlyConvertible!(R, T))
|
if (isImplicitlyConvertible!(R, T))
|
||||||
{
|
{
|
||||||
auto temp = cast(Entry*) allocator.allocate(Entry.sizeof);
|
return moveBack(this.tail, el);
|
||||||
|
|
||||||
el.moveEmplace(temp.content);
|
|
||||||
temp.next = null;
|
|
||||||
temp.prev = tail;
|
|
||||||
if (this.head is null)
|
|
||||||
{
|
|
||||||
this.head = temp;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
this.tail.next = temp;
|
|
||||||
}
|
|
||||||
|
|
||||||
this.tail = temp;
|
|
||||||
return 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Ditto.
|
/// Ditto.
|
||||||
@ -1475,11 +1516,20 @@ struct DList(T)
|
|||||||
&& isInputRange!R
|
&& isInputRange!R
|
||||||
&& isImplicitlyConvertible!(ElementType!R, T))
|
&& isImplicitlyConvertible!(ElementType!R, T))
|
||||||
{
|
{
|
||||||
size_t inserted;
|
Entry* begin, end;
|
||||||
|
const inserted = makeList(el, begin, end);
|
||||||
|
|
||||||
foreach (ref e; el)
|
if (this.tail is null)
|
||||||
{
|
{
|
||||||
inserted += insertBack(e);
|
this.head = begin;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
this.tail.next = begin;
|
||||||
|
}
|
||||||
|
if (begin !is null)
|
||||||
|
{
|
||||||
|
this.tail = end;
|
||||||
}
|
}
|
||||||
|
|
||||||
return inserted;
|
return inserted;
|
||||||
@ -1510,9 +1560,12 @@ struct DList(T)
|
|||||||
assert(l2.back == 9);
|
assert(l2.back == 9);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Ditto.
|
||||||
|
alias insert = insertBack;
|
||||||
|
|
||||||
version (assert)
|
version (assert)
|
||||||
{
|
{
|
||||||
private bool checkRangeBelonging(ref DRange!T r) const
|
private bool checkRangeBelonging(ref Range r) const
|
||||||
{
|
{
|
||||||
const(Entry*)* pos = &this.head;
|
const(Entry*)* pos = &this.head;
|
||||||
for (; pos != r.head && *pos !is null; pos = &(*pos).next)
|
for (; pos != r.head && *pos !is null; pos = &(*pos).next)
|
||||||
@ -1534,7 +1587,7 @@ struct DList(T)
|
|||||||
*
|
*
|
||||||
* Precondition: $(D_PARAM r) is extracted from this list.
|
* Precondition: $(D_PARAM r) is extracted from this list.
|
||||||
*/
|
*/
|
||||||
size_t insertBefore(R)(DRange!T r, R el)
|
size_t insertBefore(R)(Range r, R el)
|
||||||
if (isImplicitlyConvertible!(R, T))
|
if (isImplicitlyConvertible!(R, T))
|
||||||
in
|
in
|
||||||
{
|
{
|
||||||
@ -1542,7 +1595,7 @@ struct DList(T)
|
|||||||
}
|
}
|
||||||
body
|
body
|
||||||
{
|
{
|
||||||
return moveEntry(*r.head, el);
|
return moveFront(*r.head, el);
|
||||||
}
|
}
|
||||||
|
|
||||||
///
|
///
|
||||||
@ -1555,7 +1608,42 @@ struct DList(T)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Ditto.
|
/// Ditto.
|
||||||
size_t insertBefore(R)(DRange!T r, R el)
|
size_t insertBefore(Range r, ref T el) @trusted
|
||||||
|
in
|
||||||
|
{
|
||||||
|
assert(checkRangeBelonging(r));
|
||||||
|
}
|
||||||
|
body
|
||||||
|
{
|
||||||
|
auto temp = allocator.make!Entry(el, *r.head);
|
||||||
|
|
||||||
|
if (this.tail is null)
|
||||||
|
{
|
||||||
|
this.tail = temp;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
temp.prev = (*r.head).prev;
|
||||||
|
(*r.head).prev = temp;
|
||||||
|
}
|
||||||
|
|
||||||
|
*r.head = temp;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
///
|
||||||
|
@safe @nogc unittest
|
||||||
|
{
|
||||||
|
auto l1 = DList!int([234, 5, 1]);
|
||||||
|
auto l2 = DList!int([5, 1]);
|
||||||
|
int var = 234;
|
||||||
|
|
||||||
|
l2.insertBefore(l2[], var);
|
||||||
|
assert(l1 == l2);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Ditto.
|
||||||
|
size_t insertBefore(R)(Range r, R el)
|
||||||
if (!isInfinite!R
|
if (!isInfinite!R
|
||||||
&& isInputRange!R
|
&& isInputRange!R
|
||||||
&& isImplicitlyConvertible!(ElementType!R, T))
|
&& isImplicitlyConvertible!(ElementType!R, T))
|
||||||
@ -1579,32 +1667,9 @@ struct DList(T)
|
|||||||
{
|
{
|
||||||
auto l1 = DList!int([5, 234, 30, 1]);
|
auto l1 = DList!int([5, 234, 30, 1]);
|
||||||
auto l2 = DList!int([5, 1]);
|
auto l2 = DList!int([5, 1]);
|
||||||
auto l3 = DList!int([234, 30]);
|
|
||||||
auto r = l2[];
|
auto r = l2[];
|
||||||
r.popFront();
|
r.popFront();
|
||||||
l2.insertBefore(r, l3[]);
|
l2.insertBefore(r, [234, 30]);
|
||||||
assert(l1 == l2);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Ditto.
|
|
||||||
size_t insertBefore(Range r, ref T el) @trusted
|
|
||||||
in
|
|
||||||
{
|
|
||||||
assert(checkRangeBelonging(r));
|
|
||||||
}
|
|
||||||
body
|
|
||||||
{
|
|
||||||
*r.head = allocator.make!Entry(el, *r.head);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
///
|
|
||||||
@safe @nogc unittest
|
|
||||||
{
|
|
||||||
auto l1 = DList!int([234, 5, 1]);
|
|
||||||
auto l2 = DList!int([5, 1]);
|
|
||||||
int var = 234;
|
|
||||||
l2.insertBefore(l2[], var);
|
|
||||||
assert(l1 == l2);
|
assert(l1 == l2);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1622,7 +1687,133 @@ struct DList(T)
|
|||||||
*/
|
*/
|
||||||
size_t insertBefore(size_t R)(Range r, T[R] el)
|
size_t insertBefore(size_t R)(Range r, T[R] el)
|
||||||
{
|
{
|
||||||
return insertFront!(T[])(el[]);
|
return insertBefore!(T[])(r, el[]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Inserts new elements after $(D_PARAM r).
|
||||||
|
*
|
||||||
|
* Params:
|
||||||
|
* R = Type of the inserted value(s).
|
||||||
|
* r = Range extracted from this list.
|
||||||
|
* el = New element(s).
|
||||||
|
*
|
||||||
|
* Returns: The number of elements inserted.
|
||||||
|
*
|
||||||
|
* Precondition: $(D_PARAM r) is extracted from this list.
|
||||||
|
*/
|
||||||
|
size_t insertAfter(R)(Range r, R el) @trusted
|
||||||
|
if (isImplicitlyConvertible!(R, T))
|
||||||
|
in
|
||||||
|
{
|
||||||
|
assert(checkRangeBelonging(r));
|
||||||
|
}
|
||||||
|
body
|
||||||
|
{
|
||||||
|
return moveBack(*r.tail, el);
|
||||||
|
}
|
||||||
|
|
||||||
|
///
|
||||||
|
@safe @nogc unittest
|
||||||
|
{
|
||||||
|
auto l1 = DList!int([5, 234, 1]);
|
||||||
|
auto l2 = DList!int([5, 1]);
|
||||||
|
auto r = l2[];
|
||||||
|
r.popBack();
|
||||||
|
l2.insertAfter(r, 234);
|
||||||
|
assert(l1 == l2);
|
||||||
|
}
|
||||||
|
|
||||||
|
private @safe @nogc unittest
|
||||||
|
{
|
||||||
|
DList!int l;
|
||||||
|
l.insertAfter(l[], 234);
|
||||||
|
assert(l.front == 234);
|
||||||
|
assert(l.back == 234);
|
||||||
|
assert(l.length == 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Ditto.
|
||||||
|
size_t insertAfter(Range r, ref T el) @trusted
|
||||||
|
in
|
||||||
|
{
|
||||||
|
assert(checkRangeBelonging(r));
|
||||||
|
}
|
||||||
|
body
|
||||||
|
{
|
||||||
|
auto temp = allocator.make!Entry(el, null, *r.tail);
|
||||||
|
|
||||||
|
if (this.head is null)
|
||||||
|
{
|
||||||
|
this.head = temp;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
temp.next = (*r.tail).next;
|
||||||
|
(*r.tail).next = temp;
|
||||||
|
}
|
||||||
|
|
||||||
|
*r.tail = temp;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
///
|
||||||
|
@safe @nogc unittest
|
||||||
|
{
|
||||||
|
auto l1 = DList!int([5, 1, 234]);
|
||||||
|
auto l2 = DList!int([5, 1]);
|
||||||
|
int var = 234;
|
||||||
|
|
||||||
|
l2.insertAfter(l2[], var);
|
||||||
|
assert(l1 == l2);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Ditto.
|
||||||
|
size_t insertAfter(R)(Range r, R el)
|
||||||
|
if (!isInfinite!R
|
||||||
|
&& isInputRange!R
|
||||||
|
&& isImplicitlyConvertible!(ElementType!R, T))
|
||||||
|
in
|
||||||
|
{
|
||||||
|
assert(checkRangeBelonging(r));
|
||||||
|
}
|
||||||
|
body
|
||||||
|
{
|
||||||
|
size_t inserted;
|
||||||
|
foreach (e; el)
|
||||||
|
{
|
||||||
|
inserted += insertAfter(r, e);
|
||||||
|
}
|
||||||
|
return inserted;
|
||||||
|
}
|
||||||
|
|
||||||
|
///
|
||||||
|
@safe @nogc unittest
|
||||||
|
{
|
||||||
|
auto l1 = DList!int([5, 234, 30, 1]);
|
||||||
|
auto l2 = DList!int([5, 1]);
|
||||||
|
auto r = l2[];
|
||||||
|
|
||||||
|
r.popBack();
|
||||||
|
l2.insertAfter(r, [234, 30]);
|
||||||
|
assert(l1 == l2);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Inserts elements from a static array after $(D_PARAM r).
|
||||||
|
*
|
||||||
|
* Params:
|
||||||
|
* R = Static array size.
|
||||||
|
* r = Range extracted from this list.
|
||||||
|
* el = New elements.
|
||||||
|
*
|
||||||
|
* Returns: The number of elements inserted.
|
||||||
|
*
|
||||||
|
* Precondition: $(D_PARAM r) is extracted from this list.
|
||||||
|
*/
|
||||||
|
size_t insertAfter(size_t R)(Range r, T[R] el)
|
||||||
|
{
|
||||||
|
return insertAfter!(T[])(r, el[]);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -1694,7 +1885,7 @@ struct DList(T)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Removes the front element.
|
* Removes the front or back element.
|
||||||
*
|
*
|
||||||
* Precondition: $(D_INLINECODE !empty)
|
* Precondition: $(D_INLINECODE !empty)
|
||||||
*/
|
*/
|
||||||
@ -1707,12 +1898,16 @@ struct DList(T)
|
|||||||
{
|
{
|
||||||
auto n = this.head.next;
|
auto n = this.head.next;
|
||||||
|
|
||||||
this.allocator.dispose(this.head);
|
allocator.dispose(this.head);
|
||||||
this.head = n;
|
this.head = n;
|
||||||
if (this.head is null)
|
if (this.head is null)
|
||||||
{
|
{
|
||||||
this.tail = null;
|
this.tail = null;
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
this.head.prev = null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
///
|
///
|
||||||
@ -1729,12 +1924,47 @@ struct DList(T)
|
|||||||
assert(l.empty);
|
assert(l.empty);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Ditto.
|
||||||
|
void removeBack()
|
||||||
|
in
|
||||||
|
{
|
||||||
|
assert(!empty);
|
||||||
|
}
|
||||||
|
body
|
||||||
|
{
|
||||||
|
auto n = this.tail.prev;
|
||||||
|
|
||||||
|
allocator.dispose(this.tail);
|
||||||
|
this.tail = n;
|
||||||
|
if (this.tail is null)
|
||||||
|
{
|
||||||
|
this.head = null;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
this.tail.next = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
///
|
||||||
|
@safe @nogc unittest
|
||||||
|
{
|
||||||
|
auto l = DList!int([9, 8]);
|
||||||
|
|
||||||
|
assert(l.back == 8);
|
||||||
|
l.removeBack();
|
||||||
|
assert(l.back == 9);
|
||||||
|
l.removeFront();
|
||||||
|
assert(l.empty);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Removes $(D_PARAM howMany) elements from the list.
|
* Removes $(D_PARAM howMany) elements from the list.
|
||||||
*
|
*
|
||||||
* Unlike $(D_PSYMBOL removeFront()), this method doesn't fail, if it could not
|
* Unlike $(D_PSYMBOL removeFront()) and $(D_PSYMBOL removeBack()), this
|
||||||
* remove $(D_PARAM howMany) elements. Instead, if $(D_PARAM howMany) is
|
* method doesn't fail, if it could not remove $(D_PARAM howMany) elements.
|
||||||
* greater than the list length, all elements are removed.
|
* Instead, if $(D_PARAM howMany) is greater than the list length, all
|
||||||
|
* elements are removed.
|
||||||
*
|
*
|
||||||
* Params:
|
* Params:
|
||||||
* howMany = How many elements should be removed.
|
* howMany = How many elements should be removed.
|
||||||
@ -1767,13 +1997,40 @@ struct DList(T)
|
|||||||
assert(l.removeFront(3) == 0);
|
assert(l.removeFront(3) == 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Ditto.
|
||||||
|
size_t removeBack(const size_t howMany)
|
||||||
|
out (removed)
|
||||||
|
{
|
||||||
|
assert(removed <= howMany);
|
||||||
|
}
|
||||||
|
body
|
||||||
|
{
|
||||||
|
size_t i;
|
||||||
|
for (; i < howMany && !empty; ++i)
|
||||||
|
{
|
||||||
|
removeBack();
|
||||||
|
}
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
|
||||||
|
///
|
||||||
|
@safe @nogc unittest
|
||||||
|
{
|
||||||
|
DList!int l = DList!int([8, 5, 4]);
|
||||||
|
|
||||||
|
assert(l.removeBack(0) == 0);
|
||||||
|
assert(l.removeBack(2) == 2);
|
||||||
|
assert(l.removeBack(3) == 1);
|
||||||
|
assert(l.removeBack(3) == 0);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Removes $(D_PARAM r) from the list.
|
* Removes $(D_PARAM r) from the list.
|
||||||
*
|
*
|
||||||
* Params:
|
* Params:
|
||||||
* r = The range to remove.
|
* r = The range to remove.
|
||||||
*
|
*
|
||||||
* Returns: An empty range.
|
* Returns: Range spanning the elements just after $(D_PARAM r).
|
||||||
*
|
*
|
||||||
* Precondition: $(D_PARAM r) is extracted from this list.
|
* Precondition: $(D_PARAM r) is extracted from this list.
|
||||||
*/
|
*/
|
||||||
@ -1784,19 +2041,34 @@ struct DList(T)
|
|||||||
}
|
}
|
||||||
body
|
body
|
||||||
{
|
{
|
||||||
auto outOfScopeList = typeof(this)(allocator);
|
// Save references to the elements before and after the range.
|
||||||
outOfScopeList.head = *r.head;
|
Entry* tailNext, headPrev;
|
||||||
outOfScopeList.tail = r.tail;
|
if (*r.tail !is null && (*r.tail).next !is null)
|
||||||
if (r.tail is null)
|
|
||||||
{
|
{
|
||||||
*r.head = null;
|
tailNext = (*r.tail).next;
|
||||||
}
|
}
|
||||||
else
|
if (*r.head !is null)
|
||||||
{
|
{
|
||||||
outOfScopeList.tail.next = null;
|
headPrev = (*r.head).prev;
|
||||||
*r.head = r.tail.next;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Remove the elements.
|
||||||
|
Entry* e = *r.head;
|
||||||
|
while (e !is tailNext)
|
||||||
|
{
|
||||||
|
auto next = e.next;
|
||||||
|
allocator.dispose(e);
|
||||||
|
e = next;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Connect the elements before and after the removed range.
|
||||||
|
if (tailNext !is null)
|
||||||
|
{
|
||||||
|
tailNext.prev = headPrev;
|
||||||
|
}
|
||||||
|
*r.head = tailNext;
|
||||||
|
*r.tail = tail;
|
||||||
|
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1813,6 +2085,22 @@ struct DList(T)
|
|||||||
assert(l1 == l2);
|
assert(l1 == l2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Issue 260: https://issues.caraus.io/issues/260.
|
||||||
|
private @safe @nogc unittest
|
||||||
|
{
|
||||||
|
auto l1 = DList!int([5, 234, 30, 1]);
|
||||||
|
auto l2 = DList!int([5, 1]);
|
||||||
|
auto r = l1[];
|
||||||
|
|
||||||
|
r.popFront();
|
||||||
|
r.popBack();
|
||||||
|
assert(r.front == 234);
|
||||||
|
assert(r.back == 30);
|
||||||
|
|
||||||
|
assert(!l1.remove(r).empty);
|
||||||
|
assert(l1 == l2);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns: Range that iterates over all elements of the container, in
|
* Returns: Range that iterates over all elements of the container, in
|
||||||
* forward order.
|
* forward order.
|
||||||
@ -1883,7 +2171,7 @@ struct DList(T)
|
|||||||
}
|
}
|
||||||
if (that.empty)
|
if (that.empty)
|
||||||
{
|
{
|
||||||
remove(DRange!T(*next, this.tail));
|
remove(Range(*next, this.tail));
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@ -1953,3 +2241,13 @@ struct DList(T)
|
|||||||
}
|
}
|
||||||
assert(i == 3);
|
assert(i == 3);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Issue 232: https://issues.caraus.io/issues/232.
|
||||||
|
private @nogc unittest
|
||||||
|
{
|
||||||
|
class A
|
||||||
|
{
|
||||||
|
}
|
||||||
|
static assert(is(SList!(A*)));
|
||||||
|
static assert(is(DList!(A*)));
|
||||||
|
}
|
||||||
|
@ -9,6 +9,8 @@
|
|||||||
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
||||||
* Mozilla Public License, v. 2.0).
|
* Mozilla Public License, v. 2.0).
|
||||||
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
||||||
|
* Source: $(LINK2 https://github.com/caraus-ecms/tanya/blob/master/source/tanya/container/package.d,
|
||||||
|
* tanya/container/package.d)
|
||||||
*/
|
*/
|
||||||
module tanya.container;
|
module tanya.container;
|
||||||
|
|
||||||
|
@ -9,14 +9,16 @@
|
|||||||
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
||||||
* Mozilla Public License, v. 2.0).
|
* Mozilla Public License, v. 2.0).
|
||||||
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
||||||
|
* Source: $(LINK2 https://github.com/caraus-ecms/tanya/blob/master/source/tanya/container/queue.d,
|
||||||
|
* tanya/container/queue.d)
|
||||||
*/
|
*/
|
||||||
module tanya.container.queue;
|
module tanya.container.queue;
|
||||||
|
|
||||||
import core.exception;
|
import core.exception;
|
||||||
import std.traits;
|
|
||||||
import std.algorithm.mutation;
|
import std.algorithm.mutation;
|
||||||
import tanya.container.entry;
|
import tanya.container.entry;
|
||||||
import tanya.memory;
|
import tanya.memory;
|
||||||
|
import tanya.meta.trait;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* FIFO queue.
|
* FIFO queue.
|
||||||
|
@ -10,14 +10,17 @@
|
|||||||
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
||||||
* Mozilla Public License, v. 2.0).
|
* Mozilla Public License, v. 2.0).
|
||||||
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
||||||
|
* Source: $(LINK2 https://github.com/caraus-ecms/tanya/blob/master/source/tanya/container/set.d,
|
||||||
|
* tanya/container/set.d)
|
||||||
*/
|
*/
|
||||||
module tanya.container.set;
|
module tanya.container.set;
|
||||||
|
|
||||||
import std.algorithm.mutation;
|
import std.algorithm.mutation;
|
||||||
import std.traits;
|
|
||||||
import tanya.container;
|
import tanya.container;
|
||||||
import tanya.container.entry;
|
import tanya.container.entry;
|
||||||
import tanya.memory;
|
import tanya.memory;
|
||||||
|
import tanya.meta.trait;
|
||||||
|
import tanya.meta.transform;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Bidirectional range that iterates over the $(D_PSYMBOL Set)'s values.
|
* Bidirectional range that iterates over the $(D_PSYMBOL Set)'s values.
|
||||||
@ -349,7 +352,8 @@ struct Set(T)
|
|||||||
/// The maximum number of buckets the container can have.
|
/// The maximum number of buckets the container can have.
|
||||||
enum size_t maxBucketCount = primes[$ - 1];
|
enum size_t maxBucketCount = primes[$ - 1];
|
||||||
|
|
||||||
static private size_t calculateHash(ref T value)
|
static private size_t calculateHash(U)(ref const U value)
|
||||||
|
if (is(U == Unqual!T))
|
||||||
{
|
{
|
||||||
static if (isIntegral!T || isSomeChar!T || is(T == bool))
|
static if (isIntegral!T || isSomeChar!T || is(T == bool))
|
||||||
{
|
{
|
||||||
@ -361,11 +365,26 @@ struct Set(T)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static private size_t locateBucket(ref const DataType buckets, size_t hash)
|
static private size_t locateBucket(ref const DataType buckets,
|
||||||
|
const size_t hash)
|
||||||
|
in
|
||||||
|
{
|
||||||
|
assert(buckets.length > 0);
|
||||||
|
}
|
||||||
|
body
|
||||||
{
|
{
|
||||||
return hash % buckets.length;
|
return hash % buckets.length;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Returns bucket position for `hash`. `0` may mean the 0th position or an
|
||||||
|
* empty `buckets` array.
|
||||||
|
*/
|
||||||
|
private size_t locateBucket(const size_t hash) const
|
||||||
|
{
|
||||||
|
return this.data.length == 0 ? 0 : locateBucket(this.data, hash);
|
||||||
|
}
|
||||||
|
|
||||||
private enum InsertStatus : byte
|
private enum InsertStatus : byte
|
||||||
{
|
{
|
||||||
found = -1,
|
found = -1,
|
||||||
@ -376,7 +395,8 @@ struct Set(T)
|
|||||||
/*
|
/*
|
||||||
* Inserts the value in an empty or deleted bucket. If the value is
|
* Inserts the value in an empty or deleted bucket. If the value is
|
||||||
* already in there, does nothing and returns InsertStatus.found. If the
|
* already in there, does nothing and returns InsertStatus.found. If the
|
||||||
* hash array is full returns InsertStatus.failed.
|
* hash array is full returns InsertStatus.failed. Otherwise,
|
||||||
|
* InsertStatus.added is returned.
|
||||||
*/
|
*/
|
||||||
private InsertStatus insertInUnusedBucket(ref T value)
|
private InsertStatus insertInUnusedBucket(ref T value)
|
||||||
{
|
{
|
||||||
@ -456,12 +476,7 @@ struct Set(T)
|
|||||||
*/
|
*/
|
||||||
size_t remove(T value)
|
size_t remove(T value)
|
||||||
{
|
{
|
||||||
if (this.data.length == 0)
|
auto bucketPosition = locateBucket(calculateHash(value));
|
||||||
{
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
auto bucketPosition = locateBucket(this.data, calculateHash(value));
|
|
||||||
foreach (ref e; this.data[bucketPosition .. $])
|
foreach (ref e; this.data[bucketPosition .. $])
|
||||||
{
|
{
|
||||||
if (e == value) // Found.
|
if (e == value) // Found.
|
||||||
@ -478,7 +493,7 @@ struct Set(T)
|
|||||||
}
|
}
|
||||||
|
|
||||||
///
|
///
|
||||||
unittest
|
@nogc unittest
|
||||||
{
|
{
|
||||||
Set!int set;
|
Set!int set;
|
||||||
assert(8 !in set);
|
assert(8 !in set);
|
||||||
@ -500,14 +515,9 @@ struct Set(T)
|
|||||||
* Returns: $(D_KEYWORD true) if the given element exists in the container,
|
* Returns: $(D_KEYWORD true) if the given element exists in the container,
|
||||||
* $(D_KEYWORD false) otherwise.
|
* $(D_KEYWORD false) otherwise.
|
||||||
*/
|
*/
|
||||||
bool opBinaryRight(string op : "in")(auto ref T value)
|
bool opBinaryRight(string op : "in")(auto ref const T value) const
|
||||||
{
|
{
|
||||||
if (this.data.length == 0)
|
auto bucketPosition = locateBucket(calculateHash(value));
|
||||||
{
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
auto bucketPosition = locateBucket(this.data, calculateHash(value));
|
|
||||||
foreach (ref e; this.data[bucketPosition .. $])
|
foreach (ref e; this.data[bucketPosition .. $])
|
||||||
{
|
{
|
||||||
if (e == value) // Found.
|
if (e == value) // Found.
|
||||||
@ -522,37 +532,15 @@ struct Set(T)
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Ditto.
|
|
||||||
bool opBinaryRight(string op : "in")(auto ref const T value) const
|
|
||||||
{
|
|
||||||
if (this.data.length == 0)
|
|
||||||
{
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
auto bucketPosition = locateBucket(this.data, calculateHash(value));
|
|
||||||
foreach (ref e; this.data[bucketPosition .. $])
|
|
||||||
{
|
|
||||||
if (e.status == BucketStatus.used && e.content == value) // Found.
|
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
else if (e.status == BucketStatus.empty)
|
|
||||||
{
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
///
|
///
|
||||||
unittest
|
@nogc unittest
|
||||||
{
|
{
|
||||||
Set!int set;
|
Set!int set;
|
||||||
|
|
||||||
assert(5 !in set);
|
assert(5 !in set);
|
||||||
set.insert(5);
|
set.insert(5);
|
||||||
assert(5 in set);
|
assert(5 in set);
|
||||||
|
assert(8 !in set);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -633,7 +621,7 @@ struct Set(T)
|
|||||||
}
|
}
|
||||||
|
|
||||||
///
|
///
|
||||||
unittest
|
@nogc unittest
|
||||||
{
|
{
|
||||||
Set!int set;
|
Set!int set;
|
||||||
assert(set[].empty);
|
assert(set[].empty);
|
||||||
@ -647,13 +635,34 @@ struct Set(T)
|
|||||||
assert(set[].empty);
|
assert(set[].empty);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private @nogc unittest
|
||||||
|
{
|
||||||
|
const Set!int set;
|
||||||
|
assert(set[].empty);
|
||||||
|
}
|
||||||
|
|
||||||
|
private @nogc unittest
|
||||||
|
{
|
||||||
|
Set!int set;
|
||||||
|
set.insert(8);
|
||||||
|
|
||||||
|
auto r1 = set[];
|
||||||
|
auto r2 = r1.save();
|
||||||
|
|
||||||
|
r1.popFront();
|
||||||
|
assert(r1.empty);
|
||||||
|
|
||||||
|
r2.popBack();
|
||||||
|
assert(r2.empty);
|
||||||
|
}
|
||||||
|
|
||||||
private alias DataType = Array!(Bucket!T);
|
private alias DataType = Array!(Bucket!T);
|
||||||
private DataType data;
|
private DataType data;
|
||||||
private size_t lengthIndex;
|
private size_t lengthIndex;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Basic insertion logic.
|
// Basic insertion logic.
|
||||||
private unittest
|
private @nogc unittest
|
||||||
{
|
{
|
||||||
Set!int set;
|
Set!int set;
|
||||||
|
|
||||||
|
@ -3,12 +3,26 @@
|
|||||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* UTF-8 string.
|
* UTF-8 encoded string.
|
||||||
|
*
|
||||||
|
* You can create a $(D_PSYMBOL String) from a literal string, single character
|
||||||
|
* or character range. Characters can be of the type $(D_KEYWORD char),
|
||||||
|
* $(D_KEYWORD wchar) or $(D_KEYWORD dchar). Literal strings, characters and
|
||||||
|
* character ranges can be also inserted into an existing string.
|
||||||
|
*
|
||||||
|
* $(D_PSYMBOL String) is always valid UTF-8. Inserting an invalid sequence
|
||||||
|
* or working on a corrupted $(D_PSYMBOL String) causes
|
||||||
|
* $(D_PSYMBOL UTFException) to be thrown.
|
||||||
|
*
|
||||||
|
* Internally $(D_PSYMBOL String) is represented by a sequence of
|
||||||
|
* $(D_KEYWORD char)s.
|
||||||
*
|
*
|
||||||
* Copyright: Eugene Wissner 2017.
|
* Copyright: Eugene Wissner 2017.
|
||||||
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
||||||
* Mozilla Public License, v. 2.0).
|
* Mozilla Public License, v. 2.0).
|
||||||
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
||||||
|
* Source: $(LINK2 https://github.com/caraus-ecms/tanya/blob/master/source/tanya/container/string.d,
|
||||||
|
* tanya/container/string.d)
|
||||||
*/
|
*/
|
||||||
module tanya.container.string;
|
module tanya.container.string;
|
||||||
|
|
||||||
@ -16,31 +30,12 @@ import core.exception;
|
|||||||
import std.algorithm.comparison;
|
import std.algorithm.comparison;
|
||||||
import std.algorithm.mutation;
|
import std.algorithm.mutation;
|
||||||
import std.algorithm.searching;
|
import std.algorithm.searching;
|
||||||
import std.range;
|
import std.range : isInfinite, isInputRange, ElementEncodingType, hasLength,
|
||||||
import std.traits;
|
popFrontN, empty;
|
||||||
import tanya.memory;
|
import tanya.memory;
|
||||||
|
import tanya.meta.trait;
|
||||||
private ref const(wchar) front(const wchar[] str)
|
import tanya.meta.transform;
|
||||||
pure nothrow @safe @nogc
|
import tanya.range.array;
|
||||||
in
|
|
||||||
{
|
|
||||||
assert(str.length > 0);
|
|
||||||
}
|
|
||||||
body
|
|
||||||
{
|
|
||||||
return str[0];
|
|
||||||
}
|
|
||||||
|
|
||||||
private void popFront(ref const(wchar)[] str, const size_t s = 1)
|
|
||||||
pure nothrow @safe @nogc
|
|
||||||
in
|
|
||||||
{
|
|
||||||
assert(str.length >= s);
|
|
||||||
}
|
|
||||||
body
|
|
||||||
{
|
|
||||||
str = str[s .. $];
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Thrown on encoding errors.
|
* Thrown on encoding errors.
|
||||||
@ -70,7 +65,7 @@ class UTFException : Exception
|
|||||||
* E = Element type ($(D_KEYWORD char) or $(D_INLINECODE const(char))).
|
* E = Element type ($(D_KEYWORD char) or $(D_INLINECODE const(char))).
|
||||||
*/
|
*/
|
||||||
struct ByCodeUnit(E)
|
struct ByCodeUnit(E)
|
||||||
if (is(Unqual!E == char))
|
if (is(Unqual!E == char))
|
||||||
{
|
{
|
||||||
private E* begin, end;
|
private E* begin, end;
|
||||||
private alias ContainerType = CopyConstness!(E, String);
|
private alias ContainerType = CopyConstness!(E, String);
|
||||||
@ -212,7 +207,7 @@ struct ByCodeUnit(E)
|
|||||||
* E = Element type ($(D_KEYWORD char) or $(D_INLINECODE const(char))).
|
* E = Element type ($(D_KEYWORD char) or $(D_INLINECODE const(char))).
|
||||||
*/
|
*/
|
||||||
struct ByCodePoint(E)
|
struct ByCodePoint(E)
|
||||||
if (is(Unqual!E == char))
|
if (is(Unqual!E == char))
|
||||||
{
|
{
|
||||||
private E* begin, end;
|
private E* begin, end;
|
||||||
private alias ContainerType = CopyConstness!(E, String);
|
private alias ContainerType = CopyConstness!(E, String);
|
||||||
@ -358,9 +353,9 @@ struct String
|
|||||||
* Precondition: $(D_INLINECODE allocator is null).
|
* Precondition: $(D_INLINECODE allocator is null).
|
||||||
*/
|
*/
|
||||||
this(S)(const S str, shared Allocator allocator = defaultAllocator)
|
this(S)(const S str, shared Allocator allocator = defaultAllocator)
|
||||||
if (!isInfinite!S
|
if (!isInfinite!S
|
||||||
&& isInputRange!S
|
&& isInputRange!S
|
||||||
&& isSomeChar!(ElementEncodingType!S))
|
&& isSomeChar!(ElementEncodingType!S))
|
||||||
{
|
{
|
||||||
this(allocator);
|
this(allocator);
|
||||||
insertBack(str);
|
insertBack(str);
|
||||||
@ -400,7 +395,7 @@ struct String
|
|||||||
*/
|
*/
|
||||||
this(S)(S init, shared Allocator allocator = defaultAllocator)
|
this(S)(S init, shared Allocator allocator = defaultAllocator)
|
||||||
nothrow @trusted @nogc
|
nothrow @trusted @nogc
|
||||||
if (is(S == String))
|
if (is(S == String))
|
||||||
{
|
{
|
||||||
this(allocator);
|
this(allocator);
|
||||||
if (allocator !is init.allocator)
|
if (allocator !is init.allocator)
|
||||||
@ -425,7 +420,7 @@ struct String
|
|||||||
/// Ditto.
|
/// Ditto.
|
||||||
this(S)(ref S init, shared Allocator allocator = defaultAllocator)
|
this(S)(ref S init, shared Allocator allocator = defaultAllocator)
|
||||||
nothrow @trusted @nogc
|
nothrow @trusted @nogc
|
||||||
if (is(Unqual!S == String))
|
if (is(Unqual!S == String))
|
||||||
{
|
{
|
||||||
this(allocator);
|
this(allocator);
|
||||||
reserve(init.length);
|
reserve(init.length);
|
||||||
@ -456,7 +451,7 @@ struct String
|
|||||||
this(C)(const size_t n,
|
this(C)(const size_t n,
|
||||||
const C chr,
|
const C chr,
|
||||||
shared Allocator allocator = defaultAllocator) @trusted
|
shared Allocator allocator = defaultAllocator) @trusted
|
||||||
if (isSomeChar!C)
|
if (isSomeChar!C)
|
||||||
{
|
{
|
||||||
this(allocator);
|
this(allocator);
|
||||||
if (n == 0)
|
if (n == 0)
|
||||||
@ -534,10 +529,10 @@ struct String
|
|||||||
}
|
}
|
||||||
|
|
||||||
private size_t insertWideChar(C)(auto ref const C chr) @trusted
|
private size_t insertWideChar(C)(auto ref const C chr) @trusted
|
||||||
if (is(C == wchar) || is(C == dchar))
|
if (is(C == wchar) || is(C == dchar))
|
||||||
in
|
in
|
||||||
{
|
{
|
||||||
assert(capacity - length >= C.sizeof);
|
assert(capacity - length >= 3);
|
||||||
}
|
}
|
||||||
body
|
body
|
||||||
{
|
{
|
||||||
@ -555,7 +550,7 @@ struct String
|
|||||||
this.length_ += 2;
|
this.length_ += 2;
|
||||||
return 2;
|
return 2;
|
||||||
}
|
}
|
||||||
else if (chr < 0xd800 || chr - 0xe000 < 0x2000)
|
else if (chr < 0xd800 || (chr >= 0xe000 && chr <= 0xffff))
|
||||||
{
|
{
|
||||||
*dst++ = 0xe0 | (chr >> 12) & 0xff;
|
*dst++ = 0xe0 | (chr >> 12) & 0xff;
|
||||||
*dst++ = 0x80 | ((chr >> 6) & 0x3f);
|
*dst++ = 0x80 | ((chr >> 6) & 0x3f);
|
||||||
@ -593,9 +588,9 @@ struct String
|
|||||||
/// Ditto.
|
/// Ditto.
|
||||||
size_t insertBack(const wchar chr) @trusted @nogc
|
size_t insertBack(const wchar chr) @trusted @nogc
|
||||||
{
|
{
|
||||||
reserve(length + wchar.sizeof);
|
reserve(length + 3);
|
||||||
|
|
||||||
auto ret = insertWideChar(chr);
|
const ret = insertWideChar(chr);
|
||||||
if (ret == 0)
|
if (ret == 0)
|
||||||
{
|
{
|
||||||
throw defaultAllocator.make!UTFException("Invalid UTF-16 sequeunce");
|
throw defaultAllocator.make!UTFException("Invalid UTF-16 sequeunce");
|
||||||
@ -603,12 +598,34 @@ struct String
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Allocates enough space for 3-byte character.
|
||||||
|
private @safe @nogc unittest
|
||||||
|
{
|
||||||
|
String s;
|
||||||
|
s.insertBack('\u8100');
|
||||||
|
}
|
||||||
|
|
||||||
|
private @safe @nogc unittest
|
||||||
|
{
|
||||||
|
UTFException exception;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
auto s = String(1, cast(wchar) 0xd900);
|
||||||
|
}
|
||||||
|
catch (UTFException e)
|
||||||
|
{
|
||||||
|
exception = e;
|
||||||
|
}
|
||||||
|
assert(exception !is null);
|
||||||
|
defaultAllocator.dispose(exception);
|
||||||
|
}
|
||||||
|
|
||||||
/// Ditto.
|
/// Ditto.
|
||||||
size_t insertBack(const dchar chr) @trusted @nogc
|
size_t insertBack(const dchar chr) @trusted @nogc
|
||||||
{
|
{
|
||||||
reserve(length + dchar.sizeof);
|
reserve(length + dchar.sizeof);
|
||||||
|
|
||||||
auto ret = insertWideChar(chr);
|
const ret = insertWideChar(chr);
|
||||||
if (ret > 0)
|
if (ret > 0)
|
||||||
{
|
{
|
||||||
return ret;
|
return ret;
|
||||||
@ -624,6 +641,21 @@ struct String
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private @safe @nogc unittest
|
||||||
|
{
|
||||||
|
UTFException exception;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
auto s = String(1, cast(dchar) 0xd900);
|
||||||
|
}
|
||||||
|
catch (UTFException e)
|
||||||
|
{
|
||||||
|
exception = e;
|
||||||
|
}
|
||||||
|
assert(exception !is null);
|
||||||
|
defaultAllocator.dispose(exception);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Inserts a stringish range at the end of the string.
|
* Inserts a stringish range at the end of the string.
|
||||||
*
|
*
|
||||||
@ -636,9 +668,9 @@ struct String
|
|||||||
* Throws: $(D_PSYMBOL UTFException).
|
* Throws: $(D_PSYMBOL UTFException).
|
||||||
*/
|
*/
|
||||||
size_t insertBack(R)(R str) @trusted
|
size_t insertBack(R)(R str) @trusted
|
||||||
if (!isInfinite!R
|
if (!isInfinite!R
|
||||||
&& isInputRange!R
|
&& isInputRange!R
|
||||||
&& is(Unqual!(ElementEncodingType!R) == char))
|
&& is(Unqual!(ElementEncodingType!R) == char))
|
||||||
{
|
{
|
||||||
size_t size;
|
size_t size;
|
||||||
static if (hasLength!R || isNarrowString!R)
|
static if (hasLength!R || isNarrowString!R)
|
||||||
@ -700,9 +732,9 @@ struct String
|
|||||||
|
|
||||||
/// Ditto.
|
/// Ditto.
|
||||||
size_t insertBack(R)(R str) @trusted
|
size_t insertBack(R)(R str) @trusted
|
||||||
if (!isInfinite!R
|
if (!isInfinite!R
|
||||||
&& isInputRange!R
|
&& isInputRange!R
|
||||||
&& is(Unqual!(ElementEncodingType!R) == wchar))
|
&& is(Unqual!(ElementEncodingType!R) == wchar))
|
||||||
{
|
{
|
||||||
static if (hasLength!R || isNarrowString!R)
|
static if (hasLength!R || isNarrowString!R)
|
||||||
{
|
{
|
||||||
@ -739,7 +771,7 @@ struct String
|
|||||||
}
|
}
|
||||||
dchar d = (range[0] - 0xd800) | ((range[1] - 0xdc00) >> 10);
|
dchar d = (range[0] - 0xd800) | ((range[1] - 0xdc00) >> 10);
|
||||||
|
|
||||||
range.popFront(2);
|
range.popFrontN(2);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@ -766,9 +798,9 @@ struct String
|
|||||||
|
|
||||||
/// Ditto.
|
/// Ditto.
|
||||||
size_t insertBack(R)(R str) @trusted
|
size_t insertBack(R)(R str) @trusted
|
||||||
if (!isInfinite!R
|
if (!isInfinite!R
|
||||||
&& isInputRange!R
|
&& isInputRange!R
|
||||||
&& is(Unqual!(ElementEncodingType!R) == dchar))
|
&& is(Unqual!(ElementEncodingType!R) == dchar))
|
||||||
{
|
{
|
||||||
static if (hasLength!R || isSomeString!R)
|
static if (hasLength!R || isSomeString!R)
|
||||||
{
|
{
|
||||||
@ -884,8 +916,8 @@ struct String
|
|||||||
* Slicing assignment.
|
* Slicing assignment.
|
||||||
*
|
*
|
||||||
* Params:
|
* Params:
|
||||||
* R = Type of the assigned slice.
|
* R = $(D_KEYWORD char).
|
||||||
* value = New value (single value, range or string).
|
* value = Assigned character, range or string.
|
||||||
* i = Slice start.
|
* i = Slice start.
|
||||||
* j = Slice end.
|
* j = Slice end.
|
||||||
*
|
*
|
||||||
@ -897,7 +929,7 @@ struct String
|
|||||||
ByCodeUnit!char opSliceAssign(R)(ByCodeUnit!R value,
|
ByCodeUnit!char opSliceAssign(R)(ByCodeUnit!R value,
|
||||||
const size_t i,
|
const size_t i,
|
||||||
const size_t j) @trusted
|
const size_t j) @trusted
|
||||||
if (is(Unqual!R == char))
|
if (is(Unqual!R == char))
|
||||||
in
|
in
|
||||||
{
|
{
|
||||||
assert(i <= j);
|
assert(i <= j);
|
||||||
@ -906,8 +938,9 @@ struct String
|
|||||||
}
|
}
|
||||||
body
|
body
|
||||||
{
|
{
|
||||||
copy(value, this.data[i .. j]);
|
auto target = opSlice(i, j);
|
||||||
return opSlice(i, j);
|
copy(value, target);
|
||||||
|
return target;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Ditto.
|
/// Ditto.
|
||||||
@ -1198,7 +1231,7 @@ struct String
|
|||||||
* Returns: $(D_KEYWORD this).
|
* Returns: $(D_KEYWORD this).
|
||||||
*/
|
*/
|
||||||
ref String opAssign(S)(S that)
|
ref String opAssign(S)(S that)
|
||||||
if (is(S == String))
|
if (is(S == String))
|
||||||
{
|
{
|
||||||
swap(this.data, that.data);
|
swap(this.data, that.data);
|
||||||
swap(this.length_, that.length_);
|
swap(this.length_, that.length_);
|
||||||
@ -1209,7 +1242,7 @@ struct String
|
|||||||
|
|
||||||
/// Ditto.
|
/// Ditto.
|
||||||
ref String opAssign(S)(ref S that) @trusted
|
ref String opAssign(S)(ref S that) @trusted
|
||||||
if (is(Unqual!S == String))
|
if (is(Unqual!S == String))
|
||||||
{
|
{
|
||||||
reserve(that.length);
|
reserve(that.length);
|
||||||
that.data[0 .. that.length].copy(this.data[0 .. that.length]);
|
that.data[0 .. that.length].copy(this.data[0 .. that.length]);
|
||||||
@ -1236,9 +1269,9 @@ struct String
|
|||||||
* Throws: $(D_PSYMBOL UTFException).
|
* Throws: $(D_PSYMBOL UTFException).
|
||||||
*/
|
*/
|
||||||
ref String opAssign(S)(S that) nothrow
|
ref String opAssign(S)(S that) nothrow
|
||||||
if (!isInfinite!S
|
if (!isInfinite!S
|
||||||
&& isInputRange!S
|
&& isInputRange!S
|
||||||
&& isSomeChar!(ElementEncodingType!S))
|
&& isSomeChar!(ElementEncodingType!S))
|
||||||
{
|
{
|
||||||
this.length_ = 0;
|
this.length_ = 0;
|
||||||
insertBack(that);
|
insertBack(that);
|
||||||
@ -1263,14 +1296,14 @@ struct String
|
|||||||
* greater than $(D_PARAM that), if equal `0`, else `-1`.
|
* greater than $(D_PARAM that), if equal `0`, else `-1`.
|
||||||
*/
|
*/
|
||||||
int opCmp(S)(auto ref S that) const @trusted
|
int opCmp(S)(auto ref S that) const @trusted
|
||||||
if (is(Unqual!S == String))
|
if (is(Unqual!S == String))
|
||||||
{
|
{
|
||||||
return cmp(this.data[0 .. length], that.data[0 .. that.length]);
|
return cmp(this.data[0 .. length], that.data[0 .. that.length]);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Ditto.
|
/// Ditto.
|
||||||
int opCmp(S)(ByCodeUnit!S that) const @trusted
|
int opCmp(S)(ByCodeUnit!S that) const @trusted
|
||||||
if (is(Unqual!S == char))
|
if (is(Unqual!S == char))
|
||||||
{
|
{
|
||||||
return cmp(this.data[0 .. length],
|
return cmp(this.data[0 .. length],
|
||||||
that.begin[0 .. that.end - that.begin]);
|
that.begin[0 .. that.end - that.begin]);
|
||||||
@ -1278,7 +1311,7 @@ struct String
|
|||||||
|
|
||||||
/// Ditto.
|
/// Ditto.
|
||||||
int opCmp(S)(ByCodePoint!S that) const @trusted
|
int opCmp(S)(ByCodePoint!S that) const @trusted
|
||||||
if (is(Unqual!S == char))
|
if (is(Unqual!S == char))
|
||||||
{
|
{
|
||||||
return cmp(this.data[0 .. length],
|
return cmp(this.data[0 .. length],
|
||||||
that.begin[0 .. that.end - that.begin]);
|
that.begin[0 .. that.end - that.begin]);
|
||||||
@ -1308,7 +1341,7 @@ struct String
|
|||||||
* otherwise.
|
* otherwise.
|
||||||
*/
|
*/
|
||||||
bool opEquals(S)(auto ref S that) const @trusted
|
bool opEquals(S)(auto ref S that) const @trusted
|
||||||
if (is(Unqual!S == String))
|
if (is(Unqual!S == String))
|
||||||
{
|
{
|
||||||
return equal(this.data[0 .. length], that.data[0 .. that.length]);
|
return equal(this.data[0 .. length], that.data[0 .. that.length]);
|
||||||
}
|
}
|
||||||
@ -1323,7 +1356,7 @@ struct String
|
|||||||
* $(D_KEYWORD false) otherwise.
|
* $(D_KEYWORD false) otherwise.
|
||||||
*/
|
*/
|
||||||
bool opEquals(S)(ByCodeUnit!S that) const @trusted
|
bool opEquals(S)(ByCodeUnit!S that) const @trusted
|
||||||
if (is(Unqual!S == char))
|
if (is(Unqual!S == char))
|
||||||
{
|
{
|
||||||
return equal(this.data[0 .. length],
|
return equal(this.data[0 .. length],
|
||||||
that.begin[0 .. that.end - that.begin]);
|
that.begin[0 .. that.end - that.begin]);
|
||||||
@ -1331,7 +1364,7 @@ struct String
|
|||||||
|
|
||||||
/// Ditto.
|
/// Ditto.
|
||||||
bool opEquals(S)(ByCodePoint!S that) const @trusted
|
bool opEquals(S)(ByCodePoint!S that) const @trusted
|
||||||
if (is(Unqual!S == char))
|
if (is(Unqual!S == char))
|
||||||
{
|
{
|
||||||
return equal(this.data[0 .. length],
|
return equal(this.data[0 .. length],
|
||||||
that.begin[0 .. that.end - that.begin]);
|
that.begin[0 .. that.end - that.begin]);
|
||||||
@ -1386,34 +1419,56 @@ struct String
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Assigns a range or a string.
|
* Slicing assignment.
|
||||||
*
|
*
|
||||||
* Params:
|
* Params:
|
||||||
* R = Value type.
|
* R = $(D_KEYWORD char).
|
||||||
* value = Value.
|
* value = Assigned character, range or string.
|
||||||
*
|
*
|
||||||
* Returns: Assigned value.
|
* Returns: Range over the string.
|
||||||
*
|
*
|
||||||
* Precondition: $(D_INLINECODE length == value.length).
|
* Precondition: $(D_INLINECODE length == value.length).
|
||||||
*/
|
*/
|
||||||
ByCodeUnit!char opIndexAssign(R)(ByCodeUnit!R value)
|
ByCodeUnit!char opIndexAssign(R)(ByCodeUnit!R value)
|
||||||
if (is(Unqual!R == char))
|
if (is(Unqual!R == char))
|
||||||
{
|
{
|
||||||
return opSliceAssign(value, 0, length);
|
return opSliceAssign(value, 0, length);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private unittest
|
||||||
|
{
|
||||||
|
auto s1 = String("Buttercup");
|
||||||
|
auto s2 = String("Cap");
|
||||||
|
s2[] = s1[6 .. $];
|
||||||
|
assert(s2 == "cup");
|
||||||
|
}
|
||||||
|
|
||||||
/// Ditto.
|
/// Ditto.
|
||||||
ByCodeUnit!char opIndexAssign(const char value) pure nothrow @safe @nogc
|
ByCodeUnit!char opIndexAssign(const char value) pure nothrow @safe @nogc
|
||||||
{
|
{
|
||||||
return opSliceAssign(value, 0, length);
|
return opSliceAssign(value, 0, length);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private unittest
|
||||||
|
{
|
||||||
|
auto s1 = String("Wow");
|
||||||
|
s1[] = 'a';
|
||||||
|
assert(s1 == "aaa");
|
||||||
|
}
|
||||||
|
|
||||||
/// Ditto.
|
/// Ditto.
|
||||||
ByCodeUnit!char opIndexAssign(const char[] value) pure nothrow @safe @nogc
|
ByCodeUnit!char opIndexAssign(const char[] value) pure nothrow @safe @nogc
|
||||||
{
|
{
|
||||||
return opSliceAssign(value, 0, length);
|
return opSliceAssign(value, 0, length);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private unittest
|
||||||
|
{
|
||||||
|
auto s1 = String("ö");
|
||||||
|
s1[] = "oe";
|
||||||
|
assert(s1 == "oe");
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Remove all characters beloning to $(D_PARAM r).
|
* Remove all characters beloning to $(D_PARAM r).
|
||||||
*
|
*
|
||||||
@ -1427,7 +1482,7 @@ struct String
|
|||||||
* Precondition: $(D_PARAM r) refers to a region of $(D_KEYWORD this).
|
* Precondition: $(D_PARAM r) refers to a region of $(D_KEYWORD this).
|
||||||
*/
|
*/
|
||||||
R remove(R)(R r) @trusted
|
R remove(R)(R r) @trusted
|
||||||
if (is(R == ByCodeUnit!char) || is(R == ByCodePoint!char))
|
if (is(R == ByCodeUnit!char) || is(R == ByCodePoint!char))
|
||||||
in
|
in
|
||||||
{
|
{
|
||||||
assert(r.container is &this);
|
assert(r.container is &this);
|
||||||
@ -1477,10 +1532,10 @@ struct String
|
|||||||
* Precondition: $(D_PARAM r) refers to a region of $(D_KEYWORD this).
|
* Precondition: $(D_PARAM r) refers to a region of $(D_KEYWORD this).
|
||||||
*/
|
*/
|
||||||
size_t insertAfter(T, R)(R r, T el) @trusted
|
size_t insertAfter(T, R)(R r, T el) @trusted
|
||||||
if ((isSomeChar!T || (!isInfinite!T
|
if ((isSomeChar!T || (!isInfinite!T
|
||||||
&& isInputRange!T
|
&& isInputRange!T
|
||||||
&& isSomeChar!(ElementEncodingType!T)))
|
&& isSomeChar!(ElementEncodingType!T)))
|
||||||
&& (is(R == ByCodeUnit!char) || is(R == ByCodePoint!char)))
|
&& (is(R == ByCodeUnit!char) || is(R == ByCodePoint!char)))
|
||||||
in
|
in
|
||||||
{
|
{
|
||||||
assert(r.container is &this);
|
assert(r.container is &this);
|
||||||
@ -1510,10 +1565,10 @@ struct String
|
|||||||
|
|
||||||
///
|
///
|
||||||
size_t insertBefore(T, R)(R r, T el) @trusted
|
size_t insertBefore(T, R)(R r, T el) @trusted
|
||||||
if ((isSomeChar!T || (!isInfinite!T
|
if ((isSomeChar!T || (!isInfinite!T
|
||||||
&& isInputRange!T
|
&& isInputRange!T
|
||||||
&& isSomeChar!(ElementEncodingType!T)))
|
&& isSomeChar!(ElementEncodingType!T)))
|
||||||
&& (is(R == ByCodeUnit!char) || is(R == ByCodePoint!char)))
|
&& (is(R == ByCodeUnit!char) || is(R == ByCodePoint!char)))
|
||||||
in
|
in
|
||||||
{
|
{
|
||||||
assert(r.container is &this);
|
assert(r.container is &this);
|
||||||
|
@ -8,12 +8,17 @@
|
|||||||
* Copyright: Eugene Wissner 2017.
|
* Copyright: Eugene Wissner 2017.
|
||||||
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
||||||
* Mozilla Public License, v. 2.0).
|
* Mozilla Public License, v. 2.0).
|
||||||
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
* Authors: Jeff Roberts, $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
||||||
|
* Source: $(LINK2 https://github.com/caraus-ecms/tanya/blob/master/source/tanya/format/conv.d,
|
||||||
|
* tanya/format/conv.d)
|
||||||
*/
|
*/
|
||||||
module tanya.format.conv;
|
module tanya.format.conv;
|
||||||
|
|
||||||
import std.traits;
|
import tanya.container.string;
|
||||||
import tanya.memory;
|
import tanya.memory;
|
||||||
|
import tanya.memory.op;
|
||||||
|
import tanya.meta.trait;
|
||||||
|
import tanya.meta.transform;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Thrown if a type conversion fails.
|
* Thrown if a type conversion fails.
|
||||||
@ -61,7 +66,7 @@ template to(To)
|
|||||||
|
|
||||||
/// Ditto.
|
/// Ditto.
|
||||||
To to(From)(From from)
|
To to(From)(From from)
|
||||||
if (is(Unqual!To == Unqual!From) || isNumeric!From && isFloatingPoint!To)
|
if (is(Unqual!To == Unqual!From) || (isNumeric!From && isFloatingPoint!To))
|
||||||
{
|
{
|
||||||
return from;
|
return from;
|
||||||
}
|
}
|
||||||
@ -273,18 +278,23 @@ private @nogc unittest
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Converts a number to a boolean.
|
* 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:
|
* Params:
|
||||||
* From = Source type.
|
* From = Source type.
|
||||||
* To = Target type.
|
* To = Target type.
|
||||||
* from = Source value.
|
* from = Source value.
|
||||||
*
|
*
|
||||||
* Returns: $(D_KEYWORD true) if $(D_INLINECODE from > 0 && from <= 1),
|
* Returns: $(D_KEYWORD from) converted to a boolean.
|
||||||
* otherwise $(D_KEYWORD false).
|
|
||||||
*
|
*
|
||||||
* Throws: $(D_PSYMBOL ConvException) if $(D_PARAM from) is greater than `1` or
|
* Throws: $(D_PSYMBOL ConvException) if $(D_PARAM from) isn't convertible.
|
||||||
* less than `0`.
|
|
||||||
*/
|
*/
|
||||||
To to(To, From)(From from)
|
To to(To, From)(From from)
|
||||||
if (isNumeric!From && is(Unqual!To == bool) && !is(Unqual!To == Unqual!From))
|
if (isNumeric!From && is(Unqual!To == bool) && !is(Unqual!To == Unqual!From))
|
||||||
@ -306,15 +316,16 @@ if (isNumeric!From && is(Unqual!To == bool) && !is(Unqual!To == Unqual!From))
|
|||||||
"Positive number overflow");
|
"Positive number overflow");
|
||||||
}
|
}
|
||||||
|
|
||||||
private @nogc unittest
|
///
|
||||||
|
@nogc unittest
|
||||||
{
|
{
|
||||||
assert(0.0.to!bool == false);
|
assert(!0.0.to!bool);
|
||||||
assert(0.2.to!bool == true);
|
assert(0.2.to!bool);
|
||||||
assert(0.5.to!bool == true);
|
assert(0.5.to!bool);
|
||||||
assert(1.0.to!bool == true);
|
assert(1.0.to!bool);
|
||||||
|
|
||||||
assert(0.to!bool == false);
|
assert(!0.to!bool);
|
||||||
assert(1.to!bool == true);
|
assert(1.to!bool);
|
||||||
}
|
}
|
||||||
|
|
||||||
private @nogc unittest
|
private @nogc unittest
|
||||||
@ -322,7 +333,7 @@ private @nogc unittest
|
|||||||
ConvException exception;
|
ConvException exception;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
assert((-1).to!bool == true);
|
assert((-1).to!bool);
|
||||||
}
|
}
|
||||||
catch (ConvException e)
|
catch (ConvException e)
|
||||||
{
|
{
|
||||||
@ -337,7 +348,48 @@ private @nogc unittest
|
|||||||
ConvException exception;
|
ConvException exception;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
assert(2.to!bool == true);
|
assert(2.to!bool);
|
||||||
|
}
|
||||||
|
catch (ConvException e)
|
||||||
|
{
|
||||||
|
exception = e;
|
||||||
|
}
|
||||||
|
assert(exception !is null);
|
||||||
|
defaultAllocator.dispose(exception);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 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 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)
|
catch (ConvException e)
|
||||||
{
|
{
|
||||||
@ -348,17 +400,22 @@ private @nogc unittest
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Converts a boolean to a number. $(D_KEYWORD true) is `1`, $(D_KEYWORD false)
|
* Converts a boolean to $(D_PARAM To).
|
||||||
* is `0`.
|
*
|
||||||
|
* 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:
|
* Params:
|
||||||
* From = Source type.
|
* From = Source type.
|
||||||
* To = Target type.
|
* To = Target type.
|
||||||
* from = Source value.
|
* from = Source value.
|
||||||
*
|
*
|
||||||
* Returns: `1` if $(D_PARAM from) is $(D_KEYWORD true), otherwise `0`.
|
* Returns: $(D_PARAM from) converted to $(D_PARAM To).
|
||||||
*/
|
*/
|
||||||
To to(To, From)(From from)
|
To to(To, From)(const From from)
|
||||||
if (is(Unqual!From == bool) && isNumeric!To && !is(Unqual!To == Unqual!From))
|
if (is(Unqual!From == bool) && isNumeric!To && !is(Unqual!To == Unqual!From))
|
||||||
{
|
{
|
||||||
return from;
|
return from;
|
||||||
@ -386,6 +443,26 @@ pure nothrow @safe @nogc unittest
|
|||||||
assert(false.to!int == 0);
|
assert(false.to!int == 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Ditto.
|
||||||
|
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.
|
* Converts a floating point number to an integral type.
|
||||||
*
|
*
|
||||||
@ -486,7 +563,6 @@ private @nogc unittest
|
|||||||
*
|
*
|
||||||
* Throws: $(D_PSYMBOL ConvException) if $(D_PARAM from) is not a member of
|
* Throws: $(D_PSYMBOL ConvException) if $(D_PARAM from) is not a member of
|
||||||
* $(D_PSYMBOL To).
|
* $(D_PSYMBOL To).
|
||||||
|
|
||||||
*/
|
*/
|
||||||
To to(To, From)(From from)
|
To to(To, From)(From from)
|
||||||
if (isIntegral!From && is(To == enum))
|
if (isIntegral!From && is(To == enum))
|
||||||
@ -535,3 +611,81 @@ private @nogc unittest
|
|||||||
assert(exception !is null);
|
assert(exception !is null);
|
||||||
defaultAllocator.dispose(exception);
|
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");
|
||||||
|
}
|
||||||
|
File diff suppressed because it is too large
Load Diff
@ -9,15 +9,18 @@
|
|||||||
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
||||||
* Mozilla Public License, v. 2.0).
|
* Mozilla Public License, v. 2.0).
|
||||||
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
||||||
|
* Source: $(LINK2 https://github.com/caraus-ecms/tanya/blob/master/source/tanya/math/mp.d,
|
||||||
|
* tanya/math/mp.d)
|
||||||
*/
|
*/
|
||||||
module tanya.math.mp;
|
module tanya.math.mp;
|
||||||
|
|
||||||
import std.algorithm;
|
import std.algorithm;
|
||||||
import std.ascii;
|
import std.ascii;
|
||||||
import std.range;
|
import std.range;
|
||||||
import std.traits;
|
|
||||||
import tanya.container.array;
|
import tanya.container.array;
|
||||||
import tanya.memory;
|
import tanya.memory;
|
||||||
|
import tanya.meta.trait;
|
||||||
|
import tanya.meta.transform;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Algebraic sign.
|
* Algebraic sign.
|
||||||
|
@ -9,12 +9,14 @@
|
|||||||
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
||||||
* Mozilla Public License, v. 2.0).
|
* Mozilla Public License, v. 2.0).
|
||||||
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
||||||
|
* Source: $(LINK2 https://github.com/caraus-ecms/tanya/blob/master/source/tanya/math/package.d,
|
||||||
|
* tanya/math/package.d)
|
||||||
*/
|
*/
|
||||||
module tanya.math;
|
module tanya.math;
|
||||||
|
|
||||||
import std.traits;
|
|
||||||
public import tanya.math.mp;
|
public import tanya.math.mp;
|
||||||
public import tanya.math.random;
|
public import tanya.math.random;
|
||||||
|
import tanya.meta.trait;
|
||||||
|
|
||||||
version (unittest)
|
version (unittest)
|
||||||
{
|
{
|
||||||
|
@ -9,6 +9,8 @@
|
|||||||
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
||||||
* Mozilla Public License, v. 2.0).
|
* Mozilla Public License, v. 2.0).
|
||||||
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
||||||
|
* Source: $(LINK2 https://github.com/caraus-ecms/tanya/blob/master/source/tanya/math/random.d,
|
||||||
|
* tanya/math/random.d)
|
||||||
*/
|
*/
|
||||||
module tanya.math.random;
|
module tanya.math.random;
|
||||||
|
|
||||||
|
@ -12,6 +12,8 @@
|
|||||||
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
||||||
* Mozilla Public License, v. 2.0).
|
* Mozilla Public License, v. 2.0).
|
||||||
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
||||||
|
* Source: $(LINK2 https://github.com/caraus-ecms/tanya/blob/master/source/tanya/memory/allocator.d,
|
||||||
|
* tanya/memory/allocator.d)
|
||||||
*/
|
*/
|
||||||
module tanya.memory.allocator;
|
module tanya.memory.allocator;
|
||||||
|
|
||||||
@ -33,7 +35,7 @@ interface Allocator
|
|||||||
*
|
*
|
||||||
* Returns: Pointer to the new allocated memory.
|
* Returns: Pointer to the new allocated memory.
|
||||||
*/
|
*/
|
||||||
void[] allocate(const size_t size) shared nothrow @nogc;
|
void[] allocate(const size_t size) shared pure nothrow @nogc;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Deallocates a memory block.
|
* Deallocates a memory block.
|
||||||
@ -43,7 +45,7 @@ interface Allocator
|
|||||||
*
|
*
|
||||||
* Returns: Whether the deallocation was successful.
|
* Returns: Whether the deallocation was successful.
|
||||||
*/
|
*/
|
||||||
bool deallocate(void[] p) shared nothrow @nogc;
|
bool deallocate(void[] p) shared pure nothrow @nogc;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Increases or decreases the size of a memory block.
|
* Increases or decreases the size of a memory block.
|
||||||
@ -54,7 +56,7 @@ interface Allocator
|
|||||||
*
|
*
|
||||||
* Returns: Pointer to the allocated memory.
|
* Returns: Pointer to the allocated memory.
|
||||||
*/
|
*/
|
||||||
bool reallocate(ref void[] p, const size_t size) shared nothrow @nogc;
|
bool reallocate(ref void[] p, const size_t size) shared pure nothrow @nogc;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Reallocates a memory block in place if possible or returns
|
* Reallocates a memory block in place if possible or returns
|
||||||
@ -69,5 +71,11 @@ interface Allocator
|
|||||||
* Returns: $(D_KEYWORD true) if successful, $(D_KEYWORD false) otherwise.
|
* Returns: $(D_KEYWORD true) if successful, $(D_KEYWORD false) otherwise.
|
||||||
*/
|
*/
|
||||||
bool reallocateInPlace(ref void[] p, const size_t size)
|
bool reallocateInPlace(ref void[] p, const size_t size)
|
||||||
shared nothrow @nogc;
|
shared pure nothrow @nogc;
|
||||||
|
}
|
||||||
|
|
||||||
|
package template GetPureInstance(T : Allocator)
|
||||||
|
{
|
||||||
|
alias GetPureInstance = shared(T) function()
|
||||||
|
pure nothrow @nogc;
|
||||||
}
|
}
|
||||||
|
441
source/tanya/memory/arch/x86_64.d
Normal file
441
source/tanya/memory/arch/x86_64.d
Normal file
@ -0,0 +1,441 @@
|
|||||||
|
/* 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/. */
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Implementions of functions found in $(D_PSYMBOL tanya.memory.op) for x64.
|
||||||
|
*
|
||||||
|
* 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/memory/arch/x86_64.d,
|
||||||
|
* tanya/memory/arch/x86_64.d)
|
||||||
|
*/
|
||||||
|
module tanya.memory.arch.x86_64;
|
||||||
|
|
||||||
|
import tanya.memory.op;
|
||||||
|
|
||||||
|
version (D_InlineAsm_X86_64):
|
||||||
|
|
||||||
|
pragma(inline, true)
|
||||||
|
package (tanya.memory) void copy(const void[] source, void[] target)
|
||||||
|
pure nothrow @system @nogc
|
||||||
|
{
|
||||||
|
asm pure nothrow @nogc
|
||||||
|
{
|
||||||
|
naked;
|
||||||
|
|
||||||
|
// RDI and RSI should be preserved.
|
||||||
|
mov RAX, RDI;
|
||||||
|
mov R8, RSI;
|
||||||
|
}
|
||||||
|
// Set the registers for movsb/movsq.
|
||||||
|
version (Windows) asm pure nothrow @nogc
|
||||||
|
{
|
||||||
|
// RDX - source.
|
||||||
|
// RCX - target.
|
||||||
|
|
||||||
|
mov RDI, [ RCX + 8 ];
|
||||||
|
mov RSI, [ RDX + 8 ];
|
||||||
|
mov RDX, [ RDX ];
|
||||||
|
}
|
||||||
|
else asm pure nothrow @nogc
|
||||||
|
{
|
||||||
|
// RDX - source length.
|
||||||
|
// RCX - source data.
|
||||||
|
// RDI - target length
|
||||||
|
// RSI - target data.
|
||||||
|
|
||||||
|
mov RDI, RSI;
|
||||||
|
mov RSI, RCX;
|
||||||
|
}
|
||||||
|
asm pure nothrow @nogc
|
||||||
|
{
|
||||||
|
cmp RDX, 0x08;
|
||||||
|
jc aligned_1;
|
||||||
|
test EDI, 0x07;
|
||||||
|
jz aligned_8;
|
||||||
|
|
||||||
|
naligned:
|
||||||
|
movsb;
|
||||||
|
dec RDX;
|
||||||
|
test EDI, 0x07;
|
||||||
|
jnz naligned;
|
||||||
|
|
||||||
|
aligned_8:
|
||||||
|
mov RCX, RDX;
|
||||||
|
shr RCX, 0x03;
|
||||||
|
rep;
|
||||||
|
movsq;
|
||||||
|
and EDX, 0x07;
|
||||||
|
jz end;
|
||||||
|
|
||||||
|
aligned_1:
|
||||||
|
// Write the remaining bytes.
|
||||||
|
mov RCX, RDX;
|
||||||
|
rep;
|
||||||
|
movsb;
|
||||||
|
|
||||||
|
end: // Restore registers.
|
||||||
|
mov RSI, R8;
|
||||||
|
mov RDI, RAX;
|
||||||
|
|
||||||
|
ret;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
package (tanya.memory) template fill(ubyte Byte)
|
||||||
|
{
|
||||||
|
private enum const(char[]) MovArrayPointer(string Destination)()
|
||||||
|
{
|
||||||
|
string asmCode = "asm pure nothrow @nogc { mov ";
|
||||||
|
version (Windows)
|
||||||
|
{
|
||||||
|
asmCode ~= Destination ~ ", [ RCX + 8 ];";
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
asmCode ~= Destination ~ ", RSI;";
|
||||||
|
}
|
||||||
|
return asmCode ~ "}";
|
||||||
|
}
|
||||||
|
|
||||||
|
pragma(inline, true)
|
||||||
|
void fill(void[] memory)
|
||||||
|
{
|
||||||
|
asm pure nothrow @nogc
|
||||||
|
{
|
||||||
|
naked;
|
||||||
|
}
|
||||||
|
version (Windows) asm pure nothrow @nogc
|
||||||
|
{
|
||||||
|
/*
|
||||||
|
* RCX - array.
|
||||||
|
*/
|
||||||
|
mov R8, [ RCX ];
|
||||||
|
}
|
||||||
|
else asm pure nothrow @nogc
|
||||||
|
{
|
||||||
|
/*
|
||||||
|
* RSI - pointer.
|
||||||
|
* RDI - length.
|
||||||
|
*/
|
||||||
|
mov R8, RDI;
|
||||||
|
}
|
||||||
|
mixin(MovArrayPointer!"R9");
|
||||||
|
|
||||||
|
asm pure nothrow @nogc
|
||||||
|
{
|
||||||
|
// Check for zero length.
|
||||||
|
test R8, R8;
|
||||||
|
jz end;
|
||||||
|
}
|
||||||
|
// Set 128- and 64-bit registers to values we want to fill with.
|
||||||
|
static if (Byte == 0)
|
||||||
|
{
|
||||||
|
asm pure nothrow @nogc
|
||||||
|
{
|
||||||
|
xor RAX, RAX;
|
||||||
|
pxor XMM0, XMM0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
enum ulong FilledBytes = FilledBytes!Byte;
|
||||||
|
asm pure nothrow @nogc
|
||||||
|
{
|
||||||
|
mov RAX, FilledBytes;
|
||||||
|
movq XMM0, RAX;
|
||||||
|
movlhps XMM0, XMM0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
asm pure nothrow @nogc
|
||||||
|
{
|
||||||
|
// Check if the pointer is aligned to a 16-byte boundary.
|
||||||
|
and R9, -0x10;
|
||||||
|
}
|
||||||
|
// Compute the number of misaligned bytes.
|
||||||
|
mixin(MovArrayPointer!"R10");
|
||||||
|
asm pure nothrow @nogc
|
||||||
|
{
|
||||||
|
sub R10, R9;
|
||||||
|
|
||||||
|
test R10, R10;
|
||||||
|
jz aligned;
|
||||||
|
|
||||||
|
// Get the number of bytes to be written until we are aligned.
|
||||||
|
mov RDX, 0x10;
|
||||||
|
sub RDX, R10;
|
||||||
|
}
|
||||||
|
mixin(MovArrayPointer!"R9");
|
||||||
|
asm pure nothrow @nogc
|
||||||
|
{
|
||||||
|
naligned:
|
||||||
|
mov [ R9 ], AL; // Write a byte.
|
||||||
|
|
||||||
|
// Advance the pointer. Decrease the total number of bytes
|
||||||
|
// and the misaligned ones.
|
||||||
|
inc R9;
|
||||||
|
dec RDX;
|
||||||
|
dec R8;
|
||||||
|
|
||||||
|
// Checks if we are aligned.
|
||||||
|
test RDX, RDX;
|
||||||
|
jnz naligned;
|
||||||
|
|
||||||
|
aligned:
|
||||||
|
// Checks if we're done writing bytes.
|
||||||
|
test R8, R8;
|
||||||
|
jz end;
|
||||||
|
|
||||||
|
// Write 1 byte at a time.
|
||||||
|
cmp R8, 8;
|
||||||
|
jl aligned_1;
|
||||||
|
|
||||||
|
// Write 8 bytes at a time.
|
||||||
|
cmp R8, 16;
|
||||||
|
jl aligned_8;
|
||||||
|
|
||||||
|
// Write 16 bytes at a time.
|
||||||
|
cmp R8, 32;
|
||||||
|
jl aligned_16;
|
||||||
|
|
||||||
|
// Write 32 bytes at a time.
|
||||||
|
cmp R8, 64;
|
||||||
|
jl aligned_32;
|
||||||
|
|
||||||
|
aligned_64:
|
||||||
|
movdqa [ R9 ], XMM0;
|
||||||
|
movdqa [ R9 + 16 ], XMM0;
|
||||||
|
movdqa [ R9 + 32 ], XMM0;
|
||||||
|
movdqa [ R9 + 48 ], XMM0;
|
||||||
|
|
||||||
|
add R9, 64;
|
||||||
|
sub R8, 64;
|
||||||
|
|
||||||
|
cmp R8, 64;
|
||||||
|
jge aligned_64;
|
||||||
|
|
||||||
|
// Checks if we're done writing bytes.
|
||||||
|
test R8, R8;
|
||||||
|
jz end;
|
||||||
|
|
||||||
|
// Write 1 byte at a time.
|
||||||
|
cmp R8, 8;
|
||||||
|
jl aligned_1;
|
||||||
|
|
||||||
|
// Write 8 bytes at a time.
|
||||||
|
cmp R8, 16;
|
||||||
|
jl aligned_8;
|
||||||
|
|
||||||
|
// Write 16 bytes at a time.
|
||||||
|
cmp R8, 32;
|
||||||
|
jl aligned_16;
|
||||||
|
|
||||||
|
aligned_32:
|
||||||
|
movdqa [ R9 ], XMM0;
|
||||||
|
movdqa [ R9 + 16 ], XMM0;
|
||||||
|
|
||||||
|
add R9, 32;
|
||||||
|
sub R8, 32;
|
||||||
|
|
||||||
|
// Checks if we're done writing bytes.
|
||||||
|
test R8, R8;
|
||||||
|
jz end;
|
||||||
|
|
||||||
|
// Write 1 byte at a time.
|
||||||
|
cmp R8, 8;
|
||||||
|
jl aligned_1;
|
||||||
|
|
||||||
|
// Write 8 bytes at a time.
|
||||||
|
cmp R8, 16;
|
||||||
|
jl aligned_8;
|
||||||
|
|
||||||
|
aligned_16:
|
||||||
|
movdqa [ R9 ], XMM0;
|
||||||
|
|
||||||
|
add R9, 16;
|
||||||
|
sub R8, 16;
|
||||||
|
|
||||||
|
// Checks if we're done writing bytes.
|
||||||
|
test R8, R8;
|
||||||
|
jz end;
|
||||||
|
|
||||||
|
// Write 1 byte at a time.
|
||||||
|
cmp R8, 8;
|
||||||
|
jl aligned_1;
|
||||||
|
|
||||||
|
aligned_8:
|
||||||
|
mov [ R9 ], RAX;
|
||||||
|
|
||||||
|
add R9, 8;
|
||||||
|
sub R8, 8;
|
||||||
|
|
||||||
|
// Checks if we're done writing bytes.
|
||||||
|
test R8, R8;
|
||||||
|
jz end;
|
||||||
|
|
||||||
|
aligned_1:
|
||||||
|
mov [ R9 ], AL;
|
||||||
|
|
||||||
|
inc R9;
|
||||||
|
dec R8;
|
||||||
|
|
||||||
|
test R8, R8;
|
||||||
|
jnz aligned_1;
|
||||||
|
|
||||||
|
end:
|
||||||
|
ret;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pragma(inline, true)
|
||||||
|
package (tanya.memory) void copyBackward(const void[] source, void[] target)
|
||||||
|
pure nothrow @system @nogc
|
||||||
|
{
|
||||||
|
asm pure nothrow @nogc
|
||||||
|
{
|
||||||
|
naked;
|
||||||
|
|
||||||
|
// Save the registers should be restored.
|
||||||
|
mov R8, RSI;
|
||||||
|
mov R9, RDI;
|
||||||
|
}
|
||||||
|
// Prepare the registers for movsb.
|
||||||
|
version (Windows) asm pure nothrow @nogc
|
||||||
|
{
|
||||||
|
// RDX - source.
|
||||||
|
// RCX - target.
|
||||||
|
|
||||||
|
mov RAX, [ RCX + 8 ];
|
||||||
|
mov R10, [ RDX + 8 ];
|
||||||
|
mov RCX, [ RDX ];
|
||||||
|
|
||||||
|
lea RDI, [ RAX + RCX - 1 ];
|
||||||
|
lea RSI, [ R10 + RCX - 1 ];
|
||||||
|
}
|
||||||
|
else asm pure nothrow @nogc
|
||||||
|
{
|
||||||
|
// RDX - source length.
|
||||||
|
// RCX - source data.
|
||||||
|
// RDI - target length
|
||||||
|
// RSI - target data.
|
||||||
|
|
||||||
|
lea RDI, [ RSI + RDX - 1 ];
|
||||||
|
lea RSI, [ RCX + RDX - 1 ];
|
||||||
|
mov RCX, RDX;
|
||||||
|
}
|
||||||
|
asm pure nothrow @nogc
|
||||||
|
{
|
||||||
|
std; // Set the direction flag.
|
||||||
|
|
||||||
|
rep;
|
||||||
|
movsb;
|
||||||
|
|
||||||
|
cld; // Clear the direction flag.
|
||||||
|
|
||||||
|
// Restore registers.
|
||||||
|
mov RDI, R9;
|
||||||
|
mov RSI, R8;
|
||||||
|
|
||||||
|
ret;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pragma(inline, true)
|
||||||
|
package (tanya.memory) int cmp(const void[] r1, const void[] r2)
|
||||||
|
pure nothrow @system @nogc
|
||||||
|
{
|
||||||
|
asm pure nothrow @nogc
|
||||||
|
{
|
||||||
|
naked;
|
||||||
|
|
||||||
|
// RDI and RSI should be preserved.
|
||||||
|
mov R9, RDI;
|
||||||
|
mov R8, RSI;
|
||||||
|
}
|
||||||
|
// Set the registers for cmpsb/cmpsq.
|
||||||
|
version (Windows) asm pure nothrow @nogc
|
||||||
|
{
|
||||||
|
// RDX - r1.
|
||||||
|
// RCX - r2.
|
||||||
|
|
||||||
|
mov RDI, [ RCX + 8 ];
|
||||||
|
mov RSI, [ RDX + 8 ];
|
||||||
|
mov RDX, [ RDX ];
|
||||||
|
mov RCX, [ RCX ];
|
||||||
|
}
|
||||||
|
else asm pure nothrow @nogc
|
||||||
|
{
|
||||||
|
// RDX - r1 length.
|
||||||
|
// RCX - r1 data.
|
||||||
|
// RDI - r2 length
|
||||||
|
// RSI - r2 data.
|
||||||
|
|
||||||
|
mov RSI, RCX;
|
||||||
|
mov RCX, RDI;
|
||||||
|
mov RDI, R8;
|
||||||
|
}
|
||||||
|
asm pure nothrow @nogc
|
||||||
|
{
|
||||||
|
// Compare the lengths.
|
||||||
|
cmp RDX, RCX;
|
||||||
|
jl less;
|
||||||
|
jg greater;
|
||||||
|
|
||||||
|
// Check if we're aligned.
|
||||||
|
cmp RDX, 0x08;
|
||||||
|
jc aligned_1;
|
||||||
|
test EDI, 0x07;
|
||||||
|
jz aligned_8;
|
||||||
|
|
||||||
|
naligned:
|
||||||
|
cmpsb;
|
||||||
|
jl less;
|
||||||
|
jg greater;
|
||||||
|
|
||||||
|
dec RDX;
|
||||||
|
test EDI, 0x07;
|
||||||
|
jnz naligned;
|
||||||
|
|
||||||
|
aligned_8:
|
||||||
|
mov RCX, RDX;
|
||||||
|
shr RCX, 0x03;
|
||||||
|
|
||||||
|
repe;
|
||||||
|
cmpsq;
|
||||||
|
jl less;
|
||||||
|
jg greater;
|
||||||
|
|
||||||
|
and EDX, 0x07;
|
||||||
|
jz equal;
|
||||||
|
|
||||||
|
aligned_1: // Compare the remaining bytes.
|
||||||
|
mov RCX, RDX;
|
||||||
|
|
||||||
|
repe;
|
||||||
|
cmpsb;
|
||||||
|
jl less;
|
||||||
|
jg greater;
|
||||||
|
|
||||||
|
equal:
|
||||||
|
xor RAX, RAX; // Return 0.
|
||||||
|
jmp end;
|
||||||
|
|
||||||
|
greater:
|
||||||
|
mov RAX, 1;
|
||||||
|
jmp end;
|
||||||
|
|
||||||
|
less:
|
||||||
|
mov RAX, -1;
|
||||||
|
jmp end;
|
||||||
|
|
||||||
|
end: // Restore registers.
|
||||||
|
mov RSI, R8;
|
||||||
|
mov RDI, R9;
|
||||||
|
|
||||||
|
ret;
|
||||||
|
}
|
||||||
|
}
|
@ -9,9 +9,13 @@
|
|||||||
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
||||||
* Mozilla Public License, v. 2.0).
|
* Mozilla Public License, v. 2.0).
|
||||||
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
||||||
|
* Source: $(LINK2 https://github.com/caraus-ecms/tanya/blob/master/source/tanya/memory/mallocator.d,
|
||||||
|
* tanya/memory/mallocator.d)
|
||||||
*/
|
*/
|
||||||
module tanya.memory.mallocator;
|
module tanya.memory.mallocator;
|
||||||
|
|
||||||
|
version (TanyaPhobos):
|
||||||
|
|
||||||
import core.stdc.stdlib;
|
import core.stdc.stdlib;
|
||||||
import tanya.memory.allocator;
|
import tanya.memory.allocator;
|
||||||
|
|
||||||
@ -21,6 +25,13 @@ import tanya.memory.allocator;
|
|||||||
*/
|
*/
|
||||||
final class Mallocator : Allocator
|
final class Mallocator : Allocator
|
||||||
{
|
{
|
||||||
|
private alias MallocType = extern (C) void* function(size_t)
|
||||||
|
pure nothrow @system @nogc;
|
||||||
|
private alias FreeType = extern (C) void function(void*)
|
||||||
|
pure nothrow @system @nogc;
|
||||||
|
private alias ReallocType = extern (C) void* function(void*, size_t)
|
||||||
|
pure nothrow @system @nogc;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Allocates $(D_PARAM size) bytes of memory.
|
* Allocates $(D_PARAM size) bytes of memory.
|
||||||
*
|
*
|
||||||
@ -29,13 +40,13 @@ final class Mallocator : Allocator
|
|||||||
*
|
*
|
||||||
* Returns: The pointer to the new allocated memory.
|
* Returns: The pointer to the new allocated memory.
|
||||||
*/
|
*/
|
||||||
void[] allocate(const size_t size) shared nothrow @nogc
|
void[] allocate(const size_t size) shared pure nothrow @nogc
|
||||||
{
|
{
|
||||||
if (size == 0)
|
if (size == 0)
|
||||||
{
|
{
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
auto p = malloc(size + psize);
|
auto p = (cast(MallocType) &malloc)(size + psize);
|
||||||
|
|
||||||
return p is null ? null : p[psize .. psize + size];
|
return p is null ? null : p[psize .. psize + size];
|
||||||
}
|
}
|
||||||
@ -59,11 +70,11 @@ final class Mallocator : Allocator
|
|||||||
*
|
*
|
||||||
* Returns: Whether the deallocation was successful.
|
* Returns: Whether the deallocation was successful.
|
||||||
*/
|
*/
|
||||||
bool deallocate(void[] p) shared nothrow @nogc
|
bool deallocate(void[] p) shared pure nothrow @nogc
|
||||||
{
|
{
|
||||||
if (p !is null)
|
if (p !is null)
|
||||||
{
|
{
|
||||||
free(p.ptr - psize);
|
(cast(FreeType) &free)(p.ptr - psize);
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -87,7 +98,8 @@ final class Mallocator : Allocator
|
|||||||
*
|
*
|
||||||
* Returns: $(D_KEYWORD false).
|
* Returns: $(D_KEYWORD false).
|
||||||
*/
|
*/
|
||||||
bool reallocateInPlace(ref void[] p, const size_t size) shared nothrow @nogc
|
bool reallocateInPlace(ref void[] p, const size_t size)
|
||||||
|
shared pure nothrow @nogc
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -108,7 +120,7 @@ final class Mallocator : Allocator
|
|||||||
*
|
*
|
||||||
* Returns: Whether the reallocation was successful.
|
* Returns: Whether the reallocation was successful.
|
||||||
*/
|
*/
|
||||||
bool reallocate(ref void[] p, const size_t size) shared nothrow @nogc
|
bool reallocate(ref void[] p, const size_t size) shared pure nothrow @nogc
|
||||||
{
|
{
|
||||||
if (size == 0)
|
if (size == 0)
|
||||||
{
|
{
|
||||||
@ -125,7 +137,7 @@ final class Mallocator : Allocator
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
auto r = realloc(p.ptr - psize, size + psize);
|
auto r = (cast(ReallocType) &realloc)(p.ptr - psize, size + psize);
|
||||||
|
|
||||||
if (r !is null)
|
if (r !is null)
|
||||||
{
|
{
|
||||||
@ -177,12 +189,7 @@ final class Mallocator : Allocator
|
|||||||
assert(Mallocator.instance.alignment == (void*).alignof);
|
assert(Mallocator.instance.alignment == (void*).alignof);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
static private shared(Mallocator) instantiate() nothrow @nogc
|
||||||
* Static allocator instance and initializer.
|
|
||||||
*
|
|
||||||
* Returns: The global $(D_PSYMBOL Allocator) instance.
|
|
||||||
*/
|
|
||||||
static @property ref shared(Mallocator) instance() @nogc nothrow
|
|
||||||
{
|
{
|
||||||
if (instance_ is null)
|
if (instance_ is null)
|
||||||
{
|
{
|
||||||
@ -198,6 +205,16 @@ final class Mallocator : Allocator
|
|||||||
return instance_;
|
return instance_;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Static allocator instance and initializer.
|
||||||
|
*
|
||||||
|
* Returns: The global $(D_PSYMBOL Allocator) instance.
|
||||||
|
*/
|
||||||
|
static @property shared(Mallocator) instance() pure nothrow @nogc
|
||||||
|
{
|
||||||
|
return (cast(GetPureInstance!Mallocator) &instantiate)();
|
||||||
|
}
|
||||||
|
|
||||||
///
|
///
|
||||||
@nogc nothrow unittest
|
@nogc nothrow unittest
|
||||||
{
|
{
|
||||||
|
@ -9,6 +9,8 @@
|
|||||||
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
||||||
* Mozilla Public License, v. 2.0).
|
* Mozilla Public License, v. 2.0).
|
||||||
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
||||||
|
* Source: $(LINK2 https://github.com/caraus-ecms/tanya/blob/master/source/tanya/memory/mmappool.d,
|
||||||
|
* tanya/memory/mmappool.d)
|
||||||
*/
|
*/
|
||||||
module tanya.memory.mmappool;
|
module tanya.memory.mmappool;
|
||||||
|
|
||||||
@ -18,14 +20,63 @@ import tanya.memory.allocator;
|
|||||||
|
|
||||||
version (Posix)
|
version (Posix)
|
||||||
{
|
{
|
||||||
import core.stdc.errno;
|
import core.sys.posix.sys.mman : PROT_READ, PROT_WRITE, MAP_PRIVATE,
|
||||||
import core.sys.posix.sys.mman;
|
MAP_ANON, MAP_FAILED;
|
||||||
import core.sys.posix.unistd;
|
import core.sys.posix.unistd;
|
||||||
|
|
||||||
|
extern (C)
|
||||||
|
private void* mmap(void* addr,
|
||||||
|
size_t len,
|
||||||
|
int prot,
|
||||||
|
int flags,
|
||||||
|
int fd,
|
||||||
|
off_t offset) pure nothrow @system @nogc;
|
||||||
|
|
||||||
|
extern (C)
|
||||||
|
private int munmap(void* addr, size_t len) pure nothrow @system @nogc;
|
||||||
|
|
||||||
|
private void* mapMemory(const size_t len) pure nothrow @system @nogc
|
||||||
|
{
|
||||||
|
void* p = mmap(null,
|
||||||
|
len,
|
||||||
|
PROT_READ | PROT_WRITE,
|
||||||
|
MAP_PRIVATE | MAP_ANON,
|
||||||
|
-1,
|
||||||
|
0);
|
||||||
|
return p is MAP_FAILED ? null : p;
|
||||||
|
}
|
||||||
|
|
||||||
|
private bool unmapMemory(shared void* addr, const size_t len)
|
||||||
|
pure nothrow @system @nogc
|
||||||
|
{
|
||||||
|
return munmap(cast(void*) addr, len) == 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else version (Windows)
|
else version (Windows)
|
||||||
{
|
{
|
||||||
import core.sys.windows.winbase;
|
import core.sys.windows.winbase : GetSystemInfo, SYSTEM_INFO;
|
||||||
import core.sys.windows.windows;
|
|
||||||
|
extern (Windows)
|
||||||
|
private void* VirtualAlloc(void*, size_t, uint, uint)
|
||||||
|
pure nothrow @system @nogc;
|
||||||
|
|
||||||
|
extern (Windows)
|
||||||
|
private int VirtualFree(void* addr, size_t len, uint)
|
||||||
|
pure nothrow @system @nogc;
|
||||||
|
|
||||||
|
private void* mapMemory(const size_t len) pure nothrow @system @nogc
|
||||||
|
{
|
||||||
|
return VirtualAlloc(null,
|
||||||
|
len,
|
||||||
|
0x00001000, // MEM_COMMIT
|
||||||
|
0x04); // PAGE_READWRITE
|
||||||
|
}
|
||||||
|
|
||||||
|
private bool unmapMemory(shared void* addr, const size_t len)
|
||||||
|
pure nothrow @system @nogc
|
||||||
|
{
|
||||||
|
return VirtualFree(cast(void*) addr, 0, 0x8000) == 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -52,18 +103,21 @@ else version (Windows)
|
|||||||
*/
|
*/
|
||||||
final class MmapPool : Allocator
|
final class MmapPool : Allocator
|
||||||
{
|
{
|
||||||
invariant
|
version (none)
|
||||||
{
|
{
|
||||||
for (auto r = &head; *r !is null; r = &((*r).next))
|
pure nothrow @nogc invariant
|
||||||
{
|
{
|
||||||
auto block = cast(Block) (cast(void*) *r + RegionEntry.sizeof);
|
for (auto r = &head; *r !is null; r = &((*r).next))
|
||||||
do
|
|
||||||
{
|
{
|
||||||
assert(block.prev is null || block.prev.next is block);
|
auto block = cast(Block) (cast(void*) *r + RegionEntry.sizeof);
|
||||||
assert(block.next is null || block.next.prev is block);
|
do
|
||||||
assert(block.region is *r);
|
{
|
||||||
|
assert(block.prev is null || block.prev.next is block);
|
||||||
|
assert(block.next is null || block.next.prev is block);
|
||||||
|
assert(block.region is *r);
|
||||||
|
}
|
||||||
|
while ((block = block.next) !is null);
|
||||||
}
|
}
|
||||||
while ((block = block.next) !is null);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -75,13 +129,17 @@ final class MmapPool : Allocator
|
|||||||
*
|
*
|
||||||
* Returns: Pointer to the new allocated memory.
|
* Returns: Pointer to the new allocated memory.
|
||||||
*/
|
*/
|
||||||
void[] allocate(const size_t size) shared nothrow @nogc
|
void[] allocate(const size_t size) shared pure nothrow @nogc
|
||||||
{
|
{
|
||||||
if (!size)
|
if (size == 0)
|
||||||
{
|
{
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
const dataSize = addAlignment(size);
|
const dataSize = addAlignment(size);
|
||||||
|
if (dataSize < size)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
void* data = findBlock(dataSize);
|
void* data = findBlock(dataSize);
|
||||||
if (data is null)
|
if (data is null)
|
||||||
@ -103,6 +161,24 @@ final class MmapPool : Allocator
|
|||||||
assert(p.length == 0);
|
assert(p.length == 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Issue 245: https://issues.caraus.io/issues/245.
|
||||||
|
private @nogc unittest
|
||||||
|
{
|
||||||
|
// allocate() check.
|
||||||
|
size_t tooMuchMemory = size_t.max
|
||||||
|
- MmapPool.alignment_
|
||||||
|
- BlockEntry.sizeof * 2
|
||||||
|
- RegionEntry.sizeof
|
||||||
|
- MmapPool.instance.pageSize;
|
||||||
|
assert(MmapPool.instance.allocate(tooMuchMemory) is null);
|
||||||
|
|
||||||
|
assert(MmapPool.instance.allocate(size_t.max) is null);
|
||||||
|
|
||||||
|
// initializeRegion() check.
|
||||||
|
tooMuchMemory = size_t.max - MmapPool.alignment_;
|
||||||
|
assert(MmapPool.instance.allocate(tooMuchMemory) is null);
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Search for a block large enough to keep $(D_PARAM size) and split it
|
* Search for a block large enough to keep $(D_PARAM size) and split it
|
||||||
* into two blocks if the block is too large.
|
* into two blocks if the block is too large.
|
||||||
@ -112,7 +188,7 @@ final class MmapPool : Allocator
|
|||||||
*
|
*
|
||||||
* Returns: Data the block points to or $(D_KEYWORD null).
|
* Returns: Data the block points to or $(D_KEYWORD null).
|
||||||
*/
|
*/
|
||||||
private void* findBlock(const ref size_t size) shared nothrow @nogc
|
private void* findBlock(const ref size_t size) shared pure nothrow @nogc
|
||||||
{
|
{
|
||||||
Block block1;
|
Block block1;
|
||||||
RegionLoop: for (auto r = head; r !is null; r = r.next)
|
RegionLoop: for (auto r = head; r !is null; r = r.next)
|
||||||
@ -172,7 +248,7 @@ final class MmapPool : Allocator
|
|||||||
*
|
*
|
||||||
* Returns: Whether the deallocation was successful.
|
* Returns: Whether the deallocation was successful.
|
||||||
*/
|
*/
|
||||||
bool deallocate(void[] p) shared nothrow @nogc
|
bool deallocate(void[] p) shared pure nothrow @nogc
|
||||||
{
|
{
|
||||||
if (p.ptr is null)
|
if (p.ptr is null)
|
||||||
{
|
{
|
||||||
@ -194,14 +270,7 @@ final class MmapPool : Allocator
|
|||||||
{
|
{
|
||||||
block.region.next.prev = block.region.prev;
|
block.region.next.prev = block.region.prev;
|
||||||
}
|
}
|
||||||
version (Posix)
|
return unmapMemory(block.region, block.region.size);
|
||||||
{
|
|
||||||
return munmap(cast(void*) block.region, block.region.size) == 0;
|
|
||||||
}
|
|
||||||
version (Windows)
|
|
||||||
{
|
|
||||||
return VirtualFree(cast(void*) block.region, 0, MEM_RELEASE) == 0;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
// Merge blocks if neigbours are free.
|
// Merge blocks if neigbours are free.
|
||||||
if (block.next !is null && block.next.free)
|
if (block.next !is null && block.next.free)
|
||||||
@ -246,7 +315,7 @@ final class MmapPool : Allocator
|
|||||||
* Returns: $(D_KEYWORD true) if successful, $(D_KEYWORD false) otherwise.
|
* Returns: $(D_KEYWORD true) if successful, $(D_KEYWORD false) otherwise.
|
||||||
*/
|
*/
|
||||||
bool reallocateInPlace(ref void[] p, const size_t size)
|
bool reallocateInPlace(ref void[] p, const size_t size)
|
||||||
shared nothrow @nogc
|
shared pure nothrow @nogc
|
||||||
{
|
{
|
||||||
if (p is null || size == 0)
|
if (p is null || size == 0)
|
||||||
{
|
{
|
||||||
@ -262,19 +331,25 @@ final class MmapPool : Allocator
|
|||||||
|
|
||||||
if (block1.size >= size)
|
if (block1.size >= size)
|
||||||
{
|
{
|
||||||
// Enough space in the current block. Can happen because of the alignment.
|
// Enough space in the current block.
|
||||||
p = p.ptr[0 .. size];
|
p = p.ptr[0 .. size];
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
const dataSize = addAlignment(size);
|
const dataSize = addAlignment(size);
|
||||||
const delta = dataSize - addAlignment(p.length);
|
const pAlignment = addAlignment(p.length);
|
||||||
|
assert(pAlignment >= p.length, "Invalid memory chunk length");
|
||||||
|
const delta = dataSize - pAlignment;
|
||||||
|
|
||||||
if (block1.next is null
|
if (block1.next is null
|
||||||
|| !block1.next.free
|
|| !block1.next.free
|
||||||
|
|| dataSize < size
|
||||||
|| block1.next.size + BlockEntry.sizeof < delta)
|
|| block1.next.size + BlockEntry.sizeof < delta)
|
||||||
{
|
{
|
||||||
// It is the last block in the region or the next block is too small or not
|
/* * It is the last block in the region
|
||||||
// free.
|
* * The next block is too small
|
||||||
|
* * The next block isn't free
|
||||||
|
* * Requested size is too large
|
||||||
|
*/
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (block1.next.size >= delta + alignment_)
|
if (block1.next.size >= delta + alignment_)
|
||||||
@ -337,7 +412,7 @@ final class MmapPool : Allocator
|
|||||||
*
|
*
|
||||||
* Returns: Whether the reallocation was successful.
|
* Returns: Whether the reallocation was successful.
|
||||||
*/
|
*/
|
||||||
bool reallocate(ref void[] p, const size_t size) shared nothrow @nogc
|
bool reallocate(ref void[] p, const size_t size) shared pure nothrow @nogc
|
||||||
{
|
{
|
||||||
if (size == 0)
|
if (size == 0)
|
||||||
{
|
{
|
||||||
@ -398,19 +473,14 @@ final class MmapPool : Allocator
|
|||||||
MmapPool.instance.deallocate(p);
|
MmapPool.instance.deallocate(p);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
static private shared(MmapPool) instantiate() nothrow @nogc
|
||||||
* Static allocator instance and initializer.
|
|
||||||
*
|
|
||||||
* Returns: Global $(D_PSYMBOL MmapPool) instance.
|
|
||||||
*/
|
|
||||||
static @property ref shared(MmapPool) instance() nothrow @nogc
|
|
||||||
{
|
{
|
||||||
if (instance_ is null)
|
if (instance_ is null)
|
||||||
{
|
{
|
||||||
// Get system dependend page size.
|
// Get system dependend page size.
|
||||||
version (Posix)
|
version (Posix)
|
||||||
{
|
{
|
||||||
pageSize = sysconf(_SC_PAGE_SIZE);
|
size_t pageSize = sysconf(_SC_PAGE_SIZE);
|
||||||
if (pageSize < 65536)
|
if (pageSize < 65536)
|
||||||
{
|
{
|
||||||
pageSize = pageSize * 65536 / pageSize;
|
pageSize = pageSize * 65536 / pageSize;
|
||||||
@ -420,24 +490,35 @@ final class MmapPool : Allocator
|
|||||||
{
|
{
|
||||||
SYSTEM_INFO si;
|
SYSTEM_INFO si;
|
||||||
GetSystemInfo(&si);
|
GetSystemInfo(&si);
|
||||||
pageSize = si.dwPageSize;
|
size_t pageSize = si.dwPageSize;
|
||||||
}
|
}
|
||||||
|
|
||||||
const instanceSize = addAlignment(__traits(classInstanceSize,
|
const instanceSize = addAlignment(__traits(classInstanceSize,
|
||||||
MmapPool));
|
MmapPool));
|
||||||
|
|
||||||
Region head; // Will become soon our region list head
|
Region head; // Will become soon our region list head
|
||||||
void* data = initializeRegion(instanceSize, head);
|
void* data = initializeRegion(instanceSize, head, pageSize);
|
||||||
if (data !is null)
|
if (data !is null)
|
||||||
{
|
{
|
||||||
memcpy(data, typeid(MmapPool).initializer.ptr, instanceSize);
|
memcpy(data, typeid(MmapPool).initializer.ptr, instanceSize);
|
||||||
instance_ = cast(shared MmapPool) data;
|
instance_ = cast(shared MmapPool) data;
|
||||||
instance_.head = head;
|
instance_.head = head;
|
||||||
|
instance_.pageSize = pageSize;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return instance_;
|
return instance_;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Static allocator instance and initializer.
|
||||||
|
*
|
||||||
|
* Returns: Global $(D_PSYMBOL MmapPool) instance.
|
||||||
|
*/
|
||||||
|
static @property shared(MmapPool) instance() pure nothrow @nogc
|
||||||
|
{
|
||||||
|
return (cast(GetPureInstance!MmapPool) &instantiate)();
|
||||||
|
}
|
||||||
|
|
||||||
///
|
///
|
||||||
nothrow unittest
|
nothrow unittest
|
||||||
{
|
{
|
||||||
@ -453,34 +534,21 @@ final class MmapPool : Allocator
|
|||||||
*
|
*
|
||||||
* Returns: A pointer to the data.
|
* Returns: A pointer to the data.
|
||||||
*/
|
*/
|
||||||
private static void* initializeRegion(size_t size, ref Region head)
|
private static void* initializeRegion(const size_t size,
|
||||||
nothrow @nogc
|
ref Region head,
|
||||||
|
const size_t pageSize)
|
||||||
|
pure nothrow @nogc
|
||||||
{
|
{
|
||||||
const regionSize = calculateRegionSize(size);
|
const regionSize = calculateRegionSize(size, pageSize);
|
||||||
|
if (regionSize < size)
|
||||||
version (Posix)
|
|
||||||
{
|
{
|
||||||
void* p = mmap(null,
|
return null;
|
||||||
regionSize,
|
|
||||||
PROT_READ | PROT_WRITE,
|
|
||||||
MAP_PRIVATE | MAP_ANON,
|
|
||||||
-1,
|
|
||||||
0);
|
|
||||||
if (p is MAP_FAILED)
|
|
||||||
{
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else version (Windows)
|
|
||||||
|
void* p = mapMemory(regionSize);
|
||||||
|
if (p is null)
|
||||||
{
|
{
|
||||||
void* p = VirtualAlloc(null,
|
return null;
|
||||||
regionSize,
|
|
||||||
MEM_COMMIT,
|
|
||||||
PAGE_READWRITE);
|
|
||||||
if (p is null)
|
|
||||||
{
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Region region = cast(Region) p;
|
Region region = cast(Region) p;
|
||||||
@ -518,9 +586,9 @@ final class MmapPool : Allocator
|
|||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void* initializeRegion(size_t size) shared nothrow @nogc
|
private void* initializeRegion(const size_t size) shared pure nothrow @nogc
|
||||||
{
|
{
|
||||||
return initializeRegion(size, head);
|
return initializeRegion(size, this.head, this.pageSize);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -529,31 +597,24 @@ final class MmapPool : Allocator
|
|||||||
*
|
*
|
||||||
* Returns: Aligned size of $(D_PARAM x).
|
* Returns: Aligned size of $(D_PARAM x).
|
||||||
*/
|
*/
|
||||||
private static size_t addAlignment(size_t x) pure nothrow @safe @nogc
|
private static size_t addAlignment(const size_t x) pure nothrow @safe @nogc
|
||||||
out (result)
|
|
||||||
{
|
|
||||||
assert(result > 0);
|
|
||||||
}
|
|
||||||
body
|
|
||||||
{
|
{
|
||||||
return (x - 1) / alignment_ * alignment_ + alignment_;
|
return (x - 1) / alignment_ * alignment_ + alignment_;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Params:
|
* Params:
|
||||||
* x = Required space.
|
* x = Required space.
|
||||||
|
* pageSize = Page size.
|
||||||
*
|
*
|
||||||
* Returns: Minimum region size (a multiple of $(D_PSYMBOL pageSize)).
|
* Returns: Minimum region size (a multiple of $(D_PSYMBOL pageSize)).
|
||||||
*/
|
*/
|
||||||
private static size_t calculateRegionSize(size_t x) nothrow @safe @nogc
|
private static size_t calculateRegionSize(ref const size_t x,
|
||||||
out (result)
|
ref const size_t pageSize)
|
||||||
|
pure nothrow @safe @nogc
|
||||||
{
|
{
|
||||||
assert(result > 0);
|
return (x + RegionEntry.sizeof + BlockEntry.sizeof * 2)
|
||||||
}
|
/ pageSize * pageSize + pageSize;
|
||||||
body
|
|
||||||
{
|
|
||||||
x += RegionEntry.sizeof + BlockEntry.sizeof * 2;
|
|
||||||
return x / pageSize * pageSize + pageSize;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -572,7 +633,7 @@ final class MmapPool : Allocator
|
|||||||
private enum uint alignment_ = 8;
|
private enum uint alignment_ = 8;
|
||||||
|
|
||||||
private shared static MmapPool instance_;
|
private shared static MmapPool instance_;
|
||||||
private shared static size_t pageSize;
|
private shared size_t pageSize;
|
||||||
|
|
||||||
private shared struct RegionEntry
|
private shared struct RegionEntry
|
||||||
{
|
{
|
||||||
@ -597,7 +658,7 @@ final class MmapPool : Allocator
|
|||||||
|
|
||||||
// A lot of allocations/deallocations, but it is the minimum caused a
|
// A lot of allocations/deallocations, but it is the minimum caused a
|
||||||
// segmentation fault because MmapPool reallocateInPlace moves a block wrong.
|
// segmentation fault because MmapPool reallocateInPlace moves a block wrong.
|
||||||
unittest
|
private @nogc unittest
|
||||||
{
|
{
|
||||||
auto a = MmapPool.instance.allocate(16);
|
auto a = MmapPool.instance.allocate(16);
|
||||||
auto d = MmapPool.instance.allocate(16);
|
auto d = MmapPool.instance.allocate(16);
|
||||||
|
404
source/tanya/memory/op.d
Normal file
404
source/tanya/memory/op.d
Normal file
@ -0,0 +1,404 @@
|
|||||||
|
/* 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/. */
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set of operations on memory blocks.
|
||||||
|
*
|
||||||
|
* 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/memory/op.d,
|
||||||
|
* tanya/memory/op.d)
|
||||||
|
*/
|
||||||
|
module tanya.memory.op;
|
||||||
|
|
||||||
|
version (D_InlineAsm_X86_64)
|
||||||
|
{
|
||||||
|
static import tanya.memory.arch.x86_64;
|
||||||
|
}
|
||||||
|
|
||||||
|
private enum alignMask = size_t.sizeof - 1;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Copies $(D_PARAM source) into $(D_PARAM target).
|
||||||
|
*
|
||||||
|
* $(D_PARAM source) and $(D_PARAM target) shall not overlap so that
|
||||||
|
* $(D_PARAM source) points ahead of $(D_PARAM target).
|
||||||
|
*
|
||||||
|
* $(D_PARAM target) shall have enough space for $(D_INLINECODE source.length)
|
||||||
|
* elements.
|
||||||
|
*
|
||||||
|
* Params:
|
||||||
|
* source = Memory to copy from.
|
||||||
|
* target = Destination memory.
|
||||||
|
*
|
||||||
|
* See_Also: $(D_PSYMBOL copyBackward).
|
||||||
|
*
|
||||||
|
* Precondition: $(D_INLINECODE source.length <= target.length).
|
||||||
|
*/
|
||||||
|
void copy(const void[] source, void[] target) pure nothrow @trusted @nogc
|
||||||
|
in
|
||||||
|
{
|
||||||
|
assert(source.length <= target.length);
|
||||||
|
}
|
||||||
|
body
|
||||||
|
{
|
||||||
|
version (D_InlineAsm_X86_64)
|
||||||
|
{
|
||||||
|
tanya.memory.arch.x86_64.copy(source, target);
|
||||||
|
}
|
||||||
|
else // Naive implementation.
|
||||||
|
{
|
||||||
|
auto source1 = cast(const(ubyte)*) source;
|
||||||
|
auto target1 = cast(ubyte*) target;
|
||||||
|
auto count = source.length;
|
||||||
|
|
||||||
|
// Check if the pointers are aligned or at least can be aligned
|
||||||
|
// properly.
|
||||||
|
ushort naligned = (cast(size_t) source.ptr) & alignMask;
|
||||||
|
if (naligned == ((cast(size_t) target.ptr) & alignMask))
|
||||||
|
{
|
||||||
|
// Align the pointers if possible.
|
||||||
|
if (naligned != 0)
|
||||||
|
{
|
||||||
|
count -= naligned;
|
||||||
|
while (naligned--)
|
||||||
|
{
|
||||||
|
*target1++ = *source1++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Copy size_t.sizeof bytes at once.
|
||||||
|
auto longSource = cast(const(size_t)*) source1;
|
||||||
|
auto longTarget = cast(size_t*) target1;
|
||||||
|
for (; count >= size_t.sizeof; count -= size_t.sizeof)
|
||||||
|
{
|
||||||
|
*longTarget++ = *longSource++;
|
||||||
|
}
|
||||||
|
// Adjust the original pointers.
|
||||||
|
source1 = cast(const(ubyte)*) longSource;
|
||||||
|
target1 = cast(ubyte*) longTarget;
|
||||||
|
}
|
||||||
|
// Copy the remaining bytes by one.
|
||||||
|
while (count--)
|
||||||
|
{
|
||||||
|
*target1++ = *source1++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
///
|
||||||
|
pure nothrow @safe @nogc unittest
|
||||||
|
{
|
||||||
|
ubyte[9] source = [1, 2, 3, 4, 5, 6, 7, 8, 9];
|
||||||
|
ubyte[9] target;
|
||||||
|
source.copy(target);
|
||||||
|
assert(cmp(source, target) == 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
private pure nothrow @safe @nogc unittest
|
||||||
|
{
|
||||||
|
{
|
||||||
|
ubyte[0] source, target;
|
||||||
|
source.copy(target);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
ubyte[1] source = [1];
|
||||||
|
ubyte[1] target;
|
||||||
|
source.copy(target);
|
||||||
|
assert(target[0] == 1);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
ubyte[8] source = [1, 2, 3, 4, 5, 6, 7, 8];
|
||||||
|
ubyte[8] target;
|
||||||
|
source.copy(target);
|
||||||
|
assert(cmp(source, target) == 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* size_t value each of which bytes is set to `Byte`.
|
||||||
|
*/
|
||||||
|
package template FilledBytes(ubyte Byte, ubyte I = 0)
|
||||||
|
{
|
||||||
|
static if (I == size_t.sizeof)
|
||||||
|
{
|
||||||
|
enum size_t FilledBytes = Byte;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
enum size_t FilledBytes = (FilledBytes!(Byte, I + 1) << 8) | Byte;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fills $(D_PARAM memory) with single $(D_PARAM Byte)s.
|
||||||
|
*
|
||||||
|
* Param:
|
||||||
|
* Byte = The value to fill $(D_PARAM memory) with.
|
||||||
|
* memory = Memory block.
|
||||||
|
*/
|
||||||
|
void fill(ubyte Byte = 0)(void[] memory) @trusted
|
||||||
|
{
|
||||||
|
version (D_InlineAsm_X86_64)
|
||||||
|
{
|
||||||
|
tanya.memory.arch.x86_64.fill!Byte(memory);
|
||||||
|
}
|
||||||
|
else // Naive implementation.
|
||||||
|
{
|
||||||
|
auto n = memory.length;
|
||||||
|
ubyte* vp = cast(ubyte*) memory.ptr;
|
||||||
|
|
||||||
|
// Align.
|
||||||
|
while (((cast(size_t) vp) & alignMask) != 0)
|
||||||
|
{
|
||||||
|
*vp++ = Byte;
|
||||||
|
--n;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set size_t.sizeof bytes at ones.
|
||||||
|
auto sp = cast(size_t*) vp;
|
||||||
|
while (n / size_t.sizeof > 0)
|
||||||
|
{
|
||||||
|
*sp++ = FilledBytes!Byte;
|
||||||
|
n -= size_t.sizeof;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Write the remaining bytes.
|
||||||
|
vp = cast(ubyte*) sp;
|
||||||
|
while (n--)
|
||||||
|
{
|
||||||
|
*vp = Byte;
|
||||||
|
++vp;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
///
|
||||||
|
pure nothrow @safe @nogc unittest
|
||||||
|
{
|
||||||
|
ubyte[9] memory = [1, 2, 3, 4, 5, 6, 7, 8, 9];
|
||||||
|
memory.fill!0();
|
||||||
|
foreach (ubyte v; memory)
|
||||||
|
{
|
||||||
|
assert(v == 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Stress test. Checks that `fill` can handle unaligned pointers and different
|
||||||
|
// lengths.
|
||||||
|
pure nothrow @safe @nogc private unittest
|
||||||
|
{
|
||||||
|
ubyte[192] memory;
|
||||||
|
|
||||||
|
foreach (j; 0 .. 192)
|
||||||
|
{
|
||||||
|
foreach (ubyte i, ref ubyte v; memory[j .. $])
|
||||||
|
{
|
||||||
|
v = i;
|
||||||
|
}
|
||||||
|
fill(memory[j .. $]);
|
||||||
|
foreach (ubyte v; memory[j .. $])
|
||||||
|
{
|
||||||
|
assert(v == 0);
|
||||||
|
}
|
||||||
|
fill!1(memory[j .. $]);
|
||||||
|
foreach (ubyte v; memory[j .. $])
|
||||||
|
{
|
||||||
|
assert(v == 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Copies starting from the end of $(D_PARAM source) into the end of
|
||||||
|
* $(D_PARAM target).
|
||||||
|
*
|
||||||
|
* $(D_PSYMBOL copyBackward) copies the elements in reverse order, but the
|
||||||
|
* order of elements in the $(D_PARAM target) is exactly the same as in the
|
||||||
|
* $(D_PARAM source).
|
||||||
|
*
|
||||||
|
* $(D_PARAM source) and $(D_PARAM target) shall not overlap so that
|
||||||
|
* $(D_PARAM target) points ahead of $(D_PARAM source).
|
||||||
|
*
|
||||||
|
* $(D_PARAM target) shall have enough space for $(D_INLINECODE source.length)
|
||||||
|
* elements.
|
||||||
|
*
|
||||||
|
* Params:
|
||||||
|
* source = Memory to copy from.
|
||||||
|
* target = Destination memory.
|
||||||
|
*
|
||||||
|
* See_Also: $(D_PSYMBOL copy).
|
||||||
|
*
|
||||||
|
* Precondition: $(D_INLINECODE source.length <= target.length).
|
||||||
|
*/
|
||||||
|
void copyBackward(const void[] source, void[] target) pure nothrow @trusted @nogc
|
||||||
|
in
|
||||||
|
{
|
||||||
|
assert(source.length <= target.length);
|
||||||
|
}
|
||||||
|
body
|
||||||
|
{
|
||||||
|
version (D_InlineAsm_X86_64)
|
||||||
|
{
|
||||||
|
tanya.memory.arch.x86_64.copyBackward(source, target);
|
||||||
|
}
|
||||||
|
else // Naive implementation.
|
||||||
|
{
|
||||||
|
auto count = source.length;
|
||||||
|
|
||||||
|
// Try to align the pointers if possible.
|
||||||
|
if (((cast(size_t) source.ptr) & alignMask) == ((cast(size_t) target.ptr) & alignMask))
|
||||||
|
{
|
||||||
|
while (((cast(size_t) (source.ptr + count)) & alignMask) != 0)
|
||||||
|
{
|
||||||
|
if (!count--)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
(cast(ubyte[]) target)[count]
|
||||||
|
= (cast(const(ubyte)[]) source)[count];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Write as long we're aligned.
|
||||||
|
for (; count >= size_t.sizeof; count -= size_t.sizeof)
|
||||||
|
{
|
||||||
|
*(cast(size_t*) (target.ptr + count - size_t.sizeof))
|
||||||
|
= *(cast(const(size_t)*) (source.ptr + count - size_t.sizeof));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Write the remaining bytes.
|
||||||
|
while (count--)
|
||||||
|
{
|
||||||
|
(cast(ubyte[]) target)[count]
|
||||||
|
= (cast(const(ubyte)[]) source)[count];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
///
|
||||||
|
pure nothrow @safe @nogc unittest
|
||||||
|
{
|
||||||
|
ubyte[6] mem = [ 'a', 'a', 'b', 'b', 'c', 'c' ];
|
||||||
|
ubyte[6] expected = [ 'a', 'a', 'a', 'a', 'b', 'b' ];
|
||||||
|
|
||||||
|
copyBackward(mem[0 .. 4], mem[2 .. $]);
|
||||||
|
assert(cmp(expected, mem) == 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
private nothrow @safe @nogc unittest
|
||||||
|
{
|
||||||
|
ubyte[9] r1 = [ 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i' ];
|
||||||
|
ubyte[9] r2;
|
||||||
|
|
||||||
|
copyBackward(r1, r2);
|
||||||
|
assert(cmp(r1, r2) == 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Compares two memory areas $(D_PARAM r1) and $(D_PARAM r2).
|
||||||
|
*
|
||||||
|
* $(D_PSYMBOL cmp) returns a positive integer if
|
||||||
|
* $(D_INLINECODE r1.length > r2.length) or the first `n` compared bytes of
|
||||||
|
* $(D_PARAM r1) found to be greater than the first `n` bytes of $(D_PARAM r2),
|
||||||
|
*
|
||||||
|
* $(D_PSYMBOL cmp) returns a negative integer if
|
||||||
|
* $(D_INLINECODE r2.length > r1.length) or the first `n` compared bytes of
|
||||||
|
* $(D_PARAM r1) found to be less than the first `n` bytes of $(D_PARAM r2),
|
||||||
|
*
|
||||||
|
* `0` is returned otherwise.
|
||||||
|
*
|
||||||
|
* Returns: Positive integer if $(D_INLINECODE r1 > r2),
|
||||||
|
* 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
|
||||||
|
{
|
||||||
|
version (D_InlineAsm_X86_64)
|
||||||
|
{
|
||||||
|
return tanya.memory.arch.x86_64.cmp(r1, r2);
|
||||||
|
}
|
||||||
|
else // Naive implementation.
|
||||||
|
{
|
||||||
|
if (r1.length > r2.length)
|
||||||
|
{
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
else if (r1.length < r2.length)
|
||||||
|
{
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
auto p1 = cast(const(ubyte)*) r1;
|
||||||
|
auto p2 = cast(const(ubyte)*) r2;
|
||||||
|
auto count = r1.length;
|
||||||
|
|
||||||
|
// Check if the pointers are aligned or at least can be aligned
|
||||||
|
// properly.
|
||||||
|
if (((cast(size_t) p1) & alignMask) == ((cast(size_t) p2) & alignMask))
|
||||||
|
{
|
||||||
|
// Align the pointers if possible.
|
||||||
|
for (; ((cast(size_t) p1) & alignMask) != 0; ++p1, ++p2, --count)
|
||||||
|
{
|
||||||
|
if (*p1 != *p2)
|
||||||
|
{
|
||||||
|
return *p1 - *p2;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Compare size_t.sizeof bytes at once.
|
||||||
|
for (; count >= size_t.sizeof; count -= size_t.sizeof)
|
||||||
|
{
|
||||||
|
if (*(cast(const(size_t)*) p1) > *(cast(const(size_t)*) p2))
|
||||||
|
{
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
else if (*(cast(const(size_t)*) p1) < *(cast(const(size_t)*) p2))
|
||||||
|
{
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
p1 += size_t.sizeof;
|
||||||
|
p2 += size_t.sizeof;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Compare the remaining bytes by one.
|
||||||
|
for (; count--; ++p1, ++p2)
|
||||||
|
{
|
||||||
|
if (*p1 != *p2)
|
||||||
|
{
|
||||||
|
return *p1 - *p2;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
///
|
||||||
|
pure nothrow @safe @nogc unittest
|
||||||
|
{
|
||||||
|
ubyte[4] r1 = [ 'a', 'b', 'c', 'd' ];
|
||||||
|
ubyte[3] r2 = [ 'c', 'a', 'b' ];
|
||||||
|
|
||||||
|
assert(cmp(r1[0 .. 3], r2[]) < 0);
|
||||||
|
assert(cmp(r2[], r1[0 .. 3]) > 0);
|
||||||
|
|
||||||
|
assert(cmp(r1, r2) > 0);
|
||||||
|
assert(cmp(r2, r1) < 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
private pure nothrow @safe @nogc unittest
|
||||||
|
{
|
||||||
|
ubyte[16] r1 = [
|
||||||
|
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
|
||||||
|
'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p',
|
||||||
|
];
|
||||||
|
ubyte[16] r2 = [
|
||||||
|
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
|
||||||
|
'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p',
|
||||||
|
];
|
||||||
|
|
||||||
|
assert(cmp(r1, r2) == 0);
|
||||||
|
assert(cmp(r1[1 .. $], r2[1 .. $]) == 0);
|
||||||
|
assert(cmp(r1[0 .. $ - 1], r2[0 .. $ - 1]) == 0);
|
||||||
|
assert(cmp(r1[0 .. 8], r2[0 .. 8]) == 0);
|
||||||
|
}
|
@ -9,15 +9,19 @@
|
|||||||
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
||||||
* Mozilla Public License, v. 2.0).
|
* Mozilla Public License, v. 2.0).
|
||||||
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
||||||
|
* Source: $(LINK2 https://github.com/caraus-ecms/tanya/blob/master/source/tanya/memory/package.d,
|
||||||
|
* tanya/memory/package.d)
|
||||||
*/
|
*/
|
||||||
module tanya.memory;
|
module tanya.memory;
|
||||||
|
|
||||||
import core.exception;
|
import core.exception;
|
||||||
import std.algorithm.iteration;
|
import std.algorithm.iteration;
|
||||||
public import std.experimental.allocator : make;
|
import std.algorithm.mutation;
|
||||||
import std.traits;
|
import std.conv;
|
||||||
|
import std.range;
|
||||||
public import tanya.memory.allocator;
|
public import tanya.memory.allocator;
|
||||||
import tanya.memory.mmappool;
|
import tanya.memory.mmappool;
|
||||||
|
import tanya.meta.trait;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The mixin generates common methods for classes and structs using
|
* The mixin generates common methods for classes and structs using
|
||||||
@ -36,7 +40,7 @@ mixin template DefaultAllocator()
|
|||||||
*
|
*
|
||||||
* Precondition: $(D_INLINECODE allocator_ !is null)
|
* Precondition: $(D_INLINECODE allocator_ !is null)
|
||||||
*/
|
*/
|
||||||
this(shared Allocator allocator)
|
this(shared Allocator allocator) pure nothrow @safe @nogc
|
||||||
in
|
in
|
||||||
{
|
{
|
||||||
assert(allocator !is null);
|
assert(allocator !is null);
|
||||||
@ -54,7 +58,7 @@ mixin template DefaultAllocator()
|
|||||||
*
|
*
|
||||||
* Postcondition: $(D_INLINECODE allocator !is null)
|
* Postcondition: $(D_INLINECODE allocator !is null)
|
||||||
*/
|
*/
|
||||||
protected @property shared(Allocator) allocator() nothrow @safe @nogc
|
protected @property shared(Allocator) allocator() pure nothrow @safe @nogc
|
||||||
out (allocator)
|
out (allocator)
|
||||||
{
|
{
|
||||||
assert(allocator !is null);
|
assert(allocator !is null);
|
||||||
@ -69,7 +73,7 @@ mixin template DefaultAllocator()
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Ditto.
|
/// Ditto.
|
||||||
@property shared(Allocator) allocator() const nothrow @trusted @nogc
|
@property shared(Allocator) allocator() const pure nothrow @trusted @nogc
|
||||||
out (allocator)
|
out (allocator)
|
||||||
{
|
{
|
||||||
assert(allocator !is null);
|
assert(allocator !is null);
|
||||||
@ -85,25 +89,44 @@ mixin template DefaultAllocator()
|
|||||||
}
|
}
|
||||||
|
|
||||||
// From druntime
|
// From druntime
|
||||||
private extern (C) void _d_monitordelete(Object h, bool det) nothrow @nogc;
|
extern (C)
|
||||||
|
private void _d_monitordelete(Object h, bool det) pure nothrow @nogc;
|
||||||
|
|
||||||
shared Allocator allocator;
|
shared Allocator allocator;
|
||||||
|
|
||||||
shared static this() nothrow @trusted @nogc
|
shared static this() nothrow @nogc
|
||||||
{
|
{
|
||||||
allocator = MmapPool.instance;
|
allocator = MmapPool.instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
@property ref shared(Allocator) defaultAllocator() nothrow @safe @nogc
|
private shared(Allocator) getAllocatorInstance() nothrow @nogc
|
||||||
|
{
|
||||||
|
return allocator;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns: Default allocator.
|
||||||
|
*
|
||||||
|
* Postcondition: $(D_INLINECODE allocator !is null).
|
||||||
|
*/
|
||||||
|
@property shared(Allocator) defaultAllocator() pure nothrow @trusted @nogc
|
||||||
out (allocator)
|
out (allocator)
|
||||||
{
|
{
|
||||||
assert(allocator !is null);
|
assert(allocator !is null);
|
||||||
}
|
}
|
||||||
body
|
body
|
||||||
{
|
{
|
||||||
return allocator;
|
return (cast(GetPureInstance!Allocator) &getAllocatorInstance)();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the default allocator.
|
||||||
|
*
|
||||||
|
* Params:
|
||||||
|
* allocator = $(D_PSYMBOL Allocator) instance.
|
||||||
|
*
|
||||||
|
* Precondition: $(D_INLINECODE allocator !is null).
|
||||||
|
*/
|
||||||
@property void defaultAllocator(shared(Allocator) allocator) nothrow @safe @nogc
|
@property void defaultAllocator(shared(Allocator) allocator) nothrow @safe @nogc
|
||||||
in
|
in
|
||||||
{
|
{
|
||||||
@ -114,17 +137,6 @@ body
|
|||||||
.allocator = allocator;
|
.allocator = allocator;
|
||||||
}
|
}
|
||||||
|
|
||||||
private nothrow @nogc unittest
|
|
||||||
{
|
|
||||||
import tanya.memory.mallocator;
|
|
||||||
|
|
||||||
auto oldAllocator = defaultAllocator;
|
|
||||||
defaultAllocator = Mallocator.instance;
|
|
||||||
assert(defaultAllocator is Mallocator.instance);
|
|
||||||
|
|
||||||
defaultAllocator = oldAllocator;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the size in bytes of the state that needs to be allocated to hold an
|
* Returns the size in bytes of the state that needs to be allocated to hold an
|
||||||
* object of type $(D_PARAM T).
|
* object of type $(D_PARAM T).
|
||||||
@ -157,7 +169,7 @@ pure nothrow @safe @nogc
|
|||||||
return (size - 1) / alignment * alignment + alignment;
|
return (size - 1) / alignment * alignment + alignment;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/*
|
||||||
* Internal function used to create, resize or destroy a dynamic array. It
|
* Internal function used to create, resize or destroy a dynamic array. It
|
||||||
* may throw $(D_PSYMBOL OutOfMemoryError). The new
|
* may throw $(D_PSYMBOL OutOfMemoryError). The new
|
||||||
* allocated part of the array isn't initialized. This function can be trusted
|
* allocated part of the array isn't initialized. This function can be trusted
|
||||||
@ -271,7 +283,8 @@ package(tanya) void[] finalize(T)(ref T p)
|
|||||||
// shouldn't throw and if it does, it is an error anyway.
|
// shouldn't throw and if it does, it is an error anyway.
|
||||||
if (c.destructor)
|
if (c.destructor)
|
||||||
{
|
{
|
||||||
(cast(void function (Object) nothrow @safe @nogc) c.destructor)(ob);
|
alias DtorType = void function(Object) pure nothrow @safe @nogc;
|
||||||
|
(cast(DtorType) c.destructor)(ob);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
while ((c = c.base) !is null);
|
while ((c = c.base) !is null);
|
||||||
@ -322,7 +335,7 @@ private unittest
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Works with interfaces.
|
// Works with interfaces.
|
||||||
private unittest
|
private pure unittest
|
||||||
{
|
{
|
||||||
interface I
|
interface I
|
||||||
{
|
{
|
||||||
@ -336,3 +349,122 @@ private unittest
|
|||||||
defaultAllocator.dispose(i);
|
defaultAllocator.dispose(i);
|
||||||
defaultAllocator.dispose(i);
|
defaultAllocator.dispose(i);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructs a new class instance of type $(D_PARAM T) using $(D_PARAM args)
|
||||||
|
* as the parameter list for the constructor of $(D_PARAM T).
|
||||||
|
*
|
||||||
|
* Params:
|
||||||
|
* T = Class type.
|
||||||
|
* A = Types of the arguments to the constructor of $(D_PARAM T).
|
||||||
|
* allocator = Allocator.
|
||||||
|
* args = Constructor arguments of $(D_PARAM T).
|
||||||
|
*
|
||||||
|
* Returns: Newly created $(D_PSYMBOL T).
|
||||||
|
*
|
||||||
|
* Precondition: $(D_INLINECODE allocator !is null)
|
||||||
|
*/
|
||||||
|
T make(T, A...)(shared Allocator allocator, auto ref A args)
|
||||||
|
if (is(T == class))
|
||||||
|
in
|
||||||
|
{
|
||||||
|
assert(allocator !is null);
|
||||||
|
}
|
||||||
|
body
|
||||||
|
{
|
||||||
|
auto mem = (() @trusted => allocator.allocate(stateSize!T))();
|
||||||
|
if (mem is null)
|
||||||
|
{
|
||||||
|
onOutOfMemoryError();
|
||||||
|
}
|
||||||
|
scope (failure)
|
||||||
|
{
|
||||||
|
() @trusted { allocator.deallocate(mem); }();
|
||||||
|
}
|
||||||
|
|
||||||
|
return emplace!T(mem[0 .. stateSize!T], args);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructs a value object of type $(D_PARAM T) using $(D_PARAM args)
|
||||||
|
* as the parameter list for the constructor of $(D_PARAM T) and returns a
|
||||||
|
* pointer to the new object.
|
||||||
|
*
|
||||||
|
* Params:
|
||||||
|
* T = Object type.
|
||||||
|
* A = Types of the arguments to the constructor of $(D_PARAM T).
|
||||||
|
* allocator = Allocator.
|
||||||
|
* args = Constructor arguments of $(D_PARAM T).
|
||||||
|
*
|
||||||
|
* Returns: Pointer to the created object.
|
||||||
|
*
|
||||||
|
* Precondition: $(D_INLINECODE allocator !is null)
|
||||||
|
*/
|
||||||
|
T* make(T, A...)(shared Allocator allocator, auto ref A args)
|
||||||
|
if (!is(T == interface)
|
||||||
|
&& !is(T == class)
|
||||||
|
&& !isAssociativeArray!T
|
||||||
|
&& !isArray!T)
|
||||||
|
in
|
||||||
|
{
|
||||||
|
assert(allocator !is null);
|
||||||
|
}
|
||||||
|
body
|
||||||
|
{
|
||||||
|
auto mem = (() @trusted => allocator.allocate(stateSize!T))();
|
||||||
|
if (mem is null)
|
||||||
|
{
|
||||||
|
onOutOfMemoryError();
|
||||||
|
}
|
||||||
|
scope (failure)
|
||||||
|
{
|
||||||
|
() @trusted { allocator.deallocate(mem); }();
|
||||||
|
}
|
||||||
|
|
||||||
|
auto ptr = (() @trusted => (cast(T*) mem[0 .. stateSize!T].ptr))();
|
||||||
|
return emplace!T(ptr, args);
|
||||||
|
}
|
||||||
|
|
||||||
|
///
|
||||||
|
unittest
|
||||||
|
{
|
||||||
|
int* i = defaultAllocator.make!int(5);
|
||||||
|
assert(*i == 5);
|
||||||
|
defaultAllocator.dispose(i);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructs a new array with $(D_PARAM n) elements.
|
||||||
|
*
|
||||||
|
* Params:
|
||||||
|
* T = Array type.
|
||||||
|
* allocator = Allocator.
|
||||||
|
* n = Array size.
|
||||||
|
*
|
||||||
|
* Returns: Newly created array.
|
||||||
|
*
|
||||||
|
* Precondition: $(D_INLINECODE allocator !is null
|
||||||
|
* && n <= size_t.max / ElementType!T.sizeof)
|
||||||
|
*/
|
||||||
|
T make(T)(shared Allocator allocator, const size_t n)
|
||||||
|
if (isArray!T)
|
||||||
|
in
|
||||||
|
{
|
||||||
|
assert(allocator !is null);
|
||||||
|
assert(n <= size_t.max / ElementType!T.sizeof);
|
||||||
|
}
|
||||||
|
body
|
||||||
|
{
|
||||||
|
auto ret = allocator.resize!(ElementType!T)(null, n);
|
||||||
|
ret.uninitializedFill(ElementType!T.init);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
///
|
||||||
|
unittest
|
||||||
|
{
|
||||||
|
int[] i = defaultAllocator.make!(int[])(2);
|
||||||
|
assert(i.length == 2);
|
||||||
|
assert(i[0] == int.init && i[1] == int.init);
|
||||||
|
defaultAllocator.dispose(i);
|
||||||
|
}
|
||||||
|
@ -12,6 +12,8 @@
|
|||||||
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
||||||
* Mozilla Public License, v. 2.0).
|
* Mozilla Public License, v. 2.0).
|
||||||
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
||||||
|
* Source: $(LINK2 https://github.com/caraus-ecms/tanya/blob/master/source/tanya/memory/smartref.d,
|
||||||
|
* tanya/memory/smartref.d)
|
||||||
*/
|
*/
|
||||||
module tanya.memory.smartref;
|
module tanya.memory.smartref;
|
||||||
|
|
||||||
@ -20,10 +22,10 @@ import std.algorithm.comparison;
|
|||||||
import std.algorithm.mutation;
|
import std.algorithm.mutation;
|
||||||
import std.conv;
|
import std.conv;
|
||||||
import std.range;
|
import std.range;
|
||||||
import std.traits;
|
|
||||||
import tanya.memory;
|
import tanya.memory;
|
||||||
|
import tanya.meta.trait;
|
||||||
|
|
||||||
package template Payload(T)
|
private template Payload(T)
|
||||||
{
|
{
|
||||||
static if (is(T == class) || is(T == interface) || isArray!T)
|
static if (is(T == class) || is(T == interface) || isArray!T)
|
||||||
{
|
{
|
||||||
@ -35,7 +37,7 @@ package template Payload(T)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
package final class RefCountedStore(T)
|
private final class RefCountedStore(T)
|
||||||
{
|
{
|
||||||
T payload;
|
T payload;
|
||||||
size_t counter = 1;
|
size_t counter = 1;
|
||||||
@ -68,14 +70,14 @@ package final class RefCountedStore(T)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
package void separateDeleter(T)(RefCountedStore!T storage,
|
private void separateDeleter(T)(RefCountedStore!T storage,
|
||||||
shared Allocator allocator)
|
shared Allocator allocator)
|
||||||
{
|
{
|
||||||
allocator.dispose(storage.payload);
|
allocator.dispose(storage.payload);
|
||||||
allocator.dispose(storage);
|
allocator.dispose(storage);
|
||||||
}
|
}
|
||||||
|
|
||||||
package void unifiedDeleter(T)(RefCountedStore!T storage,
|
private void unifiedDeleter(T)(RefCountedStore!T storage,
|
||||||
shared Allocator allocator)
|
shared Allocator allocator)
|
||||||
{
|
{
|
||||||
auto ptr1 = finalize(storage);
|
auto ptr1 = finalize(storage);
|
||||||
@ -548,8 +550,7 @@ in
|
|||||||
}
|
}
|
||||||
body
|
body
|
||||||
{
|
{
|
||||||
auto payload = allocator.resize!(ElementType!T)(null, size);
|
return RefCounted!T(allocator.make!T(size), allocator);
|
||||||
return RefCounted!T(payload, allocator);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
///
|
///
|
||||||
@ -852,7 +853,7 @@ in
|
|||||||
}
|
}
|
||||||
body
|
body
|
||||||
{
|
{
|
||||||
auto payload = allocator.make!(T, shared Allocator, A)(args);
|
auto payload = allocator.make!(T, A)(args);
|
||||||
return Unique!T(payload, allocator);
|
return Unique!T(payload, allocator);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,301 +0,0 @@
|
|||||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
||||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
||||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Smart pointers.
|
|
||||||
*
|
|
||||||
* $(RED Deprecated. Use $(D_PSYMBOL tanya.memory.smartref) instead.
|
|
||||||
* This module will be removed in 0.8.0.)
|
|
||||||
*
|
|
||||||
* Copyright: Eugene Wissner 2016-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)
|
|
||||||
*/
|
|
||||||
deprecated("Use tanya.memory.smartref instead")
|
|
||||||
module tanya.memory.types;
|
|
||||||
|
|
||||||
import core.exception;
|
|
||||||
import std.algorithm.comparison;
|
|
||||||
import std.algorithm.mutation;
|
|
||||||
import std.conv;
|
|
||||||
import std.range;
|
|
||||||
import std.traits;
|
|
||||||
import tanya.memory;
|
|
||||||
public import tanya.memory.smartref : RefCounted, Payload;
|
|
||||||
|
|
||||||
version (unittest)
|
|
||||||
{
|
|
||||||
private struct B
|
|
||||||
{
|
|
||||||
int prop;
|
|
||||||
@disable this();
|
|
||||||
this(int param1) @nogc
|
|
||||||
{
|
|
||||||
prop = param1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* $(D_PSYMBOL Scoped) stores an object that gets destroyed at the end of its scope.
|
|
||||||
*
|
|
||||||
* Params:
|
|
||||||
* T = Value type.
|
|
||||||
*/
|
|
||||||
struct Scoped(T)
|
|
||||||
{
|
|
||||||
private Payload!T payload;
|
|
||||||
|
|
||||||
invariant
|
|
||||||
{
|
|
||||||
assert(payload is null || allocator_ !is null);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Takes ownership over $(D_PARAM value), setting the counter to 1.
|
|
||||||
* $(D_PARAM value) may be a pointer, an object or a dynamic array.
|
|
||||||
*
|
|
||||||
* Params:
|
|
||||||
* value = Value whose ownership is taken over.
|
|
||||||
* allocator = Allocator used to destroy the $(D_PARAM value) and to
|
|
||||||
* allocate/deallocate internal storage.
|
|
||||||
*
|
|
||||||
* Precondition: $(D_INLINECODE allocator !is null)
|
|
||||||
*/
|
|
||||||
this()(auto ref Payload!T value,
|
|
||||||
shared Allocator allocator = defaultAllocator)
|
|
||||||
{
|
|
||||||
this(allocator);
|
|
||||||
|
|
||||||
move(value, this.payload);
|
|
||||||
static if (__traits(isRef, value))
|
|
||||||
{
|
|
||||||
value = null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Ditto.
|
|
||||||
this(shared Allocator allocator)
|
|
||||||
in
|
|
||||||
{
|
|
||||||
assert(allocator !is null);
|
|
||||||
}
|
|
||||||
body
|
|
||||||
{
|
|
||||||
this.allocator_ = allocator;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* $(D_PSYMBOL Scoped) is noncopyable.
|
|
||||||
*/
|
|
||||||
@disable this(this);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Destroys the owned object.
|
|
||||||
*/
|
|
||||||
~this()
|
|
||||||
{
|
|
||||||
if (this.payload !is null)
|
|
||||||
{
|
|
||||||
allocator.dispose(this.payload);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Initialized this $(D_PARAM Scoped) and takes ownership over
|
|
||||||
* $(D_PARAM rhs).
|
|
||||||
*
|
|
||||||
* To reset $(D_PSYMBOL Scoped) assign $(D_KEYWORD null).
|
|
||||||
*
|
|
||||||
* If the allocator wasn't set before, $(D_PSYMBOL defaultAllocator) will
|
|
||||||
* be used. If you need a different allocator, create a new
|
|
||||||
* $(D_PSYMBOL Scoped) and assign it.
|
|
||||||
*
|
|
||||||
* Params:
|
|
||||||
* rhs = New object.
|
|
||||||
*
|
|
||||||
* Returns: $(D_KEYWORD this).
|
|
||||||
*/
|
|
||||||
ref typeof(this) opAssign()(auto ref Payload!T rhs)
|
|
||||||
{
|
|
||||||
allocator.dispose(this.payload);
|
|
||||||
move(rhs, this.payload);
|
|
||||||
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Ditto.
|
|
||||||
ref typeof(this) opAssign(typeof(null))
|
|
||||||
{
|
|
||||||
allocator.dispose(this.payload);
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Ditto.
|
|
||||||
ref typeof(this) opAssign(typeof(this) rhs)
|
|
||||||
{
|
|
||||||
swap(this.allocator_, rhs.allocator_);
|
|
||||||
swap(this.payload, rhs.payload);
|
|
||||||
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns: Reference to the owned object.
|
|
||||||
*/
|
|
||||||
Payload!T get() pure nothrow @safe @nogc
|
|
||||||
{
|
|
||||||
return payload;
|
|
||||||
}
|
|
||||||
|
|
||||||
version (D_Ddoc)
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
* Params:
|
|
||||||
* op = Operation.
|
|
||||||
*
|
|
||||||
* Dereferences the pointer. It is defined only for pointers, not for
|
|
||||||
* reference types like classes, that can be accessed directly.
|
|
||||||
*
|
|
||||||
* Returns: Reference to the pointed value.
|
|
||||||
*/
|
|
||||||
ref T opUnary(string op)()
|
|
||||||
if (op == "*");
|
|
||||||
}
|
|
||||||
else static if (isPointer!(Payload!T))
|
|
||||||
{
|
|
||||||
ref T opUnary(string op)()
|
|
||||||
if (op == "*")
|
|
||||||
{
|
|
||||||
return *payload;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
mixin DefaultAllocator;
|
|
||||||
alias get this;
|
|
||||||
}
|
|
||||||
|
|
||||||
///
|
|
||||||
@nogc unittest
|
|
||||||
{
|
|
||||||
auto p = defaultAllocator.make!int(5);
|
|
||||||
auto s = Scoped!int(p, defaultAllocator);
|
|
||||||
assert(p is null);
|
|
||||||
assert(*s == 5);
|
|
||||||
}
|
|
||||||
|
|
||||||
///
|
|
||||||
@nogc unittest
|
|
||||||
{
|
|
||||||
static bool destroyed = false;
|
|
||||||
|
|
||||||
struct F
|
|
||||||
{
|
|
||||||
~this() @nogc
|
|
||||||
{
|
|
||||||
destroyed = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
{
|
|
||||||
auto s = Scoped!F(defaultAllocator.make!F(), defaultAllocator);
|
|
||||||
}
|
|
||||||
assert(destroyed);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Constructs a new object of type $(D_PARAM T) and wraps it in a
|
|
||||||
* $(D_PSYMBOL Scoped) using $(D_PARAM args) as the parameter list for
|
|
||||||
* the constructor of $(D_PARAM T).
|
|
||||||
*
|
|
||||||
* Params:
|
|
||||||
* T = Type of the constructed object.
|
|
||||||
* A = Types of the arguments to the constructor of $(D_PARAM T).
|
|
||||||
* allocator = Allocator.
|
|
||||||
* args = Constructor arguments of $(D_PARAM T).
|
|
||||||
*
|
|
||||||
* Returns: Newly created $(D_PSYMBOL Scoped!T).
|
|
||||||
*
|
|
||||||
* Precondition: $(D_INLINECODE allocator !is null)
|
|
||||||
*/
|
|
||||||
Scoped!T scoped(T, A...)(shared Allocator allocator, auto ref A args)
|
|
||||||
if (!is(T == interface) && !isAbstractClass!T
|
|
||||||
&& !isAssociativeArray!T && !isArray!T)
|
|
||||||
in
|
|
||||||
{
|
|
||||||
assert(allocator !is null);
|
|
||||||
}
|
|
||||||
body
|
|
||||||
{
|
|
||||||
auto payload = allocator.make!(T, shared Allocator, A)(args);
|
|
||||||
return Scoped!T(payload, allocator);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Constructs a new array with $(D_PARAM size) elements and wraps it in a
|
|
||||||
* $(D_PSYMBOL Scoped).
|
|
||||||
*
|
|
||||||
* Params:
|
|
||||||
* T = Array type.
|
|
||||||
* size = Array size.
|
|
||||||
* allocator = Allocator.
|
|
||||||
*
|
|
||||||
* Returns: Newly created $(D_PSYMBOL Scoped!T).
|
|
||||||
*
|
|
||||||
* Precondition: $(D_INLINECODE allocator !is null
|
|
||||||
* && size <= size_t.max / ElementType!T.sizeof)
|
|
||||||
*/
|
|
||||||
Scoped!T scoped(T)(shared Allocator allocator, const size_t size)
|
|
||||||
@trusted
|
|
||||||
if (isArray!T)
|
|
||||||
in
|
|
||||||
{
|
|
||||||
assert(allocator !is null);
|
|
||||||
assert(size <= size_t.max / ElementType!T.sizeof);
|
|
||||||
}
|
|
||||||
body
|
|
||||||
{
|
|
||||||
auto payload = allocator.resize!(ElementType!T)(null, size);
|
|
||||||
return Scoped!T(payload, allocator);
|
|
||||||
}
|
|
||||||
|
|
||||||
private unittest
|
|
||||||
{
|
|
||||||
static assert(is(typeof(defaultAllocator.scoped!B(5))));
|
|
||||||
static assert(is(typeof(defaultAllocator.scoped!(int[])(5))));
|
|
||||||
}
|
|
||||||
|
|
||||||
private unittest
|
|
||||||
{
|
|
||||||
auto s = defaultAllocator.scoped!int(5);
|
|
||||||
assert(*s == 5);
|
|
||||||
|
|
||||||
s = null;
|
|
||||||
assert(s is null);
|
|
||||||
}
|
|
||||||
|
|
||||||
private unittest
|
|
||||||
{
|
|
||||||
auto s = defaultAllocator.scoped!int(5);
|
|
||||||
assert(*s == 5);
|
|
||||||
|
|
||||||
s = defaultAllocator.scoped!int(4);
|
|
||||||
assert(*s == 4);
|
|
||||||
}
|
|
||||||
|
|
||||||
private @nogc unittest
|
|
||||||
{
|
|
||||||
auto p1 = defaultAllocator.make!int(5);
|
|
||||||
auto p2 = p1;
|
|
||||||
auto rc = Scoped!int(p1, defaultAllocator);
|
|
||||||
|
|
||||||
assert(p1 is null);
|
|
||||||
assert(rc.get() is p2);
|
|
||||||
}
|
|
||||||
|
|
||||||
private @nogc unittest
|
|
||||||
{
|
|
||||||
auto rc = Scoped!int(defaultAllocator);
|
|
||||||
assert(rc.allocator is defaultAllocator);
|
|
||||||
}
|
|
1780
source/tanya/meta/metafunction.d
Normal file
1780
source/tanya/meta/metafunction.d
Normal file
File diff suppressed because it is too large
Load Diff
23
source/tanya/meta/package.d
Normal file
23
source/tanya/meta/package.d
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
/* 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/. */
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Template metaprogramming.
|
||||||
|
*
|
||||||
|
* This package contains utilities to acquire type information at compile-time,
|
||||||
|
* to transform from one type to another. It has also different algorithms for
|
||||||
|
* iterating, searching and modifying template 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/meta/package.d,
|
||||||
|
* tanya/meta/package.d)
|
||||||
|
*/
|
||||||
|
module tanya.meta;
|
||||||
|
|
||||||
|
public import tanya.meta.metafunction;
|
||||||
|
public import tanya.meta.trait;
|
||||||
|
public import tanya.meta.transform;
|
3069
source/tanya/meta/trait.d
Normal file
3069
source/tanya/meta/trait.d
Normal file
File diff suppressed because it is too large
Load Diff
741
source/tanya/meta/transform.d
Normal file
741
source/tanya/meta/transform.d
Normal file
@ -0,0 +1,741 @@
|
|||||||
|
/* 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/. */
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Type transformations.
|
||||||
|
*
|
||||||
|
* Templates in this module can be used to modify type qualifiers or transform
|
||||||
|
* types. They take some type as argument and return a different type after
|
||||||
|
* perfoming the specified transformation.
|
||||||
|
*
|
||||||
|
* 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/meta/transform.d,
|
||||||
|
* tanya/meta/transform.d)
|
||||||
|
*/
|
||||||
|
module tanya.meta.transform;
|
||||||
|
|
||||||
|
version (TanyaPhobos)
|
||||||
|
{
|
||||||
|
public import std.traits : Unqual,
|
||||||
|
OriginalType,
|
||||||
|
CopyConstness,
|
||||||
|
CopyTypeQualifiers,
|
||||||
|
Unsigned,
|
||||||
|
Signed,
|
||||||
|
PointerTarget,
|
||||||
|
KeyType,
|
||||||
|
ValueType,
|
||||||
|
Promoted,
|
||||||
|
InoutOf,
|
||||||
|
ConstOf,
|
||||||
|
SharedOf,
|
||||||
|
SharedInoutOf,
|
||||||
|
SharedConstOf,
|
||||||
|
ImmutableOf,
|
||||||
|
QualifierOf;
|
||||||
|
}
|
||||||
|
else:
|
||||||
|
|
||||||
|
import tanya.meta.trait;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Removes any type qualifiers from $(D_PARAM T).
|
||||||
|
*
|
||||||
|
* Removed qualifiers are:
|
||||||
|
* $(UL
|
||||||
|
* $(LI const)
|
||||||
|
* $(LI immutable)
|
||||||
|
* $(LI inout)
|
||||||
|
* $(LI shared)
|
||||||
|
* )
|
||||||
|
* and combinations of these.
|
||||||
|
*
|
||||||
|
* If the type $(D_PARAM T) doesn't have any qualifieres,
|
||||||
|
* $(D_INLINECODE Unqual!T) becomes an alias for $(D_PARAM T).
|
||||||
|
*
|
||||||
|
* Params:
|
||||||
|
* T = A type.
|
||||||
|
*
|
||||||
|
* Returns: $(D_PARAM T) without any type qualifiers.
|
||||||
|
*/
|
||||||
|
template Unqual(T)
|
||||||
|
{
|
||||||
|
static if (is(T U == const U)
|
||||||
|
|| is(T U == immutable U)
|
||||||
|
|| is(T U == inout U)
|
||||||
|
|| is(T U == inout const U)
|
||||||
|
|| is(T U == shared U)
|
||||||
|
|| is(T U == shared const U)
|
||||||
|
|| is(T U == shared inout U)
|
||||||
|
|| is(T U == shared inout const U))
|
||||||
|
{
|
||||||
|
alias Unqual = U;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
alias Unqual = T;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
///
|
||||||
|
pure nothrow @safe @nogc unittest
|
||||||
|
{
|
||||||
|
static assert(is(Unqual!bool == bool));
|
||||||
|
static assert(is(Unqual!(immutable bool) == bool));
|
||||||
|
static assert(is(Unqual!(inout bool) == bool));
|
||||||
|
static assert(is(Unqual!(inout const bool) == bool));
|
||||||
|
static assert(is(Unqual!(shared bool) == bool));
|
||||||
|
static assert(is(Unqual!(shared const bool) == bool));
|
||||||
|
static assert(is(Unqual!(shared inout const bool) == bool));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* If $(D_PARAM T) is an $(D_KEYWORD enum), $(D_INLINECODE OriginalType!T)
|
||||||
|
* evaluates to the most base type of that $(D_KEYWORD enum) and to
|
||||||
|
* $(D_PARAM T) otherwise.
|
||||||
|
*
|
||||||
|
* Params:
|
||||||
|
* T = A type.
|
||||||
|
*
|
||||||
|
* Returns: Base type of the $(D_KEYWORD enum) $(D_PARAM T) or $(D_PARAM T)
|
||||||
|
* itself.
|
||||||
|
*/
|
||||||
|
template OriginalType(T)
|
||||||
|
{
|
||||||
|
static if (is(T U == enum))
|
||||||
|
{
|
||||||
|
alias OriginalType = OriginalType!U;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
alias OriginalType = T;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
///
|
||||||
|
pure nothrow @safe @nogc unittest
|
||||||
|
{
|
||||||
|
enum E1 : const(int)
|
||||||
|
{
|
||||||
|
n = 0,
|
||||||
|
}
|
||||||
|
enum E2 : bool
|
||||||
|
{
|
||||||
|
t = true,
|
||||||
|
}
|
||||||
|
enum E3 : E2
|
||||||
|
{
|
||||||
|
t = E2.t,
|
||||||
|
}
|
||||||
|
enum E4 : const(E2)
|
||||||
|
{
|
||||||
|
t = E2.t,
|
||||||
|
}
|
||||||
|
|
||||||
|
static assert(is(OriginalType!E1 == const int));
|
||||||
|
static assert(is(OriginalType!E2 == bool));
|
||||||
|
static assert(is(OriginalType!E3 == bool));
|
||||||
|
static assert(is(OriginalType!E4 == bool));
|
||||||
|
static assert(is(OriginalType!(const E4) == bool));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Copies constness of $(D_PARAM From) to $(D_PARAM To).
|
||||||
|
*
|
||||||
|
* The following type qualifiers affect the constness and hence are copied:
|
||||||
|
* $(UL
|
||||||
|
* $(LI const)
|
||||||
|
* $(LI immutable)
|
||||||
|
* $(LI inout)
|
||||||
|
* $(LI inout const)
|
||||||
|
* )
|
||||||
|
*
|
||||||
|
* Params:
|
||||||
|
* From = Source type.
|
||||||
|
* To = Target type.
|
||||||
|
*
|
||||||
|
* Returns: $(D_PARAM To) with the constness of $(D_PARAM From).
|
||||||
|
*
|
||||||
|
* See_Also: $(D_PSYMBOL CopyTypeQualifiers).
|
||||||
|
*/
|
||||||
|
template CopyConstness(From, To)
|
||||||
|
{
|
||||||
|
static if (is(From T == immutable T))
|
||||||
|
{
|
||||||
|
alias CopyConstness = immutable To;
|
||||||
|
}
|
||||||
|
else static if (is(From T == const T) || is(From T == shared const T))
|
||||||
|
{
|
||||||
|
alias CopyConstness = const To;
|
||||||
|
}
|
||||||
|
else static if (is(From T == inout T) || is(From T == shared inout T))
|
||||||
|
{
|
||||||
|
alias CopyConstness = inout To;
|
||||||
|
}
|
||||||
|
else static if (is(From T == inout const T)
|
||||||
|
|| is(From T == shared inout const T))
|
||||||
|
{
|
||||||
|
alias CopyConstness = inout const To;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
alias CopyConstness = To;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
///
|
||||||
|
pure nothrow @safe @nogc unittest
|
||||||
|
{
|
||||||
|
static assert(is(CopyConstness!(int, char) == char));
|
||||||
|
static assert(is(CopyConstness!(const int, char) == const char));
|
||||||
|
static assert(is(CopyConstness!(immutable int, char) == immutable char));
|
||||||
|
static assert(is(CopyConstness!(inout int, char) == inout char));
|
||||||
|
static assert(is(CopyConstness!(inout const int, char) == inout const char));
|
||||||
|
|
||||||
|
static assert(is(CopyConstness!(shared int, char) == char));
|
||||||
|
static assert(is(CopyConstness!(shared const int, char) == const char));
|
||||||
|
static assert(is(CopyConstness!(shared inout int, char) == inout char));
|
||||||
|
static assert(is(CopyConstness!(shared inout const int, char) == inout const char));
|
||||||
|
|
||||||
|
static assert(is(CopyConstness!(const int, shared char) == shared const char));
|
||||||
|
static assert(is(CopyConstness!(const int, immutable char) == immutable char));
|
||||||
|
static assert(is(CopyConstness!(immutable int, const char) == immutable char));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Copies type qualifiers of $(D_PARAM From) to $(D_PARAM To).
|
||||||
|
*
|
||||||
|
* Type qualifiers copied are:
|
||||||
|
* $(UL
|
||||||
|
* $(LI const)
|
||||||
|
* $(LI immutable)
|
||||||
|
* $(LI inout)
|
||||||
|
* $(LI shared)
|
||||||
|
* )
|
||||||
|
* and combinations of these.
|
||||||
|
*
|
||||||
|
* Params:
|
||||||
|
* From = Source type.
|
||||||
|
* To = Target type.
|
||||||
|
*
|
||||||
|
* Returns: $(D_PARAM To) with the type qualifiers of $(D_PARAM From).
|
||||||
|
*
|
||||||
|
* See_Also: $(D_PSYMBOL CopyConstness).
|
||||||
|
*/
|
||||||
|
template CopyTypeQualifiers(From, To)
|
||||||
|
{
|
||||||
|
static if (is(From T == immutable T))
|
||||||
|
{
|
||||||
|
alias CopyTypeQualifiers = immutable To;
|
||||||
|
}
|
||||||
|
else static if (is(From T == const T))
|
||||||
|
{
|
||||||
|
alias CopyTypeQualifiers = const To;
|
||||||
|
}
|
||||||
|
else static if (is(From T == shared T))
|
||||||
|
{
|
||||||
|
alias CopyTypeQualifiers = shared To;
|
||||||
|
}
|
||||||
|
else static if (is(From T == shared const T))
|
||||||
|
{
|
||||||
|
alias CopyTypeQualifiers = shared const To;
|
||||||
|
}
|
||||||
|
else static if (is(From T == inout T))
|
||||||
|
{
|
||||||
|
alias CopyTypeQualifiers = inout To;
|
||||||
|
}
|
||||||
|
else static if (is(From T == shared inout T))
|
||||||
|
{
|
||||||
|
alias CopyTypeQualifiers = shared inout To;
|
||||||
|
}
|
||||||
|
else static if (is(From T == inout const T))
|
||||||
|
{
|
||||||
|
alias CopyTypeQualifiers = inout const To;
|
||||||
|
}
|
||||||
|
else static if (is(From T == shared inout const T))
|
||||||
|
{
|
||||||
|
alias CopyTypeQualifiers = shared inout const To;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
alias CopyTypeQualifiers = To;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
///
|
||||||
|
pure nothrow @safe @nogc unittest
|
||||||
|
{
|
||||||
|
static assert(is(CopyTypeQualifiers!(int, char) == char));
|
||||||
|
static assert(is(CopyTypeQualifiers!(const int, char) == const char));
|
||||||
|
static assert(is(CopyTypeQualifiers!(immutable int, char) == immutable char));
|
||||||
|
static assert(is(CopyTypeQualifiers!(inout int, char) == inout char));
|
||||||
|
static assert(is(CopyTypeQualifiers!(inout const int, char) == inout const char));
|
||||||
|
|
||||||
|
static assert(is(CopyTypeQualifiers!(shared int, char) == shared char));
|
||||||
|
static assert(is(CopyTypeQualifiers!(shared const int, char) == shared const char));
|
||||||
|
static assert(is(CopyTypeQualifiers!(shared inout int, char) == shared inout char));
|
||||||
|
static assert(is(CopyTypeQualifiers!(shared inout const int, char) == shared inout const char));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Evaluates to the unsigned counterpart of the integral type $(D_PARAM T) preserving all type qualifiers.
|
||||||
|
* If $(D_PARAM T) is already unsigned, $(D_INLINECODE Unsigned!T) aliases $(D_PARAM T).
|
||||||
|
*
|
||||||
|
* Params:
|
||||||
|
* T = A type.
|
||||||
|
*
|
||||||
|
* Returns: Unsigned counterpart of $(D_PARAM T).
|
||||||
|
*
|
||||||
|
* See_Also: $(D_PSYMBOL isSigned).
|
||||||
|
*/
|
||||||
|
template Unsigned(T)
|
||||||
|
if (isIntegral!T)
|
||||||
|
{
|
||||||
|
alias UnqualedType = Unqual!(OriginalType!T);
|
||||||
|
static if (is(UnqualedType == byte))
|
||||||
|
{
|
||||||
|
alias Unsigned = CopyTypeQualifiers!(T, ubyte);
|
||||||
|
}
|
||||||
|
else static if (is(UnqualedType == short))
|
||||||
|
{
|
||||||
|
alias Unsigned = CopyTypeQualifiers!(T, ushort);
|
||||||
|
}
|
||||||
|
else static if (is(UnqualedType == int))
|
||||||
|
{
|
||||||
|
alias Unsigned = CopyTypeQualifiers!(T, uint);
|
||||||
|
}
|
||||||
|
else static if (is(UnqualedType == long))
|
||||||
|
{
|
||||||
|
alias Unsigned = CopyTypeQualifiers!(T, ulong);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
alias Unsigned = T;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
///
|
||||||
|
pure nothrow @safe @nogc unittest
|
||||||
|
{
|
||||||
|
static assert(is(Unsigned!byte == ubyte));
|
||||||
|
static assert(is(Unsigned!short == ushort));
|
||||||
|
static assert(is(Unsigned!int == uint));
|
||||||
|
static assert(is(Unsigned!long == ulong));
|
||||||
|
|
||||||
|
static assert(is(Unsigned!(const byte) == const ubyte));
|
||||||
|
static assert(is(Unsigned!(shared byte) == shared ubyte));
|
||||||
|
static assert(is(Unsigned!(shared const byte) == shared const ubyte));
|
||||||
|
|
||||||
|
static assert(!is(Unsigned!float));
|
||||||
|
static assert(is(Unsigned!ubyte == ubyte));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Evaluates to the signed counterpart of the integral type $(D_PARAM T) preserving all type qualifiers.
|
||||||
|
* If $(D_PARAM T) is already signed, $(D_INLINECODE Signed!T) aliases $(D_PARAM T).
|
||||||
|
*
|
||||||
|
* Params:
|
||||||
|
* T = A type.
|
||||||
|
*
|
||||||
|
* Returns: Signed counterpart of $(D_PARAM T).
|
||||||
|
*
|
||||||
|
* See_Also: $(D_PSYMBOL isUnsigned).
|
||||||
|
*/
|
||||||
|
template Signed(T)
|
||||||
|
if (isIntegral!T)
|
||||||
|
{
|
||||||
|
alias UnqualedType = Unqual!(OriginalType!T);
|
||||||
|
static if (is(UnqualedType == ubyte))
|
||||||
|
{
|
||||||
|
alias Signed = CopyTypeQualifiers!(T, byte);
|
||||||
|
}
|
||||||
|
else static if (is(UnqualedType == ushort))
|
||||||
|
{
|
||||||
|
alias Signed = CopyTypeQualifiers!(T, short);
|
||||||
|
}
|
||||||
|
else static if (is(UnqualedType == uint))
|
||||||
|
{
|
||||||
|
alias Signed = CopyTypeQualifiers!(T, int);
|
||||||
|
}
|
||||||
|
else static if (is(UnqualedType == ulong))
|
||||||
|
{
|
||||||
|
alias Signed = CopyTypeQualifiers!(T, long);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
alias Signed = T;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
///
|
||||||
|
pure nothrow @safe @nogc unittest
|
||||||
|
{
|
||||||
|
static assert(is(Signed!ubyte == byte));
|
||||||
|
static assert(is(Signed!ushort == short));
|
||||||
|
static assert(is(Signed!uint == int));
|
||||||
|
static assert(is(Signed!ulong == long));
|
||||||
|
|
||||||
|
static assert(is(Signed!(const ubyte) == const byte));
|
||||||
|
static assert(is(Signed!(shared ubyte) == shared byte));
|
||||||
|
static assert(is(Signed!(shared const ubyte) == shared const byte));
|
||||||
|
|
||||||
|
static assert(!is(Signed!float));
|
||||||
|
static assert(is(Signed!byte == byte));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieves the target type `U` of a pointer `U*`.
|
||||||
|
*
|
||||||
|
* Params:
|
||||||
|
* T = Pointer type.
|
||||||
|
*
|
||||||
|
* Returns: Pointer target type.
|
||||||
|
*/
|
||||||
|
template PointerTarget(T)
|
||||||
|
{
|
||||||
|
static if (is(T U : U*))
|
||||||
|
{
|
||||||
|
alias PointerTarget = U;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
static assert(T.stringof ~ " isn't a pointer type");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
///
|
||||||
|
pure nothrow @safe @nogc unittest
|
||||||
|
{
|
||||||
|
static assert(is(PointerTarget!(bool*) == bool));
|
||||||
|
static assert(is(PointerTarget!(const bool*) == const bool));
|
||||||
|
static assert(is(PointerTarget!(const shared bool*) == const shared bool));
|
||||||
|
static assert(!is(PointerTarget!bool));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Params:
|
||||||
|
* T = The type of the associative array.
|
||||||
|
*
|
||||||
|
* Returns: The key type of the associative array $(D_PARAM T).
|
||||||
|
*/
|
||||||
|
template KeyType(T)
|
||||||
|
{
|
||||||
|
static if (is(T V : V[K], K))
|
||||||
|
{
|
||||||
|
alias KeyType = K;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
static assert(false, T.stringof ~ " isn't an associative array");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
///
|
||||||
|
pure nothrow @safe @nogc unittest
|
||||||
|
{
|
||||||
|
static assert(is(KeyType!(int[string]) == string));
|
||||||
|
static assert(!is(KeyType!(int[15])));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Params:
|
||||||
|
* T = The type of the associative array.
|
||||||
|
*
|
||||||
|
* Returns: The value type of the associative array $(D_PARAM T).
|
||||||
|
*/
|
||||||
|
template ValueType(T)
|
||||||
|
{
|
||||||
|
static if (is(T V : V[K], K))
|
||||||
|
{
|
||||||
|
alias ValueType = V;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
static assert(false, T.stringof ~ " isn't an associative array");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
///
|
||||||
|
pure nothrow @safe @nogc unittest
|
||||||
|
{
|
||||||
|
static assert(is(ValueType!(int[string]) == int));
|
||||||
|
static assert(!is(ValueType!(int[15])));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Params:
|
||||||
|
* T = Scalar type.
|
||||||
|
*
|
||||||
|
* Returns: The type $(D_PARAM T) will promote to.
|
||||||
|
*
|
||||||
|
* See_Also: $(LINK2 https://dlang.org/spec/type.html#integer-promotions,
|
||||||
|
* Integer Promotions).
|
||||||
|
*/
|
||||||
|
template Promoted(T)
|
||||||
|
if (isScalarType!T)
|
||||||
|
{
|
||||||
|
alias Promoted = CopyTypeQualifiers!(T, typeof(T.init + T.init));
|
||||||
|
}
|
||||||
|
|
||||||
|
///
|
||||||
|
pure nothrow @safe @nogc unittest
|
||||||
|
{
|
||||||
|
static assert(is(Promoted!bool == int));
|
||||||
|
static assert(is(Promoted!byte == int));
|
||||||
|
static assert(is(Promoted!ubyte == int));
|
||||||
|
static assert(is(Promoted!short == int));
|
||||||
|
static assert(is(Promoted!ushort == int));
|
||||||
|
static assert(is(Promoted!char == int));
|
||||||
|
static assert(is(Promoted!wchar == int));
|
||||||
|
static assert(is(Promoted!dchar == uint));
|
||||||
|
|
||||||
|
static assert(is(Promoted!(const bool) == const int));
|
||||||
|
static assert(is(Promoted!(shared bool) == shared int));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds $(D_KEYWORD inout) qualifier to the type $(D_PARAM T).
|
||||||
|
*
|
||||||
|
* Params:
|
||||||
|
* T = A type.
|
||||||
|
*
|
||||||
|
* Returns: $(D_INLINECODE inout(T)).
|
||||||
|
*/
|
||||||
|
alias InoutOf(T) = inout(T);
|
||||||
|
|
||||||
|
///
|
||||||
|
pure nothrow @safe @nogc unittest
|
||||||
|
{
|
||||||
|
static assert(is(InoutOf!int == inout int));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds $(D_KEYWORD inout) qualifier to the type $(D_PARAM T).
|
||||||
|
*
|
||||||
|
* Params:
|
||||||
|
* T = A type.
|
||||||
|
*
|
||||||
|
* Returns: $(D_INLINECODE inout(T)).
|
||||||
|
*/
|
||||||
|
alias ConstOf(T) = const(T);
|
||||||
|
|
||||||
|
///
|
||||||
|
pure nothrow @safe @nogc unittest
|
||||||
|
{
|
||||||
|
static assert(is(ConstOf!int == const int));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds $(D_KEYWORD inout) qualifier to the type $(D_PARAM T).
|
||||||
|
*
|
||||||
|
* Params:
|
||||||
|
* T = A type.
|
||||||
|
*
|
||||||
|
* Returns: $(D_INLINECODE inout(T)).
|
||||||
|
*/
|
||||||
|
alias SharedOf(T) = shared(T);
|
||||||
|
|
||||||
|
///
|
||||||
|
pure nothrow @safe @nogc unittest
|
||||||
|
{
|
||||||
|
static assert(is(SharedOf!int == shared int));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds $(D_KEYWORD inout) qualifier to the type $(D_PARAM T).
|
||||||
|
*
|
||||||
|
* Params:
|
||||||
|
* T = A type.
|
||||||
|
*
|
||||||
|
* Returns: $(D_INLINECODE inout(T)).
|
||||||
|
*/
|
||||||
|
alias SharedInoutOf(T) = shared(inout T);
|
||||||
|
|
||||||
|
///
|
||||||
|
pure nothrow @safe @nogc unittest
|
||||||
|
{
|
||||||
|
static assert(is(SharedInoutOf!int == shared inout int));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds $(D_KEYWORD shared const) qualifier to the type $(D_PARAM T).
|
||||||
|
*
|
||||||
|
* Params:
|
||||||
|
* T = A type.
|
||||||
|
*
|
||||||
|
* Returns: $(D_INLINECODE shared(const T)).
|
||||||
|
*/
|
||||||
|
alias SharedConstOf(T) = shared(const T);
|
||||||
|
|
||||||
|
///
|
||||||
|
pure nothrow @safe @nogc unittest
|
||||||
|
{
|
||||||
|
static assert(is(SharedConstOf!int == shared const int));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds $(D_KEYWORD immutable) qualifier to the type $(D_PARAM T).
|
||||||
|
*
|
||||||
|
* Params:
|
||||||
|
* T = A type.
|
||||||
|
*
|
||||||
|
* Returns: $(D_INLINECODE immutable(T)).
|
||||||
|
*/
|
||||||
|
alias ImmutableOf(T) = immutable(T);
|
||||||
|
|
||||||
|
///
|
||||||
|
pure nothrow @safe @nogc unittest
|
||||||
|
{
|
||||||
|
static assert(is(ImmutableOf!int == immutable int));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds $(D_KEYWORD inout const) qualifier to the type $(D_PARAM T).
|
||||||
|
*
|
||||||
|
* Params:
|
||||||
|
* T = A type.
|
||||||
|
*
|
||||||
|
* Returns: $(D_INLINECODE inout(const T)).
|
||||||
|
*/
|
||||||
|
alias InoutConstOf(T) = inout(const T);
|
||||||
|
|
||||||
|
///
|
||||||
|
pure nothrow @safe @nogc unittest
|
||||||
|
{
|
||||||
|
static assert(is(InoutConstOf!int == inout const int));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds $(D_KEYWORD shared inout const) qualifier to the type $(D_PARAM T).
|
||||||
|
*
|
||||||
|
* Params:
|
||||||
|
* T = A type.
|
||||||
|
*
|
||||||
|
* Returns: $(D_INLINECODE shared(inout const T)).
|
||||||
|
*/
|
||||||
|
alias SharedInoutConstOf(T) = shared(inout const T);
|
||||||
|
|
||||||
|
///
|
||||||
|
pure nothrow @safe @nogc unittest
|
||||||
|
{
|
||||||
|
static assert(is(SharedInoutConstOf!int == shared inout const int));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a template with one argument which applies all qualifiers of
|
||||||
|
* $(D_PARAM T) on its argument if instantiated.
|
||||||
|
*
|
||||||
|
* Params:
|
||||||
|
* T = A type.
|
||||||
|
*
|
||||||
|
* Returns: $(D_INLINECODE shared(inout const T)).
|
||||||
|
*/
|
||||||
|
template QualifierOf(T)
|
||||||
|
{
|
||||||
|
static if (is(T U == const U))
|
||||||
|
{
|
||||||
|
alias QualifierOf = ConstOf;
|
||||||
|
}
|
||||||
|
else static if (is(T U == immutable U))
|
||||||
|
{
|
||||||
|
alias QualifierOf = ImmutableOf;
|
||||||
|
}
|
||||||
|
else static if (is(T U == inout U))
|
||||||
|
{
|
||||||
|
alias QualifierOf = InoutOf;
|
||||||
|
}
|
||||||
|
else static if (is(T U == inout const U))
|
||||||
|
{
|
||||||
|
alias QualifierOf = InoutConstOf;
|
||||||
|
}
|
||||||
|
else static if (is(T U == shared U))
|
||||||
|
{
|
||||||
|
alias QualifierOf = SharedOf;
|
||||||
|
}
|
||||||
|
else static if (is(T U == shared const U))
|
||||||
|
{
|
||||||
|
alias QualifierOf = SharedConstOf;
|
||||||
|
}
|
||||||
|
else static if (is(T U == shared inout U))
|
||||||
|
{
|
||||||
|
alias QualifierOf = SharedInoutOf;
|
||||||
|
}
|
||||||
|
else static if (is(T U == shared inout const U))
|
||||||
|
{
|
||||||
|
alias QualifierOf = SharedInoutConstOf;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
alias QualifierOf(T) = T;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
///
|
||||||
|
pure nothrow @safe @nogc unittest
|
||||||
|
{
|
||||||
|
alias MutableOf = QualifierOf!int;
|
||||||
|
static assert(is(MutableOf!uint == uint));
|
||||||
|
|
||||||
|
alias ConstOf = QualifierOf!(const int);
|
||||||
|
static assert(is(ConstOf!uint == const uint));
|
||||||
|
|
||||||
|
alias InoutOf = QualifierOf!(inout int);
|
||||||
|
static assert(is(InoutOf!uint == inout uint));
|
||||||
|
|
||||||
|
alias InoutConstOf = QualifierOf!(inout const int);
|
||||||
|
static assert(is(InoutConstOf!uint == inout const uint));
|
||||||
|
|
||||||
|
alias ImmutableOf = QualifierOf!(immutable int);
|
||||||
|
static assert(is(ImmutableOf!uint == immutable uint));
|
||||||
|
|
||||||
|
alias SharedOf = QualifierOf!(shared int);
|
||||||
|
static assert(is(SharedOf!uint == shared uint));
|
||||||
|
|
||||||
|
alias SharedConstOf = QualifierOf!(shared const int);
|
||||||
|
static assert(is(SharedConstOf!uint == shared const uint));
|
||||||
|
|
||||||
|
alias SharedInoutOf = QualifierOf!(shared inout int);
|
||||||
|
static assert(is(SharedInoutOf!uint == shared inout uint));
|
||||||
|
|
||||||
|
alias SharedInoutConstOf = QualifierOf!(shared inout const int);
|
||||||
|
static assert(is(SharedInoutConstOf!uint == shared inout const uint));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Determines the type of $(D_PARAM T). If $(D_PARAM T) is already a type,
|
||||||
|
* $(D_PSYMBOL TypeOf) aliases itself to $(D_PARAM T).
|
||||||
|
*
|
||||||
|
* $(D_PSYMBOL TypeOf) evaluates to $(D_KEYWORD void) for template arguments.
|
||||||
|
*
|
||||||
|
* The symbols that don't have a type and aren't types cannot be used as
|
||||||
|
* arguments to $(D_PSYMBOL TypeOf).
|
||||||
|
*
|
||||||
|
* Params:
|
||||||
|
* T = Expression, type or template.
|
||||||
|
*
|
||||||
|
* Returns: The type of $(D_PARAM T).
|
||||||
|
*/
|
||||||
|
alias TypeOf(T) = T;
|
||||||
|
|
||||||
|
/// Ditto.
|
||||||
|
template TypeOf(alias T)
|
||||||
|
if (isExpressions!T || isTemplate!T)
|
||||||
|
{
|
||||||
|
alias TypeOf = typeof(T);
|
||||||
|
}
|
||||||
|
|
||||||
|
///
|
||||||
|
pure nothrow @safe @nogc unittest
|
||||||
|
{
|
||||||
|
struct S(T)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
static assert(is(TypeOf!S == void));
|
||||||
|
static assert(is(TypeOf!int == int));
|
||||||
|
static assert(is(TypeOf!true == bool));
|
||||||
|
static assert(!is(TypeOf!(tanya.meta)));
|
||||||
|
}
|
@ -9,26 +9,15 @@
|
|||||||
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
||||||
* Mozilla Public License, v. 2.0).
|
* Mozilla Public License, v. 2.0).
|
||||||
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
||||||
|
* Source: $(LINK2 https://github.com/caraus-ecms/tanya/blob/master/source/tanya/net/inet.d,
|
||||||
|
* tanya/net/inet.d)
|
||||||
*/
|
*/
|
||||||
module tanya.net.inet;
|
module tanya.net.inet;
|
||||||
|
|
||||||
import std.math;
|
import std.math;
|
||||||
import std.range.primitives;
|
import std.range.primitives;
|
||||||
import std.traits;
|
import tanya.meta.trait;
|
||||||
|
import tanya.meta.transform;
|
||||||
version (unittest)
|
|
||||||
{
|
|
||||||
version (Windows)
|
|
||||||
{
|
|
||||||
import core.sys.windows.winsock2;
|
|
||||||
version = PlattformUnittest;
|
|
||||||
}
|
|
||||||
else version (Posix)
|
|
||||||
{
|
|
||||||
import core.sys.posix.arpa.inet;
|
|
||||||
version = PlattformUnittest;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Represents an unsigned integer as an $(D_KEYWORD ubyte) range.
|
* Represents an unsigned integer as an $(D_KEYWORD ubyte) range.
|
||||||
@ -210,79 +199,6 @@ private unittest
|
|||||||
static assert(!is(NetworkOrder!1));
|
static assert(!is(NetworkOrder!1));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Tests against the system's htonl, htons.
|
|
||||||
version (PlattformUnittest)
|
|
||||||
{
|
|
||||||
private unittest
|
|
||||||
{
|
|
||||||
for (uint counter; counter <= 8 * uint.sizeof; ++counter)
|
|
||||||
{
|
|
||||||
const value = pow(2, counter) - 1;
|
|
||||||
const inNetworkOrder = htonl(value);
|
|
||||||
const p = cast(ubyte*) &inNetworkOrder;
|
|
||||||
auto networkOrder = NetworkOrder!4(value);
|
|
||||||
|
|
||||||
assert(networkOrder.length == 4);
|
|
||||||
assert(!networkOrder.empty);
|
|
||||||
assert(networkOrder.front == *p);
|
|
||||||
assert(networkOrder.back == *(p + 3));
|
|
||||||
|
|
||||||
networkOrder.popBack();
|
|
||||||
assert(networkOrder.length == 3);
|
|
||||||
assert(networkOrder.front == *p);
|
|
||||||
assert(networkOrder.back == *(p + 2));
|
|
||||||
|
|
||||||
networkOrder.popFront();
|
|
||||||
assert(networkOrder.length == 2);
|
|
||||||
assert(networkOrder.front == *(p + 1));
|
|
||||||
assert(networkOrder.back == *(p + 2));
|
|
||||||
|
|
||||||
networkOrder.popFront();
|
|
||||||
assert(networkOrder.length == 1);
|
|
||||||
assert(networkOrder.front == *(p + 2));
|
|
||||||
assert(networkOrder.back == *(p + 2));
|
|
||||||
|
|
||||||
networkOrder.popBack();
|
|
||||||
assert(networkOrder.length == 0);
|
|
||||||
assert(networkOrder.empty);
|
|
||||||
}
|
|
||||||
|
|
||||||
for (ushort counter; counter <= 8 * ushort.sizeof; ++counter)
|
|
||||||
{
|
|
||||||
const value = cast(ushort) (pow(2, counter) - 1);
|
|
||||||
const inNetworkOrder = htons(value);
|
|
||||||
const p = cast(ubyte*) &inNetworkOrder;
|
|
||||||
|
|
||||||
auto networkOrder = NetworkOrder!2(value);
|
|
||||||
|
|
||||||
assert(networkOrder.length == 2);
|
|
||||||
assert(!networkOrder.empty);
|
|
||||||
assert(networkOrder.front == *p);
|
|
||||||
assert(networkOrder.back == *(p + 1));
|
|
||||||
|
|
||||||
networkOrder.popBack();
|
|
||||||
assert(networkOrder.length == 1);
|
|
||||||
assert(networkOrder.front == *p);
|
|
||||||
assert(networkOrder.back == *p);
|
|
||||||
|
|
||||||
networkOrder.popBack();
|
|
||||||
assert(networkOrder.length == 0);
|
|
||||||
assert(networkOrder.empty);
|
|
||||||
|
|
||||||
networkOrder = NetworkOrder!2(value);
|
|
||||||
|
|
||||||
networkOrder.popFront();
|
|
||||||
assert(networkOrder.length == 1);
|
|
||||||
assert(networkOrder.front == *(p + 1));
|
|
||||||
assert(networkOrder.back == *(p + 1));
|
|
||||||
|
|
||||||
networkOrder.popFront();
|
|
||||||
assert(networkOrder.length == 0);
|
|
||||||
assert(networkOrder.empty);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Converts the $(D_KEYWORD ubyte) input range $(D_PARAM range) to
|
* Converts the $(D_KEYWORD ubyte) input range $(D_PARAM range) to
|
||||||
* $(D_PARAM T).
|
* $(D_PARAM T).
|
||||||
@ -328,29 +244,3 @@ pure nothrow @safe @nogc unittest
|
|||||||
auto networkOrder = NetworkOrder!4(value);
|
auto networkOrder = NetworkOrder!4(value);
|
||||||
assert(networkOrder.toHostOrder() == value);
|
assert(networkOrder.toHostOrder() == value);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Tests against the system's htonl, htons.
|
|
||||||
version (PlattformUnittest)
|
|
||||||
{
|
|
||||||
private unittest
|
|
||||||
{
|
|
||||||
for (uint counter; counter <= 8 * uint.sizeof; ++counter)
|
|
||||||
{
|
|
||||||
const value = pow(2, counter) - 1;
|
|
||||||
const inNetworkOrder = htonl(value);
|
|
||||||
const p = cast(ubyte*) &inNetworkOrder;
|
|
||||||
auto networkOrder = NetworkOrder!4(value);
|
|
||||||
|
|
||||||
assert(p[0 .. uint.sizeof].toHostOrder() == value);
|
|
||||||
}
|
|
||||||
for (ushort counter; counter <= 8 * ushort.sizeof; ++counter)
|
|
||||||
{
|
|
||||||
const value = cast(ushort) (pow(2, counter) - 1);
|
|
||||||
const inNetworkOrder = htons(value);
|
|
||||||
const p = cast(ubyte*) &inNetworkOrder;
|
|
||||||
auto networkOrder = NetworkOrder!2(value);
|
|
||||||
|
|
||||||
assert(p[0 .. ushort.sizeof].toHostOrder() == value);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
@ -9,6 +9,8 @@
|
|||||||
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
||||||
* Mozilla Public License, v. 2.0).
|
* Mozilla Public License, v. 2.0).
|
||||||
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
||||||
|
* Source: $(LINK2 https://github.com/caraus-ecms/tanya/blob/master/source/tanya/net/package.d,
|
||||||
|
* tanya/net/package.d)
|
||||||
*/
|
*/
|
||||||
module tanya.net;
|
module tanya.net;
|
||||||
|
|
||||||
|
@ -9,11 +9,12 @@
|
|||||||
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
||||||
* Mozilla Public License, v. 2.0).
|
* Mozilla Public License, v. 2.0).
|
||||||
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
||||||
|
* Source: $(LINK2 https://github.com/caraus-ecms/tanya/blob/master/source/tanya/net/uri.d,
|
||||||
|
* tanya/net/uri.d)
|
||||||
*/
|
*/
|
||||||
module tanya.net.uri;
|
module tanya.net.uri;
|
||||||
|
|
||||||
import std.ascii : isAlphaNum, isDigit;
|
import std.ascii : isAlphaNum, isDigit;
|
||||||
import std.traits : isSomeString;
|
|
||||||
import std.uni : isAlpha, isNumber;
|
import std.uni : isAlpha, isNumber;
|
||||||
import tanya.memory;
|
import tanya.memory;
|
||||||
|
|
||||||
@ -70,17 +71,16 @@ struct URL
|
|||||||
/**
|
/**
|
||||||
* Attempts to parse an URL from a string.
|
* Attempts to parse an URL from a string.
|
||||||
* Output string data (scheme, user, etc.) are just slices of input string
|
* Output string data (scheme, user, etc.) are just slices of input string
|
||||||
* (e.g., no memory allocation and copying).
|
* (i.e., no memory allocation and copying).
|
||||||
*
|
*
|
||||||
* Params:
|
* Params:
|
||||||
* source = The string containing the URL.
|
* source = The string containing the URL.
|
||||||
*
|
*
|
||||||
* Throws: $(D_PSYMBOL URIException) if the URL is malformed.
|
* Throws: $(D_PSYMBOL URIException) if the URL is malformed.
|
||||||
*/
|
*/
|
||||||
this(const char[] source) @nogc
|
this(const char[] source) pure @nogc
|
||||||
{
|
{
|
||||||
auto value = source;
|
ptrdiff_t pos = -1, endPos = source.length, start;
|
||||||
ptrdiff_t pos = -1, endPos = value.length, start;
|
|
||||||
|
|
||||||
foreach (i, ref c; source)
|
foreach (i, ref c; source)
|
||||||
{
|
{
|
||||||
@ -88,57 +88,54 @@ struct URL
|
|||||||
{
|
{
|
||||||
pos = i;
|
pos = i;
|
||||||
}
|
}
|
||||||
if (endPos == value.length && (c == '?' || c == '#'))
|
if (endPos == source.length && (c == '?' || c == '#'))
|
||||||
{
|
{
|
||||||
endPos = i;
|
endPos = i;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if the colon is a part of the scheme or the port and parse
|
// Check if the colon is a part of the scheme or the port and parse
|
||||||
// the appropriate part
|
// the appropriate part.
|
||||||
if (value.length > 1 && value[0] == '/' && value[1] == '/')
|
if (source.length > 1 && source[0] == '/' && source[1] == '/')
|
||||||
{
|
{
|
||||||
// Relative scheme
|
// Relative scheme.
|
||||||
start = 2;
|
start = 2;
|
||||||
}
|
}
|
||||||
else if (pos > 0)
|
else if (pos > 0)
|
||||||
{
|
{
|
||||||
// Validate scheme
|
// Validate scheme:
|
||||||
// [ toLower(alpha) | digit | "+" | "-" | "." ]
|
// [ toLower(alpha) | digit | "+" | "-" | "." ]
|
||||||
foreach (ref c; value[0 .. pos])
|
foreach (ref c; source[0 .. pos])
|
||||||
{
|
{
|
||||||
if (!c.isAlphaNum && c != '+' && c != '-' && c != '.')
|
if (!c.isAlphaNum && c != '+' && c != '-' && c != '.')
|
||||||
{
|
{
|
||||||
if (endPos > pos)
|
|
||||||
{
|
|
||||||
if (!parsePort(value[pos .. $]))
|
|
||||||
{
|
|
||||||
throw make!URIException(defaultAllocator,
|
|
||||||
"Failed to parse port");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
goto ParsePath;
|
goto ParsePath;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (value.length == pos + 1) // only scheme is available
|
if (source.length == pos + 1) // only "scheme:" is available.
|
||||||
{
|
{
|
||||||
this.scheme = value[0 .. $ - 1];
|
this.scheme = source[0 .. $ - 1];
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
else if (value.length > pos + 1 && value[pos + 1] == '/')
|
else if (source.length > pos + 1 && source[pos + 1] == '/')
|
||||||
{
|
{
|
||||||
this.scheme = value[0 .. pos];
|
this.scheme = source[0 .. pos];
|
||||||
|
|
||||||
if (value.length > pos + 2 && value[pos + 2] == '/')
|
if (source.length > pos + 2 && source[pos + 2] == '/')
|
||||||
{
|
{
|
||||||
start = pos + 3;
|
start = pos + 3;
|
||||||
if (this.scheme == "file"
|
|
||||||
&& value.length > start
|
if (source.length <= start)
|
||||||
&& value[start] == '/')
|
|
||||||
{
|
{
|
||||||
// Windows drive letters
|
// Only "scheme://" is available.
|
||||||
if (value.length - start > 2 && value[start + 2] == ':')
|
return;
|
||||||
|
}
|
||||||
|
if (this.scheme == "file" && source[start] == '/')
|
||||||
|
{
|
||||||
|
// Windows drive letters.
|
||||||
|
if (source.length - start > 2
|
||||||
|
&& source[start + 2] == ':')
|
||||||
{
|
{
|
||||||
++start;
|
++start;
|
||||||
}
|
}
|
||||||
@ -151,20 +148,21 @@ struct URL
|
|||||||
goto ParsePath;
|
goto ParsePath;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else // certain schemas like mailto: and zlib: may not have any / after them
|
else
|
||||||
{
|
{
|
||||||
|
// Schemas like mailto: and zlib: may not have any slash after
|
||||||
if (!parsePort(value[pos .. $]))
|
// them.
|
||||||
|
if (!parsePort(source[pos .. $]))
|
||||||
{
|
{
|
||||||
this.scheme = value[0 .. pos];
|
this.scheme = source[0 .. pos];
|
||||||
start = pos + 1;
|
start = pos + 1;
|
||||||
goto ParsePath;
|
goto ParsePath;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (pos == 0 && parsePort(value[pos .. $]))
|
else if (pos == 0 && parsePort(source[pos .. $]))
|
||||||
{
|
{
|
||||||
// An URL shouldn't begin with a port number
|
// An URL shouldn't begin with a port number.
|
||||||
throw defaultAllocator.make!URIException("URL begins with port");
|
throw defaultAllocator.make!URIException("URL begins with port");
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@ -172,33 +170,33 @@ struct URL
|
|||||||
goto ParsePath;
|
goto ParsePath;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse host
|
// Parse host.
|
||||||
pos = -1;
|
pos = -1;
|
||||||
for (ptrdiff_t i = start; i < value.length; ++i)
|
for (ptrdiff_t i = start; i < source.length; ++i)
|
||||||
{
|
{
|
||||||
if (value[i] == '@')
|
if (source[i] == '@')
|
||||||
{
|
{
|
||||||
pos = i;
|
pos = i;
|
||||||
}
|
}
|
||||||
else if (value[i] == '/')
|
else if (source[i] == '/')
|
||||||
{
|
{
|
||||||
endPos = i;
|
endPos = i;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check for login and password
|
// Check for login and password.
|
||||||
if (pos != -1)
|
if (pos != -1)
|
||||||
{
|
{
|
||||||
// *( unreserved / pct-encoded / sub-delims / ":" )
|
// *( unreserved / pct-encoded / sub-delims / ":" )
|
||||||
foreach (i, c; value[start .. pos])
|
foreach (i, c; source[start .. pos])
|
||||||
{
|
{
|
||||||
if (c == ':')
|
if (c == ':')
|
||||||
{
|
{
|
||||||
if (this.user is null)
|
if (this.user is null)
|
||||||
{
|
{
|
||||||
this.user = value[start .. start + i];
|
this.user = source[start .. start + i];
|
||||||
this.pass = value[start + i + 1 .. pos];
|
this.pass = source[start + i + 1 .. pos];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (!c.isAlpha &&
|
else if (!c.isAlpha &&
|
||||||
@ -217,23 +215,23 @@ struct URL
|
|||||||
}
|
}
|
||||||
if (this.user is null)
|
if (this.user is null)
|
||||||
{
|
{
|
||||||
this.user = value[start .. pos];
|
this.user = source[start .. pos];
|
||||||
}
|
}
|
||||||
|
|
||||||
start = ++pos;
|
start = ++pos;
|
||||||
}
|
}
|
||||||
|
|
||||||
pos = endPos;
|
pos = endPos;
|
||||||
if (endPos <= 1 || value[start] != '[' || value[endPos - 1] != ']')
|
if (endPos <= 1 || source[start] != '[' || source[endPos - 1] != ']')
|
||||||
{
|
{
|
||||||
// Short circuit portscan
|
// Short circuit portscan.
|
||||||
// IPv6 embedded address
|
// IPv6 embedded address.
|
||||||
for (ptrdiff_t i = endPos - 1; i >= start; --i)
|
for (ptrdiff_t i = endPos - 1; i >= start; --i)
|
||||||
{
|
{
|
||||||
if (value[i] == ':')
|
if (source[i] == ':')
|
||||||
{
|
{
|
||||||
pos = i;
|
pos = i;
|
||||||
if (this.port == 0 && !parsePort(value[i .. endPos]))
|
if (this.port == 0 && !parsePort(source[i .. endPos]))
|
||||||
{
|
{
|
||||||
this.scheme = this.user = this.pass = null;
|
this.scheme = this.user = this.pass = null;
|
||||||
throw defaultAllocator.make!URIException("Invalid port");
|
throw defaultAllocator.make!URIException("Invalid port");
|
||||||
@ -243,16 +241,16 @@ struct URL
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if we have a valid host, if we don't reject the string as url
|
// Check if we have a valid host, if we don't reject the string as URL.
|
||||||
if (pos <= start)
|
if (pos <= start)
|
||||||
{
|
{
|
||||||
this.scheme = this.user = this.pass = null;
|
this.scheme = this.user = this.pass = null;
|
||||||
throw defaultAllocator.make!URIException("Invalid host");
|
throw defaultAllocator.make!URIException("Invalid host");
|
||||||
}
|
}
|
||||||
|
|
||||||
this.host = value[start .. pos];
|
this.host = source[start .. pos];
|
||||||
|
|
||||||
if (endPos == value.length)
|
if (endPos == source.length)
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -260,9 +258,9 @@ struct URL
|
|||||||
start = endPos;
|
start = endPos;
|
||||||
|
|
||||||
ParsePath:
|
ParsePath:
|
||||||
endPos = value.length;
|
endPos = source.length;
|
||||||
pos = -1;
|
pos = -1;
|
||||||
foreach (i, ref c; value[start .. $])
|
foreach (i, ref c; source[start .. $])
|
||||||
{
|
{
|
||||||
if (c == '?' && pos == -1)
|
if (c == '?' && pos == -1)
|
||||||
{
|
{
|
||||||
@ -281,15 +279,15 @@ struct URL
|
|||||||
|
|
||||||
if (pos > start)
|
if (pos > start)
|
||||||
{
|
{
|
||||||
this.path = value[start .. pos];
|
this.path = source[start .. pos];
|
||||||
}
|
}
|
||||||
if (endPos >= ++pos)
|
if (endPos >= ++pos)
|
||||||
{
|
{
|
||||||
this.query = value[pos .. endPos];
|
this.query = source[pos .. endPos];
|
||||||
}
|
}
|
||||||
if (++endPos <= value.length)
|
if (++endPos <= source.length)
|
||||||
{
|
{
|
||||||
this.fragment = value[endPos .. $];
|
this.fragment = source[endPos .. $];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -310,13 +308,9 @@ struct URL
|
|||||||
|
|
||||||
for (; i < port.length && port[i].isDigit() && i <= 6; ++i)
|
for (; i < port.length && port[i].isDigit() && i <= 6; ++i)
|
||||||
{
|
{
|
||||||
lPort += (port[i] - '0') / cast(float)(10 ^^ (i - 1));
|
lPort += (port[i] - '0') / cast(float) (10 ^^ (i - 1));
|
||||||
}
|
}
|
||||||
if (i == 1 && (i == port.length || port[i] == '/'))
|
if (i != 1 && (i == port.length || port[i] == '/'))
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
else if (i == port.length || port[i] == '/')
|
|
||||||
{
|
{
|
||||||
lPort *= 10 ^^ (i - 2);
|
lPort *= 10 ^^ (i - 2);
|
||||||
if (lPort > ushort.max)
|
if (lPort > ushort.max)
|
||||||
@ -384,7 +378,7 @@ struct URL
|
|||||||
assert(u.fragment == "fragment");
|
assert(u.fragment == "fragment");
|
||||||
}
|
}
|
||||||
|
|
||||||
private unittest
|
private @nogc unittest
|
||||||
{
|
{
|
||||||
auto u = URL("127.0.0.1");
|
auto u = URL("127.0.0.1");
|
||||||
assert(u.path == "127.0.0.1");
|
assert(u.path == "127.0.0.1");
|
||||||
@ -398,6 +392,11 @@ private unittest
|
|||||||
assert(u.host == "127.0.0.1");
|
assert(u.host == "127.0.0.1");
|
||||||
assert(u.port == 9000);
|
assert(u.port == 9000);
|
||||||
|
|
||||||
|
u = URL("127.0.0.1:80");
|
||||||
|
assert(u.host == "127.0.0.1");
|
||||||
|
assert(u.port == 80);
|
||||||
|
assert(u.path is null);
|
||||||
|
|
||||||
u = URL("//example.net");
|
u = URL("//example.net");
|
||||||
assert(u.host == "example.net");
|
assert(u.host == "example.net");
|
||||||
assert(u.scheme is null);
|
assert(u.scheme is null);
|
||||||
@ -409,6 +408,7 @@ private unittest
|
|||||||
u = URL("localhost:8080");
|
u = URL("localhost:8080");
|
||||||
assert(u.host == "localhost");
|
assert(u.host == "localhost");
|
||||||
assert(u.port == 8080);
|
assert(u.port == 8080);
|
||||||
|
assert(u.path is null);
|
||||||
|
|
||||||
u = URL("ftp:");
|
u = URL("ftp:");
|
||||||
assert(u.scheme == "ftp");
|
assert(u.scheme == "ftp");
|
||||||
@ -442,21 +442,9 @@ private unittest
|
|||||||
u = URL("zlib:/home/user/file.gz");
|
u = URL("zlib:/home/user/file.gz");
|
||||||
assert(u.scheme == "zlib");
|
assert(u.scheme == "zlib");
|
||||||
assert(u.path == "/home/user/file.gz");
|
assert(u.path == "/home/user/file.gz");
|
||||||
}
|
|
||||||
|
|
||||||
private @nogc unittest
|
u = URL("h_tp:asdf");
|
||||||
{
|
assert(u.path == "h_tp:asdf");
|
||||||
URIException exception;
|
|
||||||
try
|
|
||||||
{
|
|
||||||
auto u = URL("h_tp:asdf");
|
|
||||||
}
|
|
||||||
catch (URIException e)
|
|
||||||
{
|
|
||||||
exception = e;
|
|
||||||
}
|
|
||||||
assert(exception !is null);
|
|
||||||
defaultAllocator.dispose(exception);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private @nogc unittest
|
private @nogc unittest
|
||||||
@ -524,7 +512,7 @@ private @nogc unittest
|
|||||||
URIException exception;
|
URIException exception;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
auto u = URL(":/");
|
auto u = URL("http://blah.com:66000");
|
||||||
}
|
}
|
||||||
catch (URIException e)
|
catch (URIException e)
|
||||||
{
|
{
|
||||||
@ -534,23 +522,24 @@ private @nogc unittest
|
|||||||
defaultAllocator.dispose(exception);
|
defaultAllocator.dispose(exception);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Issue 254: https://issues.caraus.io/issues/254.
|
||||||
|
private @system @nogc unittest
|
||||||
|
{
|
||||||
|
auto u = URL("ftp://");
|
||||||
|
assert(u.scheme == "ftp");
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Attempts to parse an URL from a string and returns the specified component
|
* Attempts to parse an URL from a string and returns the specified component
|
||||||
* of the URL or $(D_PSYMBOL URL) if no component is specified.
|
* of the URL or $(D_PSYMBOL URL) if no component is specified.
|
||||||
*
|
*
|
||||||
* Params:
|
* Params:
|
||||||
* T = "scheme", "host", "port", "user", "pass", "path", "query",
|
* T = "scheme", "host", "port", "user", "pass", "path", "query",
|
||||||
* "fragment" or $(D_KEYWORD null) for a struct with all components.
|
* "fragment".
|
||||||
* source = The string containing the URL.
|
* source = The string containing the URL.
|
||||||
*
|
*
|
||||||
* Returns: Requested URL component.
|
* Returns: Requested URL component.
|
||||||
*/
|
*/
|
||||||
URL parseURL(const char[] source) @nogc
|
|
||||||
{
|
|
||||||
return URL(source);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Ditto.
|
|
||||||
auto parseURL(string T)(const char[] source)
|
auto parseURL(string T)(const char[] source)
|
||||||
if (T == "scheme"
|
if (T == "scheme"
|
||||||
|| T == "host"
|
|| T == "host"
|
||||||
@ -565,6 +554,12 @@ if (T == "scheme"
|
|||||||
return mixin("ret." ~ T);
|
return mixin("ret." ~ T);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Ditto.
|
||||||
|
URL parseURL(const char[] source) @nogc
|
||||||
|
{
|
||||||
|
return URL(source);
|
||||||
|
}
|
||||||
|
|
||||||
///
|
///
|
||||||
@nogc unittest
|
@nogc unittest
|
||||||
{
|
{
|
||||||
|
@ -9,9 +9,9 @@
|
|||||||
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
||||||
* Mozilla Public License, v. 2.0).
|
* Mozilla Public License, v. 2.0).
|
||||||
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
||||||
|
* Source: $(LINK2 https://github.com/caraus-ecms/tanya/blob/master/source/tanya/network/package.d,
|
||||||
|
* tanya/network/package.d)
|
||||||
*/
|
*/
|
||||||
module tanya.network;
|
module tanya.network;
|
||||||
|
|
||||||
public import tanya.network.inet;
|
|
||||||
public import tanya.network.socket;
|
public import tanya.network.socket;
|
||||||
public import tanya.network.url;
|
|
||||||
|
@ -9,6 +9,8 @@
|
|||||||
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
||||||
* Mozilla Public License, v. 2.0).
|
* Mozilla Public License, v. 2.0).
|
||||||
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
||||||
|
* Source: $(LINK2 https://github.com/caraus-ecms/tanya/blob/master/source/tanya/network/socket.d,
|
||||||
|
* tanya/network/socket.d)
|
||||||
*/
|
*/
|
||||||
module tanya.network.socket;
|
module tanya.network.socket;
|
||||||
|
|
||||||
|
File diff suppressed because it is too large
Load Diff
@ -9,6 +9,8 @@
|
|||||||
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
||||||
* Mozilla Public License, v. 2.0).
|
* Mozilla Public License, v. 2.0).
|
||||||
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
||||||
|
* Source: $(LINK2 https://github.com/caraus-ecms/tanya/blob/master/source/tanya/os/error.d,
|
||||||
|
* tanya/os/error.d)
|
||||||
*/
|
*/
|
||||||
module tanya.os.error;
|
module tanya.os.error;
|
||||||
|
|
||||||
|
@ -10,6 +10,8 @@
|
|||||||
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
||||||
* Mozilla Public License, v. 2.0).
|
* Mozilla Public License, v. 2.0).
|
||||||
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
||||||
|
* Source: $(LINK2 https://github.com/caraus-ecms/tanya/blob/master/source/tanya/os/package.d,
|
||||||
|
* tanya/os/package.d)
|
||||||
*/
|
*/
|
||||||
module tanya.os;
|
module tanya.os;
|
||||||
|
|
||||||
|
220
source/tanya/range/array.d
Normal file
220
source/tanya/range/array.d
Normal file
@ -0,0 +1,220 @@
|
|||||||
|
/* 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/. */
|
||||||
|
|
||||||
|
/**
|
||||||
|
* $(D_PSYMBOL tanya.range.array) implements range primitives for built-in arrays.
|
||||||
|
*
|
||||||
|
* This module is a submodule of
|
||||||
|
* $(LINK2 https://github.com/caraus-ecms/tanya/blob/master/source/tanya/range/package.d, tanya.range).
|
||||||
|
*
|
||||||
|
* After importing of
|
||||||
|
* $(LINK2 https://github.com/caraus-ecms/tanya/blob/master/source/tanya/range/array.d, tanya/range/array.d)
|
||||||
|
* built-in arrays can act as bidirectional ranges. For that to work the module
|
||||||
|
* defines a set of functions that accept a built-in array of any type as their
|
||||||
|
* first argument, so thanks to UFCS (Uniform Function Call Syntax) they can be
|
||||||
|
* called as if they were array member functions. For example the arrays the
|
||||||
|
* `.length`-property, but no `.empty` property. So here can be find the
|
||||||
|
* $(D_PSYMBOL empty) function. Since $(D_INLINECODE empty(array)) and
|
||||||
|
* $(D_INLINECODE array.empty) are equal for the arrays, arrays get a faked
|
||||||
|
* property `empty`.
|
||||||
|
*
|
||||||
|
* The functions in this module don't change array elements or its underlying
|
||||||
|
* storage, but some functions alter the slice. Each array maintains a pointer
|
||||||
|
* to its data and the length and there can be several pointers which point to
|
||||||
|
* the same data. Array pointer can be advanced and the length can be reduced
|
||||||
|
* without changing the underlying storage. So slices offer the possibility to
|
||||||
|
* have multiple views into the same array, point to different positions inside
|
||||||
|
* it.
|
||||||
|
*
|
||||||
|
* Strings ($(D_INLINECODE char[]), (D_INLINECODE wchar[]) and
|
||||||
|
* (D_INLINECODE dchar[])) are treated as any other normal array, they aren't
|
||||||
|
* auto-decoded.
|
||||||
|
*
|
||||||
|
* 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/range/array.d,
|
||||||
|
* tanya/range/array.d)
|
||||||
|
*/
|
||||||
|
module tanya.range.array;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the first element of the $(D_PARAM array).
|
||||||
|
*
|
||||||
|
* The element is returned by reference, so $(D_PSYMBOL front) can be also used
|
||||||
|
* to change the first element of $(D_PARAM array) if it is mutable.
|
||||||
|
*
|
||||||
|
* Params:
|
||||||
|
* T = Element type of $(D_PARAM array).
|
||||||
|
* array = Built-in array.
|
||||||
|
*
|
||||||
|
* Returns: First element.
|
||||||
|
*
|
||||||
|
* Precondition: $(D_INLINECODE array.length > 0).
|
||||||
|
*/
|
||||||
|
@property ref T front(T)(T[] array)
|
||||||
|
in
|
||||||
|
{
|
||||||
|
assert(array.length > 0);
|
||||||
|
}
|
||||||
|
body
|
||||||
|
{
|
||||||
|
return array[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
///
|
||||||
|
pure nothrow @safe @nogc unittest
|
||||||
|
{
|
||||||
|
string s = "Wenn die Wunde nicht mehr wehtut, schmerzt die Narbe";
|
||||||
|
static assert(is(typeof(s.front) == immutable char));
|
||||||
|
assert(s.front == 'W');
|
||||||
|
|
||||||
|
wstring w = "Волны несутся, гремя и сверкая";
|
||||||
|
static assert(is(typeof(w.front) == immutable wchar));
|
||||||
|
assert(w.front == 'В');
|
||||||
|
|
||||||
|
dstring d = "Для писателя память - это почти все";
|
||||||
|
static assert(is(typeof(d.front) == immutable dchar));
|
||||||
|
assert(d.front == 'Д');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the last element of the $(D_PARAM array).
|
||||||
|
*
|
||||||
|
* The element is returned by reference, so $(D_PSYMBOL back) can be also used
|
||||||
|
* to change the last element of $(D_PARAM array) if it is mutable.
|
||||||
|
*
|
||||||
|
* Params:
|
||||||
|
* T = Element type of $(D_PARAM array).
|
||||||
|
* array = Built-in array.
|
||||||
|
*
|
||||||
|
* Returns: Last element.
|
||||||
|
*
|
||||||
|
* Precondition: $(D_INLINECODE array.length > 0).
|
||||||
|
*/
|
||||||
|
@property ref T back(T)(T[] array)
|
||||||
|
in
|
||||||
|
{
|
||||||
|
assert(array.length > 0);
|
||||||
|
}
|
||||||
|
body
|
||||||
|
{
|
||||||
|
return array[$ - 1];
|
||||||
|
}
|
||||||
|
|
||||||
|
///
|
||||||
|
pure nothrow @safe @nogc unittest
|
||||||
|
{
|
||||||
|
string s = "Brecht";
|
||||||
|
static assert(is(typeof(s.back) == immutable char));
|
||||||
|
assert(s.back == 't');
|
||||||
|
|
||||||
|
wstring w = "Тютчев";
|
||||||
|
static assert(is(typeof(w.back) == immutable wchar));
|
||||||
|
assert(w.back == 'в');
|
||||||
|
|
||||||
|
dstring d = "Паустовский";
|
||||||
|
static assert(is(typeof(d.back) == immutable dchar));
|
||||||
|
assert(d.back == 'й');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* $(D_PSYMBOL popFront) and $(D_PSYMBOL popBack) advance the $(D_PARAM array)
|
||||||
|
* and remove one element from its back, respectively.
|
||||||
|
*
|
||||||
|
* $(D_PSYMBOL popFront) and $(D_PSYMBOL popBack) don't alter the array
|
||||||
|
* storage, they only narrow the view into the array.
|
||||||
|
*
|
||||||
|
* Params:
|
||||||
|
* T = Element type of $(D_PARAM array).
|
||||||
|
* array = Built-in array.
|
||||||
|
*
|
||||||
|
* Precondition: $(D_INLINECODE array.length > 0).
|
||||||
|
*/
|
||||||
|
void popFront(T)(ref T[] array)
|
||||||
|
in
|
||||||
|
{
|
||||||
|
assert(array.length > 0);
|
||||||
|
}
|
||||||
|
body
|
||||||
|
{
|
||||||
|
array = array[1 .. $];
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Ditto.
|
||||||
|
void popBack(T)(ref T[] array)
|
||||||
|
in
|
||||||
|
{
|
||||||
|
assert(array.length > 0);
|
||||||
|
}
|
||||||
|
body
|
||||||
|
{
|
||||||
|
array = array[0 .. $ - 1];
|
||||||
|
}
|
||||||
|
|
||||||
|
///
|
||||||
|
pure nothrow @safe @nogc unittest
|
||||||
|
{
|
||||||
|
wstring array = "Der finstere Ozean der Metaphysik. Nietzsche";
|
||||||
|
|
||||||
|
array.popFront();
|
||||||
|
assert(array.length == 43);
|
||||||
|
assert(array.front == 'e');
|
||||||
|
|
||||||
|
array.popBack();
|
||||||
|
assert(array.length == 42);
|
||||||
|
assert(array.back == 'h');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests whether $(D_PARAM array) is empty.
|
||||||
|
*
|
||||||
|
* Params:
|
||||||
|
* T = Element type of $(D_PARAM array).
|
||||||
|
* array = Built-in array.
|
||||||
|
*
|
||||||
|
* Returns: $(D_KEYWORD true) if $(D_PARAM array) has no elements,
|
||||||
|
* $(D_KEYWORD false) otherwise.
|
||||||
|
*/
|
||||||
|
@property bool empty(T)(const T[] array)
|
||||||
|
{
|
||||||
|
return array.length == 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
///
|
||||||
|
pure nothrow @safe @nogc unittest
|
||||||
|
{
|
||||||
|
int[1] array;
|
||||||
|
assert(!array.empty);
|
||||||
|
assert(array[1 .. 1].empty);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a copy of the slice $(D_PARAM array).
|
||||||
|
*
|
||||||
|
* $(D_PSYMBOL save) doesn't copy the array itself, but only the data pointer
|
||||||
|
* and the length.
|
||||||
|
*
|
||||||
|
* Params:
|
||||||
|
* T = Element type of $(D_PARAM array).
|
||||||
|
* array = Built-in array.
|
||||||
|
*
|
||||||
|
* Returns: A copy of the slice $(D_PARAM array).
|
||||||
|
*/
|
||||||
|
@property T[] save(T)(T[] array)
|
||||||
|
{
|
||||||
|
return array;
|
||||||
|
}
|
||||||
|
|
||||||
|
///
|
||||||
|
pure nothrow @safe @nogc unittest
|
||||||
|
{
|
||||||
|
ubyte[8] array;
|
||||||
|
auto slice = array.save;
|
||||||
|
|
||||||
|
assert(slice.length == array.length);
|
||||||
|
slice.popFront();
|
||||||
|
assert(slice.length < array.length);
|
||||||
|
}
|
@ -3,17 +3,16 @@
|
|||||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Internet utilities.
|
* This package contains generic function and templates to be used with D
|
||||||
*
|
* ranges.
|
||||||
* $(RED Deprecated. Use $(D_PSYMBOL tanya.net.inet) instead.
|
|
||||||
* This module will be removed in 0.8.0.)
|
|
||||||
*
|
*
|
||||||
* Copyright: Eugene Wissner 2017.
|
* Copyright: Eugene Wissner 2017.
|
||||||
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
||||||
* Mozilla Public License, v. 2.0).
|
* Mozilla Public License, v. 2.0).
|
||||||
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
||||||
|
* Source: $(LINK2 https://github.com/caraus-ecms/tanya/blob/master/source/tanya/range/package.d,
|
||||||
|
* tanya/range/package.d)
|
||||||
*/
|
*/
|
||||||
deprecated("Use tanya.net.inet instead")
|
module tanya.range;
|
||||||
module tanya.network.inet;
|
|
||||||
|
|
||||||
public import tanya.net.inet;
|
public import tanya.range.array;
|
@ -12,10 +12,12 @@
|
|||||||
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
||||||
* Mozilla Public License, v. 2.0).
|
* Mozilla Public License, v. 2.0).
|
||||||
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
||||||
|
* Source: $(LINK2 https://github.com/caraus-ecms/tanya/blob/master/source/tanya/typecons.d,
|
||||||
|
* tanya/typecons.d)
|
||||||
*/
|
*/
|
||||||
module tanya.typecons;
|
module tanya.typecons;
|
||||||
|
|
||||||
import std.meta;
|
import tanya.meta.metafunction;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* $(D_PSYMBOL Pair) can store two heterogeneous objects.
|
* $(D_PSYMBOL Pair) can store two heterogeneous objects.
|
||||||
|
Reference in New Issue
Block a user