From ad54a24cf085b5706ca8d5558683fc12272a0a35 Mon Sep 17 00:00:00 2001 From: minggo Date: Fri, 19 Jul 2019 11:57:11 +0800 Subject: [PATCH] sync 19634 and fix conflict (#19930) --- cocos/2d/CCSpriteFrameCache.cpp | 29 +++-------------- cocos/2d/CCSpriteFrameCache.h | 3 -- cocos/base/ccUtils.cpp | 19 ++++++++++++ cocos/base/ccUtils.h | 7 +++++ tests/cpp-tests/Classes/UnitTest/UnitTest.cpp | 31 +++++++++++++++++++ tests/cpp-tests/Classes/UnitTest/UnitTest.h | 9 ++++++ 6 files changed, 71 insertions(+), 27 deletions(-) diff --git a/cocos/2d/CCSpriteFrameCache.cpp b/cocos/2d/CCSpriteFrameCache.cpp index c9792b1fa2..a5316ae876 100644 --- a/cocos/2d/CCSpriteFrameCache.cpp +++ b/cocos/2d/CCSpriteFrameCache.cpp @@ -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" @@ -77,24 +78,6 @@ SpriteFrameCache::~SpriteFrameCache() { } -void SpriteFrameCache::parseIntegerList(const std::string &string, std::vector &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(val); - cStr = endptr; - } while (*endptr != '\0'); -} - void SpriteFrameCache::initializePolygonInfo(const Size &textureSize, const Size &spriteSize, const std::vector &vertices, @@ -260,12 +243,10 @@ void SpriteFrameCache::addSpriteFramesWithDictionary(ValueMap& dictionary, Textu if(frameDict.find("vertices") != frameDict.end()) { - std::vector vertices; - parseIntegerList(frameDict["vertices"].asString(), vertices); - std::vector verticesUV; - parseIntegerList(frameDict["verticesUV"].asString(), verticesUV); - std::vector indices; - parseIntegerList(frameDict["triangles"].asString(), indices); + using cocos2d::utils::parseIntegerList; + std::vector vertices = parseIntegerList(frameDict["vertices"].asString()); + std::vector verticesUV = parseIntegerList(frameDict["verticesUV"].asString()); + std::vector indices = parseIntegerList(frameDict["triangles"].asString()); PolygonInfo info; initializePolygonInfo(textureSize, spriteSourceSize, vertices, verticesUV, indices, info); diff --git a/cocos/2d/CCSpriteFrameCache.h b/cocos/2d/CCSpriteFrameCache.h index 7b87fd9b48..f7135ed5d9 100644 --- a/cocos/2d/CCSpriteFrameCache.h +++ b/cocos/2d/CCSpriteFrameCache.h @@ -291,9 +291,6 @@ protected: */ void removeSpriteFramesFromDictionary(ValueMap& dictionary); - /** Parses list of space-separated integers */ - void parseIntegerList(const std::string &string, std::vector &res); - /** Configures PolygonInfo class with the passed sizes + triangles */ void initializePolygonInfo(const Size &textureSize, const Size &spriteSize, diff --git a/cocos/base/ccUtils.cpp b/cocos/base/ccUtils.cpp index 8b6a4b529a..9a91569c78 100644 --- a/cocos/base/ccUtils.cpp +++ b/cocos/base/ccUtils.cpp @@ -35,6 +35,7 @@ THE SOFTWARE. #include "base/CCEventDispatcher.h" #include "base/base64.h" #include "base/ccConstants.h" +#include "base/ccUTF8.h" #include "renderer/CCCustomCommand.h" #include "renderer/CCRenderer.h" #include "renderer/CCTextureCache.h" @@ -667,6 +668,24 @@ std::vector getNormalMat3OfMat4(const Mat4 &mat) return normalMat; } +std::vector parseIntegerList(const std::string &intsString) { + std::vector 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(i)); + cStr= endptr; + } + + return result; +} + } NS_CC_END diff --git a/cocos/base/ccUtils.h b/cocos/base/ccUtils.h index 8d066ed20b..2455551f83 100644 --- a/cocos/base/ccUtils.h +++ b/cocos/base/ccUtils.h @@ -207,6 +207,13 @@ namespace utils */ CC_DLL std::vector getNormalMat3OfMat4(const Mat4 &mat); + /** + @brief Parses a list of space-separated integers. + @return Vector of ints. + * @js NA + * @lua NA + */ + CC_DLL std::vector parseIntegerList(const std::string &intsString); } NS_CC_END diff --git a/tests/cpp-tests/Classes/UnitTest/UnitTest.cpp b/tests/cpp-tests/Classes/UnitTest/UnitTest.cpp index 89b8d2ba7b..5574dbe787 100644 --- a/tests/cpp-tests/Classes/UnitTest/UnitTest.cpp +++ b/tests/cpp-tests/Classes/UnitTest/UnitTest.cpp @@ -25,6 +25,7 @@ #include "UnitTest.h" #include "ui/UIHelper.h" #include "network/Uri.h" +#include "base/ccUtils.h" USING_NS_CC; using namespace cocos2d::network; @@ -73,6 +74,7 @@ UnitTests::UnitTests() ADD_TEST_CASE(ValueTest); 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 @@ -920,6 +922,35 @@ std::string UIHelperSubStringTest::subtitle() const return "ui::Helper::getSubStringOfUTF8String Test"; } +// ParseIntegerListTest +void ParseIntegerListTest::onEnter() { + UnitTestDemo::onEnter(); + + { + using cocos2d::utils::parseIntegerList; + + std::vector res1{}; + EXPECT_EQ(res1, parseIntegerList("")); + + std::vector res2{1}; + EXPECT_EQ(res2, parseIntegerList("1")); + + std::vector res3{1, 2}; + EXPECT_EQ(res3, parseIntegerList("1 2")); + + std::vector 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 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() { diff --git a/tests/cpp-tests/Classes/UnitTest/UnitTest.h b/tests/cpp-tests/Classes/UnitTest/UnitTest.h index dfa5af7430..23ce9ad675 100644 --- a/tests/cpp-tests/Classes/UnitTest/UnitTest.h +++ b/tests/cpp-tests/Classes/UnitTest/UnitTest.h @@ -79,6 +79,15 @@ public: 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: