Skip to content

Commit 40ec511

Browse files
summerinsectsminggo
authored andcommitted
improvements for StringUtils::StringUTF8 (cocos2d#18356)
* 1. add 'const' 2. add range support for 'getAsCharSequence' * remove default arguments * use another variable to make it more clear * rename isAnsi-> isASCII
1 parent 25c2403 commit 40ec511

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

cocos/base/ccUTF8.cpp

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "platform/CCCommon.h"
2828
#include "base/CCConsole.h"
2929
#include "ConvertUTF.h"
30+
#include <limits>
3031

3132
NS_CC_BEGIN
3233

@@ -379,12 +380,28 @@ void StringUTF8::replace(const std::string& newStr)
379380
}
380381

381382
std::string StringUTF8::getAsCharSequence() const
383+
{
384+
return getAsCharSequence(0, std::numeric_limits<std::size_t>::max());
385+
}
386+
387+
std::string StringUTF8::getAsCharSequence(std::size_t pos) const
388+
{
389+
return getAsCharSequence(pos, std::numeric_limits<std::size_t>::max());
390+
}
391+
392+
std::string StringUTF8::getAsCharSequence(std::size_t pos, std::size_t len) const
382393
{
383394
std::string charSequence;
395+
std::size_t maxLen = _str.size() - pos;
396+
if (len > maxLen)
397+
{
398+
len = maxLen;
399+
}
384400

385-
for (auto& charUtf8 : _str)
401+
std::size_t endPos = len + pos;
402+
while (pos < endPos)
386403
{
387-
charSequence.append(charUtf8._char);
404+
charSequence.append(_str[pos++]._char);
388405
}
389406

390407
return charSequence;

cocos/base/ccUTF8.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ class CC_DLL StringUTF8
196196
struct CharUTF8
197197
{
198198
std::string _char;
199-
bool isAnsi() { return _char.size() == 1; }
199+
bool isASCII() const { return _char.size() == 1; }
200200
};
201201
typedef std::vector<CharUTF8> CharUTF8Store;
202202

@@ -208,12 +208,15 @@ class CC_DLL StringUTF8
208208
void replace(const std::string& newStr);
209209

210210
std::string getAsCharSequence() const;
211+
std::string getAsCharSequence(std::size_t pos) const;
212+
std::string getAsCharSequence(std::size_t pos, std::size_t len) const;
211213

212214
bool deleteChar(std::size_t pos);
213215
bool insert(std::size_t pos, const std::string& insertStr);
214216
bool insert(std::size_t pos, const StringUTF8& insertStr);
215217

216218
CharUTF8Store& getString() { return _str; }
219+
const CharUTF8Store& getString() const { return _str; }
217220

218221
private:
219222
CharUTF8Store _str;

0 commit comments

Comments
 (0)