2929// ==========================================================================================
3030
3131#define STYML_VERSION_MAJOR 1
32- #define STYML_VERSION_MINOR 0
32+ #define STYML_VERSION_MINOR 1
3333#define STYML_VERSION_PATCH 0
3434#define STYML_VERSION (STYML_VERSION_MAJOR * 100 * 100 + STYML_VERSION_MINOR * 100 + STYML_VERSION_PATCH)
3535
@@ -148,7 +148,7 @@ inline void STYML_PRINTF_CHECK(3, 4) throwParsing(int lineNbr, const char* lineS
148148
149149 // Add the line copy
150150 if (lengthToWrite > 0 ) {
151- alreadyWritten += std::min (snprintf (tmpStr + alreadyWritten, lengthToWrite + 1 , " %s" , lineStartPtr),
151+ alreadyWritten += std::min (snprintf (tmpStr + alreadyWritten, ( size_t ) lengthToWrite + 1 , " %s" , lineStartPtr),
152152 lengthToWrite); // +1 for zero termination
153153 if (lengthToWrite == LineCopySize) { alreadyWritten += snprintf (tmpStr + alreadyWritten, 4 , " ..." ); }
154154 snprintf (tmpStr + alreadyWritten, 2 , " \" " );
@@ -216,9 +216,9 @@ struct convert<UnsignedInt, std::enable_if_t<std::is_integral<UnsignedInt>::valu
216216 static std::string encode (const UnsignedInt& typedValue) { return std::to_string (typedValue); }
217217 static void decode (const char * strValue, UnsignedInt& typedValue)
218218 {
219- errno = 0 ;
220- char * endptr = nullptr ;
221- long long number = strtoull (strValue, &endptr, 0 );
219+ errno = 0 ;
220+ char * endptr = nullptr ;
221+ unsigned long long number = strtoull (strValue, &endptr, 0 );
222222 if (endptr == strValue || errno != 0 ) {
223223 throwMessage<ConvertException>(" Convert error: unable to convert the string into an unsigned integer: '%s'" , strValue);
224224 }
@@ -292,22 +292,21 @@ class Element
292292 static constexpr uint32_t TypeShift = 29 ; // Type is on 3 bits
293293 static constexpr uint32_t CompoundMask = (1 << TypeShift) - 1 ; // The 29 remaining bits are for the first data
294294 public:
295- Element (NodeType kind) : d(((uint32_t )kind) << TypeShift), typed{0 , 0 , 0 } {}
295+ Element (NodeType kind) : d(((uint32_t )kind) << TypeShift), typed{{ 0 , 0 , 0 } } {}
296296 Element (NodeType kind, uint32_t stringIdx, uint32_t stringSize)
297- : d((((uint32_t )kind) << TypeShift) | (stringSize & CompoundMask)), typed{stringIdx, 0 , 0 }
297+ : d((((uint32_t )kind) << TypeShift) | (stringSize & CompoundMask)), typed{{ stringIdx, 0 , 0 } }
298298 {
299299 assert (kind == KEY || kind == VALUE || kind == COMMENT);
300300 }
301- Element (NodeType kind, uint32_t stringIdx, uint32_t stringSize, int eltIdx)
302- : d((((uint32_t )kind) << TypeShift) | (stringSize & CompoundMask)), typed{stringIdx, 0 , 0 }
301+ Element (NodeType kind, uint32_t stringIdx, uint32_t stringSize, uint32_t eltIdx)
302+ : d((((uint32_t )kind) << TypeShift) | (stringSize & CompoundMask)), typed{{ stringIdx, 0 , 0 } }
303303 {
304304 assert (kind == KEY);
305305 typed.key .eltIdx = eltIdx;
306306 }
307307 Element (Element&& rhs) noexcept
308308 {
309- // codechecker_suppress [bugprone-undefined-memory-manipulation]
310- memcpy (this , (char *)&rhs, sizeof (Element));
309+ memcpy ((char *)this , (char *)&rhs, sizeof (Element));
311310 memset ((char *)&rhs, 0 , sizeof (Element));
312311 }
313312 Element (const Element& rhs) = delete ;
@@ -636,7 +635,7 @@ class Context
636635
637636 void addString (const char * text, uint32_t textSize, Element* elt)
638637 {
639- uint32_t stringIdx = (int )arena.size ();
638+ uint32_t stringIdx = (uint32_t )arena.size ();
640639 uint32_t stringSize = textSize + 1 ;
641640 arena.resize (arena.size () + stringSize);
642641 memcpy (arena.data () + stringIdx, text, textSize * sizeof (char ));
@@ -648,7 +647,7 @@ class Context
648647
649648 void addToSession (const char * text, uint32_t textSize)
650649 {
651- uint32_t startChunkIdx = (int )arena.size ();
650+ uint32_t startChunkIdx = (uint32_t )arena.size ();
652651 arena.resize (startChunkIdx + textSize);
653652 memcpy (arena.data () + startChunkIdx, text, textSize * sizeof (char ));
654653 }
@@ -661,7 +660,7 @@ class Context
661660 stringSize = (uint32_t )arena.size () - sessionStartIdx;
662661 }
663662
664- const char * getString (int stringIdx) const { return (const char *)(arena.data () + stringIdx); }
663+ const char * getString (uint32_t stringIdx) const { return (const char *)(arena.data () + stringIdx); }
665664
666665 // Accelerated map access
667666 // ======================
@@ -677,7 +676,7 @@ class Context
677676 if (keyHash < FirstValid) keyHash += FirstValid; // Infinitesimal pessimisation of a few first values of hash. Worth it.
678677
679678 uint32_t mask = (_maxEntryQty - 1 ) & (~(KeyDirAssocQty - 1 ));
680- int idx = keyHash & mask;
679+ uint32_t idx = keyHash & mask;
681680 uint32_t probeIncr = 1 ;
682681
683682 while (true ) {
@@ -706,7 +705,7 @@ class Context
706705 if (keyHash < FirstValid) keyHash += FirstValid;
707706
708707 uint32_t mask = (_maxEntryQty - 1 ) & (~(KeyDirAssocQty - 1 ));
709- int idx = keyHash & mask;
708+ uint32_t idx = keyHash & mask;
710709 uint32_t probeIncr = 1 ;
711710 uint32_t cellId = 0 ;
712711
@@ -739,7 +738,7 @@ class Context
739738 if (keyHash < FirstValid) keyHash += FirstValid;
740739
741740 uint32_t mask = (_maxEntryQty - 1 ) & (~(KeyDirAssocQty - 1 ));
742- int idx = keyHash & mask;
741+ uint32_t idx = keyHash & mask;
743742 uint32_t probeIncr = 1 ;
744743
745744 while (true ) {
@@ -749,7 +748,7 @@ class Context
749748 detail::Element* childElt = &elements[parentElt->getSub (_entries[idx + cellId].childIndex )];
750749 if (childElt->getType () == KEY && childElt->getStringSize () == keySize + 1 && // +1 due to zero termination included
751750 strncmp (getString (childElt->getStringIdx ()), key, keySize) == 0 ) {
752- int oldChildIndex = _entries[idx + cellId].childIndex ;
751+ uint32_t oldChildIndex = _entries[idx + cellId].childIndex ;
753752 _entries[idx + cellId] = {Tombstone, UINT_MAX};
754753 return oldChildIndex;
755754 }
@@ -872,7 +871,7 @@ struct StringHelper {
872871 void addChunk (const char * text, uint32_t textSize)
873872 {
874873 assert (textSize < (1U << 31 ));
875- uint32_t startIdx = (int )arena.size ();
874+ uint32_t startIdx = (uint32_t )arena.size ();
876875 arena.resize (startIdx + textSize);
877876 if (textSize > 0 ) { memcpy (arena.data () + startIdx, text, textSize * sizeof (char )); }
878877 }
@@ -882,7 +881,7 @@ struct StringHelper {
882881 // Adjust the size
883882 while (textSize > 0 && (text[textSize - 1 ] == ' ' || text[textSize - 1 ] == ' \t ' )) { --textSize; }
884883
885- uint32_t startIdx = (int )arena.size ();
884+ uint32_t startIdx = (uint32_t )arena.size ();
886885 arena.resize (startIdx + textSize);
887886 if (textSize > 0 ) { memcpy (arena.data () + startIdx, text, textSize * sizeof (char )); }
888887 }
@@ -983,8 +982,9 @@ dumpAsPyStruct(Context* context, bool withIndent)
983982 for (int i = 0 ; i < indent; ++i) sh.addChunk (indentStr, indentSize);
984983 }
985984 sh.addChar (' [' );
986- for (int i = v->getSubQty () - 1 ; i >= 0 ; --i) { // Reverse order
987- stack.emplace_back (&context->elements [v->getSub (i)], indent + 1 , false , !isOneLiner, i == (int )(v->getSubQty () - 1 ));
985+ for (int i = (int )v->getSubQty () - 1 ; i >= 0 ; --i) { // Reverse order
986+ stack.emplace_back (&context->elements [v->getSub ((uint32_t )i)], indent + 1 , false , !isOneLiner,
987+ i == (int )(v->getSubQty () - 1 ));
988988 }
989989 }
990990 }
@@ -1005,8 +1005,9 @@ dumpAsPyStruct(Context* context, bool withIndent)
10051005 for (int i = 0 ; i < indent; ++i) sh.addChunk (indentStr, indentSize);
10061006 }
10071007 sh.addChar (' {' );
1008- for (int i = v->getSubQty () - 1 ; i >= 0 ; --i) { // Reverse order
1009- stack.emplace_back (&context->elements [v->getSub (i)], indent + 1 , false , !isOneLiner, i == (int )(v->getSubQty () - 1 ));
1008+ for (int i = (int )v->getSubQty () - 1 ; i >= 0 ; --i) { // Reverse order
1009+ stack.emplace_back (&context->elements [v->getSub ((uint32_t )i)], indent + 1 , false , !isOneLiner,
1010+ i == (int )(v->getSubQty () - 1 ));
10101011 }
10111012 }
10121013 }
@@ -1073,7 +1074,6 @@ dumpAsPyStruct(Context* context, bool withIndent)
10731074
10741075 if (!sh.arena .empty () && sh.arena .back () == ' ,' ) sh.arena .pop_back ();
10751076
1076- sh.addChar (' \0 ' );
10771077 return std::string (sh.arena .data (), sh.arena .size ());
10781078}
10791079
@@ -1133,8 +1133,8 @@ dumpAsYaml(Context* context)
11331133 sh.addChar (' ' );
11341134 ++indent;
11351135 }
1136- for (int i = v->getSubQty () - 1 ; i >= 0 ; --i) { // Reverse order
1137- stack.emplace_back (&context->elements [v->getSub (i)], indent, SEQUENCE);
1136+ for (int i = ( int ) v->getSubQty () - 1 ; i >= 0 ; --i) { // Reverse order
1137+ stack.emplace_back (&context->elements [v->getSub (( uint32_t ) i)], indent, SEQUENCE);
11381138 }
11391139 isFirst = false ;
11401140 }
@@ -1147,8 +1147,8 @@ dumpAsYaml(Context* context)
11471147 sh.addChar (' ' );
11481148 ++indent;
11491149 }
1150- for (int i = v->getSubQty () - 1 ; i >= 0 ; --i) { // Reverse order
1151- stack.emplace_back (&context->elements [v->getSub (i)], indent, MAP);
1150+ for (int i = ( int ) v->getSubQty () - 1 ; i >= 0 ; --i) { // Reverse order
1151+ stack.emplace_back (&context->elements [v->getSub (( uint32_t ) i)], indent, MAP);
11521152 }
11531153 if (parentType == SEQUENCE) {
11541154 stack.back ().indent -= 1 ;
@@ -1193,7 +1193,7 @@ dumpAsYaml(Context* context)
11931193 bool isSingleQuote = (newLineCount == 0 );
11941194 if (!isPlain && newLineCount > 0 ) {
11951195 // Select between case 3 and case 4 by removing the count of trailing newlines
1196- int lastIdx = textSize - 1 ;
1196+ uint32_t lastIdx = textSize - 1 ;
11971197 while (text[lastIdx] == ' \n ' ) {
11981198 --newLineCount;
11991199 --lastIdx;
@@ -1287,7 +1287,7 @@ dumpAsYaml(Context* context)
12871287 if (v->getType () != KEY) { lastIsKey = false ; }
12881288
12891289 // Comment piggybacking
1290- int nextCommentEltIdx = v->getNextCommentIndex ();
1290+ uint32_t nextCommentEltIdx = v->getNextCommentIndex ();
12911291 while (nextCommentEltIdx) {
12921292 const Element& elt = context->elements [nextCommentEltIdx];
12931293
@@ -1306,7 +1306,6 @@ dumpAsYaml(Context* context)
13061306
13071307 } // End of loop on stack
13081308
1309- sh.addChar (' \0 ' );
13101309 return std::string (sh.arena .data (), sh.arena .size ());
13111310}
13121311
@@ -1323,8 +1322,8 @@ class Node
13231322 // ======================================
13241323
13251324 explicit Node () {}
1326- explicit Node (int eltIdx, detail::Context* context) : _eltIdx(eltIdx), _context(context) {}
1327- explicit Node (int eltIdx, detail::Context* context, std::string nonExistingKey)
1325+ explicit Node (uint32_t eltIdx, detail::Context* context) : _eltIdx(eltIdx), _context(context) {}
1326+ explicit Node (uint32_t eltIdx, detail::Context* context, std::string nonExistingKey)
13281327 : _eltIdx(eltIdx), _context(context), _nonExistingKey(std::move(nonExistingKey))
13291328 {
13301329 }
@@ -1972,8 +1971,8 @@ struct TokenParser {
19721971inline TokenParser
19731972getToken (const char * text, uint32_t endIdx, int parentIndent, Context* context, StringHelper& sh, int & colNbr, int & lineNbr, uint32_t & idx)
19741973{
1975- bool isNewLine = (colNbr == 0 );
1976- int initIdx = idx;
1974+ bool isNewLine = (colNbr == 0 );
1975+ uint32_t initIdx = idx;
19771976
19781977 // Go to first non space
19791978 uint32_t idxFnp = idx;
@@ -2086,7 +2085,7 @@ getToken(const char* text, uint32_t endIdx, int parentIndent, Context* context,
20862085 throwParsing (lineNbr, text + initIdx, " Parse error: using tabulation is not accepted for indentation" );
20872086 }
20882087 // Compute the expected indent (folded or literal strings case only)
2089- int effectiveIndent = (uint32_t )(nonSpaceIdx - idx);
2088+ uint32_t effectiveIndent = (uint32_t )(nonSpaceIdx - idx);
20902089 if (targetIndent < 0 ) {
20912090 // Initial empty line case
20922091 if (text[nonSpaceIdx] == ' \n ' || text[nonSpaceIdx] == ' \r ' ) { // @BUG Need to handle comments too
@@ -2207,7 +2206,9 @@ getToken(const char* text, uint32_t endIdx, int parentIndent, Context* context,
22072206 lineEndIdx = rollbackLineEndIdx;
22082207 } else {
22092208 if (!sh.empty ()) { sh.addChar (' \n ' ); }
2210- if (lineEndIdx >= idx + targetIndent) { sh.addChunk (text + idx + targetIndent, lineEndIdx - (idx + targetIndent)); }
2209+ if (lineEndIdx >= idx + (uint32_t )targetIndent) {
2210+ sh.addChunk (text + idx + targetIndent, lineEndIdx - (idx + (uint32_t )targetIndent));
2211+ }
22112212 }
22122213 }
22132214
@@ -2220,15 +2221,18 @@ getToken(const char* text, uint32_t endIdx, int parentIndent, Context* context,
22202221 lineEndIdx = rollbackLineEndIdx;
22212222 } else {
22222223 // Indentation behavior
2223- bool isIndented = (lineEndIdx <= idx + targetIndent || (!sh.empty () && text[idx + targetIndent] == ' ' ));
2224+ bool isIndented =
2225+ (lineEndIdx <= idx + (uint32_t )targetIndent || (!sh.empty () && text[idx + (uint32_t )targetIndent] == ' ' ));
22242226 if (isIndented || indentedFoldedLine) {
22252227 sh.addChar (' \n ' );
2226- } else if (lineEndIdx > idx + targetIndent && !sh.empty () && sh.arena .back () != ' \n ' ) {
2228+ } else if (lineEndIdx > idx + ( uint32_t ) targetIndent && !sh.empty () && sh.arena .back () != ' \n ' ) {
22272229 sh.addChar (' ' );
22282230 }
22292231
2230- indentedFoldedLine = (lineEndIdx > idx + targetIndent && text[idx + targetIndent] == ' ' );
2231- if (lineEndIdx > idx + targetIndent) { sh.addChunk (text + idx + targetIndent, lineEndIdx - (idx + targetIndent)); }
2232+ indentedFoldedLine = (lineEndIdx > idx + (uint32_t )targetIndent && text[idx + (uint32_t )targetIndent] == ' ' );
2233+ if (lineEndIdx > idx + (uint32_t )targetIndent) {
2234+ sh.addChunk (text + idx + targetIndent, lineEndIdx - (idx + (uint32_t )targetIndent));
2235+ }
22322236 }
22332237 }
22342238
@@ -2338,7 +2342,7 @@ parse(const char* text, uint32_t textSize)
23382342 int mlStringParentIndent = -1 ;
23392343 int indexColNbr = 0 ;
23402344 int tokenLineNbr = 1 ;
2341- int tokenIdx = 0 ;
2345+ uint32_t tokenIdx = 0 ;
23422346
23432347 while (!isEndOfInput && !stack.empty ()) {
23442348#ifdef DEBUG_PARSING
@@ -2359,18 +2363,18 @@ parse(const char* text, uint32_t textSize)
23592363
23602364 switch (token.type ) {
23612365 case TokenType::Comment: {
2362- int eltIdx = (int )elements.size ();
2366+ uint32_t eltIdx = (uint32_t )elements.size ();
23632367 elements.emplace_back (COMMENT, token.stringIdx , token.stringSize );
23642368 if (isStartingWithNewLine) { elements.back ().setStandalone (); }
23652369
2366- int parentCommentEltIdx = parent.eltIdx ;
2370+ uint32_t parentCommentEltIdx = parent.eltIdx ;
23672371 if (elements[parentCommentEltIdx].getType () == UNKNOWN && stack.size () >= 2 ) {
23682372 parentCommentEltIdx =
23692373 stack[stack.size () - 2 ].eltIdx ; // If the last is unknown, then the for-last must be a key or sequence
23702374 }
23712375
23722376 if (elements[parentCommentEltIdx].getType () != UNKNOWN) {
2373- int tmpIdx = 0 ;
2377+ uint32_t tmpIdx = 0 ;
23742378 while ((tmpIdx = elements[parentCommentEltIdx].getNextCommentIndex ()) != 0 ) { parentCommentEltIdx = tmpIdx; }
23752379 elements[parentCommentEltIdx].setComment (eltIdx);
23762380 }
@@ -2421,7 +2425,7 @@ parse(const char* text, uint32_t textSize)
24212425 " Parse error: probably bad indentation with caret, as the parent ('%s') already has a value" ,
24222426 to_string (KEY)); // Reachable state?
24232427 }
2424- int eltIdx = (int )elements.size ();
2428+ uint32_t eltIdx = (uint32_t )elements.size ();
24252429 elements.emplace_back (SEQUENCE);
24262430 stack.emplace_back (eltIdx, colNbr, colNbr);
24272431 elements[parent.eltIdx ].add (eltIdx);
@@ -2431,7 +2435,7 @@ parse(const char* text, uint32_t textSize)
24312435
24322436 // Create the next node, untyped. This is required to handle the case of empty values in sequences.
24332437 assert (elements[parent.eltIdx ].getType () == SEQUENCE);
2434- int eltIdx = (int )elements.size ();
2438+ uint32_t eltIdx = (uint32_t )elements.size ();
24352439 elements.emplace_back (UNKNOWN);
24362440 stack.emplace_back (eltIdx, colNbr, -1 );
24372441 elements[parent.eltIdx ].add (eltIdx);
@@ -2479,7 +2483,7 @@ parse(const char* text, uint32_t textSize)
24792483 to_string (parentElt.getType ()));
24802484 }
24812485
2482- int eltIdx = (int )elements.size ();
2486+ uint32_t eltIdx = (uint32_t )elements.size ();
24832487 elements.emplace_back (MAP);
24842488 stack.emplace_back (eltIdx, parent.indent , -1 );
24852489 elements[parent.eltIdx ].add (eltIdx);
@@ -2489,7 +2493,7 @@ parse(const char* text, uint32_t textSize)
24892493
24902494 // Add key
24912495 if (parent.childIndent < 0 ) { stack.back ().childIndent = colNbr; }
2492- int eltIdx = (int )elements.size ();
2496+ uint32_t eltIdx = (uint32_t )elements.size ();
24932497 elements.emplace_back (KEY, token.stringIdx , token.stringSize );
24942498 stack.emplace_back (eltIdx, colNbr, -1 );
24952499 assert (elements[parent.eltIdx ].getType () != KEY || elements[parent.eltIdx ].getSubQty () == 0 );
@@ -2504,7 +2508,7 @@ parse(const char* text, uint32_t textSize)
25042508
25052509 // Create the next node, untyped. This is required to handle the case of empty values in sequences.
25062510 assert (elements[parent.eltIdx ].getType () == KEY);
2507- eltIdx = (int )elements.size ();
2511+ eltIdx = (uint32_t )elements.size ();
25082512 elements.emplace_back (UNKNOWN);
25092513 stack.emplace_back (eltIdx, colNbr, -1 );
25102514 elements[parent.eltIdx ].add (eltIdx);
0 commit comments