Skip to content

Commit cc197ee

Browse files
committed
move parseIntegerList to a free function in ccUtils, add few testcases
1 parent 7f72ee6 commit cc197ee

File tree

6 files changed

+72
-27
lines changed

6 files changed

+72
-27
lines changed

cocos/2d/CCSpriteFrameCache.cpp

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ THE SOFTWARE.
3939
#include "base/CCNS.h"
4040
#include "base/ccMacros.h"
4141
#include "base/ccUTF8.h"
42+
#include "base/ccUtils.h"
4243
#include "base/CCDirector.h"
4344
#include "renderer/CCTexture2D.h"
4445
#include "renderer/CCTextureCache.h"
@@ -77,24 +78,6 @@ SpriteFrameCache::~SpriteFrameCache()
7778
{
7879
}
7980

80-
void SpriteFrameCache::parseIntegerList(const std::string &string, std::vector<int> &res)
81-
{
82-
size_t n = std::count(string.begin(), string.end(), ' ');
83-
res.resize(n + 1);
84-
85-
const char *cStr = string.c_str();
86-
char *endptr;
87-
88-
int i = 0;
89-
do {
90-
long int val = strtol(cStr, &endptr, 10);
91-
if (endptr == cStr)
92-
return;
93-
res[i++] = static_cast<int>(val);
94-
cStr = endptr;
95-
} while (*endptr != '\0');
96-
}
97-
9881
void SpriteFrameCache::initializePolygonInfo(const Size &textureSize,
9982
const Size &spriteSize,
10083
const std::vector<int> &vertices,
@@ -260,12 +243,10 @@ void SpriteFrameCache::addSpriteFramesWithDictionary(ValueMap& dictionary, Textu
260243

261244
if(frameDict.find("vertices") != frameDict.end())
262245
{
263-
std::vector<int> vertices;
264-
parseIntegerList(frameDict["vertices"].asString(), vertices);
265-
std::vector<int> verticesUV;
266-
parseIntegerList(frameDict["verticesUV"].asString(), verticesUV);
267-
std::vector<int> indices;
268-
parseIntegerList(frameDict["triangles"].asString(), indices);
246+
using cocos2d::utils::parseIntegerList;
247+
std::vector<int> vertices = parseIntegerList(frameDict["vertices"].asString());
248+
std::vector<int> verticesUV = parseIntegerList(frameDict["verticesUV"].asString());
249+
std::vector<int> indices = parseIntegerList(frameDict["triangles"].asString());
269250

270251
PolygonInfo info;
271252
initializePolygonInfo(textureSize, spriteSourceSize, vertices, verticesUV, indices, info);

cocos/2d/CCSpriteFrameCache.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -304,9 +304,6 @@ class CC_DLL SpriteFrameCache : public Ref
304304
*/
305305
void removeSpriteFramesFromDictionary(ValueMap& dictionary);
306306

307-
/** Parses list of space-separated integers */
308-
void parseIntegerList(const std::string &string, std::vector<int> &res);
309-
310307
/** Configures PolygonInfo class with the passed sizes + triangles */
311308
void initializePolygonInfo(const Size &textureSize,
312309
const Size &spriteSize,

cocos/base/ccUtils.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ THE SOFTWARE.
3434
#include "base/CCAsyncTaskPool.h"
3535
#include "base/CCEventDispatcher.h"
3636
#include "base/base64.h"
37+
#include "base/ccUTF8.h"
3738
#include "renderer/CCCustomCommand.h"
3839
#include "renderer/CCRenderer.h"
3940
#include "renderer/CCTextureCache.h"
@@ -526,6 +527,24 @@ LanguageType getLanguageTypeByISO2(const char* code)
526527
return ret;
527528
}
528529

530+
std::vector<int> parseIntegerList(const std::string &intsString) {
531+
std::vector<int> result;
532+
533+
const char *cStr = intsString.c_str();
534+
char *endptr;
535+
536+
for (long int i = strtol(cStr, &endptr, 10); endptr != cStr; i = strtol(cStr, &endptr, 10)) {
537+
if (errno == ERANGE) {
538+
errno = 0;
539+
CCLOGWARN("%s contains out of range integers", intsString);
540+
}
541+
result.push_back(static_cast<int>(i));
542+
cStr= endptr;
543+
}
544+
545+
return result;
546+
}
547+
529548
}
530549

531550
NS_CC_END

cocos/base/ccUtils.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,14 @@ namespace utils
188188
* @lua NA
189189
*/
190190
CC_DLL LanguageType getLanguageTypeByISO2(const char* code);
191+
192+
/**
193+
@brief Parses a list of space-separated integers.
194+
@return Vector of ints.
195+
* @js NA
196+
* @lua NA
197+
*/
198+
CC_DLL std::vector<int> parseIntegerList(const std::string &intsString);
191199
}
192200

193201
NS_CC_END

tests/cpp-tests/Classes/UnitTest/UnitTest.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "RefPtrTest.h"
2727
#include "ui/UIHelper.h"
2828
#include "network/Uri.h"
29+
#include "base/ccUtils.h"
2930

3031
USING_NS_CC;
3132
using namespace cocos2d::network;
@@ -75,6 +76,7 @@ UnitTests::UnitTests()
7576
ADD_TEST_CASE(RefPtrTest);
7677
ADD_TEST_CASE(UTFConversionTest);
7778
ADD_TEST_CASE(UIHelperSubStringTest);
79+
ADD_TEST_CASE(ParseIntegerListTest);
7880
ADD_TEST_CASE(ParseUriTest);
7981
ADD_TEST_CASE(ResizableBufferAdapterTest);
8082
#ifdef UNIT_TEST_FOR_OPTIMIZED_MATH_UTIL
@@ -908,6 +910,35 @@ std::string UIHelperSubStringTest::subtitle() const
908910
return "ui::Helper::getSubStringOfUTF8String Test";
909911
}
910912

913+
// ParseIntegerListTest
914+
void ParseIntegerListTest::onEnter() {
915+
UnitTestDemo::onEnter();
916+
917+
{
918+
using cocos2d::utils::parseIntegerList;
919+
920+
std::vector<int> res1{};
921+
EXPECT_EQ(res1, parseIntegerList(""));
922+
923+
std::vector<int> res2{1};
924+
EXPECT_EQ(res2, parseIntegerList("1"));
925+
926+
std::vector<int> res3{1, 2};
927+
EXPECT_EQ(res3, parseIntegerList("1 2"));
928+
929+
std::vector<int> res4{2, 4, 3, 1, 4, 2, 0, 4, 1, 0, 4, 5};
930+
EXPECT_EQ(res4, parseIntegerList("2 4 3 1 4 2 0 4 1 0 4 5"));
931+
932+
std::vector<int> res5{73, 48, 57, 117, 27, 117, 29, 77, 14, 62, 26, 7, 55, 2};
933+
EXPECT_EQ(res5, parseIntegerList("73 48 57 117 27 117 29 77 14 62 26 7 55 2"));
934+
}
935+
}
936+
937+
std::string ParseIntegerListTest::subtitle() const
938+
{
939+
return "utils::parseIntegerList Test";
940+
}
941+
911942
// ParseUriTest
912943
void ParseUriTest::onEnter()
913944
{

tests/cpp-tests/Classes/UnitTest/UnitTest.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,15 @@ class UIHelperSubStringTest : public UnitTestDemo
7979
virtual void onEnter() override;
8080
virtual std::string subtitle() const override;
8181
};
82+
83+
class ParseIntegerListTest : public UnitTestDemo
84+
{
85+
public:
86+
CREATE_FUNC(ParseIntegerListTest);
87+
virtual void onEnter() override;
88+
virtual std::string subtitle() const override;
89+
};
90+
8291
class ParseUriTest : public UnitTestDemo
8392
{
8493
public:

0 commit comments

Comments
 (0)