Skip to content

Commit 66f9df8

Browse files
committed
splitting string for windows
1 parent a812b42 commit 66f9df8

1 file changed

Lines changed: 24 additions & 4 deletions

File tree

tools/jse_embed_spec.cpp

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,12 +148,23 @@ namespace
148148
{
149149
const std::string delimiter = i == 0 ? "JSE_JSON" : "JSE_JSON_" + std::to_string(i);
150150
if (value.find(")" + delimiter + "\"") == std::string::npos)
151-
return "R\"" + delimiter + "(\n" + value + "\n)" + delimiter + "\"";
151+
return "R\"" + delimiter + "(" + value + ")" + delimiter + "\"";
152152
}
153153

154154
throw std::runtime_error("Failed to find a valid raw string delimiter.");
155155
}
156156

157+
std::vector<std::string> split_string(const std::string &value, const std::size_t chunk_size)
158+
{
159+
std::vector<std::string> chunks;
160+
chunks.reserve((value.size() + chunk_size - 1) / chunk_size);
161+
162+
for (std::size_t start = 0; start < value.size(); start += chunk_size)
163+
chunks.push_back(value.substr(start, chunk_size));
164+
165+
return chunks;
166+
}
167+
157168
void ensure_parent_directory(const std::filesystem::path &path)
158169
{
159170
const auto parent = path.parent_path();
@@ -206,17 +217,26 @@ namespace
206217

207218
std::string source_content(const Options &options, const jse::json &rules)
208219
{
220+
constexpr std::size_t max_string_literal_chunk_size = 8000;
221+
209222
const auto namespaces = split_namespace(options.namespace_name);
210223
const auto header_name = options.output_header.filename().string();
211-
const std::string rules_text = rules.dump(4, ' ', true);
224+
const std::string rules_text = "\n" + rules.dump(4, ' ', true) + "\n";
225+
const auto rules_text_chunks = split_string(rules_text, max_string_literal_chunk_size);
212226

213227
std::ostringstream os;
214228
os << "#include \"" << header_name << "\"\n\n";
229+
os << "#include <string>\n\n";
215230
open_namespaces(os, namespaces);
216231
os << "const nlohmann::json &" << options.function_name << "()\n";
217232
os << "{\n";
218-
os << " static const nlohmann::json value = nlohmann::json::parse("
219-
<< raw_string_literal(rules_text) << ");\n";
233+
os << " static const nlohmann::json value = []() {\n";
234+
os << " std::string text;\n";
235+
os << " text.reserve(" << rules_text.size() << ");\n";
236+
for (const auto &chunk : rules_text_chunks)
237+
os << " text += " << raw_string_literal(chunk) << ";\n";
238+
os << " return nlohmann::json::parse(text);\n";
239+
os << " }();\n";
220240
os << " return value;\n";
221241
os << "}\n";
222242
if (!namespaces.empty())

0 commit comments

Comments
 (0)