muscleman 0.3.1

A buffer utility
Documentation
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
use crate::ByteOrder::ByteOrder;

#[allow(dead_code)]
pub struct Buffer {
    // The buffer's data
    data: Vec<u8>,

    // The buffer's length
    length: usize,

    // The buffer's capacity
    capacity: usize,

    // The buffer's position
    position: usize,

    // The buffer's mark
    mark: Option<usize>,

    // The buffer's byte order
    byte_order: ByteOrder,

    // The buffer's string encoding
    string_encoding: &'static str,

    // The buffer's string terminator
    string_terminator: &'static str
}

impl Buffer {

    //#region Constants

    // The default capacity of a buffer
    pub const DEFAULT_CAPACITY: usize = 1024;

    // The default byte order of a buffer
    pub const DEFAULT_BYTE_ORDER: ByteOrder = ByteOrder::BigEndian;

    // The default string encoding of a buffer
    pub const DEFAULT_STRING_ENCODING: &'static str = "utf-8";

    // The default string terminator of a buffer
    pub const DEFAULT_STRING_TERMINATOR: &'static str = "\0";

    //#endregion

    //#region Constructors

    // Default constructor
    pub fn new() -> Buffer {
        Buffer {
            data: Vec::with_capacity(Buffer::DEFAULT_CAPACITY),
            length: 0,
            capacity: Buffer::DEFAULT_CAPACITY,
            position: 0,
            mark: None,
            byte_order: Buffer::DEFAULT_BYTE_ORDER,
            string_encoding: Buffer::DEFAULT_STRING_ENCODING,
            string_terminator: Buffer::DEFAULT_STRING_TERMINATOR
        }
    }

    // All arguments constructor
    pub fn new_with_all_args(capacity: usize, byte_order: ByteOrder, string_encoding: &'static str, string_terminator: &'static str) -> Buffer {
        Buffer {
            data: Vec::with_capacity(capacity),
            length: 0,
            capacity: capacity,
            position: 0,
            mark: None,
            byte_order: byte_order,
            string_encoding: string_encoding,
            string_terminator: string_terminator
        }
    }

    //#endregion

    //# region Properties

    /// Gets the buffer's data
    pub fn get_data(&self) -> Vec<u8> {
        self.data.clone()
    }

    //#endregion Properties

    //#region Reading methods

    //#region Basic reading methods

    /// Reads a byte from the buffer.
    pub fn read_byte(&mut self) -> Option<u8> {
        if self.position < self.length {
            let byte = self.data[self.position];
            self.position += 1;
            Some(byte)
        } else {
            None
        }
    }

    /// Reads n bytes from the buffer.
    pub fn read_bytes(&mut self, n: usize) -> Option<Vec<u8>> {
        if self.position + n <= self.length {
            let mut bytes = Vec::with_capacity(n);
            for i in 0..n {
                bytes.push(self.data[self.position + i]);
            }
            self.position += n;
            Some(bytes)
        } else {
            None
        }
    }

    /// Reads a boolean from the buffer.
    pub fn read_boolean(&mut self) -> Option<bool> {
        if self.position < self.length {
            let byte = self.data[self.position];
            self.position += 1;
            Some(byte != 0)
        } else {
            None
        }
    }

    //#endregion Basic reading methods

    //#region Integer reading methods

    //# region Signed integer reading methods

    /// Reads a signed 8-bit integer from the buffer.
    pub fn read_i8(&mut self) -> Option<i8> {
        if self.position < self.length {
            let byte = self.data[self.position];
            self.position += 1;
            Some(byte as i8)
        } else {
            None
        }
    }

    /// Reads a signed 16-bit integer from the buffer.
    pub fn read_i16(&mut self) -> Option<i16> {
        if self.position + 2 <= self.length {
            let mut bytes = [0; 2];
            for i in 0..2 {
                bytes[i] = self.data[self.position + i];
            }
            self.position += 2;
            Some(i16::from_be_bytes(bytes))
        } else {
            None
        }
    }

    /// Reads a signed 32-bit integer from the buffer.
    pub fn read_i32(&mut self) -> Option<i32> {
        if self.position + 4 <= self.length {
            let mut bytes = [0; 4];
            for i in 0..4 {
                bytes[i] = self.data[self.position + i];
            }
            self.position += 4;
            Some(i32::from_be_bytes(bytes))
        } else {
            None
        }
    }

    /// Reads a signed 64-bit integer from the buffer.
    pub fn read_i64(&mut self) -> Option<i64> {
        if self.position + 8 <= self.length {
            let mut bytes = [0; 8];
            for i in 0..8 {
                bytes[i] = self.data[self.position + i];
            }
            self.position += 8;
            Some(i64::from_be_bytes(bytes))
        } else {
            None
        }
    }

    //# endregion Signed integer reading methods

    //# region Unsigned integer reading methods

    /// Reads an unsigned 8-bit integer from the buffer.
    pub fn read_u8(&mut self) -> Option<u8> {
        if self.position < self.length {
            let byte = self.data[self.position];
            self.position += 1;
            Some(byte)
        } else {
            None
        }
    }

    /// Reads an unsigned 16-bit integer from the buffer.
    pub fn read_u16(&mut self) -> Option<u16> {
        if self.position + 2 <= self.length {
            let mut bytes = [0; 2];
            for i in 0..2 {
                bytes[i] = self.data[self.position + i];
            }
            self.position += 2;
            Some(u16::from_be_bytes(bytes))
        } else {
            None
        }
    }

    /// Reads an unsigned 32-bit integer from the buffer.
    pub fn read_u32(&mut self) -> Option<u32> {
        if self.position + 4 <= self.length {
            let mut bytes = [0; 4];
            for i in 0..4 {
                bytes[i] = self.data[self.position + i];
            }
            self.position += 4;
            Some(u32::from_be_bytes(bytes))
        } else {
            None
        }
    }

    /// Reads an unsigned 64-bit integer from the buffer.
    pub fn read_u64(&mut self) -> Option<u64> {
        if self.position + 8 <= self.length {
            let mut bytes = [0; 8];
            for i in 0..8 {
                bytes[i] = self.data[self.position + i];
            }
            self.position += 8;
            Some(u64::from_be_bytes(bytes))
        } else {
            None
        }
    }

    //# endregion Unsigned integer reading methods

    //#region VarInt reading methods

    /// Reads a VarInt from the buffer.
    pub fn read_varint(&mut self) -> Option<i64> {
        let mut result: i64 = 0;
        let mut shift: u8 = 0;
        let mut byte: u8;
        loop {
            if self.position < self.length {
                byte = self.data[self.position];
                self.position += 1;
            } else {
                return None;
            }
            result |= ((byte & 0x7F) as i64) << shift;
            if byte & 0x80 == 0 {
                break;
            }
            shift += 7;
        }
        Some(result)
    }

    //#endregion VarInt reading methods

    //#endregion Integer reading methods

    //#region Floating-point reading methods

    /// Reads a 32-bit floating-point number from the buffer.
    pub fn read_f32(&mut self) -> Option<f32> {
        if self.position + 4 <= self.length {
            let mut bytes = [0; 4];
            for i in 0..4 {
                bytes[i] = self.data[self.position + i];
            }
            self.position += 4;
            Some(f32::from_be_bytes(bytes))
        } else {
            None
        }
    }

    /// Reads a 64-bit floating-point number from the buffer.
    pub fn read_f64(&mut self) -> Option<f64> {
        if self.position + 8 <= self.length {
            let mut bytes = [0; 8];
            for i in 0..8 {
                bytes[i] = self.data[self.position + i];
            }
            self.position += 8;
            Some(f64::from_be_bytes(bytes))
        } else {
            None
        }
    }

    //#endregion Floating-point reading methods

    //#region String reading methods

    /// Reads a string from the buffer.
    /// Reads until the first null byte.
    pub fn read_string(&mut self) -> Option<String> {
        let mut string = String::new();
        while self.position < self.length {
            let byte = self.data[self.position];
            self.position += 1;
            if byte == 0 {
                break;
            }
            string.push(byte as char);
        }
        Some(string)
    }

    /// Reads a string from the buffer.
    /// Reads the length of the string from the buffer. Then reads that many bytes.
    pub fn read_string_with_length(&mut self) -> Option<String> {
        let length = self.read_u32()?;
        let mut string = String::new();
        for _ in 0..length {
            let byte = self.read_u8()?;
            string.push(byte as char);
        }
        Some(string)
    }

    //#endregion String reading methods

    //#endregion Reading methods

    //#region Writing methods

    //#region Basic writing methods

    /// Writes a byte to the buffer.
    pub fn write_byte(&mut self, byte: u8) {
        self.data.push(byte);
        self.length += 1;
    }

    /// Writes a byte array to the buffer.
    pub fn write_bytes(&mut self, bytes: &[u8]) {
        for byte in bytes {
            self.data.push(*byte);
        }
        self.length += bytes.len();
    }

    //#endregion Basic writing methods

    //#region Integer writing methods

    //# region Signed integer writing methods

    /// Writes a signed 8-bit integer to the buffer.
    pub fn write_i8(&mut self, value: i8) {
        self.data.push(value as u8);
        self.length += 1;
    }

    /// Writes a signed 16-bit integer to the buffer.
    pub fn write_i16(&mut self, value: i16) {
        let bytes = value.to_be_bytes();
        for byte in &bytes {
            self.data.push(*byte);
        }
        self.length += 2;
    }

    /// Writes a signed 32-bit integer to the buffer.
    pub fn write_i32(&mut self, value: i32) {
        let bytes = value.to_be_bytes();
        for byte in &bytes {
            self.data.push(*byte);
        }
        self.length += 4;
    }

    /// Writes a signed 64-bit integer to the buffer.
    pub fn write_i64(&mut self, value: i64) {
        let bytes = value.to_be_bytes();
        for byte in &bytes {
            self.data.push(*byte);
        }
        self.length += 8;
    }

    //# endregion Signed integer writing methods

    //# region Unsigned integer writing methods

    /// Writes an unsigned 8-bit integer to the buffer.
    pub fn write_u8(&mut self, value: u8) {
        self.data.push(value);
        self.length += 1;
    }

    /// Writes an unsigned 16-bit integer to the buffer.
    pub fn write_u16(&mut self, value: u16) {
        let bytes = value.to_be_bytes();
        for byte in &bytes {
            self.data.push(*byte);
        }
        self.length += 2;
    }

    /// Writes an unsigned 32-bit integer to the buffer.
    pub fn write_u32(&mut self, value: u32) {
        let bytes = value.to_be_bytes();
        for byte in &bytes {
            self.data.push(*byte);
        }
        self.length += 4;
    }

    /// Writes an unsigned 64-bit integer to the buffer.
    pub fn write_u64(&mut self, value: u64) {
        let bytes = value.to_be_bytes();
        for byte in &bytes {
            self.data.push(*byte);
        }
        self.length += 8;
    }

    //# endregion Unsigned integer writing methods

    //#region VarInt writing methods

    /// Writes a VarInt to the buffer.
    pub fn write_varint(&mut self, value: i64) {
        let mut value = value;
        loop {
            let mut temp = (value & 0x7F) as u8;
            value >>= 7;
            if value != 0 {
                temp |= 0x80;
            }
            self.data.push(temp);
            self.length += 1;
            if value == 0 {
                break;
            }
        }
    }

    //#endregion VarInt writing methods

    //#endregion Integer writing methods

    //#region Floating-point writing methods

    /// Writes a 32-bit floating-point number to the buffer.
    pub fn write_f32(&mut self, value: f32) {
        let bytes = value.to_be_bytes();
        for byte in &bytes {
            self.data.push(*byte);
        }
        self.length += 4;
    }

    /// Writes a 64-bit floating-point number to the buffer.
    pub fn write_f64(&mut self, value: f64) {
        let bytes = value.to_be_bytes();
        for byte in &bytes {
            self.data.push(*byte);
        }
        self.length += 8;
    }

    //#endregion Floating-point writing methods

    //#region String writing methods

    /// Writes a string to the buffer.
    /// Writes as a null-terminated string.
    pub fn write_string(&mut self, string: &str) {
        for byte in string.bytes() {
            self.data.push(byte);
        }
        self.data.push(0);
        self.length += string.len() + 1;
    }

    /// Writes a string to the buffer.
    /// Writes the length of the string as a 32-bit unsigned integer, then writes the string.
    pub fn write_string_with_length(&mut self, string: &str) {
        self.write_u32(string.len() as u32);
        for byte in string.bytes() {
            self.data.push(byte);
        }
        self.length += string.len();
    }

    //#endregion String writing methods

    //#endregion Writing methods
}