定制 mpd/util-strint-cmp 二次开发

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

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

mpd/util-strint-cmp

Composer 安装命令:

composer require mpd/util-strint-cmp

包简介

Library to compare large integers with arbitrary precisions, provides own pure php implementation, but can also use bcmath or GMP on background when available

README 文档

README

mpd\Util\StrIntCmp
Packagist Version Packagist Dependency Version GitHub License

Library to compare large integers with arbitrary precisions, provides own pure php implementation, but can also use bcmath or GMP on background when available

Installation

composer require mpd/util-strint-cmp
Compatible ^php7.1, ^php8. Tests require php8

Usage

use mpd\Util\StrIntCmp\StrIntCmp;

$a = '-350317994473186284556312754796813594860152465582658277960247';
$b = '-63245865813051830934382781315128857497460797902242257911260';

//use the best backend available. in order(gmp > bcmath > StrIntCmp::strIntCmp)
echo(StrIntCmp::cmp($a, $b)); // -1
//force use own implementation
echo(StrIntCmp::strIntCmp($a, $b)); // -1

echo(StrIntCmp::cmp("5", "2")); //  1
echo(StrIntCmp::cmp("2", "5")); // -1
echo(StrIntCmp::cmp("7", "7")); //  0

echo(StrIntCmp::isValidInt("75a")); // false
echo(StrIntCmp::isValidInt("14"));  // true

For performance reasons valid integer strings are assumed and no additional validation is done, it's your responsibility to provide valid format [+-]?[0-9]+. You can use isValidInt() to check if you cant be sure. invalid integers are basically undefined behaviour.
Yes + prefixed numbers are handled correctly too.
Note -0 is considered UB, depending on backend -0 == 0 or -0 < 0

class StrIntCmp {
    /* Properties */
    const SIZE; 
    public static $handler;
    /* Methods */
    public static cmp($a, $b): int
    public static strIntCmp($a, $b): int
    public static isValidInt($a): bool
    public static init(): void
}
  • SIZE maximal length of decimal represented string that can be represented as native int on current platform
  • $handler comparator to use. automatically set to optimal known, but you can set it to any function providing standard three-way comparison if you want
  • cmp() unified api of all backends
  • strIntCmp() direct call to backend implemented by this pkg
  • isValidInt() check if string is considered valid integer
  • init() resets $handler to optimal backend. Normally is not needed to call unless you wish to undo manual change of $handler or additional extensions was loaded during runtime

Motivation

We have been in need to compare large numbers with arbitrary precision anywhere, which is problematic on some hostings, and problems starts earlier than you might think.

platform (64bit scope)
When host system is 32bit php int is to, larger numbers silently become floats which sometimes results in incorrect results

polyfill
you can easily solve this by using libraries like bcmath or gmp... but these are not written in php, they are php extensions which needs to be installed physically in the system, and most standard web hosting dost let you touch that, they are sometimes available, but you need to modify your code depending on which one is available and sometimes there is none.
This library isolates you from this, you just call compare method, and receive result. It has its own pure php implementation, but prefers to use gmp/bcmath when available for better performance.

writing new
Research for similar library wasn't much successful, few pure php math implementations was found, but they usually looks abandoned since php5 was new and tends to be very large, and also don't act as "polyfill"

only comparators?
Idea of universal polyfill is tempting, but currently there is no need for more functionality rather for things being quickly done, and it also allows this lib to be small.
On internal or even external request it might become reality.
But anyway if you're doing that sensitive operations maybe you should look for better hosting that provides php extension.

Testing

composer run test
Test set is mainly based on random asserts generated by gen.py <no of tests>. Note due to how phpunit works, is not recommended creating more than about 500k asserts. this already takes quite long and requires nearly 4gb of ram for some reason. These generated tests are not included for size reasons. generate your own by running script, which also results in better testing. if you found values that don't work correctly send them to us please.
Recommendations for some critical values welcomed.

mpd/util-strint-cmp 适用场景与选型建议

mpd/util-strint-cmp 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 4 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 01 月 04 日, 在 PHP 生态内属于活跃度较高的组件。

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

围绕 mpd/util-strint-cmp 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 4
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 31
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: LGPL-3.0-only
  • 更新时间: 2025-01-04