@@ -32,13 +32,13 @@ THE SOFTWARE.
32
32
#include < unordered_map>
33
33
#include < sstream>
34
34
#include < regex>
35
- // #include "2d/TMXTiledMap.h"
35
+
36
36
#include " base/ZipUtils.h"
37
37
#include " base/Director.h"
38
38
#include " base/Utils.h"
39
39
#include " platform/FileUtils.h"
40
-
41
- // using namespace std;
40
+ # include < ranges >
41
+ # include < charconv >
42
42
43
43
namespace ax
44
44
{
@@ -452,10 +452,9 @@ void TMXMapInfo::startElement(void* /*ctx*/, const char* name, const char** atts
452
452
453
453
TMXLayerInfo* layer = tmxMapInfo->getLayers ().back ();
454
454
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 );
456
456
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 ();
459
458
}
460
459
else if (encoding == " base64" )
461
460
{
@@ -534,7 +533,7 @@ void TMXMapInfo::startElement(void* /*ctx*/, const char* name, const char** atts
534
533
if (tmxMapInfo->getParentElement () == TMXPropertyNone)
535
534
{
536
535
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 ());
538
537
tmxMapInfo->setStoringCharacters (false );
539
538
}
540
539
else if (tmxMapInfo->getParentElement () == TMXPropertyMap)
@@ -596,36 +595,36 @@ void TMXMapInfo::startElement(void* /*ctx*/, const char* name, const char** atts
596
595
ValueVector pointsArray;
597
596
pointsArray.reserve (10 );
598
597
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 , ' ' ))
603
602
{
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 ;
608
605
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, ' ,' ))
619
607
{
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 ;
622
622
}
623
-
624
623
// add to points array
625
- pointsArray.emplace_back (Value (pointDict));
624
+ pointsArray.emplace_back (Value (std::move ( pointDict) ));
626
625
}
627
626
628
- dict[" points" ] = Value (pointsArray);
627
+ dict[" points" ] = Value (std::move ( pointsArray) );
629
628
}
630
629
}
631
630
else if (elementName == " polyline" )
@@ -641,36 +640,32 @@ void TMXMapInfo::startElement(void* /*ctx*/, const char* name, const char** atts
641
640
ValueVector pointsArray;
642
641
pointsArray.reserve (10 );
643
642
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, ' ' ))
648
646
{
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 ;
653
648
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, ' ,' ))
664
650
{
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 ;
667
664
}
668
-
669
665
// add to points array
670
- pointsArray.emplace_back (Value (pointDict));
666
+ pointsArray.emplace_back (Value (std::move ( pointDict) ));
671
667
}
672
-
673
- dict[" polylinePoints" ] = Value (pointsArray);
668
+ dict[" polylinePoints" ] = Value (std::move (pointsArray));
674
669
}
675
670
}
676
671
else if (elementName == " animation" )
@@ -701,7 +696,7 @@ void TMXMapInfo::endElement(void* /*ctx*/, const char* name)
701
696
tmxMapInfo->setStoringCharacters (false );
702
697
703
698
TMXLayerInfo* layer = tmxMapInfo->getLayers ().back ();
704
- auto currentString = tmxMapInfo->getCurrentString ();
699
+ auto currentString = tmxMapInfo->getCurrentString ();
705
700
706
701
auto buffer = utils::base64Decode (currentString);
707
702
if (buffer.empty ())
@@ -712,7 +707,7 @@ void TMXMapInfo::endElement(void* /*ctx*/, const char* name)
712
707
713
708
if (tmxMapInfo->getLayerAttribs () & (TMXLayerAttribGzip | TMXLayerAttribZlib))
714
709
{
715
- Vec2 s = layer->_layerSize ;
710
+ Vec2 s = layer->_layerSize ;
716
711
// int sizeHint = s.width * s.height * sizeof(uint32_t);
717
712
ssize_t sizeHint = s.width * s.height * sizeof (unsigned int );
718
713
@@ -741,31 +736,17 @@ void TMXMapInfo::endElement(void* /*ctx*/, const char* name)
741
736
tmxMapInfo->setStoringCharacters (false );
742
737
auto currentString = tmxMapInfo->getCurrentString ();
743
738
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
+ });
767
748
768
- layer->_tiles = buffer .release_pointer ();
749
+ layer->_tiles = tileGids .release_pointer ();
769
750
770
751
tmxMapInfo->setCurrentString (" " );
771
752
}
@@ -884,4 +865,4 @@ TMXTileAnimInfo* TMXTileAnimInfo::create(uint32_t tileID)
884
865
return ret;
885
866
}
886
867
887
- }
868
+ } // namespace ax
0 commit comments