Dart API Referencedart:utfUtf16CodeUnitDecoder

Utf16CodeUnitDecoder class

An Iterator<int> of codepoints built on an Iterator of UTF-16 code units. The parameters can override the default Unicode replacement character. Set the replacementCharacter to null to throw an ArgumentError rather than replace the bad value.

class Utf16CodeUnitDecoder implements Iterator<int> {
  final _ListRangeIterator utf16CodeUnitIterator;
  final int replacementCodepoint;

  Utf16CodeUnitDecoder(List<int> utf16CodeUnits, [int offset = 0, int length,
      int this.replacementCodepoint =
      UNICODE_REPLACEMENT_CHARACTER_CODEPOINT]) :
      utf16CodeUnitIterator = (new _ListRange(utf16CodeUnits, offset, length))
          .iterator();

  Utf16CodeUnitDecoder.fromListRangeIterator(
      _ListRangeIterator this.utf16CodeUnitIterator,
      int this.replacementCodepoint);

  Iterator<int> iterator() => this;

  bool hasNext() => utf16CodeUnitIterator.hasNext();

  int next() {
    int value = utf16CodeUnitIterator.next();
    if (value < 0) {
      if (replacementCodepoint != null) {
        return replacementCodepoint;
      } else {
        throw new ArgumentError(
            "Invalid UTF16 at ${utf16CodeUnitIterator.position}");
      }
    } else if (value < UNICODE_UTF16_RESERVED_LO ||
        (value > UNICODE_UTF16_RESERVED_HI && value <= UNICODE_PLANE_ONE_MAX)) {
      // transfer directly
      return value;
    } else if (value < UNICODE_UTF16_SURROGATE_UNIT_1_BASE &&
        utf16CodeUnitIterator.hasNext()) {
      // merge surrogate pair
      int nextValue = utf16CodeUnitIterator.next();
      if (nextValue >= UNICODE_UTF16_SURROGATE_UNIT_1_BASE &&
          nextValue <= UNICODE_UTF16_RESERVED_HI) {
        value = (value - UNICODE_UTF16_SURROGATE_UNIT_0_BASE) << 10;
        value += UNICODE_UTF16_OFFSET +
            (nextValue - UNICODE_UTF16_SURROGATE_UNIT_1_BASE);
        return value;
      } else {
        if (nextValue >= UNICODE_UTF16_SURROGATE_UNIT_0_BASE &&
           nextValue < UNICODE_UTF16_SURROGATE_UNIT_1_BASE) {
          utf16CodeUnitIterator.backup();
        }
        if (replacementCodepoint != null) {
          return replacementCodepoint;
        } else {
          throw new ArgumentError(
              "Invalid UTF16 at ${utf16CodeUnitIterator.position}");
        }
      }
    } else if (replacementCodepoint != null) {
      return replacementCodepoint;
    } else {
      throw new ArgumentError(
          "Invalid UTF16 at ${utf16CodeUnitIterator.position}");
    }
  }
}

Implements

Iterator<E>

Constructors

new Utf16CodeUnitDecoder(List<int> utf16CodeUnits, [int offset = 0, int length, int replacementCodepoint = UNICODE_REPLACEMENT_CHARACTER_CODEPOINT]) #

Utf16CodeUnitDecoder(List<int> utf16CodeUnits, [int offset = 0, int length,
    int this.replacementCodepoint =
    UNICODE_REPLACEMENT_CHARACTER_CODEPOINT]) :
    utf16CodeUnitIterator = (new _ListRange(utf16CodeUnits, offset, length))
        .iterator();

new Utf16CodeUnitDecoder.fromListRangeIterator(_ListRangeIterator utf16CodeUnitIterator, int replacementCodepoint) #

Utf16CodeUnitDecoder.fromListRangeIterator(
    _ListRangeIterator this.utf16CodeUnitIterator,
    int this.replacementCodepoint);

Properties

final int replacementCodepoint #

final int replacementCodepoint;

final _ListRangeIterator utf16CodeUnitIterator #

final _ListRangeIterator utf16CodeUnitIterator;

Methods

bool hasNext() #

Returns whether the Iterator has elements left.

docs inherited from Iterator<E>
bool hasNext() => utf16CodeUnitIterator.hasNext();

Iterator<int> iterator() #

Iterator<int> iterator() => this;

int next() #

Gets the next element in the iteration. Throws a NoMoreElementsException if no element is left.

docs inherited from Iterator<E>
int next() {
  int value = utf16CodeUnitIterator.next();
  if (value < 0) {
    if (replacementCodepoint != null) {
      return replacementCodepoint;
    } else {
      throw new ArgumentError(
          "Invalid UTF16 at ${utf16CodeUnitIterator.position}");
    }
  } else if (value < UNICODE_UTF16_RESERVED_LO ||
      (value > UNICODE_UTF16_RESERVED_HI && value <= UNICODE_PLANE_ONE_MAX)) {
    // transfer directly
    return value;
  } else if (value < UNICODE_UTF16_SURROGATE_UNIT_1_BASE &&
      utf16CodeUnitIterator.hasNext()) {
    // merge surrogate pair
    int nextValue = utf16CodeUnitIterator.next();
    if (nextValue >= UNICODE_UTF16_SURROGATE_UNIT_1_BASE &&
        nextValue <= UNICODE_UTF16_RESERVED_HI) {
      value = (value - UNICODE_UTF16_SURROGATE_UNIT_0_BASE) << 10;
      value += UNICODE_UTF16_OFFSET +
          (nextValue - UNICODE_UTF16_SURROGATE_UNIT_1_BASE);
      return value;
    } else {
      if (nextValue >= UNICODE_UTF16_SURROGATE_UNIT_0_BASE &&
         nextValue < UNICODE_UTF16_SURROGATE_UNIT_1_BASE) {
        utf16CodeUnitIterator.backup();
      }
      if (replacementCodepoint != null) {
        return replacementCodepoint;
      } else {
        throw new ArgumentError(
            "Invalid UTF16 at ${utf16CodeUnitIterator.position}");
      }
    }
  } else if (replacementCodepoint != null) {
    return replacementCodepoint;
  } else {
    throw new ArgumentError(
        "Invalid UTF16 at ${utf16CodeUnitIterator.position}");
  }
}