Skip to content

Commit

Permalink
add escape_controls
Browse files Browse the repository at this point in the history
  • Loading branch information
fuzziqersoftware committed Sep 8, 2024
1 parent 7b77cc0 commit 7517358
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/Strings.cc
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,39 @@ string escape_quotes(const string& s) {
return ret;
}

string escape_controls(const string& s, bool escape_non_ascii) {
string ret;
for (size_t x = 0; x < s.size(); x++) {
char ch = s[x];
if (ch == '\"') {
ret += "\\\"";
} else if (ch == '\'') {
ret += "\\\'";
} else if (ch == '\\') {
ret += "\\\\";
} else if (ch == '\t') {
ret += "\\t";
} else if (ch == '\r') {
ret += "\\r";
} else if (ch == '\n') {
ret += "\\n";
} else if (ch == '\f') {
ret += "\\f";
} else if (ch == '\b') {
ret += "\\b";
} else if (ch == '\a') {
ret += "\\a";
} else if (ch == '\v') {
ret += "\\v";
} else if (ch < 0x20 || (ch > 0x7E && escape_non_ascii)) {
ret += string_printf("\\x%02X", static_cast<uint8_t>(ch));
} else {
ret += ch;
}
}
return ret;
}

string escape_url(const string& s, bool escape_slash) {
string ret;
for (char ch : s) {
Expand Down
8 changes: 8 additions & 0 deletions src/Strings.hh
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,16 @@ void strip_multiline_comments(StrT& s, bool allow_unterminated = false) {
}

std::string escape_quotes(const std::string& s);
std::string escape_controls(const std::string& s, bool escape_non_ascii);
std::string escape_url(const std::string& s, bool escape_slash = false);

inline std::string escape_controls_ascii(const std::string& s) {
return escape_controls(s, true);
}
inline std::string escape_controls_utf8(const std::string& s) {
return escape_controls(s, false);
}

std::string string_printf(const char* fmt, ...) ATTR_PRINTF(1, 2);
std::wstring wstring_printf(const wchar_t* fmt, ...);
std::string string_vprintf(const char* fmt, va_list va);
Expand Down

0 comments on commit 7517358

Please sign in to comment.