stringToIdentity function

Identity stringToIdentity(
  1. String s
)

Implementation

Identity stringToIdentity(String s) {
  String name = '', category = '';

  // Find unescaped separator; note that the string may contain an escaped
  // backslash before the separator.
  int slash = -1;
  int pos = 0;
  while ((pos = s.indexOf('/', pos)) != -1) {
    int escapes = 0;

    while ((pos - escapes > 0) && s[pos - escapes - 1] == '\\') {
      escapes++;
    }

    // We ignore escaped escapes
    if (escapes.isEven) {
      if (slash == -1) {
        slash = pos;
      } else {
        throw IdentityParseException("unescaped '/' in identity `$s'");
      }
    }

    pos++;
  }

  if (slash == -1) {
    name = s.unescape(start: 0, special: '/');
  } else {
    category = s.unescape(start: 0, end: slash, special: '/');

    if (slash + 1 < s.length) {
      name = s.unescape(start: slash + 1, special: '/');
    }
  }

  return Identity(name: name, category: category);
}