1 |
douglas |
362 |
// Represent |
2 |
|
|
// |
3 |
|
|
// Douglas Thrift |
4 |
|
|
// |
5 |
|
|
// $Id$ |
6 |
|
|
|
7 |
douglas |
370 |
#include "Binary.hpp" |
8 |
douglas |
362 |
|
9 |
douglas |
370 |
Binary::Binary(const ext::String& string, bool signed_) : bytes(string.GetSize() / 8, 0) |
10 |
douglas |
362 |
{ |
11 |
douglas |
370 |
if (string.IsEmpty()) |
12 |
|
|
{ |
13 |
|
|
bytes.InsertLast(0); |
14 |
douglas |
362 |
|
15 |
douglas |
370 |
return; |
16 |
douglas |
365 |
} |
17 |
douglas |
370 |
// string.insert(0, 8 - off, signed_ && string[0] == '1' ? '1' : '0'); |
18 |
douglas |
365 |
|
19 |
douglas |
370 |
size_t index(string.GetSize() % 8), offset(index); |
20 |
douglas |
365 |
|
21 |
douglas |
370 |
_rmforeach (ext::Vector<byte_t>, byte, bytes) _rfor (byte_t, bit, 0, 8) *byte |= (string[index++] == '1') << bit; |
22 |
douglas |
362 |
|
23 |
douglas |
370 |
if (offset != 0) |
24 |
|
|
{ |
25 |
|
|
bytes.InsertLast(0); |
26 |
douglas |
362 |
|
27 |
douglas |
370 |
_rfor (byte_t, bit, offset, 8) bytes.Last() |= (signed_ && string[0] == '1') << bit; |
28 |
douglas |
362 |
|
29 |
douglas |
370 |
index = 0; |
30 |
douglas |
365 |
|
31 |
douglas |
370 |
_rfor (byte_t, bit, 0, offset) bytes.Last() |= (string[index++] == '1') << bit; |
32 |
|
|
} |
33 |
douglas |
365 |
} |
34 |
|
|
|
35 |
douglas |
370 |
Binary::operator ext::String() const |
36 |
douglas |
362 |
{ |
37 |
douglas |
370 |
ext::String string; |
38 |
douglas |
362 |
|
39 |
douglas |
370 |
_rforeach (ext::Vector<byte_t>, byte, bytes) _rfor (byte_t, bit, 0, 8) string.InsertLast(1 & *byte >> bit ? '1' : '0'); |
40 |
douglas |
362 |
|
41 |
|
|
return string; |
42 |
|
|
} |