• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mlocati / ip-lib / 17970326531

24 Sep 2025 08:01AM UTC coverage: 98.679% (+0.004%) from 98.675%
17970326531

Pull #106

github

web-flow
Merge de686c9a1 into 052df264c
Pull Request #106: Let getAddressAtOffset() accept numeric strings

78 of 79 new or added lines in 2 files covered. (98.73%)

4 existing lines in 1 file now uncovered.

1046 of 1060 relevant lines covered (98.68%)

216.08 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

97.73
/src/Service/NumberInChunks.php
1
<?php
2

3
namespace IPLib\Service;
4

5
/**
6
 * @internal
7
 * @readonly
8
 */
9
class NumberInChunks
10
{
11
    const CHUNKSIZE_BYTES = 8;
12

13
    const CHUNKSIZE_WORDS = 16;
14

15
    /**
16
     * @var bool
17
     */
18
    public $negative;
19

20
    /**
21
     * @var int[]
22
     */
23
    public $chunks;
24

25
    /**
26
     * @var int
27
     */
28
    public $chunkSize;
29

30
    /**
31
     * @param bool $negative
32
     * @param int[] $chunks
33
     * @param int $chunkSize
34
     */
35
    public function __construct($negative, array $chunks, $chunkSize)
36
    {
37
        $this->negative = $negative;
26✔
38
        $this->chunks = $chunks;
26✔
39
        $this->chunkSize = $chunkSize;
26✔
40
    }
26✔
41

42
    /**
43
     * @param int $int
44
     * @param int $chunkSize
45
     *
46
     * @return \IPLib\Service\NumberInChunks
47
     */
48
    public static function fromInteger($int, $chunkSize)
49
    {
50
        if ($int === 0) {
18✔
51
            return new self(false, array(0), $chunkSize);
1✔
52
        }
53
        $negative = $int < 0;
17✔
54
        if ($negative) {
17✔
55
            $positiveInt = -$int;
9✔
56
            if (is_float($positiveInt)) { // -PHP_INT_MIN is bigger than PHP_INT_MAX
9✔
NEW
57
                return self::fromNumericString((string) $int, $chunkSize);
×
58
            }
59
            $int = $positiveInt;
9✔
60
        }
61
        $bitMask = (1 << $chunkSize) - 1;
17✔
62
        $chunks = array();
17✔
63
        while ($int !== 0) {
17✔
64
            $chunks[] = $int & $bitMask;
17✔
65
            $int >>= $chunkSize;
17✔
66
        }
67

68
        return new self($negative, array_reverse($chunks), $chunkSize);
17✔
69
    }
70

71
    /**
72
     * @param string $numericString a string normalized with BinaryMath::normalizeIntegerString()
73
     * @param int $chunkSize
74
     *
75
     * @return \IPLib\Service\NumberInChunks
76
     */
77
    public static function fromNumericString($numericString, $chunkSize)
78
    {
79
        if ($numericString === '0') {
8✔
80
            return new self(false, array(0), $chunkSize);
1✔
81
        }
82
        $negative = $numericString[0] === '-';
7✔
83
        if ($negative) {
7✔
84
            $numericString = substr($numericString, 1);
7✔
85
        }
86
        $chunks = array();
7✔
87
        while ($numericString !== '0') {
7✔
88
            $chunks[] = self::modulo($numericString, $chunkSize);
7✔
89
            $numericString = self::divide($numericString, $chunkSize);
7✔
90
        }
91

92
        return new self($negative, array_reverse($chunks), $chunkSize);
7✔
93
    }
94

95
    /**
96
     * @param string $numericString
97
     * @param int $chunkSize
98
     *
99
     * @return int
100
     */
101
    private static function modulo($numericString, $chunkSize)
102
    {
103
        $carry = 0;
7✔
104
        $len = strlen($numericString);
7✔
105
        for ($i = 0; $i < $len; $i++) {
7✔
106
            $digit = (int) $numericString[$i];
7✔
107
            $carry = ($carry * 10 + $digit) % (1 << $chunkSize);
7✔
108
        }
109

110
        return $carry;
7✔
111
    }
112

113
    /**
114
     * @param string $numericString
115
     * @param int $chunkSize
116
     *
117
     * @return string
118
     */
119
    private static function divide($numericString, $chunkSize)
120
    {
121
        $divisor = 1 << $chunkSize;
7✔
122
        $quotient = '';
7✔
123
        $carry = 0;
7✔
124
        $len = strlen($numericString);
7✔
125
        for ($i = 0; $i < $len; $i++) {
7✔
126
            $digit = (int) $numericString[$i];
7✔
127
            $value = $carry * 10 + $digit;
7✔
128
            $quotient .= (string) ($value >> $chunkSize);
7✔
129
            $carry = $value % $divisor;
7✔
130
        }
131

132
        return ltrim($quotient, '0') ?: '0';
7✔
133
    }
134
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc