-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathstring.h
executable file
·130 lines (114 loc) · 2.57 KB
/
string.h
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
// The core library - copyright GarageGames. The core library is released under the MIT Open Source software license. See /license.txt in this distribution for specific license terms.
class string
{
protected:
struct string_record
{
uint32 ref_count;
uint32 len;
char data[1];
};
static string_record *_get_null_string_record()
{
static string_record null_string = { 0x7FFFFFFF, 0, { 0 } };
return &null_string;
}
string_record *_string_record;
void _dec_ref()
{
if(!--_string_record->ref_count && _string_record != _get_null_string_record())
memory_deallocate(_string_record);
}
public:
string()
{
_string_record = _get_null_string_record();
_string_record->ref_count++;
}
~string()
{
_dec_ref();
}
string(const char *string_data)
{
_string_record = _get_null_string_record();
_string_record->ref_count++;
set(string_data);
}
string(const char *string_data, uint32 len)
{
_string_record = _get_null_string_record();
_string_record->ref_count++;
set(string_data, len);
}
string(const unsigned char *data, uint32 len)
{
_string_record = _get_null_string_record();
_string_record->ref_count++;
set(data, len);
}
string(const string &the_string)
{
_string_record = the_string._string_record;
_string_record->ref_count++;
}
string& operator=(const string &the_string)
{
_dec_ref();
_string_record = the_string._string_record;
_string_record->ref_count++;
return *this;
}
void set(const char *string_data)
{
uint32 len = strlen(string_data);
set(string_data, len);
}
void set(const char *string_data, uint32 len)
{
reserve(len);
memcpy(_string_record->data, string_data, len);
}
void reserve(uint32 len)
{
_dec_ref();
_string_record = (string_record *) memory_allocate(sizeof(string_record) + len);
_string_record->len = len;
_string_record->data[len] = 0; // ensure null termination after buffer length
_string_record->ref_count = 1;
}
void set(const unsigned char *string_data, uint32 len)
{
set((const char *) string_data, len);
}
operator const char *()
{
return _string_record->data;
}
const char *c_str()
{
return _string_record->data;
}
uint8 *data()
{
return (uint8 *) _string_record->data;
}
const uint8 *data() const
{
return (uint8 *) _string_record->data;
}
uint32 len() const
{
return _string_record->len;
}
};
inline string format_string(const char *fmt, ...)
{
formatted_string_buffer buf;
va_list args;
va_start(args, fmt);
uint32 len = buf.format(fmt, args);
string return_value;
return_value.set(buf.c_str(), len);
return return_value;
}