|
8 | 8 |
|
9 | 9 | namespace lib_ruby_parser |
10 | 10 | { |
| 11 | + // A sequence of Bytes, potentially invalid in UTF-8 |
11 | 12 | class Bytes |
12 | 13 | { |
| 14 | + // Pointer to beginning of the byte sequence |
13 | 15 | char *bytes_; |
| 16 | + |
| 17 | + // Size of the byte sequence |
14 | 18 | uint32_t size_; |
| 19 | + |
| 20 | + // Indicates that Bytes are "borrowed", |
| 21 | + // when set to true disables cleanup in destructor. |
| 22 | + // TODO: remove this hack by making a cleanup on all call sites. |
15 | 23 | bool borrowed = false; |
16 | 24 |
|
17 | 25 | public: |
| 26 | + // Destructor |
18 | 27 | ~Bytes(); |
19 | 28 |
|
| 29 | + // Constructs Bytes from a string (by copying) |
20 | 30 | explicit Bytes(std::string s); |
| 31 | + |
| 32 | + // Constructs Bytes from a given ptr and size (by taking ownership) |
21 | 33 | explicit Bytes(char *ptr, uint32_t size); |
| 34 | + |
| 35 | + // Constructs Bytes from a BytePtr (by taking ownership) |
22 | 36 | explicit Bytes(BytePtr byte_ptr); |
23 | 37 |
|
24 | 38 | Bytes(Bytes &&); |
25 | 39 | Bytes(const Bytes &) = delete; |
26 | 40 | Bytes &operator=(Bytes &&other); |
27 | 41 |
|
| 42 | + // Returns a BytePtr that shares data with initial Bytes |
| 43 | + // Deleting returned BytePtr will cause a segfault. |
28 | 44 | BytePtr borrow_ptr() const; |
| 45 | + |
| 46 | + // Consumes `this` and returns a BytePtr with own data. |
29 | 47 | BytePtr into_ptr(); |
| 48 | + |
| 49 | + // Returns size of the byte sequence |
30 | 50 | uint32_t size() const; |
| 51 | + |
| 52 | + // Returns a copy of `this` |
31 | 53 | Bytes clone() const; |
| 54 | + |
| 55 | + // Returns a byte at the given index |
32 | 56 | char at(uint32_t idx) const; |
| 57 | + |
| 58 | + // Returns a sub-sequence at the given range |
33 | 59 | Bytes range(uint32_t begin, uint32_t end) const; |
| 60 | + |
| 61 | + // Returns a string that contains the same byte sequence (by copying) |
34 | 62 | std::string to_string() const; |
| 63 | + |
| 64 | + // Returns a UTF-8 valid string that contains the same byte sequence. |
| 65 | + // Invalid UTF-8 bytes are replaced with a special REPLACEMENT char. |
35 | 66 | std::string to_string_lossy() const; |
| 67 | + |
| 68 | + // Marks `this` as borrowed, so that destructor will not free the inner pointer. |
| 69 | + // TODO: remove this hack. |
36 | 70 | void mark_borrowed(); |
37 | 71 |
|
38 | 72 | bool operator==(const Bytes &other); |
|
0 commit comments