Skip to content

Commit

Permalink
[C++] Style tidy up after merge of PR #716.
Browse files Browse the repository at this point in the history
  • Loading branch information
mjpt777 committed Sep 14, 2019
1 parent d5f80a5 commit b8f46e8
Showing 1 changed file with 26 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2283,9 +2283,7 @@ private CharSequence generateDisplay(
final List<Token> varData,
final String indent)
{
final StringBuilder sb = new StringBuilder();

sb.append(String.format("\n" +
return String.format("\n" +
indent + "template<typename CharT, typename Traits>\n" +
indent + "friend std::basic_ostream<CharT, Traits>& operator<<(\n" +
indent + " std::basic_ostream<CharT, Traits>& builder, %1$s _writer)\n" +
Expand All @@ -2302,9 +2300,7 @@ private CharSequence generateDisplay(
indent + " return builder;\n" +
indent + "}\n",
formatClassName(name),
appendDisplay(fields, groups, varData, indent + INDENT)));

return sb.toString();
appendDisplay(fields, groups, varData, indent + INDENT));
}

private CharSequence generateGroupDisplay(
Expand All @@ -2314,8 +2310,7 @@ private CharSequence generateGroupDisplay(
final List<Token> varData,
final String indent)
{
final StringBuilder sb = new StringBuilder();
sb.append(String.format("\n" +
return String.format("\n" +
indent + "template<typename CharT, typename Traits>\n" +
indent + "friend std::basic_ostream<CharT, Traits>& operator<<(\n" +
indent + " std::basic_ostream<CharT, Traits>& builder, %1$s writer)\n" +
Expand All @@ -2326,19 +2321,15 @@ private CharSequence generateGroupDisplay(
indent + " return builder;\n" +
indent + "}\n",
formatClassName(name),
appendDisplay(fields, groups, varData, indent + INDENT)));

return sb.toString();
appendDisplay(fields, groups, varData, indent + INDENT));
}

private CharSequence generateCompositeDisplay(
final String name,
final List<Token> tokens,
final String indent)
{
final StringBuilder sb = new StringBuilder();
final boolean[] atLeastOne = {false};
sb.append(String.format("\n" +
return String.format("\n" +
indent + "template<typename CharT, typename Traits>\n" +
indent + "friend std::basic_ostream<CharT, Traits>& operator<<(\n" +
indent + " std::basic_ostream<CharT, Traits>& builder, %1$s writer)\n" +
Expand All @@ -2349,9 +2340,7 @@ private CharSequence generateCompositeDisplay(
indent + " return builder;\n" +
indent + "}\n\n",
formatClassName(name),
appendDisplay(tokens, new ArrayList<>(), new ArrayList<>(), indent + INDENT)));

return sb.toString();
appendDisplay(tokens, new ArrayList<>(), new ArrayList<>(), indent + INDENT));
}

private CharSequence appendDisplay(
Expand All @@ -2362,6 +2351,7 @@ private CharSequence appendDisplay(
{
final StringBuilder sb = new StringBuilder();
final boolean[] atLeastOne = {false};

for (int i = 0, size = fields.size(); i < size;)
{
final Token fieldToken = fields.get(i);
Expand Down Expand Up @@ -2394,8 +2384,6 @@ private CharSequence appendDisplay(
throw new IllegalStateException("tokens must begin with BEGIN_GROUP: token=" + groupToken);
}

sb.append(indent + "//" + groupToken + "\n");

if (atLeastOne[0])
{
sb.append(
Expand All @@ -2410,7 +2398,9 @@ private CharSequence appendDisplay(
indent + " writer.%2$s().forEach([&](%1$s& %2$s)\n" +
indent + " {\n" +
indent + " if (atLeastOne)\n" +
indent + " {\n" +
indent + " builder << \", \";\n" +
indent + " }\n" +
indent + " atLeastOne = true;\n" +
indent + " builder << %2$s;\n" +
indent + " });\n" +
Expand All @@ -2420,7 +2410,6 @@ private CharSequence appendDisplay(
formatPropertyName(groupToken.name()),
groupToken.name()));


i = findEndSignal(groups, i, Signal.END_GROUP, groupToken.name());
}

Expand All @@ -2432,8 +2421,6 @@ private CharSequence appendDisplay(
throw new IllegalStateException("tokens must begin with BEGIN_VAR_DATA: token=" + varDataToken);
}

sb.append(indent + "//" + varDataToken + "\n");

if (atLeastOne[0])
{
sb.append(
Expand All @@ -2444,15 +2431,12 @@ private CharSequence appendDisplay(
final String characterEncoding = varData.get(i + 3).encoding().characterEncoding();
final String getAsStringFunction = "get" + toUpperFirstChar(varDataToken.name()) + "AsString().c_str()";
sb.append(indent + "builder << R\"(\"" + varDataToken.name() + "\": )\";\n");

if (null == characterEncoding)
{
sb.append(
indent + "builder << '\"' <<\n" +
indent + INDENT + "writer." + getAsStringFunction + " << '\"';\n\n");
/*sb.append(indent + "builder << " + varDataName + "Length() << \" bytes of raw data\";\n");
append(sb, indent,
"parentMessage.limit(parentMessage.limit() + " + varDataName + "HeaderLength() + " +
varDataName + "Length());");*/
}
else
{
Expand All @@ -2463,7 +2447,7 @@ private CharSequence appendDisplay(

i += varDataToken.componentTokenCount();
}
return sb.toString();
return sb;
}

private CharSequence writeTokenDisplay(
Expand All @@ -2475,8 +2459,6 @@ private CharSequence writeTokenDisplay(
final StringBuilder sb = new StringBuilder();
final String fieldName = "writer." + formatPropertyName(fieldTokenName);

sb.append(indent + "//" + typeToken + "\n");

if (typeToken.encodedLength() <= 0 || typeToken.isConstantEncoding())
{
return sb.toString();
Expand Down Expand Up @@ -2520,7 +2502,9 @@ private CharSequence writeTokenDisplay(
indent + " for (size_t i = 0; i < " + fieldName + "Length(); i++)\n" +
indent + " {\n" +
indent + " if (i)\n" +
indent + " {\n" +
indent + " builder << ',';\n" +
indent + " }\n" +
indent + " builder << +" + fieldName + "(i);\n" +
indent + " }\n" +
indent + "}\n" +
Expand All @@ -2533,9 +2517,12 @@ private CharSequence writeTokenDisplay(
if (typeToken.encoding().primitiveType() == PrimitiveType.CHAR)
{
sb.append(
indent + "if (std::isprint(" + fieldName + "())) {\n" +
indent + "if (std::isprint(" + fieldName + "()))\n" +
indent + "{\n" +
indent + " builder << '\"' << (char)" + fieldName + "() << '\"';\n" +
indent + "} else {\n" +
indent + "}\n" +
indent + "else\n" +
indent + "{\n" +
indent + " builder << (int)" + fieldName + "();\n" +
indent + "}\n");
}
Expand All @@ -2549,15 +2536,17 @@ private CharSequence writeTokenDisplay(
case BEGIN_ENUM:
sb.append(indent + "builder << '\"' << " + fieldName + "() << '\"';\n");
break;

case BEGIN_SET:
sb.append(indent + "builder << " + fieldName + "();\n");
break;

case BEGIN_COMPOSITE:
sb.append(indent + "builder << " + fieldName + "();\n");
break;
}

return sb.toString();
return sb;
}

private CharSequence generateChoicesDisplay(final String name, final List<Token> tokens)
Expand All @@ -2584,8 +2573,8 @@ private CharSequence generateChoicesDisplay(final String name, final List<Token>
for (int i = 0, size = choiceTokens.size(); i < size; i++)
{
final Token token = choiceTokens.get(i);
sb.append(indent + " //" + token + "\n");
final String choiceName = "writer." + formatPropertyName(token.name());

sb.append(
indent + " if (" + choiceName + "())\n" +
indent + " {\n");
Expand All @@ -2612,7 +2601,7 @@ private CharSequence generateChoicesDisplay(final String name, final List<Token>
indent + " return builder;\n" +
indent + "}\n");

return sb.toString();
return sb;
}

private CharSequence generateEnumDisplay(final List<Token> tokens, final Token encodingToken)
Expand All @@ -2625,15 +2614,13 @@ private CharSequence generateEnumDisplay(final List<Token> tokens, final Token e
" {\n" +
" switch (value)\n" +
" {\n",
enumName
));
enumName));

for (final Token token : tokens)
{
sb.append(String.format(
" case %1$s: return \"%1$s\";\n",
token.name())
);
token.name()));
}

sb.append(String.format(
Expand All @@ -2648,8 +2635,7 @@ private CharSequence generateEnumDisplay(final List<Token> tokens, final Token e
" {\n" +
" return os << %1$s::c_str(m);\n" +
" }\n",
enumName
));
enumName));

return sb;
}
Expand Down

0 comments on commit b8f46e8

Please sign in to comment.