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
6namespace sead {
7
8template <typename CharType>
9inline const CharType&
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
22template <typename CharType>
23inline s32
25{
27
29 s32 length = 0;
30
31 for (;;)
32 {
34 break;
35
36 length++;
37 }
38
40 {
41 SEAD_ASSERT_MSG(false, "too long string\n");
42 return 0;
43 }
44
45 return length;
46}
47
48template <typename CharType>
49inline const SafeStringBase<CharType>
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
60}
61
62template <typename CharType>
63inline bool
65{
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
83template <typename CharType>
84inline s32
86{
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);
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
118template <typename CharType>
119inline s32
121{
122 s32 len = calcLength();
124
125 for (s32 i = 0; i <= len - token_len; i++)
127 return i;
128
129 return -1;
130}
131
132template <typename CharType>
133inline s32
135{
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));
148
149 return size;
150}
151
152template <typename CharType>
153inline s32
155{
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;
168
169 if (cpy_length < 0)
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
182 if (at + cpy_length > len)
184
185 return cpy_length;
186}
187
188template <typename CharType>
189inline s32
194
195template <typename CharType>
196inline s32
198{
199 s32 len = this->calcLength();
200 if (len >= getBufferSize() - 1)
202 SEAD_ASSERT_MSG(false, "Buffer overflow. (Buffer Size: %d, Length: %d)", getBufferSize(), len);
203 return 0;
204 }
205
208 mutable_string_top[len + 1] = 0;
209
210 return 1;
211}
212
213template <typename CharType>
214inline s32
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
231
232 return trim_length;
233}
234
235template <s32 N>
237 : FixedSafeStringBase<char, N>()
238{
239 va_list va;
241 (void)this->formatV(format_string, va);
242 va_end(va);
243}
244
245template <s32 N>
254
255} // namespace sead
Definition seadAssert.h:44
#define SEAD_ASSERT(condition)
Definition seadAssert.h:24
#define SEAD_ASSERT_MSG(condition, format,...)
Definition seadAssert.h:33