Skip to content

Commit 88a6f79

Browse files
committed
[C++] Trim strings for fixed length arrays.
1 parent 8ae4895 commit 88a6f79

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1361,7 +1361,10 @@ private CharSequence generateArrayProperty(
13611361
sb.append(String.format("\n" +
13621362
indent + " std::string get%1$sAsString() const\n" +
13631363
indent + " {\n" +
1364-
indent + " std::string result(m_buffer + m_offset + %2$d, %3$d);\n" +
1364+
indent + " const char *buffer = m_buffer + m_offset + %2$d;\n" +
1365+
indent + " size_t length = 0;\n\n" +
1366+
indent + " for (; length < %3$d && *(buffer + length) != '\\0'; ++length);\n" +
1367+
indent + " std::string result(buffer, length);\n\n" +
13651368
indent + " return result;\n" +
13661369
indent + " }\n",
13671370
toUpperFirstChar(propertyName),
@@ -1372,7 +1375,10 @@ private CharSequence generateArrayProperty(
13721375
indent + " #if __cplusplus >= 201703L\n" +
13731376
indent + " std::string_view get%1$sAsStringView() const SBE_NOEXCEPT\n" +
13741377
indent + " {\n" +
1375-
indent + " std::string_view result(m_buffer + m_offset + %2$d, %3$d);\n" +
1378+
indent + " const char *buffer = m_buffer + m_offset + %2$d;\n" +
1379+
indent + " size_t length = 0;\n\n" +
1380+
indent + " for (; length < %3$d && *(buffer + length) != '\\0'; ++length);\n" +
1381+
indent + " std::string_view result(buffer, length);\n\n" +
13761382
indent + " return result;\n" +
13771383
indent + " }\n" +
13781384
indent + " #endif\n",

0 commit comments

Comments
 (0)