forked from rubinius/rubinius
/
bytearray.cpp
184 lines (144 loc) · 5.25 KB
/
bytearray.cpp
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
/* The simple ByteArray class, used to implement String. */
#include <stdint.h>
#include "vm.hpp"
#include "objectmemory.hpp"
#include "primitives.hpp"
#include "builtin/bytearray.hpp"
#include "builtin/class.hpp"
#include "builtin/exception.hpp"
#include "builtin/fixnum.hpp"
#include "builtin/string.hpp"
#include "builtin/tuple.hpp"
#include "object_utils.hpp"
#include "ontology.hpp"
namespace rubinius {
void ByteArray::init(STATE) {
GO(bytearray).set(ontology::new_class_under(state,
"ByteArray", G(rubinius)));
G(bytearray)->set_object_type(state, ByteArrayType);
G(bytearray)->name(state, state->symbol("Rubinius::ByteArray"));
}
ByteArray* ByteArray::create(STATE, native_int bytes) {
assert(bytes >= 0 && bytes < INT32_MAX);
size_t body = bytes;
ByteArray* ba = state->vm()->new_object_bytes<ByteArray>(G(bytearray), body);
if(unlikely(!ba)) {
Exception::memory_error(state);
}
ba->full_size_ = body;
memset(ba->bytes, 0, bytes);
return ba;
}
ByteArray* ByteArray::create_pinned(STATE, native_int bytes) {
assert(bytes >= 0 && bytes < INT32_MAX);
size_t body = bytes;
ByteArray* ba = state->memory()->new_object_bytes_mature<ByteArray>(state, G(bytearray), body);
if(unlikely(!ba)) {
Exception::memory_error(state);
}
if(!ba->pin()) {
rubinius::bug("unable to allocate pinned ByteArray");
}
ba->full_size_ = body;
memset(ba->bytes, 0, bytes);
return ba;
}
size_t ByteArray::Info::object_size(const ObjectHeader* obj) {
const ByteArray *ba = reinterpret_cast<const ByteArray*>(obj);
assert(ba);
return ba->full_size_;
}
void ByteArray::Info::mark(Object* t, ObjectMark& mark) {
// @todo implement
}
ByteArray* ByteArray::allocate(STATE, Fixnum* bytes) {
native_int size = bytes->to_native();
if(size < 0) {
Exception::argument_error(state, "negative byte array size");
} else if (size > INT32_MAX) {
Exception::argument_error(state, "too large byte array size");
}
return ByteArray::create(state, size);
}
Fixnum* ByteArray::size(STATE) {
return Fixnum::from(size());
}
Fixnum* ByteArray::get_byte(STATE, Fixnum* index) {
native_int idx = index->to_native();
if(idx < 0 || idx >= size()) {
Exception::object_bounds_exceeded_error(state, "index out of bounds");
}
return Fixnum::from(this->bytes[idx]);
}
Fixnum* ByteArray::set_byte(STATE, Fixnum* index, Fixnum* value) {
native_int idx = index->to_native();
if(idx < 0 || idx >= size()) {
Exception::object_bounds_exceeded_error(state, "index out of bounds");
}
this->bytes[idx] = value->to_native();
return Fixnum::from(this->bytes[idx]);
}
Fixnum* ByteArray::move_bytes(STATE, Fixnum* start, Fixnum* count, Fixnum* dest) {
native_int src = start->to_native();
native_int cnt = count->to_native();
native_int dst = dest->to_native();
if(src < 0) {
Exception::object_bounds_exceeded_error(state, "start less than zero");
} else if(dst < 0) {
Exception::object_bounds_exceeded_error(state, "dest less than zero");
} else if(cnt < 0) {
Exception::object_bounds_exceeded_error(state, "count less than zero");
} else if((dst + cnt) > size()) {
Exception::object_bounds_exceeded_error(state, "move is beyond end of bytearray");
} else if((src + cnt) > size()) {
Exception::object_bounds_exceeded_error(state, "move is more than available bytes");
}
memmove(this->bytes + dst, this->bytes + src, cnt);
return count;
}
ByteArray* ByteArray::fetch_bytes(STATE, Fixnum* start, Fixnum* count) {
native_int src = start->to_native();
native_int cnt = count->to_native();
if(src < 0) {
Exception::object_bounds_exceeded_error(state, "start less than zero");
} else if(cnt < 0) {
Exception::object_bounds_exceeded_error(state, "count less than zero");
} else if((src + cnt) > size()) {
Exception::object_bounds_exceeded_error(state, "fetch is more than available bytes");
}
ByteArray* ba = ByteArray::create(state, cnt + 1);
memcpy(ba->bytes, this->bytes + src, cnt);
ba->bytes[cnt] = 0;
return ba;
}
Fixnum* ByteArray::compare_bytes(STATE, ByteArray* other, Fixnum* a, Fixnum* b) {
native_int slim = a->to_native();
native_int olim = b->to_native();
if(slim < 0) {
Exception::object_bounds_exceeded_error(state,
"bytes of self to compare is less than zero");
} else if(olim < 0) {
Exception::object_bounds_exceeded_error(state,
"bytes of other to compare is less than zero");
}
// clamp limits to actual sizes
native_int m = size() < slim ? size() : slim;
native_int n = other->size() < olim ? other->size() : olim;
// only compare the shortest string
native_int len = m < n ? m : n;
native_int cmp = memcmp(this->bytes, other->bytes, len);
// even if substrings are equal, check actual requested limits
// of comparison e.g. "xyz", "xyzZ"
if(cmp == 0) {
if(m < n) {
return Fixnum::from(-1);
} else if(m > n) {
return Fixnum::from(1);
} else {
return Fixnum::from(0);
}
} else {
return cmp < 0 ? Fixnum::from(-1) : Fixnum::from(1);
}
}
}