Sha256: 7df61f74f7978a14fd1369ed5263aabac773c092fc6bc64f30c1acb631649b52

Contents?: true

Size: 1.16 KB

Versions: 395

Compression:

Stored size: 1.16 KB

Contents

<?php

/**
 * @param array $integers
 * @return array
 */
function vlq_encode(array $integers)
{
    $result = [];

    foreach ($integers as $integer) {
        if ($integer > 0xfffffff) {
            throw new InvalidArgumentException('The value is greater than the maximum allowed.');
        }

        $bytes = [];
        do {
            $byte = 0x7f & $integer;
            array_unshift($bytes, empty($bytes) ? $byte : 0x80 | $byte);
            $integer >>= 7;
        } while ($integer > 0);
        $result = array_merge($result, $bytes);
    }

    return $result;
}

/**
 * @param array $bytes
 * @return array
 */
function vlq_decode(array $bytes)
{
    $result = [];
    $integer = 0;

    foreach ($bytes as $byte) {
        if ($integer > 0xfffffff - 0x7f) {
            throw new OverflowException('The value exceeds the maximum allowed.');
        }

        $integer <<= 7;
        $integer |= 0x7f & $byte;
        
        if (($byte & 0x80) === 0) {
            $result[] = $integer;
            $integer = 0;
        }
    }

    if (($byte & 0x80) !== 0) {
        throw new InvalidArgumentException('Incomplete byte sequence.');
    }

    return $result;
}

Version data entries

395 entries across 395 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/php/exercises/variable-length-quantity/example.php
trackler-2.2.1.179 tracks/php/exercises/variable-length-quantity/example.php
trackler-2.2.1.178 tracks/php/exercises/variable-length-quantity/example.php
trackler-2.2.1.177 tracks/php/exercises/variable-length-quantity/example.php
trackler-2.2.1.176 tracks/php/exercises/variable-length-quantity/example.php
trackler-2.2.1.175 tracks/php/exercises/variable-length-quantity/example.php
trackler-2.2.1.174 tracks/php/exercises/variable-length-quantity/example.php
trackler-2.2.1.173 tracks/php/exercises/variable-length-quantity/example.php
trackler-2.2.1.172 tracks/php/exercises/variable-length-quantity/example.php
trackler-2.2.1.171 tracks/php/exercises/variable-length-quantity/example.php
trackler-2.2.1.170 tracks/php/exercises/variable-length-quantity/example.php
trackler-2.2.1.169 tracks/php/exercises/variable-length-quantity/example.php
trackler-2.2.1.167 tracks/php/exercises/variable-length-quantity/example.php
trackler-2.2.1.166 tracks/php/exercises/variable-length-quantity/example.php
trackler-2.2.1.165 tracks/php/exercises/variable-length-quantity/example.php
trackler-2.2.1.164 tracks/php/exercises/variable-length-quantity/example.php
trackler-2.2.1.163 tracks/php/exercises/variable-length-quantity/example.php
trackler-2.2.1.162 tracks/php/exercises/variable-length-quantity/example.php
trackler-2.2.1.161 tracks/php/exercises/variable-length-quantity/example.php
trackler-2.2.1.160 tracks/php/exercises/variable-length-quantity/example.php