summaryrefslogtreecommitdiff
path: root/vendor/bundle/ruby/3.4.0/gems/sass-embedded-1.101.0-arm64-darwin/lib/sass/compiler/varint.rb
blob: 798777979ef03c42b7c12d84732154677a3e6f32 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# frozen_string_literal: true

module Sass
  class Compiler
    # The {Varint} module.
    #
    # It reads and writes varints.
    module Varint
      module_function

      def length(value)
        return 1 if value < 128

        (value.bit_length + 6) / 7
      end

      def read(readable)
        value = bits = 0
        loop do
          byte = readable.readbyte
          value |= (byte & 0x7f) << bits
          bits += 7
          break if byte < 0x80
        end
        value
      end

      def write(writeable, value)
        until value < 0x80
          writeable << ((value & 0x7f) | 0x80)
          value >>= 7
        end
        writeable << value
      end
    end

    private_constant :Varint
  end
end