Class: MemoryIO::Types::CPP::String
- Defined in:
- lib/memory_io/types/cpp/string.rb
Overview
The ‘std::string` class in C++11.
The std::string class can be seen as:
class string {
void* _M_dataplus;
size_t string_length;
union {
char local_buf[15 + 1];
size_t allocated_capacity;
}
};
Constant Summary collapse
- LOCAL_CAPACITY =
std::string uses inlined-buffer if string length isn’t larger than LOCAL_CAPACITY.
15
Constants inherited from Type
Instance Attribute Summary collapse
- #capacity ⇒ Integer readonly
- #data ⇒ ::String
- #dataplus ⇒ Integer readonly
Class Method Summary collapse
- .read(stream) ⇒ CPP::String
-
.write(stream, obj) ⇒ void
Write a String object to stream.
Instance Method Summary collapse
-
#initialize(data, capacity, dataplus) ⇒ String
constructor
Instantiate a String object.
-
#inspect ⇒ String
Custom inspect view.
-
#length ⇒ Integer
(also: #size)
String length.
Methods inherited from Type
find, keep_pos, read_size_t, register, write_size_t
Constructor Details
#initialize(data, capacity, dataplus) ⇒ String
Instantiate a MemoryIO::Types::CPP::String object.
32 33 34 35 36 |
# File 'lib/memory_io/types/cpp/string.rb', line 32 def initialize(data, capacity, dataplus) @data = data @capacity = capacity @dataplus = dataplus end |
Instance Attribute Details
#capacity ⇒ Integer (readonly)
23 24 25 |
# File 'lib/memory_io/types/cpp/string.rb', line 23 def capacity @capacity end |
#data ⇒ ::String
22 23 24 |
# File 'lib/memory_io/types/cpp/string.rb', line 22 def data @data end |
#dataplus ⇒ Integer (readonly)
24 25 26 |
# File 'lib/memory_io/types/cpp/string.rb', line 24 def dataplus @dataplus end |
Class Method Details
.read(stream) ⇒ CPP::String
84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/memory_io/types/cpp/string.rb', line 84 def read(stream) dataplus = read_size_t(stream) length = read_size_t(stream) union = stream.read(LOCAL_CAPACITY + 1) if length > LOCAL_CAPACITY capacity = MemoryIO::Util.unpack(union[0, Type::SIZE_T]) data = keep_pos(stream, pos: dataplus) { |s| s.read(length) } else capacity = LOCAL_CAPACITY data = union[0, length] end new(data, capacity, dataplus) end |
.write(stream, obj) ⇒ void
This method returns an undefined value.
Write a MemoryIO::Types::CPP::String object to stream.
104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/memory_io/types/cpp/string.rb', line 104 def write(stream, obj) write_size_t(stream, obj.dataplus) write_size_t(stream, obj.length) pos = stream.pos if obj.length > LOCAL_CAPACITY keep_pos(stream, pos: obj.dataplus) { |s| s.write(obj.data + "\x00") } write_size_t(stream, obj.capacity) else stream.write(obj.data + "\x00") end stream.pos = pos + LOCAL_CAPACITY + 1 end |
Instance Method Details
#inspect ⇒ String
TODO:
Let it be colorful in pry.
Custom inspect view.
60 61 62 63 64 65 66 |
# File 'lib/memory_io/types/cpp/string.rb', line 60 def inspect format("#<%s @data=%s, @capacity=%d, @dataplus=0x%0#{SIZE_T * 2}x>", self.class.name, data.inspect, capacity, dataplus) end |
#length ⇒ Integer Also known as: size
String length.
41 42 43 |
# File 'lib/memory_io/types/cpp/string.rb', line 41 def length @data.size end |