stripAcceleratorMarkers static method

String stripAcceleratorMarkers(
  1. String label,
  2. {void setIndex(
    1. int index
    )?}
)

Strips out any accelerator markers from the given label, and unescapes any escaped ampersands.

If setIndex is supplied, it will be called before this function returns with the index in the returned string of the accelerator character.

To indicate which letters in the label are to be used as accelerators, add an "&" character before the character in the string. If more than one character has an "&" in front of it, then the characters appearing earlier in the string are preferred. To represent a literal "&", insert "&&" into the string. All other ampersands will be removed from the string before calling MenuAcceleratorLabel.builder. Bare ampersands at the end of the string or before whitespace are stripped and ignored.

Implementation

static String stripAcceleratorMarkers(String label, {void Function(int index)? setIndex}) {
  int quotedAmpersands = 0;
  final StringBuffer displayLabel = StringBuffer();
  int acceleratorIndex = -1;
  // Use characters so that we don't split up surrogate pairs and interpret
  // them incorrectly.
  final Characters labelChars = label.characters;
  final Characters ampersand = '&'.characters;
  bool lastWasAmpersand = false;
  for (int i = 0; i < labelChars.length; i += 1) {
    // Stop looking one before the end, since a single ampersand at the end is
    // just treated as a quoted ampersand.
    final Characters character = labelChars.characterAt(i);
    if (lastWasAmpersand) {
      lastWasAmpersand = false;
      displayLabel.write(character);
      continue;
    }
    if (character != ampersand) {
      displayLabel.write(character);
      continue;
    }
    if (i == labelChars.length - 1) {
      // Strip bare ampersands at the end of a string.
      break;
    }
    lastWasAmpersand = true;
    final Characters acceleratorCharacter = labelChars.characterAt(i + 1);
    if (acceleratorIndex == -1 && acceleratorCharacter != ampersand &&
        acceleratorCharacter.toString().trim().isNotEmpty) {
      // Don't set the accelerator index if the character is an ampersand,
      // or whitespace.
      acceleratorIndex = i - quotedAmpersands;
    }
    // As we encounter '&<character>' pairs, the following indices must be
    // adjusted so that they correspond with indices in the stripped string.
    quotedAmpersands += 1;
  }
  setIndex?.call(acceleratorIndex);
  return displayLabel.toString();
}