Skip to content

Commit d2a418a

Browse files
authored
Merge pull request #758 from denizevrenci/var_len_str
[C++] Var length string simplifications and improvements
2 parents fa95ef1 + b54a9af commit d2a418a

File tree

1 file changed

+24
-15
lines changed

1 file changed

+24
-15
lines changed

sbe-tool/src/main/java/uk/co/real_logic/sbe/generation/cpp/CppGenerator.java

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -531,9 +531,12 @@ private void generateVarData(
531531
indent + " %3$s lengthFieldValue = %4$s(length);\n" +
532532
indent + " sbePosition(lengthPosition + lengthOfLengthField);\n" +
533533
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 + " }" +
537540
indent + " return *this;\n" +
538541
indent + " }\n",
539542
propertyName,
@@ -591,25 +594,31 @@ private void generateVarData(
591594
new Formatter(sb).format("\n" +
592595
indent + " %1$s &put%2$s(const std::string& str)\n" +
593596
indent + " {\n" +
594-
indent + " if (str.length() > %6$d)\n" +
597+
indent + " if (str.length() > %4$d)\n" +
595598
indent + " {\n" +
596599
indent + " throw std::runtime_error(\"std::string too long for length type [E109]\");\n" +
597600
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()));" +
607602
indent + " }\n",
608603
className,
609604
propertyName,
610-
lengthOfLengthField,
611605
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,
613622
lengthToken.encoding().applicableMaxValue().longValue());
614623

615624
i += token.componentTokenCount();

0 commit comments

Comments
 (0)