Documentation

Std.Data.ByteSlice

This module defines the ByteSlice structure. It's a modified version of the SubArray code, with some functions specific to ByteSlice.

Internal representation of ByteSlice, which is an abbreviation for Slice ByteSliceData.

  • byteArray : ByteArray

    The underlying byte array.

  • start : Nat

    The starting index of the region of interest (inclusive).

  • stop : Nat

    The ending index of the region of interest (exclusive).

  • start_le_stop : self.start self.stop

    The starting index is no later than the ending index.

    The ending index is exclusive. If the starting and ending indices are equal, then the byte slice is empty.

  • stop_le_size_byteArray : self.stop self.byteArray.size

    The stopping index is no later than the end of the byte array.

    The ending index is exclusive. If it is equal to the size of the byte array, then the last byte of the byte array is in the byte slice.

Instances For
    @[reducible, inline]
    abbrev ByteSlice :

    A region of some underlying byte array.

    A byte slice contains a byte array together with the start and end indices of a region of interest. Byte slices can be used to avoid copying or allocating space, while being more convenient than tracking the bounds by hand. The region of interest consists of every index that is both greater than or equal to start and strictly less than stop.

    Equations
    Instances For
      @[inline]

      The underlying byte array.

      Equations
      Instances For
        @[inline]

        The starting index of the region of interest (inclusive).

        Equations
        Instances For
          @[inline]

          The ending index of the region of interest (exclusive).

          Equations
          Instances For

            The starting index is no later than the ending index.

            The ending index is exclusive. If the starting and ending indices are equal, then the byte slice is empty.

            The stopping index is no later than the end of the byte array.

            The ending index is exclusive. If it is equal to the size of the byte array, then the last byte of the byte array is in the byte slice.

            Computes the size of the byte slice.

            Equations
            Instances For
              def ByteSlice.get (s : ByteSlice) (i : Fin s.size) :

              Extracts a byte from the byte slice.

              The index is relative to the start of the byte slice, rather than the underlying byte array.

              Equations
              Instances For
                Equations
                @[inline]
                def ByteSlice.getD (s : ByteSlice) (i : Nat) (v₀ : UInt8) :

                Extracts a byte from the byte slice, or returns a default value v₀ when the index is out of bounds.

                The index is relative to the start and end of the byte slice, rather than the underlying byte array.

                Equations
                Instances For
                  @[reducible, inline]
                  abbrev ByteSlice.get! (s : ByteSlice) (i : Nat) :

                  Extracts a byte from the byte slice, or returns a default value when the index is out of bounds.

                  The index is relative to the start and end of the byte slice, rather than the underlying byte array. The default value is 0.

                  Equations
                  Instances For

                    The empty byte slice.

                    This empty byte slice is backed by an empty byte array.

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

                      Creates a new ByteSlice of a ByteArray

                      Equations
                      • ByteSlice.ofByteArray ba = { internalRepresentation := { byteArray := ba, start := 0, stop := ba.size, start_le_stop := , stop_le_size_byteArray := } }
                      Instances For

                        Converts a byte slice back to a byte array by copying the relevant portion.

                        Equations
                        Instances For
                          @[extern lean_byteslice_beq]

                          Comparison function

                          Equations
                          Instances For
                            @[inline]
                            def ByteSlice.foldrM {β : Type v} {m : Type v → Type w} [Monad m] (f : UInt8βm β) (init : β) (as : ByteSlice) :
                            m β

                            Folds a monadic operation from right to left over the bytes in a byte slice.

                            An accumulator of type β is constructed by starting with init and monadically combining each byte of the byte slice with the current accumulator value in turn, moving from the end to the start. The monad in question may permit early termination or repetition.

                            Examples:

                            #eval (ByteArray.mk #[1, 2, 3]).toByteSlice.foldrM (init := 0) fun x acc =>
                              some x.toNat + acc
                            
                            some 6
                            
                            Equations
                            Instances For
                              @[inline]
                              def ByteSlice.forM {m : Type v → Type w} [Monad m] (f : UInt8m PUnit) (as : ByteSlice) :

                              Runs a monadic action on each byte of a byte slice.

                              The bytes are processed starting at the lowest index and moving up.

                              Equations
                              Instances For
                                @[inline]
                                def ByteSlice.foldr {β : Type v} (f : UInt8ββ) (init : β) (as : ByteSlice) :
                                β

                                Folds an operation from right to left over the bytes in a byte slice.

                                An accumulator of type β is constructed by starting with init and combining each byte of the byte slice with the current accumulator value in turn, moving from the end to the start.

                                Examples:

                                • (ByteArray.mk #[1, 2, 3]).toByteSlice.foldr (·.toNat + ·) 0 = 6
                                • (ByteArray.mk #[1, 2, 3]).toByteSlice.popFront.foldr (·.toNat + ·) 0 = 5
                                Equations
                                Instances For
                                  def ByteSlice.slice (s : ByteSlice) (start : Nat := 0) (stop : Nat := s.size) :

                                  Creates a sub-slice of the byte slice with the given bounds.

                                  If start or stop are not valid bounds for a sub-slice, then they are clamped to the slice's size. Additionally, the starting index is clamped to the ending index.

                                  The indices are relative to the current slice, not the underlying byte array.

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

                                    Checks if the byte slice contains a specific byte value.

                                    Returns true if any byte in the slice equals the given value, false otherwise.

                                    Equations
                                    Instances For
                                      def ByteArray.toByteSlice (as : ByteArray) (start : Nat := 0) (stop : Nat := as.size) :

                                      Returns a byte slice of a byte array, with the given bounds.

                                      If start or stop are not valid bounds for a byte slice, then they are clamped to byte array's size. Additionally, the starting index is clamped to the ending index.

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