Skip to content

Commit

Permalink
fix(toolpp): stop hardcoding string length in bufferstream write call…
Browse files Browse the repository at this point in the history
…s everywhere
  • Loading branch information
craftablescience committed Oct 23, 2024
1 parent f4eb553 commit 98a047f
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 49 deletions.
2 changes: 1 addition & 1 deletion ext/bufferstream
97 changes: 49 additions & 48 deletions src/toolpp/FGD.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <sourcepp/String.h>

using namespace sourcepp;
using namespace std::string_view_literals;
using namespace toolpp;

namespace {
Expand Down Expand Up @@ -467,11 +468,11 @@ void writeOptionalKeyValueStrings(BufferStream& writer, std::initializer_list<st
static constexpr auto writeOptionalString = [](BufferStream& stream, const std::string& str) {
if (!str.empty()) {
stream
.write(" : \"", 4)
.write(" : \""sv, false)
.write(str, false)
.write('\"');
} else {
stream.write(" :", 2);
stream.write(" :"sv, false);
}
};
for (auto revString = std::rbegin(strings); revString != std::rend(strings); ++revString) {
Expand Down Expand Up @@ -605,66 +606,66 @@ FGDWriter FGDWriter::begin() {

FGDWriter& FGDWriter::include(const std::string& fgdPath) {
this->writer
.write("@include \"", 10)
.write("@include \""sv, false)
.write(fgdPath, false)
.write("\"\n\n", 3);
.write("\"\n\n"sv, false);
return *this;
}

FGDWriter& FGDWriter::version(int version) {
this->writer
.write("@version(", 9)
.write("@version("sv, false)
.write(std::to_string(version), false)
.write(")\n\n", 3);
.write(")\n\n"sv, false);
return *this;
}

FGDWriter& FGDWriter::mapSize(math::Vec2i mapSize) {
this->writer
.write("@mapsize(", 9)
.write("@mapsize("sv, false)
.write(std::to_string(mapSize[0]), false)
.write(", ", 2)
.write(", "sv, false)
.write(std::to_string(mapSize[1]), false)
.write(")\n\n", 3);
.write(")\n\n"sv, false);
return *this;
}

FGDWriter& FGDWriter::materialExclusionDirs(const std::vector<std::string>& dirs) {
this->writer.write("@MaterialExclusion\n[\n", 21);
this->writer.write("@MaterialExclusion\n[\n"sv, false);
for (const auto& dir : dirs) {
this->writer << '\t' << '\"';
this->writer.write(dir, false);
this->writer << '\"' << '\n';
}
this->writer.write("]\n\n", 3);
this->writer.write("]\n\n"sv, false);
return *this;
}

FGDWriter::AutoVisGroupWriter FGDWriter::beginAutoVisGroup(const std::string& parentName) {
this->writer
.write("@AutoVisGroup = \"", 17)
.write("@AutoVisGroup = \""sv, false)
.write(parentName, false)
.write("\"\n[\n", 4);
.write("\"\n[\n"sv, false);
return AutoVisGroupWriter{*this};
}

FGDWriter::AutoVisGroupWriter& FGDWriter::AutoVisGroupWriter::visGroup(const std::string& name, const std::vector<std::string>& entities) {
this->parent.writer
.write("\t\"", 2)
.write("\t\""sv, false)
.write(name, false)
.write("\"\n\t[\n", 5);
.write("\"\n\t[\n"sv, false);
for (const auto& entity : entities) {
this->parent.writer
.write("\t\t\"", 3)
.write("\t\t\""sv, false)
.write(entity, false)
.write("\"\n", 2);
.write("\"\n"sv, false);
}
this->parent.writer.write("\t]\n", 3);
this->parent.writer.write("\t]\n"sv, false);
return *this;
}

FGDWriter& FGDWriter::AutoVisGroupWriter::endAutoVisGroup() const {
this->parent.writer.write("]\n\n", 3);
this->parent.writer.write("]\n\n"sv, false);
return this->parent;
}

Expand All @@ -684,20 +685,20 @@ FGDWriter::EntityWriter FGDWriter::beginEntity(const std::string& classType, con
}
}
this->writer
.write("= ", 2)
.write("= "sv, false)
.write(name, false)
.write(" :", 2);
.write(" :"sv, false);
// Put the description on the same line if it's short
if (description.size() < 32) {
this->writer
.write(" \"", 2)
.write(" \""sv, false)
.write(description, false);
} else {
this->writer
.write("\n\t\"", 3)
.write("\n\t\""sv, false)
.write(description, false);
}
this->writer.write("\"\n[\n", 4);
this->writer.write("\"\n[\n"sv, false);
return EntityWriter{*this};
}

Expand All @@ -709,10 +710,10 @@ FGDWriter::EntityWriter& FGDWriter::EntityWriter::keyValue(const std::string& na
.write(valueType, false)
.write(')');
if (readOnly) {
this->parent.writer.write(" readonly", 9);
this->parent.writer.write(" readonly"sv, false);
}
if (report) {
this->parent.writer.write(" report", 7);
this->parent.writer.write(" report"sv, false);
}
::writeOptionalKeyValueStrings(this->parent.writer, {displayName, valueDefault, description});
this->parent.writer << '\n';
Expand All @@ -723,60 +724,60 @@ FGDWriter::EntityWriter::KeyValueChoicesWriter FGDWriter::EntityWriter::beginKey
this->parent.writer
.write('\t')
.write(name, false)
.write("(choices)", 9);
.write("(choices)"sv, false);
if (readOnly) {
this->parent.writer.write(" readonly", 9);
this->parent.writer.write(" readonly"sv, false);
}
if (report) {
this->parent.writer.write(" report", 7);
this->parent.writer.write(" report"sv, false);
}
::writeOptionalKeyValueStrings(this->parent.writer, {displayName, valueDefault, description});
this->parent.writer.write(" =\n\t[\n", 6);
this->parent.writer.write(" =\n\t[\n"sv, false);
return KeyValueChoicesWriter{*this};
}

FGDWriter::EntityWriter::KeyValueChoicesWriter& FGDWriter::EntityWriter::KeyValueChoicesWriter::choice(const std::string& value, const std::string& displayName) {
this->parent.parent.writer
.write("\t\t\"", 3)
.write("\t\t\""sv, false)
.write(value, false)
.write("\" : \"", 5)
.write("\" : \""sv, false)
.write(displayName, false)
.write("\"\n", 2);
.write("\"\n"sv, false);
return *this;
}

FGDWriter::EntityWriter& FGDWriter::EntityWriter::KeyValueChoicesWriter::endKeyValueChoices() const {
this->parent.parent.writer.write("\t]\n", 3);
this->parent.parent.writer.write("\t]\n"sv, false);
return this->parent;
}

FGDWriter::EntityWriter::KeyValueFlagsWriter FGDWriter::EntityWriter::beginKeyValueFlags(const std::string& name, const std::string& displayName, const std::string& description, bool readOnly, bool report) {
this->parent.writer
.write('\t')
.write(name, false)
.write("(flags)", 7);
.write("(flags)"sv, false);
if (readOnly) {
this->parent.writer.write(" readonly", 9);
this->parent.writer.write(" readonly"sv, false);
}
if (report) {
this->parent.writer.write(" report", 7);
this->parent.writer.write(" report"sv, false);
}
::writeOptionalKeyValueStrings(this->parent.writer, {displayName, description});
this->parent.writer.write(" =\n\t[\n", 6);
this->parent.writer.write(" =\n\t[\n"sv, false);
return KeyValueFlagsWriter{*this};
}

FGDWriter::EntityWriter::KeyValueFlagsWriter& FGDWriter::EntityWriter::KeyValueFlagsWriter::flag(uint64_t value, const std::string& displayName, bool enabledByDefault, const std::string& description) {
this->parent.parent.writer
.write("\t\t", 2)
.write("\t\t"sv, false)
.write(std::to_string(value), false)
.write(" : \"", 4)
.write(" : \""sv, false)
.write(displayName, false)
.write("\" : ", 4)
.write("\" : "sv, false)
.write(std::to_string(enabledByDefault), false);
if (!description.empty()) {
this->parent.parent.writer
.write(" : \"", 4)
.write(" : \""sv, false)
.write(description, false)
.write('\"');
}
Expand All @@ -785,21 +786,21 @@ FGDWriter::EntityWriter::KeyValueFlagsWriter& FGDWriter::EntityWriter::KeyValueF
}

FGDWriter::EntityWriter& FGDWriter::EntityWriter::KeyValueFlagsWriter::endKeyValueFlags() const {
this->parent.parent.writer.write("\t]\n", 3);
this->parent.parent.writer.write("\t]\n"sv, false);
return this->parent;
}

FGDWriter::EntityWriter& FGDWriter::EntityWriter::input(const std::string& name, const std::string& valueType, const std::string& description) {
this->parent.writer
.write('\t')
.write("input ", 6)
.write("input "sv, false)
.write(name, false)
.write('(')
.write(valueType, false)
.write(')');
if (!description.empty()) {
this->parent.writer
.write(" : \"", 4)
.write(" : \""sv, false)
.write(description, false)
.write('\"');
}
Expand All @@ -810,14 +811,14 @@ FGDWriter::EntityWriter& FGDWriter::EntityWriter::input(const std::string& name,
FGDWriter::EntityWriter& FGDWriter::EntityWriter::output(const std::string& name, const std::string& valueType, const std::string& description) {
this->parent.writer
.write('\t')
.write("output ", 7)
.write("output "sv, false)
.write(name, false)
.write('(')
.write(valueType, false)
.write(')');
if (!description.empty()) {
this->parent.writer
.write(" : \"", 4)
.write(" : \""sv, false)
.write(description, false)
.write('\"');
}
Expand All @@ -826,7 +827,7 @@ FGDWriter::EntityWriter& FGDWriter::EntityWriter::output(const std::string& name
}

FGDWriter& FGDWriter::EntityWriter::endEntity() const {
this->parent.writer.write("]\n\n", 3);
this->parent.writer.write("]\n\n"sv, false);
return this->parent;
}

Expand Down

0 comments on commit 98a047f

Please sign in to comment.