-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutf_string.hh
107 lines (90 loc) · 2.47 KB
/
utf_string.hh
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
#ifndef UTF_STRING_HH
#define UTF_STRING_HH
#include <string>
#include <stdio.h>
#include <assert.h>
#include "functions.hh"
#include "types.hh"
#include "message_node.hh"
class method_desc;
class class_desc;
extern unsigned int string_hash_function(byte* p);
class utf_string {
protected:
int len; // string length without trailing \0
byte* data;
public:
bool operator == (utf_string const& str) const {
return len == str.len && memcmp(data, str.data, len) == 0;
}
bool operator != (utf_string const& str) const {
return len != str.len || memcmp(data, str.data, len) != 0;
}
bool operator == (const char* str) const {
return strcmp((char*)data, str) == 0;
}
bool operator != (const char* str) const {
return strcmp((char*)data, str) != 0;
}
unsigned hash() const {
return string_hash_function(data);
}
int first_char() const { return data[0]; }
void operator = (utf_string const& str) {
len = str.len;
data = str.data;
}
utf_string operator + (const char* suffix) const {
utf_string str;
str.len = len + strlen(suffix);
str.data = new byte[str.len+1];
memcpy(str.data, data, len);
memcpy(str.data+len, suffix, str.len - len);
str.data[str.len] = 0; // zero terminated string
return str;
}
void append(int offs, utf_string const& suffix) {
assert(offs <= len);
len = offs + suffix.len;
byte* new_data = new byte[len+1];
memcpy(new_data, data, offs);
memcpy(new_data+offs, suffix.data, suffix.len);
new_data[len] = 0; // zero terminated string
delete[] data;
data = new_data;
}
int rindex(byte ch) const {
byte* p = (byte*)strrchr((char*)data, ch);
return p ? p - data : -1;
}
void set_size(int size) { len = size; }
const char* as_asciz() const { return (const char*)data; }
utf_string(int length, byte* str) {
len = length;
data = new byte[length+1];
memcpy(data, str, length);
data[length] = 0;
}
utf_string(const char* str) {
len = strlen(str);
data = (byte*)str;
}
utf_string(utf_string const& str) {
len = str.len;
data = str.data;
}
utf_string(utf_string const& str, bool foo) {
len = str.len;
data = str.data;
if (FILE_SEP != '/') {
// Produce valid operationg system dependent file name
for (char* p = (char *) data; *p != '\0'; p++) {
if (*p == '/') {
*p = FILE_SEP;
}
}
}
}
utf_string() { len = 0; data = NULL; }
};
#endif