forked from facebookarchive/warp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ranges.d
97 lines (77 loc) · 1.56 KB
/
ranges.d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/****
* Generic ranges not found in Phobos.
* Copyright: 2013 by Digital Mars
* License: $(LINK2 http://boost.org/LICENSE_1_0.txt, Boost License 1.0).
* Authors: Walter Bright
*/
module ranges;
/**************
* BitBucket is an OutputRange that throws away its output.
*/
struct BitBucket(E)
{
static void put(E e) { }
}
/*****************************************
*/
struct EmptyInputRange(E)
{
enum bool empty = true;
enum E front = E.init;
static void popFront() { assert(0); }
}
/*****************************************
*/
struct StaticArrayBuffer(E, size_t N)
{
size_t i;
E[N] arr = void;
void initialize() { i = 0; }
void put(E e)
{
if (i >= N) {
detectedOverflow();
}
arr[i] = e;
++i;
}
void put(E[] e)
{
if (i + e.length >= N + 1) {
detectedOverflow();
}
arr[i .. i + e.length] = e[];
i += e.length;
}
E[] opSlice()
{
return arr[0 .. i];
}
@property size_t length() { return i; }
private:
void detectedOverflow() {
import main;
err_fatal("Buffer overflowed. Possibly caused by forgetting to "
"complete a git merge in your code.");
}
}
//import std.stdio;
unittest
{
StaticArrayBuffer!(ubyte, 2) buf = void;
buf.initialize();
buf.put('a');
buf.put('b');
//writefln("'%s'", buf.get());
assert(buf[] == "ab");
}
unittest
{
ubyte[2] buf = void;
size_t i;
buf[i] = 'a';
++i;
buf[i] = 'b';
++i;
assert(buf[0 .. i] == "ab");
}