Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 5 additions & 24 deletions cocos/2d/CCSpriteFrameCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ THE SOFTWARE.
#include "base/CCNS.h"
#include "base/ccMacros.h"
#include "base/ccUTF8.h"
#include "base/ccUtils.h"
#include "base/CCDirector.h"
#include "renderer/CCTexture2D.h"
#include "renderer/CCTextureCache.h"
Expand Down Expand Up @@ -77,24 +78,6 @@ SpriteFrameCache::~SpriteFrameCache()
{
}

void SpriteFrameCache::parseIntegerList(const std::string &string, std::vector<int> &res)
{
size_t n = std::count(string.begin(), string.end(), ' ');
res.resize(n + 1);

const char *cStr = string.c_str();
char *endptr;

int i = 0;
do {
long int val = strtol(cStr, &endptr, 10);
if (endptr == cStr)
return;
res[i++] = static_cast<int>(val);
cStr = endptr;
} while (*endptr != '\0');
}

void SpriteFrameCache::initializePolygonInfo(const Size &textureSize,
const Size &spriteSize,
const std::vector<int> &vertices,
Expand Down Expand Up @@ -260,12 +243,10 @@ void SpriteFrameCache::addSpriteFramesWithDictionary(ValueMap& dictionary, Textu

if(frameDict.find("vertices") != frameDict.end())
{
std::vector<int> vertices;
parseIntegerList(frameDict["vertices"].asString(), vertices);
std::vector<int> verticesUV;
parseIntegerList(frameDict["verticesUV"].asString(), verticesUV);
std::vector<int> indices;
parseIntegerList(frameDict["triangles"].asString(), indices);
using cocos2d::utils::parseIntegerList;
std::vector<int> vertices = parseIntegerList(frameDict["vertices"].asString());
std::vector<int> verticesUV = parseIntegerList(frameDict["verticesUV"].asString());
std::vector<int> indices = parseIntegerList(frameDict["triangles"].asString());

PolygonInfo info;
initializePolygonInfo(textureSize, spriteSourceSize, vertices, verticesUV, indices, info);
Expand Down
3 changes: 0 additions & 3 deletions cocos/2d/CCSpriteFrameCache.h
Original file line number Diff line number Diff line change
Expand Up @@ -304,9 +304,6 @@ class CC_DLL SpriteFrameCache : public Ref
*/
void removeSpriteFramesFromDictionary(ValueMap& dictionary);

/** Parses list of space-separated integers */
void parseIntegerList(const std::string &string, std::vector<int> &res);

/** Configures PolygonInfo class with the passed sizes + triangles */
void initializePolygonInfo(const Size &textureSize,
const Size &spriteSize,
Expand Down
19 changes: 19 additions & 0 deletions cocos/base/ccUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ THE SOFTWARE.
#include "base/CCAsyncTaskPool.h"
#include "base/CCEventDispatcher.h"
#include "base/base64.h"
#include "base/ccUTF8.h"
#include "renderer/CCCustomCommand.h"
#include "renderer/CCRenderer.h"
#include "renderer/CCTextureCache.h"
Expand Down Expand Up @@ -526,6 +527,24 @@ LanguageType getLanguageTypeByISO2(const char* code)
return ret;
}

std::vector<int> parseIntegerList(const std::string &intsString) {
std::vector<int> result;

const char *cStr = intsString.c_str();
char *endptr;

for (long int i = strtol(cStr, &endptr, 10); endptr != cStr; i = strtol(cStr, &endptr, 10)) {
if (errno == ERANGE) {
errno = 0;
CCLOGWARN("%s contains out of range integers", intsString.c_str());
}
result.push_back(static_cast<int>(i));
cStr= endptr;
}

return result;
}

}

NS_CC_END
8 changes: 8 additions & 0 deletions cocos/base/ccUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,14 @@ namespace utils
* @lua NA
*/
CC_DLL LanguageType getLanguageTypeByISO2(const char* code);

/**
@brief Parses a list of space-separated integers.
@return Vector of ints.
* @js NA
* @lua NA
*/
CC_DLL std::vector<int> parseIntegerList(const std::string &intsString);
}

NS_CC_END
Expand Down
31 changes: 31 additions & 0 deletions tests/cpp-tests/Classes/UnitTest/UnitTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "RefPtrTest.h"
#include "ui/UIHelper.h"
#include "network/Uri.h"
#include "base/ccUtils.h"

USING_NS_CC;
using namespace cocos2d::network;
Expand Down Expand Up @@ -75,6 +76,7 @@ UnitTests::UnitTests()
ADD_TEST_CASE(RefPtrTest);
ADD_TEST_CASE(UTFConversionTest);
ADD_TEST_CASE(UIHelperSubStringTest);
ADD_TEST_CASE(ParseIntegerListTest);
ADD_TEST_CASE(ParseUriTest);
ADD_TEST_CASE(ResizableBufferAdapterTest);
#ifdef UNIT_TEST_FOR_OPTIMIZED_MATH_UTIL
Expand Down Expand Up @@ -908,6 +910,35 @@ std::string UIHelperSubStringTest::subtitle() const
return "ui::Helper::getSubStringOfUTF8String Test";
}

// ParseIntegerListTest
void ParseIntegerListTest::onEnter() {
UnitTestDemo::onEnter();

{
using cocos2d::utils::parseIntegerList;

std::vector<int> res1{};
EXPECT_EQ(res1, parseIntegerList(""));

std::vector<int> res2{1};
EXPECT_EQ(res2, parseIntegerList("1"));

std::vector<int> res3{1, 2};
EXPECT_EQ(res3, parseIntegerList("1 2"));

std::vector<int> res4{2, 4, 3, 1, 4, 2, 0, 4, 1, 0, 4, 5};
EXPECT_EQ(res4, parseIntegerList("2 4 3 1 4 2 0 4 1 0 4 5"));

std::vector<int> res5{73, 48, 57, 117, 27, 117, 29, 77, 14, 62, 26, 7, 55, 2};
EXPECT_EQ(res5, parseIntegerList("73 48 57 117 27 117 29 77 14 62 26 7 55 2"));
}
}

std::string ParseIntegerListTest::subtitle() const
{
return "utils::parseIntegerList Test";
}

// ParseUriTest
void ParseUriTest::onEnter()
{
Expand Down
9 changes: 9 additions & 0 deletions tests/cpp-tests/Classes/UnitTest/UnitTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,15 @@ class UIHelperSubStringTest : public UnitTestDemo
virtual void onEnter() override;
virtual std::string subtitle() const override;
};

class ParseIntegerListTest : public UnitTestDemo
{
public:
CREATE_FUNC(ParseIntegerListTest);
virtual void onEnter() override;
virtual std::string subtitle() const override;
};

class ParseUriTest : public UnitTestDemo
{
public:
Expand Down