@@ -531,9 +531,12 @@ private void generateVarData(
531
531
indent + " %3$s lengthFieldValue = %4$s(length);\n " +
532
532
indent + " sbePosition(lengthPosition + lengthOfLengthField);\n " +
533
533
indent + " std::memcpy(m_buffer + lengthPosition, &lengthFieldValue, sizeof(%3$s));\n " +
534
- indent + " std::uint64_t pos = sbePosition();\n " +
535
- indent + " sbePosition(pos + length);\n " +
536
- indent + " std::memcpy(m_buffer + pos, src, length);\n " +
534
+ indent + " if (length != %3$s(0))" +
535
+ indent + " {" +
536
+ indent + " std::uint64_t pos = sbePosition();\n " +
537
+ indent + " sbePosition(pos + length);\n " +
538
+ indent + " std::memcpy(m_buffer + pos, src, length);\n " +
539
+ indent + " }" +
537
540
indent + " return *this;\n " +
538
541
indent + " }\n " ,
539
542
propertyName ,
@@ -591,25 +594,31 @@ private void generateVarData(
591
594
new Formatter (sb ).format ("\n " +
592
595
indent + " %1$s &put%2$s(const std::string& str)\n " +
593
596
indent + " {\n " +
594
- indent + " if (str.length() > %6 $d)\n " +
597
+ indent + " if (str.length() > %4 $d)\n " +
595
598
indent + " {\n " +
596
599
indent + " throw std::runtime_error(\" std::string too long for length type [E109]\" );\n " +
597
600
indent + " }\n " +
598
- indent + " std::uint64_t lengthOfLengthField = %3$d;\n " +
599
- indent + " std::uint64_t lengthPosition = sbePosition();\n " +
600
- indent + " %4$s lengthFieldValue = %5$s(static_cast<%4$s>(str.length()));\n " +
601
- indent + " sbePosition(lengthPosition + lengthOfLengthField);\n " +
602
- indent + " std::memcpy(m_buffer + lengthPosition, &lengthFieldValue, sizeof(%4$s));\n " +
603
- indent + " std::uint64_t pos = sbePosition();\n " +
604
- indent + " sbePosition(pos + str.length());\n " +
605
- indent + " std::memcpy(m_buffer + pos, str.c_str(), str.length());\n " +
606
- indent + " return *this;\n " +
601
+ indent + " return put%2$s(str.data(), static_cast<%3$s>(str.length()));" +
607
602
indent + " }\n " ,
608
603
className ,
609
604
propertyName ,
610
- lengthOfLengthField ,
611
605
lengthCppType ,
612
- lengthByteOrderStr ,
606
+ lengthToken .encoding ().applicableMaxValue ().longValue ());
607
+
608
+ new Formatter (sb ).format ("\n " +
609
+ indent + " #if __cplusplus >= 201703L\n " +
610
+ indent + " %1$s &put%2$s(const std::string_view str)\n " +
611
+ indent + " {\n " +
612
+ indent + " if (str.length() > %4$d)\n " +
613
+ indent + " {\n " +
614
+ indent + " throw std::runtime_error(\" std::string too long for length type [E109]\" );\n " +
615
+ indent + " }\n " +
616
+ indent + " return put%2$s(str.data(), static_cast<%3$s>(str.length()));" +
617
+ indent + " }\n " +
618
+ indent + " #endif\n " ,
619
+ className ,
620
+ propertyName ,
621
+ lengthCppType ,
613
622
lengthToken .encoding ().applicableMaxValue ().longValue ());
614
623
615
624
i += token .componentTokenCount ();
0 commit comments