   ) {
     let scalarValue: UInt32 = UInt32(input)
 
-    if scalarValue <= UInt32(UInt16.max) {
-      processCodeUnit(UInt16(scalarValue))
+    if scalarValue <= UInt32(extendingOrTruncating: UInt16.max) {
+      processCodeUnit(UInt16(extendingOrTruncating: scalarValue))
     }
     else {
-      let lead_offset = UInt32(0xd800) - UInt32(0x10000 &>> 10)
+      let lead_offset =
+        (0xd800 as UInt32) - UInt32(extendingOrTruncating: 0x10000 &>> 10)
       processCodeUnit(UInt16(lead_offset + (scalarValue &>> (10 as UInt32))))
       processCodeUnit(UInt16(0xdc00 + (scalarValue & 0x3ff)))
     }

    into processCodeUnit: (CodeUnit) -> Void
  ) {
    var c = UInt32(input)
    var buf3 = UInt8(c & 0xFF)

    if c >= UInt32(1 &<< 7) {
      c &>>= 6
      buf3 = (buf3 & 0x3F) | 0x80 // 10xxxxxx
      var buf2 = UInt8(c & 0xFF)
      if c < UInt32(1 &<< 5) {
        buf2 |= 0xC0              // 110xxxxx
      }
      else {
        c &>>= 6
        buf2 = (buf2 & 0x3F) | 0x80 // 10xxxxxx
        var buf1 = UInt8(c & 0xFF)
        if c < UInt32(1 &<< 4) {
          buf1 |= 0xE0              // 1110xxxx
        }
        else {
          c &>>= 6
          buf1 = (buf1 & 0x3F) | 0x80 // 10xxxxxx
          processCodeUnit(UInt8(c | 0xF0)) // 11110xxx
        }
        processCodeUnit(buf1)
      }

      guard let codeUnit = input.next() else { return .emptyInput }
      // ASCII, return immediately.
      if codeUnit & 0x80 == 0 {
        return .scalarValue(UnicodeScalar(
          _unchecked: UInt32(extendingOrTruncating: codeUnit)))
      }
      // Non-ASCII, proceed to buffering mode.
      _decodeBuffer = UInt32(extendingOrTruncating: codeUnit)
      _bitsInBuffer = 8
    } else if (_decodeBuffer & 0x80 == 0) {
      // ASCII in buffer.  We don't refill the buffer so we can return
