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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
|
#pragma once
#include <cstdio>
#include <stdarg.h>
#include <string.h>
#include "allocator.h"
constexpr U32 strlen_ct( const char* str ) {
U32 len = 0;
for( ; !!str[len]; ++len );
return len;
}
template <U32 N>
struct ARRSTR {
char data[N]{ 0 };
enum {
size = N
};
ARRSTR() {
memset( data, 0, N );
}
ARRSTR( const char* str ) {
memcpy( data, str, strlen_ct( str ) );
}
ARRSTR( const ARRSTR<N>& str ) {
memcpy( data, str.data, N );
}
template <U32 other>
auto operator+( const ARRSTR<other>& rhs ) {
const U32 l1 = strlen_ct( data );
const U32 l2 = strlen_ct( rhs.data );
if( l1 + l2 >= N ) {
dlog( "STR::operator+(): string overflow" );
abort();
return *this;
}
memcpy( data + l1, rhs.data, l2 );
data[l1 + l2] = '\0';
return *this;
}
template <U32 other>
auto concat( const ARRSTR<other>& str ) {
return *this + str;
}
operator char*() { return data; }
};
template <typename CT>
struct __str : public LIST<CT> {
__str() : LIST<CT>() {}
__str( const CT* fmt, ... ) : LIST<CT>() {
va_list args;
va_start( args, fmt );
va_list args2;
va_copy( args2, args );
U32 c = vsnprintf( 0, 0, fmt, args );
va_end( args );
this->data = 0;
this->reserve( c * 2 );
vsnprintf( this->data, c + 1, fmt, args2 );
this->data[c] = 0;
this->size = c;
va_end( args2 );
}
__str( const __str<CT>& rhs ) : LIST<CT>( rhs ) {
this->data[this->size] = 0;
}
__str<CT>& operator=( const __str<CT>& other ) {
if( this == &other )
return *this;
if( this->data && this->data != other.data )
free( this->data );
this->data = 0;
if( !other.capacity || !other.size ) {
this->capacity = 1;
this->size = 0;
this->data = (CT*)malloc( sizeof(CT) );
memset( this->data, 0, sizeof(CT) );
return *this;
}
this->data = (CT*)malloc( other.capacity * sizeof( CT ) );
memcpy( this->data, other.data, (other.size + 1) * sizeof( CT ) );
this->size = other.size;
this->capacity = other.capacity;
return *this;
}
const bool operator==( const __str<CT>& rhs ) { return this->equals( rhs ); }
const bool operator!=( const __str<CT>& rhs ) { return !(*this == rhs); }
const bool operator==( const CT* rhs ) { return this->equals( rhs ); }
const bool operator!=( const CT* rhs ) { return !(*this == rhs); }
__str<CT> operator+( const __str<CT>& rhs ) { __str<CT> ret = *this; return ret.append( rhs ); }
__str<CT> operator+( const CT* rhs ) { __str<CT> ret = *this; return ret.append( rhs ); }
__str<CT> operator+( const CT rhs ) { __str<CT> ret = *this; ret.push( rhs ); return ret; }
__str<CT>& operator+=( const __str<CT>& rhs ) { return this->append( rhs ); }
__str<CT>& operator+=( const CT* rhs ) { return this->append( rhs ); }
__str<CT>& operator+=( const CT rhs ) { this->push( rhs ); return this; }
operator CT*() { return this->data; }
CT operator[]( U32 i ) {
return this->data[i];
}
U8 equals( const __str<CT>& rhs ) {
if( rhs.size != this->size )
return 0;
for( U32 i = 0; i < this->size; ++i ) {
if( this->data[i] != rhs.data[i] )
return 0;
}
return 1;
}
U8 equals( const CT* rhs ) {
for( U32 i = 0; i < this->size; ++i ) {
if( !rhs[i] || this->data[i] != rhs[i] )
return 0;
}
return 1;
}
__str<CT>& fmt( const char* fmt, ... ) {
va_list args;
va_start( args, fmt );
va_list args2;
va_copy( args2, args );
U32 c = this->size + vsnprintf( 0, 0, fmt, args );
va_end( args );
if( c > this->capacity )
this->reserve( c * 2 );
vsnprintf( this->data + this->size, c + 1, fmt, args2 );
this->data[c] = 0;
this->size = c;
va_end( args2 );
}
__str<CT>& append( const CT* str ) {
U32 len;
for( len = 0; !!str[len]; ++len );
if( this->size + len > this->capacity )
this->reserve( this->size * 2 );
memcpy( this->data + this->size, str, len * sizeof(CT) );
this->size += len;
this->data[this->size] = 0;
return *this;
}
__str<CT>& append( const __str<CT>& str ) {
U32 len = str.len;
if( this->size + len + 1 >= this->capacity )
this->reserve( this->size + len + 1 );
memcpy( this->data + this->size, str, len * sizeof(CT) );
this->size += len;
this->data[this->size] = 0;
return *this;
}
CT* push( const CT& item ) {
if( this->capacity <= this->size + 1 )
this->grow();
this->data[this->size++] = item;
this->data[this->size] = 0;
return &this->data[this->size - 1];
}
U32 idx_of( const CT* str ) {
return idx_of( str, 0 );
}
U32 idx_of( const CT* str, U32 offset ) {
for( U32 i = offset; i < this->size; ++i ) {
U8 found = 1;
for( U32 i2 = 0; !!str[i2] && i + i2 < this->size; ++i2 ) {
if( this->data[i + i2] != str[i2] ) {
found = 0;
break;
}
}
if( found )
return i;
}
}
CT* find( const CT* str ) {
for( U32 i = 0; i < this->size; ++i ) {
U8 found = 1;
for( U32 i2 = 0; !!str[i2] && i + i2 < this->size; ++i2 ) {
if( this->data[i + i2] != str[i2] ) {
found = 0;
break;
}
}
if( found )
return this->data + i;
}
return 0;
}
LIST_ITERATOR<CT> end() {
return LIST_ITERATOR<CT>( this->data + this->size );
}
};
using STR = __str<char>;
using WSTR = __str<wchar_t>;
|