Documentation

Init.Data.String.Decode

UTF-8 decoding #

This file contains the low-level proof that UTF-8 decoding and encoding are inverses. An important corollary is that attempting to decode a byte array that is valid UTF-8 will succeed.

Char.utf8Size #

theorem Char.utf8Size_eq_two_iff {c : Char} :
c.utf8Size = 2 ↔ 127 < c.val ∧ c.val ≤ 2047
theorem Char.utf8Size_eq_three_iff {c : Char} :
c.utf8Size = 3 ↔ 2047 < c.val ∧ c.val ≤ 65535

utf8EncodeChar #

utf8EncodeChar low-level API #

Returns the sequence of bytes in a character's UTF-8 encoding.

Equations
  • One or more equations did not get rendered due to their size.
Instances For

    utf8EncodeChar BitVec API #

    Size one #

    Size two #

    Size three #

    Size four #

    parseFirstByte #

    parseFirstByte definition #

    Instances For
      @[implicit_reducible, inline]
      Equations
      • One or more equations did not get rendered due to their size.
      Instances For

        parseFirstByte low-level API #

        parseFirstByte BitVec API #

        Size one #

        Size two #

        Size three #

        Size four #

        UInt8.isUTF8ContinuationByte definition & API #

        @[inline]

        Checks whether a byte is a UTF-8 continuation byte, that is, whether it has the form 0b10xxxxxx.

        Equations
        Instances For

          parseFirstByte, isUTF8ContinuationByte and utf8EncodeChar #

          assemble₁ #

          @[implicit_reducible, inline]
          Equations
          Instances For

            assemble₂ #

            @[inline]
            Equations
            Instances For
              @[inline]
              Equations
              • One or more equations did not get rendered due to their size.
              Instances For

                assemble₃ #

                @[inline]
                Equations
                Instances For
                  @[inline]
                  Equations
                  • One or more equations did not get rendered due to their size.
                  Instances For
                    @[inline]
                    Equations
                    • One or more equations did not get rendered due to their size.
                    Instances For

                      assemble₄ #

                      @[inline]
                      Equations
                      • One or more equations did not get rendered due to their size.
                      Instances For
                        @[inline]
                        Equations
                        • One or more equations did not get rendered due to their size.
                        Instances For
                          @[inline]
                          Equations
                          • One or more equations did not get rendered due to their size.
                          Instances For
                            @[implicit_reducible, inline]

                            Decodes and returns the Char whose UTF-8 encoding begins at i in bytes.

                            Returns none if i is not the start of a valid UTF-8 encoding of a character.

                            Equations
                            • One or more equations did not get rendered due to their size.
                            Instances For
                              @[implicit_reducible]

                              Decodes a list of bytes as UTF-8, stopping at the first invalid sequence.

                              This is the model used by String.toList. It is structurally recursive on the list, so the kernel reduces it in time linear in the number of bytes, unlike ByteArray.utf8Decode?, which indexes into an array.

                              Equations
                              Instances For
                                @[inline]
                                Equations
                                • One or more equations did not get rendered due to their size.
                                Instances For

                                  utf8DecodeChar? low-level API #

                                  Main theorems #

                                  utf8DecodeChar?_utf8EncodeChar_append and toByteArray_utf8EncodeChar_of_utf8DecodeChar?_eq_some are the two main results that together imply that UTF-8 encoding and decoding are inverse.

                                  Corollaries #

                                  @[inline]
                                  def ByteArray.utf8DecodeChar (bytes : ByteArray) (i : Nat) (h : (bytes.utf8DecodeChar? i).isSome = true) :

                                  Decodes and returns the Char whose UTF-8 encoding begins at i in bytes.

                                  This function requires a proof that there is, in fact, a valid Char at i. utf8DecodeChar? is an alternative function that returns Option Char instead of requiring a proof ahead of time.

                                  Equations
                                  Instances For
                                    theorem ByteArray.utf8DecodeChar_extract_congr {bytes : ByteArray} (i j j' : Nat) {h : ((bytes.extract i j).utf8DecodeChar? 0).isSome = true} {h' : ((bytes.extract i j').utf8DecodeChar? 0).isSome = true} :
                                    (bytes.extract i j).utf8DecodeChar 0 h = (bytes.extract i j').utf8DecodeChar 0 h'

                                    UInt8.IsUTF8FirstByte #

                                    Predicate for whether a byte has the bit pattern of a UTF-8 leading byte, that is, one of the forms 0b0xxxxxxx, 0b110xxxxx, 0b1110xxxx or 0b11110xxx. Every first byte of the UTF-8 encoding of a Unicode scalar value satisfies this, but so do some bytes that never occur in valid UTF-8 (0xC0, 0xC1 and 0xF5 to 0xF7).

                                    Equations
                                    Instances For
                                      @[instance_reducible, inline]
                                      Equations
                                      @[inline]
                                      Equations
                                      Instances For

                                        Further corollaries #