Skip to content

Commit 6defc32

Browse files
committed
Improve Console::Utility::trim and move to StringUtils
- Use c++20 reature ranges - Use c++20 0-GC split to instead std::getline
1 parent 0f2dcbe commit 6defc32

File tree

5 files changed

+180
-168
lines changed

5 files changed

+180
-168
lines changed

core/2d/TMXXMLParser.cpp

Lines changed: 62 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,13 @@ THE SOFTWARE.
3232
#include <unordered_map>
3333
#include <sstream>
3434
#include <regex>
35-
// #include "2d/TMXTiledMap.h"
35+
3636
#include "base/ZipUtils.h"
3737
#include "base/Director.h"
3838
#include "base/Utils.h"
3939
#include "platform/FileUtils.h"
40-
41-
// using namespace std;
40+
#include <ranges>
41+
#include <charconv>
4242

4343
namespace ax
4444
{
@@ -452,10 +452,9 @@ void TMXMapInfo::startElement(void* /*ctx*/, const char* name, const char** atts
452452

453453
TMXLayerInfo* layer = tmxMapInfo->getLayers().back();
454454
Vec2 layerSize = layer->_layerSize;
455-
auto tilesAmount = static_cast<size_t>(layerSize.width * layerSize.height);
455+
auto tilesAmount = static_cast<size_t>(layerSize.width * layerSize.height);
456456

457-
layer->_tiles =
458-
(uint32_t*)axstd::pod_vector<uint32_t>(tilesAmount, 0U).release_pointer();
457+
layer->_tiles = (uint32_t*)axstd::pod_vector<uint32_t>(tilesAmount, 0U).release_pointer();
459458
}
460459
else if (encoding == "base64")
461460
{
@@ -534,7 +533,7 @@ void TMXMapInfo::startElement(void* /*ctx*/, const char* name, const char** atts
534533
if (tmxMapInfo->getParentElement() == TMXPropertyNone)
535534
{
536535
AXLOGD("TMX tile map: Parent element is unsupported. Cannot add property named '{}' with value '{}'",
537-
attributeDict["name"].asString(), attributeDict["value"].asString());
536+
attributeDict["name"].asString(), attributeDict["value"].asString());
538537
tmxMapInfo->setStoringCharacters(false);
539538
}
540539
else if (tmxMapInfo->getParentElement() == TMXPropertyMap)
@@ -596,36 +595,36 @@ void TMXMapInfo::startElement(void* /*ctx*/, const char* name, const char** atts
596595
ValueVector pointsArray;
597596
pointsArray.reserve(10);
598597

599-
// parse points string into a space-separated set of points
600-
std::stringstream pointsStream(value);
601-
std::string pointPair;
602-
while (std::getline(pointsStream, pointPair, ' '))
598+
const auto offsetX = static_cast<int>(objectGroup->getPositionOffset().x);
599+
const auto offsetY = static_cast<int>(objectGroup->getPositionOffset().y);
600+
// std::views::split 2~3x faster than std::getline
601+
for (auto pt : std::views::split(value, ' '))
603602
{
604-
// parse each point combo into a comma-separated x,y point
605-
std::stringstream pointStream(pointPair);
606-
std::string xStr, yStr;
607-
603+
std::string_view citem{pt.data(), pt.size()};
604+
int idx = 0;
608605
ValueMap pointDict;
609-
610-
// set x
611-
if (std::getline(pointStream, xStr, ','))
612-
{
613-
int x = atoi(xStr.c_str()) + (int)objectGroup->getPositionOffset().x;
614-
pointDict["x"] = Value(x);
615-
}
616-
617-
// set y
618-
if (std::getline(pointStream, yStr, ','))
606+
for (auto subrgn : std::views::split(pt, ','))
619607
{
620-
int y = atoi(yStr.c_str()) + (int)objectGroup->getPositionOffset().y;
621-
pointDict["y"] = Value(y);
608+
int axisVal = 0;
609+
std::string_view word(subrgn.data());
610+
std::from_chars(word.data(), word.data() + word.length(), axisVal, 10);
611+
switch (idx++)
612+
{
613+
case 0:
614+
pointDict["x"] = Value(axisVal + offsetX);
615+
break;
616+
case 1:
617+
pointDict["y"] = Value(axisVal + offsetY);
618+
break;
619+
}
620+
if (idx == 2)
621+
break;
622622
}
623-
624623
// add to points array
625-
pointsArray.emplace_back(Value(pointDict));
624+
pointsArray.emplace_back(Value(std::move(pointDict)));
626625
}
627626

628-
dict["points"] = Value(pointsArray);
627+
dict["points"] = Value(std::move(pointsArray));
629628
}
630629
}
631630
else if (elementName == "polyline")
@@ -641,36 +640,32 @@ void TMXMapInfo::startElement(void* /*ctx*/, const char* name, const char** atts
641640
ValueVector pointsArray;
642641
pointsArray.reserve(10);
643642

644-
// parse points string into a space-separated set of points
645-
std::stringstream pointsStream(value);
646-
std::string pointPair;
647-
while (std::getline(pointsStream, pointPair, ' '))
643+
const auto offsetX = static_cast<int>(objectGroup->getPositionOffset().x);
644+
const auto offsetY = static_cast<int>(objectGroup->getPositionOffset().y);
645+
for (auto pt : std::views::split(value, ' '))
648646
{
649-
// parse each point combo into a comma-separated x,y point
650-
std::stringstream pointStream(pointPair);
651-
std::string xStr, yStr;
652-
647+
int idx = 0;
653648
ValueMap pointDict;
654-
655-
// set x
656-
if (std::getline(pointStream, xStr, ','))
657-
{
658-
int x = atoi(xStr.c_str()) + (int)objectGroup->getPositionOffset().x;
659-
pointDict["x"] = Value(x);
660-
}
661-
662-
// set y
663-
if (std::getline(pointStream, yStr, ','))
649+
for (auto pt_axis : std::views::split(pt, ','))
664650
{
665-
int y = atoi(yStr.c_str()) + (int)objectGroup->getPositionOffset().y;
666-
pointDict["y"] = Value(y);
651+
int axisVal = 0;
652+
std::from_chars(pt_axis.data(), pt_axis.data() + pt_axis.size(), axisVal, 10);
653+
switch (idx++)
654+
{
655+
case 0:
656+
pointDict["x"] = Value(axisVal + offsetX);
657+
break;
658+
case 1:
659+
pointDict["y"] = Value(axisVal + offsetY);
660+
break;
661+
}
662+
if (idx == 2)
663+
break;
667664
}
668-
669665
// add to points array
670-
pointsArray.emplace_back(Value(pointDict));
666+
pointsArray.emplace_back(Value(std::move(pointDict)));
671667
}
672-
673-
dict["polylinePoints"] = Value(pointsArray);
668+
dict["polylinePoints"] = Value(std::move(pointsArray));
674669
}
675670
}
676671
else if (elementName == "animation")
@@ -701,7 +696,7 @@ void TMXMapInfo::endElement(void* /*ctx*/, const char* name)
701696
tmxMapInfo->setStoringCharacters(false);
702697

703698
TMXLayerInfo* layer = tmxMapInfo->getLayers().back();
704-
auto currentString = tmxMapInfo->getCurrentString();
699+
auto currentString = tmxMapInfo->getCurrentString();
705700

706701
auto buffer = utils::base64Decode(currentString);
707702
if (buffer.empty())
@@ -712,7 +707,7 @@ void TMXMapInfo::endElement(void* /*ctx*/, const char* name)
712707

713708
if (tmxMapInfo->getLayerAttribs() & (TMXLayerAttribGzip | TMXLayerAttribZlib))
714709
{
715-
Vec2 s = layer->_layerSize;
710+
Vec2 s = layer->_layerSize;
716711
// int sizeHint = s.width * s.height * sizeof(uint32_t);
717712
ssize_t sizeHint = s.width * s.height * sizeof(unsigned int);
718713

@@ -741,31 +736,17 @@ void TMXMapInfo::endElement(void* /*ctx*/, const char* name)
741736
tmxMapInfo->setStoringCharacters(false);
742737
auto currentString = tmxMapInfo->getCurrentString();
743738

744-
std::vector<std::string> gidTokens;
745-
std::stringstream filestr;
746-
filestr << currentString;
747-
std::string sRow;
748-
while (std::getline(filestr, sRow, '\n'))
749-
{
750-
std::string sGID;
751-
std::istringstream rowstr(sRow);
752-
while (std::getline(rowstr, sGID, ','))
753-
{
754-
gidTokens.emplace_back(sGID);
755-
}
756-
}
757-
758-
// 32-bits per gid
759-
axstd::pod_vector<uint32_t> buffer(gidTokens.size());
760-
uint32_t* bufferPtr = buffer.data();
761-
for (const auto& gidToken : gidTokens)
762-
{
763-
auto tileGid = (uint32_t)strtoul(gidToken.c_str(), nullptr, 10);
764-
*bufferPtr = tileGid;
765-
bufferPtr++;
766-
}
739+
axstd::pod_vector<uint32_t> tileGids;
740+
axstd::split_cb(currentString, '\n', [&tileGids](const char* first, const char* last) {
741+
axstd::split_cb(std::string_view{first, static_cast<size_t>(last - first)}, ',',
742+
[&tileGids](const char* _first, const char* _last) {
743+
unsigned int gid{0};
744+
std::from_chars(_first, _last, gid);
745+
tileGids.push_back(gid);
746+
});
747+
});
767748

768-
layer->_tiles = buffer.release_pointer();
749+
layer->_tiles = tileGids.release_pointer();
769750

770751
tmxMapInfo->setCurrentString("");
771752
}
@@ -884,4 +865,4 @@ TMXTileAnimInfo* TMXTileAnimInfo::create(uint32_t tileID)
884865
return ret;
885866
}
886867

887-
}
868+
} // namespace ax

0 commit comments

Comments
 (0)