               _decodeBuffer = UInt32(codeUnit)
               _bitsInBuffer = 8
             }
-          } else { // Exhausted generator.
-            _atEnd = true
+          } else {
+            _didExhaustGenerator = true
             return .EmptyInput
           }
         } else if(_decodeBuffer & 0x80 == 0) {

            // We use & 0x1f to make the compiler omit a bounds check branch.
            _decodeBuffer |= (UInt32(codeUnit) << UInt32(_bitsInBuffer & 0x1f))
            _bitsInBuffer = _bitsInBuffer &+ 8
          } else { // Exhausted generator.
            _atEnd = true
            if _bitsInBuffer == 0 { return .EmptyInput }
            break // We still have some bytes left in our buffer.
          }


  /// Whether we have exhausted the generator.  Note that this doesn't mean
  /// we are done decoding, as there might still be bytes left in the buffer.
  var _didExhaustGenerator: Bool = false

  /// Start or continue decoding a UTF-8 sequence.
  ///
