encode method

  1. @override
PhoneticEncoding? encode(
  1. String input
)
override

Encodes a string using the NYSIIS algorithm as configured. This encoder does not produce any PhoneticEncoding.alternates values.

Returns a PhoneticEncoding for the input String or null if the input is empty (after cleaning up).

Implementation

@override
PhoneticEncoding? encode(String input) {
  // clean up the input and convert to uppercase
  input = PhoneticUtils.clean(input);
  if (input.isEmpty) {
    return null;
  }

  // NOTE: This implementation performs the transcoding "in-place" by using
  // a strategy that would be similar to the pointer based strategy that is
  // inferred in the original algorithm.

  // copy the string so we can get a modifiable array/list of chars
  final chars = List<int>.from(input.codeUnits);

  // transcode first letters
  _transcodeFirstLetters(chars);

  // transcode last letters
  _transcodeLastLetters(chars);

  // encode remaining letters
  final encoding = _encodeLetters(chars);

  // apply the final rules to the encoding
  _applyFinalRules(encoding);

  // truncate the encoding to maxLength if required
  var finalEncoding = String.fromCharCodes(encoding);
  if (maxLength > 0 && finalEncoding.length > maxLength) {
    finalEncoding = finalEncoding.substring(0, maxLength);
  }

  return PhoneticEncoding(finalEncoding);
}