普贤教程 › 包参考
ulid 包
可排序唯一 ID(Crockford Base32)
| 项 | 值 |
|---|---|
| 包名 | ulid |
| 版本 | 0.2.0 |
| 可用版本 | 0.1.0 / 0.2.0 |
| 入口 | ulid.px |
| 分类 | 标识、校验与版本 |
| 文件数 / 行数 | 1 / 220 |
安装与引入
bash
pxpkg init # 首次 pxpkg add ulid@0.2.0 pxpkg install
px
import ulid所属分类教程:标识、校验与版本
公开接口
| 函数 | 参数 |
|---|---|
ul_new_at | ms, rand_hex |
ul_new | |
ul_ts | ul |
ul_decode | ul |
ul_valid | s |
其余 14 个 def(内部与辅助,可直接调用但不属稳定接口)
| 函数 | 参数 |
|---|---|
ul_val | c |
ul_encode40 | v |
ul_hex40 | h |
ul_hex_valid | h |
ul_hex_lower | h |
ul_encode_ts | ts |
ul_decode_ts | ts10 |
ul_decode40 | s8 |
ul_hex_of40 | v |
ul_compare | a, b |
ul_min | a, b |
ul_max | a, b |
ul_increase | ul |
ul_short | ul |
包内说明(源码头注释)
text
============================================================ 普贤生态库:registry/ulid —— 可排序唯一 ID(Crockford Base32) ------------------------------------------------------------ 纯函数库(无 IO、无状态),编译(px build)与解释(px run)双模式一致。 ul_new() → str 26 字符(48bit 毫秒时间戳 + 80bit 随机) ul_new_at(ms, rand_hex) → str 指定毫秒时间戳与 80bit 随机 hex(测试用; rand_hex 空串 → os_random_hex(10)) ul_ts(ul) → Ok(int) | Err 解析出毫秒时间戳 ul_decode(ul) → Ok({ts, rand}) | Err 分解(rand 为小写 hex) ul_valid(s) → bool 长度 26 + 全部在 Crockford 字母表 说明: - 字母表 Crockford:0123456789ABCDEFGHJKMNPQRSTVWXYZ(不含 I/L/O/U) - 编码:前 10 字符 = 48bit 时间戳(首字符仅 3bit 有效 ≤ 7),后 16 字符 = 80bit 随机 - 随机部分拆 2×40bit 编码(40bit 以内 int 不溢出) ============================================================