Documentation

Init.Data.ByteArray.Basic

Equations
Instances For
    theorem ByteArray.ext_iff {x y : ByteArray} :
    x = y x.data = y.data
    @[reducible, inline, deprecated ByteArray.emptyWithCapacity (since := "2025-03-12")]
    Equations
    Instances For
      @[extern lean_sarray_size]

      Retrieves the size of the array as a platform-specific fixed-width integer.

      Because USize is big enough to address all memory on every platform that Lean supports, there are in practice no ByteArrays that have more elements that USize can count.

      Equations
      Instances For
        @[extern lean_byte_array_uget]
        def ByteArray.uget (a : ByteArray) (i : USize) (h : i.toNat < a.size := by get_elem_tactic) :

        Retrieves the byte at the indicated index. Callers must prove that the index is in bounds. The index is represented by a platform-specific fixed-width integer (either 32 or 64 bits).

        Because USize is big enough to address all memory on every platform that Lean supports, there are in practice no ByteArrays for which uget cannot retrieve all elements.

        Equations
        Instances For
          @[extern lean_byte_array_get]

          Retrieves the byte at the indicated index. Panics if the index is out of bounds.

          Equations
          Instances For
            @[extern lean_byte_array_fget]
            def ByteArray.get (a : ByteArray) (i : Nat) (h : i < a.size := by get_elem_tactic) :

            Retrieves the byte at the indicated index. Callers must prove that the index is in bounds.

            Use uget for a more efficient alternative or get! for a variant that panics if the index is out of bounds.

            Equations
            • { data := bs }.get x✝¹ x✝ = bs[x✝¹]
            Instances For
              Equations
              @[extern lean_byte_array_set]

              Replaces the byte at the given index.

              The array is modified in-place if there are no other references to it.

              If the index is out of bounds, the array is returned unmodified.

              Equations
              • { data := bs }.set! x✝¹ x✝ = { data := bs.set! x✝¹ x✝ }
              Instances For
                @[extern lean_byte_array_fset]
                def ByteArray.set (a : ByteArray) (i : Nat) :
                UInt8(h : autoParam (i < a.size) _auto✝) → ByteArray

                Replaces the byte at the given index.

                No bounds check is performed, but the function requires a proof that the index is in bounds. This proof can usually be omitted, and will be synthesized automatically.

                The array is modified in-place if there are no other references to it.

                Equations
                • { data := bs }.set x✝² x✝¹ x✝ = { data := bs.set x✝² x✝¹ x✝ }
                Instances For
                  @[extern lean_byte_array_uset]
                  def ByteArray.uset (a : ByteArray) (i : USize) :

                  Replaces the byte at the given index.

                  No bounds check is performed, but the function requires a proof that the index is in bounds. This proof can usually be omitted, and will be synthesized automatically.

                  The array is modified in-place if there are no other references to it.

                  Equations
                  • { data := bs }.uset x✝² x✝¹ x✝ = { data := bs.uset x✝² x✝¹ x✝ }
                  Instances For
                    @[extern lean_byte_array_hash]

                    Computes a hash for a ByteArray.

                    Returns true when s contains zero bytes.

                    Equations
                    Instances For
                      @[extern lean_byte_array_copy_slice]
                      def ByteArray.copySlice (src : ByteArray) (srcOff : Nat) (dest : ByteArray) (destOff len : Nat) (exact : Bool := true) :

                      Copies the slice at [srcOff, srcOff + len) in src to [destOff, destOff + len) in dest, growing dest if necessary. If exact is false, the capacity will be doubled when grown.

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

                        Copies the bytes with indices b (inclusive) to e (exclusive) to a new ByteArray.

                        Equations
                        Instances For
                          @[inline]
                          Equations
                          Instances For
                            @[simp]
                            @[simp]
                            theorem ByteArray.append_eq {a b : ByteArray} :
                            a.append b = a ++ b
                            @[simp]

                            Converts a packed array of bytes to a linked list.

                            Equations
                            Instances For
                              @[irreducible]
                              Equations
                              Instances For
                                @[inline]
                                def ByteArray.findFinIdx? (a : ByteArray) (p : UInt8Bool) (start : Nat := 0) :

                                Finds the index of the first byte in a for which p returns true. If no byte in a satisfies p, then the result is none.

                                The index is returned along with a proof that it is a valid index in the array.

                                Equations
                                Instances For
                                  @[irreducible, specialize #[]]
                                  Equations
                                  Instances For
                                    @[inline]
                                    def ByteArray.findIdx? (a : ByteArray) (p : UInt8Bool) (start : Nat := 0) :

                                    Finds the index of the first byte in a for which p returns true. If no byte in a satisfies p, then the result is none.

                                    The variant findFinIdx? additionally returns a proof that the found index is in bounds.

                                    Equations
                                    Instances For
                                      @[irreducible, specialize #[]]
                                      Equations
                                      Instances For
                                        @[inline]
                                        unsafe def ByteArray.forInUnsafe {β : Type v} {m : Type v → Type w} [Monad m] (as : ByteArray) (b : β) (f : UInt8βm (ForInStep β)) :
                                        m β

                                        An efficient implementation of ForIn.forIn for ByteArray that uses USize rather than Nat for indices.

                                        We claim this unsafe implementation is correct because an array cannot have more than USize.size elements in our runtime. This is similar to the Array version.

                                        Equations
                                        Instances For
                                          @[specialize #[]]
                                          unsafe def ByteArray.forInUnsafe.loop {β : Type v} {m : Type v → Type w} [Monad m] (as : ByteArray) (f : UInt8βm (ForInStep β)) (sz i : USize) (b : β) :
                                          m β
                                          Equations
                                          • One or more equations did not get rendered due to their size.
                                          Instances For
                                            @[implemented_by ByteArray.forInUnsafe]
                                            def ByteArray.forIn {β : Type v} {m : Type v → Type w} [Monad m] (as : ByteArray) (b : β) (f : UInt8βm (ForInStep β)) :
                                            m β

                                            The reference implementation of ForIn.forIn for ByteArray.

                                            In compiled code, this is replaced by the more efficient ByteArray.forInUnsafe.

                                            Equations
                                            Instances For
                                              def ByteArray.forIn.loop {β : Type v} {m : Type v → Type w} [Monad m] (as : ByteArray) (f : UInt8βm (ForInStep β)) (i : Nat) (h : i as.size) (b : β) :
                                              m β
                                              Equations
                                              Instances For
                                                instance ByteArray.instForInUInt8 {m : Type u_1 → Type u_2} :
                                                Equations
                                                @[inline]
                                                unsafe def ByteArray.foldlMUnsafe {β : Type v} {m : Type v → Type w} [Monad m] (f : βUInt8m β) (init : β) (as : ByteArray) (start : Nat := 0) (stop : Nat := as.size) :
                                                m β

                                                An efficient implementation of a monadic left fold on for ByteArray that uses USize rather than Nat for indices.

                                                We claim this unsafe implementation is correct because an array cannot have more than USize.size elements in our runtime. This is similar to the Array version.

                                                Equations
                                                • One or more equations did not get rendered due to their size.
                                                Instances For
                                                  @[specialize #[]]
                                                  unsafe def ByteArray.foldlMUnsafe.fold {β : Type v} {m : Type v → Type w} [Monad m] (f : βUInt8m β) (as : ByteArray) (i stop : USize) (b : β) :
                                                  m β
                                                  Equations
                                                  Instances For
                                                    @[implemented_by ByteArray.foldlMUnsafe]
                                                    def ByteArray.foldlM {β : Type v} {m : Type v → Type w} [Monad m] (f : βUInt8m β) (init : β) (as : ByteArray) (start : Nat := 0) (stop : Nat := as.size) :
                                                    m β

                                                    A monadic left fold on ByteArray that iterates over an array from low to high indices, computing a running value.

                                                    Each element of the array is combined with the value from the prior elements using a monadic function f. The initial value init is the starting value before any elements have been processed.

                                                    Equations
                                                    • One or more equations did not get rendered due to their size.
                                                    Instances For
                                                      def ByteArray.foldlM.loop {β : Type v} {m : Type v → Type w} [Monad m] (f : βUInt8m β) (as : ByteArray) (stop : Nat) (h : stop as.size) (i j : Nat) (b : β) :
                                                      m β
                                                      Equations
                                                      • One or more equations did not get rendered due to their size.
                                                      Instances For
                                                        @[inline]
                                                        def ByteArray.foldl {β : Type v} (f : βUInt8β) (init : β) (as : ByteArray) (start : Nat := 0) (stop : Nat := as.size) :
                                                        β

                                                        A left fold on ByteArray that iterates over an array from low to high indices, computing a running value.

                                                        Each element of the array is combined with the value from the prior elements using a function f. The initial value init is the starting value before any elements have been processed.

                                                        ByteArray.foldlM is a monadic variant of this function.

                                                        Equations
                                                        Instances For

                                                          Iterator over the bytes (UInt8) of a ByteArray.

                                                          Typically created by arr.iter, where arr is a ByteArray.

                                                          An iterator is valid if the position i is valid for the array arr, meaning 0 ≤ i ≤ arr.size

                                                          Most operations on iterators return arbitrary values if the iterator is not valid. The functions in the ByteArray.Iterator API should rule out the creation of invalid iterators, with two exceptions:

                                                          • array : ByteArray

                                                            The array the iterator is for.

                                                          • idx : Nat

                                                            The current position.

                                                            This position is not necessarily valid for the array, for instance if one keeps calling Iterator.next when Iterator.atEnd is true. If the position is not valid, then the current byte is (default : UInt8).

                                                          Instances For

                                                            Creates an iterator at the beginning of an array.

                                                            Equations
                                                            Instances For
                                                              @[reducible, inline]

                                                              Creates an iterator at the beginning of an array.

                                                              Equations
                                                              Instances For

                                                                The size of an array iterator is the number of bytes remaining.

                                                                Equations

                                                                The number of bytes remaining in the iterator.

                                                                Equations
                                                                Instances For

                                                                  The current position.

                                                                  This position is not necessarily valid for the array, for instance if one keeps calling Iterator.next when Iterator.atEnd is true. If the position is not valid, then the current byte is (default : UInt8).

                                                                  Equations
                                                                  Instances For
                                                                    @[inline]

                                                                    True if the iterator is past the array's last byte.

                                                                    Equations
                                                                    Instances For
                                                                      @[inline]

                                                                      The byte at the current position.

                                                                      On an invalid position, returns (default : UInt8).

                                                                      Equations
                                                                      Instances For
                                                                        @[inline]

                                                                        Moves the iterator's position forward by one byte, unconditionally.

                                                                        It is only valid to call this function if the iterator is not at the end of the array, i.e. Iterator.atEnd is false; otherwise, the resulting iterator will be invalid.

                                                                        Equations
                                                                        • { array := arr, idx := i }.next = { array := arr, idx := i + 1 }
                                                                        Instances For
                                                                          @[inline]

                                                                          Decreases the iterator's position.

                                                                          If the position is zero, this function is the identity.

                                                                          Equations
                                                                          • { array := arr, idx := i }.prev = { array := arr, idx := i - 1 }
                                                                          Instances For
                                                                            @[inline]

                                                                            True if the iterator is valid; that is, it is not past the array's last byte.

                                                                            Equations
                                                                            Instances For
                                                                              @[inline]

                                                                              The byte at the current position. -

                                                                              Equations
                                                                              Instances For
                                                                                @[inline]

                                                                                Moves the iterator's position forward by one byte. -

                                                                                Equations
                                                                                • { array := arr, idx := i }.next' h_1 = { array := arr, idx := i + 1 }
                                                                                Instances For
                                                                                  @[inline]

                                                                                  True if the position is not zero.

                                                                                  Equations
                                                                                  Instances For
                                                                                    @[inline]

                                                                                    Moves the iterator's position to the end of the array.

                                                                                    Given i : ByteArray.Iterator, note that i.toEnd.atEnd is always true.

                                                                                    Equations
                                                                                    Instances For
                                                                                      @[inline]

                                                                                      Moves the iterator's position several bytes forward.

                                                                                      The resulting iterator is only valid if the number of bytes to skip is less than or equal to the number of bytes left in the iterator.

                                                                                      Equations
                                                                                      • { array := arr, idx := i }.forward x✝ = { array := arr, idx := i + x✝ }
                                                                                      Instances For
                                                                                        @[inline]

                                                                                        Moves the iterator's position several bytes forward.

                                                                                        The resulting iterator is only valid if the number of bytes to skip is less than or equal to the number of bytes left in the iterator.

                                                                                        Equations
                                                                                        Instances For
                                                                                          @[inline]

                                                                                          Moves the iterator's position several bytes back.

                                                                                          If asked to go back more bytes than available, stops at the beginning of the array.

                                                                                          Equations
                                                                                          • { array := arr, idx := i }.prevn x✝ = { array := arr, idx := i - x✝ }
                                                                                          Instances For