Skip to content

Commit

Permalink
[sample] NMI string transforming example to deal with '\x##' and '\u#…
Browse files Browse the repository at this point in the history
…###' escaped characters in strings
  • Loading branch information
pajama-coder committed Sep 26, 2023
1 parent 4ec5fd4 commit 780acce
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions samples/nmi/string-transform/string-transform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,12 @@ class StringTransformPipeline : public nmi::PipelineBase {
case 't': c = '\t'; break;
case 'v': c = '\v'; break;
case '0': c = '\0'; break;
case 'x': c = '\x10'; break;
case 'u': c = '\x11'; break;
}
m_current_string += c;
m_has_escaped = false;
} if (c == m_current_quote) {
} else if (c == m_current_quote) {
output(m_current_string);
output(c);
m_current_string.clear();
Expand Down Expand Up @@ -80,7 +82,7 @@ class StringTransformPipeline : public nmi::PipelineBase {
} else if (c == '/') {
if (m_last_char == '/') {
m_state = LINE_COMMENT;
} else if (m_last_char != '_' && !std::isalnum(m_last_char)) {
} else if (m_last_non_space != '_' && !std::isalnum(m_last_non_space)) {
m_state = REGEXP_MAYBE;
}
} else if (c == '*') {
Expand All @@ -89,10 +91,12 @@ class StringTransformPipeline : public nmi::PipelineBase {
}
} else if (m_state == REGEXP_MAYBE) {
m_state = REGEXP;
m_has_escaped = (c == '\\');
} else if (c == '"' || c == '\'') {
m_current_quote = c;
}
m_last_char = c;
if (!std::isspace(c)) m_last_non_space = c;
}
}
}
Expand All @@ -118,6 +122,7 @@ class StringTransformPipeline : public nmi::PipelineBase {
output(c);
} else {
switch (c) {
case '\0': output('\\'); output('0'); break;
case '\\': output('\\'); output('\\'); break;
case '\a': output('\\'); output('a'); break;
case '\b': output('\\'); output('b'); break;
Expand All @@ -126,6 +131,8 @@ class StringTransformPipeline : public nmi::PipelineBase {
case '\r': output('\\'); output('r'); break;
case '\t': output('\\'); output('t'); break;
case '\v': output('\\'); output('v'); break;
case '\x10': output('\\'); output('x'); break;
case '\x11': output('\\'); output('u'); break;
default: output(c); break;
}
}
Expand All @@ -145,6 +152,7 @@ class StringTransformPipeline : public nmi::PipelineBase {
std::string m_current_string;
State m_state = NORMAL;
char m_last_char = 0;
char m_last_non_space = 0;
bool m_has_escaped = false;
};

Expand Down

0 comments on commit 780acce

Please sign in to comment.