src/developer/debug/zxdb/console/format_name.cc
// Escapes the given identifier component. This does not put the "$(...) around it, but handles the
// stuff in the middle. There are two things that need to happen: handling parens and escaping
// backslashes.
//
// If parens are balanced inside the component (there is a closing paren for each opening one), they
// do not need escaping. In most cases, these will be balanced because it's some kind of textual
// representation for a function call or something. The parser can handle this and it's much more
// readable.
//
// The algorithm is to assume the parens are balanced and just escape backslashes. If it finds the
// parens are unbalanced, it recursively calls itself with paren escaping forced on. We don't try
// to be intelligent about only escaping the unbalanced parens, they all get escaped in this mode.
Escapes the given identifier component. This does not put the "$(...) around it, but handles the
stuff in the middle. There are two things that need to happen: handling parens and escaping
backslashes.

If parens are balanced inside the component (there is a closing paren for each opening one), they
do not need escaping. In most cases, these will be balanced because it's some kind of textual
representation for a function call or something. The parser can handle this and it's much more
readable.

The algorithm is to assume the parens are balanced and just escape backslashes. If it finds the
parens are unbalanced, it recursively calls itself with paren escaping forced on. We don't try
to be intelligent about only escaping the unbalanced parens, they all get escaped in this mode.
Escapes the given identifier component . This does not put the " $ ( ... ) around it but handles the stuff in the middle . There are two things that need to happen : handling parens and escaping backslashes . If parens are balanced inside the component ( there is a closing paren for each opening one ) they do not need escaping . In most cases these will be balanced because it ' s some kind of textual representation for a function call or something . The parser can handle this and it ' s much more readable . The algorithm is to assume the parens are balanced and just escape backslashes . If it finds the parens are unbalanced it recursively calls itself with paren escaping forced on . We don ' t try to be intelligent about only escaping the unbalanced parens they all get escaped in this mode .
std::string EscapeComponent(const std::string& name, bool force_escape_parens = false) {
  std::string output;
  int paren_nesting = 0;
  for (char c : name) {
    if (c == '(') {
      if (force_escape_parens) {
        output.push_back('\\');
        output.push_back('(');
      } else {
        paren_nesting++;
        output.push_back('(');
      }
    } else if (c == ')') {
      if (force_escape_parens) {
        output.push_back('\\');
        output.push_back(')');
      } else if (paren_nesting == 0) {
        return EscapeComponent(name, true);
      } else {
        paren_nesting--;
        output.push_back(')');
      }
    } else if (c == '$' || c == '\\') {
      output.push_back('\\');
      output.push_back(c);
    } else {
      output.push_back(c);
    }
  }
  if (paren_nesting > 0) {
    return EscapeComponent(name, true);
  }
  return output;
}
std :: string EscapeComponent ( const std :: string & name , bool force_escape_parens = false ) { std :: string output ; int paren_nesting = 0 ; for ( char c : name ) { if ( c == ' ' ) { if ( force_escape_parens ) { output . push_back ( ' \\ ' ) ; output . push_back ( ' ' ) ; } else { paren_nesting ++ ; output . push_back ( ' ' ) ; } } else if ( c == ' ' ) { if ( force_escape_parens ) { output . push_back ( ' \\ ' ) ; output . push_back ( ' ' ) ; } else if ( paren_nesting == 0 ) { return EscapeComponent ( name , true ) ; } else { paren_nesting -- ; output . push_back ( ' ' ) ; } } else if ( c == ' ' || c == ' \\ ' ) { output . push_back ( ' \\ ' ) ; output . push_back ( c ) ; } else { output . push_back ( c ) ; } } if ( paren_nesting > 0 ) { return EscapeComponent ( name , true ) ; } return output ; }
Escapes the given identifier component.
Escapes the given identifier component .
// Lone ')' without an opening paren. // Found closing paren for a previous opening one. // Always escape these. // Everything else is a literal. // There was an opening paren with no closing paren.