Remix.run Logo
scottlamb 3 days ago

> The sane thing would be to loop through building the output string, adding the replacement for each symbol as you go.

As follows:

    // SYMBOL_REF should be a class-level static final to avoid recompiling on each call.

    int pos = 0; // input[..pos] has been processed.
    StringBuilder out = new StringBuilder(); // could also guess at length here.
    Matcher m = SYMBOL_REF.matcher(input);
    while (m.find()) {
      String replacement = symbols.get(m.group(1));
      if (replacement == null) {
        continue; // no such symbol; keep literal `$foo`.
      }
      out.append(input, pos, m.start());
      out.append(replacement);
      pos = m.end();
    }
    out.append(input, pos, input.length());
(Apparently there's also now a Matcher.replaceAll one could use, but it's arguably cheating to outsource the loop to a method that probably didn't exist when the Uncle Bob version was written, and it's slightly less efficient in the "no such symbol" case.)

Coding style must serve the purpose of aiding understanding. If you have strong opinions about the coding style of `replace` but those opinions don't lead to recognition that the implementation was incorrect and inefficient, your opinions are bad and you should feel bad. Stop writing garbage books and blog posts!

</rant>