Code with Finding: |
class PeepholeSubstituteAlternateSyntax {
/**
* returns a string node that can safely be rendered inside /brackets/.
*/
private static Node makeForwardSlashBracketSafe(Node n) {
String s = n.getString();
// sb contains everything in s[0:pos]
StringBuilder sb = null;
int pos = 0;
for (int i = 0; i < s.length(); ++i) {
switch (s.charAt(i)) {
case '\\': // skip over the next char after a '\\'.
++i;
break;
case '/': // escape it
if (null == sb) { sb = new StringBuilder(s.length() + 16); }
sb.append(s, pos, i).append('\\');
pos = i;
break;
}
}
// don't discard useful line-number info if there were no changes
if (null == sb) { return n.cloneTree(); }
sb.append(s, pos, s.length());
return Node.newString(sb.toString()).copyInformationFrom(n);
}
}
|