1 /*
2  * This file was automatically generated by sel-utils and
3  * released under the MIT License.
4  * 
5  * License: https://github.com/sel-project/sel-utils/blob/master/LICENSE
6  * Repository: https://github.com/sel-project/sel-utils
7  */
8 module sul.utils.buffer;
9 
10 import std.bitmanip;
11 import std.system : Endian;
12 
13 class Buffer {
14 
15 	public ubyte[] _buffer;
16 	public size_t _index;
17 
18 	public pure nothrow @property @safe @nogc Buffer bufferInstance() {
19 		return this;
20 	}
21 
22 	public pure nothrow @safe void writeBytes(ubyte[] bytes) {
23 		this._buffer ~= bytes;
24 	}
25 
26 	public pure nothrow @trusted void writeString(string str) {
27 		this.writeBytes(cast(ubyte[])str);
28 	}
29 
30 	public pure nothrow @safe ubyte[] readBytes(size_t length) {
31 		immutable end = this._index + length;
32 		if(this._buffer.length < end) return (ubyte[]).init;
33 		auto ret = this._buffer[this._index..end].dup;
34 		this._index = end;
35 		return ret;
36 	}
37 
38 	public pure nothrow @trusted string readString(size_t length) {
39 		return cast(string)this.readBytes(length);
40 	}
41 
42 	public pure nothrow @safe void writeBigEndianBool(bool a) {
43 		this._buffer ~= a;
44 	}
45 
46 	public pure nothrow @safe bool readBigEndianBool() {
47 		immutable end = this._index + 1;
48 		if(this._buffer.length < end) return bool.init;
49 		ubyte[1] bytes = this._buffer[this._index..end];
50 		this._index = end;
51 		return cast(bool)bytes[0];
52 	}
53 
54 	public pure nothrow @safe void writeLittleEndianBool(bool a) {
55 		this._buffer ~= a;
56 	}
57 
58 	public pure nothrow @safe bool readLittleEndianBool() {
59 		immutable end = this._index + 1;
60 		if(this._buffer.length < end) return bool.init;
61 		ubyte[1] bytes = this._buffer[this._index..end];
62 		this._index = end;
63 		return cast(bool)bytes[0];
64 	}
65 
66 	public pure nothrow @safe void writeBigEndianByte(byte a) {
67 		this._buffer ~= a;
68 	}
69 
70 	public pure nothrow @safe byte readBigEndianByte() {
71 		immutable end = this._index + 1;
72 		if(this._buffer.length < end) return byte.init;
73 		ubyte[1] bytes = this._buffer[this._index..end];
74 		this._index = end;
75 		return cast(byte)bytes[0];
76 	}
77 
78 	public pure nothrow @safe void writeLittleEndianByte(byte a) {
79 		this._buffer ~= a;
80 	}
81 
82 	public pure nothrow @safe byte readLittleEndianByte() {
83 		immutable end = this._index + 1;
84 		if(this._buffer.length < end) return byte.init;
85 		ubyte[1] bytes = this._buffer[this._index..end];
86 		this._index = end;
87 		return cast(byte)bytes[0];
88 	}
89 
90 	public pure nothrow @safe void writeBigEndianUbyte(ubyte a) {
91 		this._buffer ~= a;
92 	}
93 
94 	public pure nothrow @safe ubyte readBigEndianUbyte() {
95 		immutable end = this._index + 1;
96 		if(this._buffer.length < end) return byte.init;
97 		ubyte[1] bytes = this._buffer[this._index..end];
98 		this._index = end;
99 		return cast(byte)bytes[0];
100 	}
101 
102 	public pure nothrow @safe void writeLittleEndianUbyte(ubyte a) {
103 		this._buffer ~= a;
104 	}
105 
106 	public pure nothrow @safe ubyte readLittleEndianUbyte() {
107 		immutable end = this._index + 1;
108 		if(this._buffer.length < end) return byte.init;
109 		ubyte[1] bytes = this._buffer[this._index..end];
110 		this._index = end;
111 		return cast(byte)bytes[0];
112 	}
113 
114 	public pure nothrow @safe void writeBigEndianShort(short a) {
115 		this._buffer ~= nativeToBigEndian!short(a);
116 	}
117 
118 	public pure nothrow @safe short readBigEndianShort() {
119 		immutable end = this._index + 2;
120 		if(this._buffer.length < end) return short.init;
121 		ubyte[2] bytes = this._buffer[this._index..end];
122 		this._index = end;
123 		return bigEndianToNative!short(bytes);
124 	}
125 
126 	public pure nothrow @safe void writeLittleEndianShort(short a) {
127 		this._buffer ~= nativeToLittleEndian!short(a);
128 	}
129 
130 	public pure nothrow @safe short readLittleEndianShort() {
131 		immutable end = this._index + 2;
132 		if(this._buffer.length < end) return short.init;
133 		ubyte[2] bytes = this._buffer[this._index..end];
134 		this._index = end;
135 		return littleEndianToNative!short(bytes);
136 	}
137 
138 	public pure nothrow @safe void writeBigEndianUshort(ushort a) {
139 		this._buffer ~= nativeToBigEndian!ushort(a);
140 	}
141 
142 	public pure nothrow @safe ushort readBigEndianUshort() {
143 		immutable end = this._index + 2;
144 		if(this._buffer.length < end) return short.init;
145 		ubyte[2] bytes = this._buffer[this._index..end];
146 		this._index = end;
147 		return bigEndianToNative!short(bytes);
148 	}
149 
150 	public pure nothrow @safe void writeLittleEndianUshort(ushort a) {
151 		this._buffer ~= nativeToLittleEndian!ushort(a);
152 	}
153 
154 	public pure nothrow @safe ushort readLittleEndianUshort() {
155 		immutable end = this._index + 2;
156 		if(this._buffer.length < end) return short.init;
157 		ubyte[2] bytes = this._buffer[this._index..end];
158 		this._index = end;
159 		return littleEndianToNative!short(bytes);
160 	}
161 
162 	public pure nothrow @safe void writeBigEndianTriad(int a) {
163 		this._buffer ~= nativeToBigEndian!int(a)[$-3..$];
164 	}
165 
166 	public pure nothrow @safe int readBigEndianTriad() {
167 		immutable end = this._index + 3;
168 		if(this._buffer.length < end) return int.init;
169 		ubyte[4] bytes = new ubyte[1] ~ this._buffer[this._index..end];
170 		this._index = end;
171 		return bigEndianToNative!int(bytes);
172 	}
173 
174 	public pure nothrow @safe void writeLittleEndianTriad(int a) {
175 		this._buffer ~= nativeToLittleEndian!int(a)[0..$-1];
176 	}
177 
178 	public pure nothrow @safe int readLittleEndianTriad() {
179 		immutable end = this._index + 3;
180 		if(this._buffer.length < end) return int.init;
181 		ubyte[4] bytes = this._buffer[this._index..end] ~ new ubyte[1];
182 		this._index = end;
183 		return littleEndianToNative!int(bytes);
184 	}
185 
186 	public pure nothrow @safe void writeBigEndianInt(int a) {
187 		this._buffer ~= nativeToBigEndian!int(a);
188 	}
189 
190 	public pure nothrow @safe int readBigEndianInt() {
191 		immutable end = this._index + 4;
192 		if(this._buffer.length < end) return int.init;
193 		ubyte[4] bytes = this._buffer[this._index..end];
194 		this._index = end;
195 		return bigEndianToNative!int(bytes);
196 	}
197 
198 	public pure nothrow @safe void writeLittleEndianInt(int a) {
199 		this._buffer ~= nativeToLittleEndian!int(a);
200 	}
201 
202 	public pure nothrow @safe int readLittleEndianInt() {
203 		immutable end = this._index + 4;
204 		if(this._buffer.length < end) return int.init;
205 		ubyte[4] bytes = this._buffer[this._index..end];
206 		this._index = end;
207 		return littleEndianToNative!int(bytes);
208 	}
209 
210 	public pure nothrow @safe void writeBigEndianUint(uint a) {
211 		this._buffer ~= nativeToBigEndian!uint(a);
212 	}
213 
214 	public pure nothrow @safe uint readBigEndianUint() {
215 		immutable end = this._index + 4;
216 		if(this._buffer.length < end) return int.init;
217 		ubyte[4] bytes = this._buffer[this._index..end];
218 		this._index = end;
219 		return bigEndianToNative!int(bytes);
220 	}
221 
222 	public pure nothrow @safe void writeLittleEndianUint(uint a) {
223 		this._buffer ~= nativeToLittleEndian!uint(a);
224 	}
225 
226 	public pure nothrow @safe uint readLittleEndianUint() {
227 		immutable end = this._index + 4;
228 		if(this._buffer.length < end) return int.init;
229 		ubyte[4] bytes = this._buffer[this._index..end];
230 		this._index = end;
231 		return littleEndianToNative!int(bytes);
232 	}
233 
234 	public pure nothrow @safe void writeBigEndianLong(long a) {
235 		this._buffer ~= nativeToBigEndian!long(a);
236 	}
237 
238 	public pure nothrow @safe long readBigEndianLong() {
239 		immutable end = this._index + 8;
240 		if(this._buffer.length < end) return long.init;
241 		ubyte[8] bytes = this._buffer[this._index..end];
242 		this._index = end;
243 		return bigEndianToNative!long(bytes);
244 	}
245 
246 	public pure nothrow @safe void writeLittleEndianLong(long a) {
247 		this._buffer ~= nativeToLittleEndian!long(a);
248 	}
249 
250 	public pure nothrow @safe long readLittleEndianLong() {
251 		immutable end = this._index + 8;
252 		if(this._buffer.length < end) return long.init;
253 		ubyte[8] bytes = this._buffer[this._index..end];
254 		this._index = end;
255 		return littleEndianToNative!long(bytes);
256 	}
257 
258 	public pure nothrow @safe void writeBigEndianUlong(ulong a) {
259 		this._buffer ~= nativeToBigEndian!ulong(a);
260 	}
261 
262 	public pure nothrow @safe ulong readBigEndianUlong() {
263 		immutable end = this._index + 8;
264 		if(this._buffer.length < end) return long.init;
265 		ubyte[8] bytes = this._buffer[this._index..end];
266 		this._index = end;
267 		return bigEndianToNative!long(bytes);
268 	}
269 
270 	public pure nothrow @safe void writeLittleEndianUlong(ulong a) {
271 		this._buffer ~= nativeToLittleEndian!ulong(a);
272 	}
273 
274 	public pure nothrow @safe ulong readLittleEndianUlong() {
275 		immutable end = this._index + 8;
276 		if(this._buffer.length < end) return long.init;
277 		ubyte[8] bytes = this._buffer[this._index..end];
278 		this._index = end;
279 		return littleEndianToNative!long(bytes);
280 	}
281 
282 	public pure nothrow @safe void writeBigEndianFloat(float a) {
283 		this._buffer ~= nativeToBigEndian!float(a);
284 	}
285 
286 	public pure nothrow @safe float readBigEndianFloat() {
287 		immutable end = this._index + 4;
288 		if(this._buffer.length < end) return float.init;
289 		ubyte[4] bytes = this._buffer[this._index..end];
290 		this._index = end;
291 		return bigEndianToNative!float(bytes);
292 	}
293 
294 	public pure nothrow @safe void writeLittleEndianFloat(float a) {
295 		this._buffer ~= nativeToLittleEndian!float(a);
296 	}
297 
298 	public pure nothrow @safe float readLittleEndianFloat() {
299 		immutable end = this._index + 4;
300 		if(this._buffer.length < end) return float.init;
301 		ubyte[4] bytes = this._buffer[this._index..end];
302 		this._index = end;
303 		return littleEndianToNative!float(bytes);
304 	}
305 
306 	public pure nothrow @safe void writeBigEndianDouble(double a) {
307 		this._buffer ~= nativeToBigEndian!double(a);
308 	}
309 
310 	public pure nothrow @safe double readBigEndianDouble() {
311 		immutable end = this._index + 8;
312 		if(this._buffer.length < end) return double.init;
313 		ubyte[8] bytes = this._buffer[this._index..end];
314 		this._index = end;
315 		return bigEndianToNative!double(bytes);
316 	}
317 
318 	public pure nothrow @safe void writeLittleEndianDouble(double a) {
319 		this._buffer ~= nativeToLittleEndian!double(a);
320 	}
321 
322 	public pure nothrow @safe double readLittleEndianDouble() {
323 		immutable end = this._index + 8;
324 		if(this._buffer.length < end) return double.init;
325 		ubyte[8] bytes = this._buffer[this._index..end];
326 		this._index = end;
327 		return littleEndianToNative!double(bytes);
328 	}
329 
330 }