定制 axy/codecs-base64vlq 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

axy/codecs-base64vlq

Composer 安装命令:

composer require axy/codecs-base64vlq

包简介

Codec for VLQ (variable-length quantity) Base64 algorithm

README 文档

README

Codec for VLQ (variable-length quantity) Base64 algorithm (PHP).

Latest Stable Version Minimum PHP Version Tests Coverage Status License

Documentation

VQL + Base64

Base64 allows us to represent a sequence of numbers in a text string that can be stored and transmit in text formats (JSON, XML, etc).

VLQ allows us to represent an integer in a sequence numbers with little digit capacity. For example 6 bit is the limit for Base64. The resulting numbers are called "VLQ digits". Small input number is represented by fewer VLQ-digits than big number. Thus VLQ is most effective if the input sequence is contains mainly the small numbers.

VLQ+Base64 allows us effectively represented a sequence of integers (dominated by a small number of) in the text format.

For example, it used in JavaScript/CSS source map.

The Algorithm

For example, we have a block of numbers: [12345, -12345, 0].

(1). VLQ only works with unsigned integers. Transfer the sign bit to the end of the integer.

12345 in binary is 11000000111001. Added 0 (positive) to the end: 110000001110010.

For -12345 take a positive form and add 1 (negative) to the end: 110000001110011.

Result is [110000001110010, 110000001110011, 0].

(2). Transform to the VLQ-sequence. For Base64 we need a block of 6-bit numbers. Most significant bit is reserved - it is "continuation".

Split numbers to groups of 5 bits: [11000 00011 10010, 11000 00011 10011, 00000]. Output starting from the least significant bits. If the group is not the last in the current number then set the continuation bit.

Result: [110010 100011 011000 110011 100011 011000 000000]. Or decimal [50, 35, 24, 51, 35, 24, 0]. These are VLQ digits.

(3). Replace the numbers on the letters of the Base64-alphabet. The standard alphabet is A..Za..z0..9+/.

Result is yjYzjYA.

How to use the library

use axy\codecs\base64vlq\Encoder;

$encoder = new Encoder();

$encoder->encode([12345, -12345, 0]); // yjYzjYA
$encoder->decode('yjYzjYA'); //  [12345, 012345, 9]

$encoder->decode('Variable+Length+QuantitY'); // [-10, 13, -13349, -13 ... -12797139]

Or default encoder can be obtained as follow:

$encoder = Encoder::getStandardInstance();

In this case, the object creation and preliminary calculations are performed only once.

Custom options

The standard encoder uses standard options:

  1. Transfer the sign bit.
  2. 6-bit VLQ digits.
  3. Standard alphabet: A..Za..z0..9+/.

You can apply your settings:

Encoder::__construct([array|string $alphabet, int $bits, bool $signed = true])
/* Custom alphabet, 4 bits, no sign transfer */
$encoder = new Encoder('My Alphabet', 3, false);

$encoder->encode([12345, 6789]); // phalllApplhhhy

Custom alphabet can be specified as a string (My Alphabet) or as an array ([1 => 'A', 10 => 'B', 15 => 'C', 20 => 'D']).

Exceptions

The error classes are located in the namespace axy\codecs\base64vlq\Encoder\Errors.

  • Error
    • VLQ
      • InvalidVLQSequence
    • Base64
      • InvalidBase64
      • InvalidBase64Input
InvalidVLQSequence
$encoder->decode('Az'); // VLQ sequence is invalid: [0,51]

z is 51 (the continuation bit = 1). The last digit must have 0 in continuation bit.

InvalidBase64
$encoder->decode('A*A'); // Base-64 string is invalid: "A*A"

* are not in the standard Base64 alphabet.

InvalidBase64Input

For the standard encoder this exception should not occur.

Can occur for incorrect custom options:

$encoder = new Encoder('qwe', 10);
$encoder->encode([10, 20, 30]); // Number 20 is not found in Base64 alphabet

10 bits is 1024 variants, but the alphabet contains only 3 letter.

axy/codecs-base64vlq 适用场景与选型建议

axy/codecs-base64vlq 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 763.72k 次下载、GitHub Stars 达 3, 最近一次更新时间为 2015 年 02 月 22 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「base64」 「codec」 「VLQ」 「Variable length quantity」 「Source map」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 axy/codecs-base64vlq 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 axy/codecs-base64vlq 我们能提供哪些服务?
定制开发 / 二次开发

基于 axy/codecs-base64vlq 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 763.72k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 3
  • 点击次数: 27
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 3
  • Watchers: 1
  • Forks: 4
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-02-22