Skip to content

Commit

Permalink
implement unescape using O(n)
Browse files Browse the repository at this point in the history
  • Loading branch information
wgtmac committed Jan 7, 2025
1 parent 6b6c0b5 commit 35a9b5a
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions lang/c++/impl/Compiler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,20 @@ int64_t getLongField(const Entity &e, const Object &m,
// Unescape double quotes (") for de-serialization. This method complements the
// method NodeImpl::escape() which is used for serialization.
static void unescape(string &s) {
std::string::size_type pos = 0;
while ((pos = s.find("\\\"", pos)) != std::string::npos) {
s.replace(pos, 2, "\"");
pos += 1;
size_t writePos = 0, readPos = 0;
while (readPos < s.length()) {
if (readPos + 1 < s.length() && s[readPos] == '\\' && s[readPos + 1] == '\"') {
s[writePos++] = '\"';
readPos += 2;
} else if (writePos != readPos) {
s[writePos++] = s[readPos++];
} else {
writePos++;
readPos++;
}
}
if (writePos != s.length()) {
s.resize(writePos);
}
}

Expand Down

0 comments on commit 35a9b5a

Please sign in to comment.