Skip to content

Commit f1a0c21

Browse files
Adding helper function for outputting yaml escaped text
1 parent 24329e4 commit f1a0c21

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

Include/runcpp2/ParseUtil.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ namespace runcpp2
3535

3636
std::string GetValue(ryml::ConstNodeRef node);
3737
std::string GetKey(ryml::ConstNodeRef node);
38+
std::string GetEscapedYAMLString(const std::string& input);
3839
}
3940

4041
#endif

Src/runcpp2/ParseUtil.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,3 +337,24 @@ std::string runcpp2::GetKey(ryml::ConstNodeRef node)
337337
{
338338
return std::string(node.key().str, node.key().len);
339339
}
340+
341+
std::string runcpp2::GetEscapedYAMLString(const std::string& input)
342+
{
343+
std::string output = "\"";
344+
345+
for(char c : input)
346+
{
347+
switch(c)
348+
{
349+
case '\\': output += "\\\\"; break;
350+
case '\"': output += "\\\""; break;
351+
case '\'': output += "\\\'"; break;
352+
case '\n': output += "\\n"; break;
353+
case '\t': output += "\\t"; break;
354+
default: output += c;
355+
}
356+
}
357+
358+
output += "\"";
359+
return output;
360+
}

0 commit comments

Comments
 (0)