-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbytes.hpp
More file actions
144 lines (126 loc) · 4.15 KB
/
bytes.hpp
File metadata and controls
144 lines (126 loc) · 4.15 KB
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
#ifndef SS_BYTES_HPP
#define SS_BYTES_HPP
#include "builtin.hpp"
#include <string>
#include <stdexcept>
namespace shedskin {
class bytes : public pyobj {
private:
unsigned char* data;
size_t capacity;
size_t length;
public:
bytes() : data(nullptr), capacity(0), length(0) {}
bytes(const std::string& str) {
length = str.length();
capacity = length;
data = new unsigned char[capacity];
for (size_t i = 0; i < length; ++i) {
data[i] = static_cast<unsigned char>(str.at(i));
}
}
bytes(const unsigned char* arr, size_t len) {
length = len;
capacity = len;
data = new unsigned char[capacity];
for (size_t i = 0; i < len; ++i) {
data[i] = arr[i];
}
}
bytes(const char* str) {
length = 0;
while (str[length]) ++length;
capacity = length;
data = new unsigned char[capacity];
for (size_t i = 0; i < length; ++i) {
data[i] = static_cast<unsigned char>(str[i]);
}
}
bytes(const char* str, size_t len) {
length = len;
capacity = len;
data = new unsigned char[capacity];
const unsigned char* start = reinterpret_cast<const unsigned char*>(str);
for (size_t i = 0; i < len; ++i) {
data[i] = start[i];
}
}
~bytes() {
delete[] data;
}
// Copy constructor
bytes(const bytes& other) {
length = other.length;
capacity = other.capacity;
data = new unsigned char[capacity];
for (size_t i = 0; i < length; ++i) {
data[i] = other.data[i];
}
}
// Assignment operator
bytes& operator=(const bytes& other) {
if (this != &other) {
delete[] data;
length = other.length;
capacity = other.capacity;
data = new unsigned char[capacity];
for (size_t i = 0; i < length; ++i) {
data[i] = other.data[i];
}
}
return *this;
}
size_t len() const {
return length;
}
size_t size() const {
return length;
}
const unsigned char* data_ptr() const {
return data;
}
unsigned char __getitem__(__ss_int index) const {
if (index < 0) {
index += static_cast<__ss_int>(length); // Support pour les index négatifs
}
if (index < 0 || index >= static_cast<__ss_int>(length)) {
throw std::out_of_range("Index out of range");
}
return data[index];
}
void append(unsigned char value) {
if (length == capacity) {
size_t new_capacity = (capacity == 0) ? 1 : capacity * 2;
unsigned char* new_data = new unsigned char[new_capacity];
for (size_t i = 0; i < length; ++i) {
new_data[i] = data[i];
}
delete[] data;
data = new_data;
capacity = new_capacity;
}
data[length++] = value;
}
std::string to_string() const {
return std::string(reinterpret_cast<const char*>(data), length);
}
bool equals(const bytes* other) const {
if (!other) return false;
if (length != other->length) return false;
for (size_t i = 0; i < length; ++i) {
if (data[i] != other->data[i]) return false;
}
return true;
}
void print() const {
for (size_t i = 0; i < length; ++i) {
std::cout << static_cast<int>(data[i]) << " ";
}
std::cout << std::endl;
}
};
__ss_int len(shedskin::bytes* b) {
return b ? static_cast<__ss_int>(b->size()) : 0;
}
} // namespace shedskin
#endif // SS_BYTES_HPP