sead
Loading...
Searching...
No Matches
seadSafeString.hpp
Go to the documentation of this file.
1
#
pragma
once
2
3
#
include
<
basis
/
seadAssert
.
h
>
4
#
include
<
prim
/
seadMemUtil
.
h
>
5
6
namespace
sead
{
7
8
template
<
typename
CharType
>
9
inline
const
CharType
&
10
SafeStringBase
<
CharType
>::
at
(
s32
idx
)
const
11
{
12
s32
len
=
calcLength
();
13
if
(
idx
< 0 ||
idx
>
len
)
14
{
15
SEAD_ASSERT_MSG
(
false
,
"index(%d) out of range[0, %d]"
,
idx
,
len
);
16
return
cNullChar
;
17
}
18
19
return
mStringTop
[
idx
];
20
}
21
22
template
<
typename
CharType
>
23
inline
s32
24
SafeStringBase
<
CharType
>::
calcLength
()
const
25
{
26
SEAD_ASSERT
(
mStringTop
);
27
28
assureTerminationImpl_
();
29
s32
length
= 0;
30
31
for
(;;)
32
{
33
if
(
length
>
cMaximumLength
||
mStringTop
[
length
] == 0)
34
break
;
35
36
length
++;
37
}
38
39
if
(
length
>
cMaximumLength
)
40
{
41
SEAD_ASSERT_MSG
(
false
,
"too long string\n"
);
42
return
0;
43
}
44
45
return
length
;
46
}
47
48
template
<
typename
CharType
>
49
inline
const
SafeStringBase
<
CharType
>
50
SafeStringBase
<
CharType
>::
getPart
(
s32
at
)
const
51
{
52
s32
len
=
calcLength
();
53
if
(
at
< 0 ||
at
>
len
)
54
{
55
SEAD_ASSERT_MSG
(
false
,
"index(%d) out of range[0, %d]"
,
at
,
len
);
56
return
cEmptyString
;
57
}
58
59
return
SafeStringBase
<
CharType
>(
mStringTop
+
at
);
60
}
61
62
template
<
typename
CharType
>
63
inline
bool
64
SafeStringBase
<
CharType
>::
isEqual
(
const
SafeStringBase
<
CharType
>&
rhs
)
const
65
{
66
assureTerminationImpl_
();
67
if
(
cstr
() ==
rhs
.
cstr
())
68
return
true
;
69
70
for
(
s32
i
= 0;
i
<=
cMaximumLength
;
i
++)
71
{
72
if
(
unsafeAt_
(
i
) !=
rhs
.
unsafeAt_
(
i
))
73
return
false
;
74
75
else
if
(
unsafeAt_
(
i
) == 0)
76
return
true
;
77
}
78
79
SEAD_ASSERT_MSG
(
false
,
"too long string\n"
);
80
return
false
;
81
}
82
83
template
<
typename
CharType
>
84
inline
s32
85
SafeStringBase
<
CharType
>::
comparen
(
const
SafeStringBase
<
CharType
>&
rhs
,
s32
n
)
const
86
{
87
assureTerminationImpl_
();
88
if
(
cstr
() ==
rhs
.
cstr
())
89
return
0;
90
91
if
(
n
>
cMaximumLength
)
92
{
93
SEAD_ASSERT_MSG
(
false
,
"paramater(%d) out of bounds [0, %d]"
,
n
,
cMaximumLength
);
94
n
=
cMaximumLength
;
95
}
96
97
for
(
s32
i
= 0;
i
<
n
;
i
++)
98
{
99
if
(
unsafeAt_
(
i
) == 0 &&
rhs
.
unsafeAt_
(
i
) == 0)
100
return
0;
101
102
else
if
(
unsafeAt_
(
i
) == 0)
103
return
-1;
104
105
else
if
(
rhs
.
unsafeAt_
(
i
) == 0)
106
return
1;
107
108
else
if
(
unsafeAt_
(
i
) <
rhs
.
unsafeAt_
(
i
))
109
return
-1;
110
111
else
if
(
unsafeAt_
(
i
) >
rhs
.
unsafeAt_
(
i
))
112
return
1;
113
}
114
115
return
0;
116
}
117
118
template
<
typename
CharType
>
119
inline
s32
120
SafeStringBase
<
CharType
>::
findIndex
(
const
SafeStringBase
<
CharType
>&
token
)
const
121
{
122
s32
len
=
calcLength
();
123
s32
token_len
=
token
.
calcLength
();
124
125
for
(
s32
i
= 0;
i
<=
len
-
token_len
;
i
++)
126
if
(
SafeStringBase
<
CharType
>(&
mStringTop
[
i
]).
comparen
(
token
,
token_len
) == 0)
127
return
i
;
128
129
return
-1;
130
}
131
132
template
<
typename
CharType
>
133
inline
s32
134
BufferedSafeStringBase
<
CharType
>::
copy
(
const
SafeStringBase
<
CharType
>&
rhs
,
s32
size
)
135
{
136
CharType
*
mutable_string_top
=
getMutableStringTop_
();
137
if
(
size
< 0)
138
size
=
rhs
.
calcLength
();
139
140
if
(
size
>=
getBufferSize
())
141
{
142
SEAD_ASSERT_MSG
(
false
,
"Buffer overflow. (Buffer Size: %d, Copy Size: %d)"
,
getBufferSize
(),
size
);
143
size
=
getBufferSize
() - 1;
144
}
145
146
MemUtil
::
copy
(
mutable_string_top
,
rhs
.
cstr
(),
static_cast
<
size_t
>(
size
) *
sizeof
(
CharType
));
147
mutable_string_top
[
size
] = 0;
148
149
return
size
;
150
}
151
152
template
<
typename
CharType
>
153
inline
s32
154
BufferedSafeStringBase
<
CharType
>::
copyAt
(
s32
at
,
const
SafeStringBase
<
CharType
>&
src
,
s32
cpy_length
)
155
{
156
CharType
*
mutable_string_top
=
getMutableStringTop_
();
157
s32
len
=
this
->
calcLength
();
158
159
if
(
at
< 0)
160
{
161
at
=
len
+
at
+ 1;
162
if
(
at
< 0)
163
{
164
SEAD_ASSERT_MSG
(
false
,
"at(%d) out of range[0, %d]"
,
at
,
len
);
165
at
= 0;
166
}
167
}
168
169
if
(
cpy_length
< 0)
170
cpy_length
=
src
.
calcLength
();
171
172
if
(
at
+
cpy_length
>=
getBufferSize
())
173
{
174
SEAD_ASSERT_MSG
(
false
,
"Buffer overflow. (Buffer Size: %d, At: %d, Copy Length: %d)"
,
getBufferSize
(),
at
,
cpy_length
);
175
cpy_length
=
getBufferSize
() -
at
- 1;
176
}
177
178
if
(
cpy_length
<= 0)
179
return
0;
180
181
MemUtil
::
copy
(
mutable_string_top
+
at
,
src
.
cstr
(),
cpy_length
*
sizeof
(
CharType
));
182
if
(
at
+
cpy_length
>
len
)
183
mutable_string_top
[
at
+
cpy_length
] = 0;
184
185
return
cpy_length
;
186
}
187
188
template
<
typename
CharType
>
189
inline
s32
190
BufferedSafeStringBase
<
CharType
>::
append
(
const
SafeStringBase
<
CharType
>&
src
,
s32
append_length
)
191
{
192
return
copyAt
(-1,
src
,
append_length
);
193
}
194
195
template
<
typename
CharType
>
196
inline
s32
197
BufferedSafeStringBase
<
CharType
>::
append
(
CharType
src_chr
)
198
{
199
s32
len
=
this
->
calcLength
();
200
if
(
len
>=
getBufferSize
() - 1)
201
{
202
SEAD_ASSERT_MSG
(
false
,
"Buffer overflow. (Buffer Size: %d, Length: %d)"
,
getBufferSize
(),
len
);
203
return
0;
204
}
205
206
CharType
*
mutable_string_top
=
getMutableStringTop_
();
207
mutable_string_top
[
len
] =
src_chr
;
208
mutable_string_top
[
len
+ 1] = 0;
209
210
return
1;
211
}
212
213
template
<
typename
CharType
>
214
inline
s32
215
BufferedSafeStringBase
<
CharType
>::
trim
(
s32
trim_length
)
216
{
217
if
(
trim_length
>=
getBufferSize
())
218
{
219
SEAD_ASSERT_MSG
(
false
,
"trim_length(%d) out of bounds. [0,%d) \n"
,
trim_length
,
getBufferSize
());
220
return
this
->
calcLength
();
221
}
222
223
if
(
trim_length
< 0)
224
{
225
SEAD_ASSERT_MSG
(
false
,
"trim_length(%d) out of bounds. [0,%d) \n"
,
trim_length
,
getBufferSize
());
226
trim_length
= 0;
227
}
228
229
CharType
*
mutable_string_top
=
getMutableStringTop_
();
230
mutable_string_top
[
trim_length
] = 0;
231
232
return
trim_length
;
233
}
234
235
template
<
s32
N
>
236
FormatFixedSafeString
<
N
>::
FormatFixedSafeString
(
const
char
*
format_string
, ...)
237
:
FixedSafeStringBase
<
char
,
N
>()
238
{
239
va_list
va
;
240
va_start
(
va
,
format_string
);
241
(
void
)
this
->
formatV
(
format_string
,
va
);
242
va_end
(
va
);
243
}
244
245
template
<
s32
N
>
246
WFormatFixedSafeString
<
N
>::
WFormatFixedSafeString
(
const
char16
*
format_string
, ...)
247
:
FixedSafeStringBase
<
char16
,
N
>()
248
{
249
va_list
va
;
250
va_start
(
va
,
format_string
);
251
(
void
)
this
->
formatV
(
format_string
,
va
);
252
va_end
(
va
);
253
}
254
255
}
// namespace sead
sead
Definition
seadAssert.h:44
SEAD_ASSERT
#define SEAD_ASSERT(condition)
Definition
seadAssert.h:24
SEAD_ASSERT_MSG
#define SEAD_ASSERT_MSG(condition, format,...)
Definition
seadAssert.h:33
engine
library
include
prim
seadSafeString.hpp
Generated by
1.14.0