docs: @SuppressWarnings("PMD.NcssCount")
    static String escape(String str) {
      StringBuilder builder = new StringBuilder();
      for (int i = 0; i < str.length(); ++i) {
        char c = str.charAt(i);
        switch (c) {
          case '\t': builder.append("\\t"); break;
          case '\n': builder.append("\\n"); break;
          case '\r': builder.append("\\r"); break;
          case '\f': builder.append("\\f"); break;
          case '\\': builder.append("\\\\"); break;
          case '^':  builder.append("\\^"); break;
          case '$':  builder.append("\\$"); break;
          case '.':  builder.append("\\."); break;
          case '?':  builder.append("\\?"); break;
          case '*':  builder.append("\\*"); break;
          case '+':  builder.append("\\+"); break;
          case '[':  builder.append("\\["); break;
          case ']':  builder.append("\\]"); break;
          case '(':  builder.append("\\("); break;
          case ')':  builder.append("\\)"); break;
          case '{':  builder.append("\\{"); break;
          case '}':  builder.append("\\}"); break;
          default:
            if (c <= ' ' || c > '~') {
              builder.append(String.format("\\u%04x", (int) c));
            } else {
              builder.append(c);
            }
            break;
        }
      }
      return builder.toString();
    }