findByName static method

Emoji? findByName(
  1. String name, {
  2. EmojiStatus? status,
})

Searches for emoji by name.

The search is performed using the binary search algorithm.

If a status value is specified, an emoji with the specified status is searched for.

If no status value is specified, the emoji with the best status is searched for.

In either case, if the name lookup succeeds and the status lookup fails, the best value found is returned.

Since searching is a resource-intensive operation, it is recommended to call this method only to initialize static variables.

Implementation

static Emoji? findByName(String name, {EmojiStatus? status}) {
  var left = 0;
  var right = _data.length;
  int middle;
  while (left < right) {
    middle = (left + right) >> 1;
    final element = _data[middle];
    final result = name.compareTo(element.name);
    if (result > 0) {
      left = middle + 1;
    } else if (result < 0) {
      right = middle;
    } else if (result == 0) {
      if (element.status == status) {
        return element;
      }

      final elements = [element];
      for (var i = middle + 1; i < right; i++) {
        final value = _data[i];
        if (value.name != name) {
          break;
        }

        if (value.status == status) {
          return value;
        }

        elements.add(value);
      }

      for (var i = middle - 1; i >= 0; i--) {
        final value = _data[i];
        if (value.name != name) {
          break;
        }

        if (value.status == status) {
          return value;
        }

        elements.add(value);
      }

      elements.sort((a, b) {
        int getRank(EmojiStatus s) {
          switch (s) {
            case EmojiStatus.component:
              return 3;
            case EmojiStatus.fullyQualified:
              return 0;
            case EmojiStatus.minimallyQualified:
              return 1;
            case EmojiStatus.unqualified:
              return 2;
          }
        }

        return getRank(a.status).compareTo(getRank(b.status));
      });

      return elements.first;
    }
  }

  return null;
}