encode method

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

Encodes a string using the Refined Soundex algorithm as configured.

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, allowLatin: false);
  if (input.isEmpty) {
    return null;
  }

  // we'll write to a buffer to avoid string copies
  final soundex = StringBuffer();

  // always write first character
  soundex.writeCharCode(input.codeUnitAt(0));

  int? last, current;
  last = $asterisk;

  // encode all characters
  for (var charCode in input.codeUnits) {
    current = soundexMapping[charCode];
    if (current == last || current == null) {
      continue;
    } else {
      soundex.writeCharCode(current);
    }

    if (maxLength > 0 && soundex.length >= maxLength) {
      break;
    }

    last = current;
  }

  return PhoneticEncoding(soundex.toString());
}