pretty

package
v1.4.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 16, 2026 License: MIT, MIT Imports: 4 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

pretty

源自 tidwall/pretty v1.3.0, 包路径改为 github.com/fufuok/pkg/xjson/pretty.

对上游的修正

以下修正在同步上游时必须保留.

位置 上游问题 本地修正 回归测试
appendPrettyObject 单行试排 v1.3.0 起对象也尝试单行输出. 试排时先完整渲染整个子树, 再比较宽度, 放不下就整段丢弃, 嵌套越深重复越多. 同机基准中比 v1.2.2 慢 3–6 倍, SortKeys 时分配约多 3 倍 试排中长度一旦超过可用宽度就立即放弃. 试排结果本来就会被丢弃, 输出不变 TestSingleLineTrialAbort, BenchmarkPrettyNested

修正后的输出与上游 v1.3.0 逐字节一致: 随机配置类文档 30 万份, 各在 6 种宽度及开关 SortKeys 下比较 (共 360 万次), 另有 400 万次畸形输入, 均无差异. WSL Ubuntu 20.04 基准 (Width 80):

文档 v1.2.2 上游 v1.3.0 本地
25KB 配置 74µs 359µs 117µs
25KB 配置, SortKeys 258µs, 411KB 1182µs, 1221KB 342µs, 495KB
8.8KB, 12 层嵌套 39µs 247µs 72µs

与 v1.2.2 相比剩余的开销, 来自对象单行试排这个功能本身.

本地优化: Ugly/UglyInPlace 只读快进

同步上游时必须保留. 输出与上游逐字节一致, 只改变扫描方式:

  • 先只读扫描到字符串外第一个可删除字节 (<= ' '), 此前内容原样保留. UglyInPlace 遇到无空白输入时不写内存, 直接返回原切片; Ugly 始终返回独立副本, 不与输入共用底层数组.
  • 之后的压紧和前缀扫描相同: 字符串前 16 字节逐字节处理, 更长的部分用 IndexByte 跳到下一个引号再整段复制. 只用 IndexByte 时短字符串反而变慢; 按 8 字节并行比较 (SWAR) 的方案在短键值 JSON 上慢约 2 倍, 均已否决.
  • 原地压缩时, 已复制的字符串在 src 中可能已被覆盖, 所以长字符串的反斜杠奇偶回溯在 out 上进行, 回溯最远停在开引号.

验证: 与旧逐字节实现 (测试内 uglyReference) 30 万组随机差分及边界表一致 (TestUglyMatchesReferenceRandom, TestUglyBoundaryCases), 原生 fuzz FuzzUgly 约 4400 万次无差异, 测试同时检查两种入口的所有权契约. 基准为 BenchmarkUglyShapes, 原地版本计时包含每轮恢复输入的复制.

评估过但不采纳: Ugly 的 make 会先清零整个缓冲区. 改用 append([]byte(nil), json...)[:l:l] 可以免清零, 且仍保持 cap == len, 但收益只在大输入上出现 (1.5MiB 时清零约 85µs, 不超过 8%), 600B 时测不出差别, 而仓内唯一调用方是 gjson 的 @ugly 修饰符, 不在热路径上.

该循环对代码对齐非常敏感, 同一份旧代码平移 32 字节可相差约 35%. 对比时须用多种布局平移 (例如在包内插入填充函数) 并取中位数, 不能只跑单个二进制.

已知问题 (上游同样存在, 暂不修改)

  • 输出长度随嵌套深度平方增长, 递归过深还会栈溢出. 只用于格式化可信数据.

同步上游

用 go mod download -json github.com/tidwall/pretty@<version> 取得源码, 逐版本对比后合入功能改动. 保留导入路径、中文注释和本文件列出的修正, 并更新 xjson/README.md 的版本表.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultOptions = &Options{Width: 80, Prefix: "", Indent: "  ", SortKeys: false}

DefaultOptions is the default options for pretty formats.

Functions

func Color

func Color(src []byte, style *Style) []byte

Color 按 style 为 JSON 添加颜色; style 为 nil 时使用默认的 TerminalStyle.

func Pretty

func Pretty(json []byte) []byte

Pretty 将输入 JSON 格式化为更易读的文本, 通过换行和缩进展示各元素.

func PrettyOptions

func PrettyOptions(json []byte, opts *Options) []byte

PrettyOptions is like Pretty but with customized options.

func Spec

func Spec(src []byte) []byte

Spec 将注释和尾逗号替换为空白, 供普通 JSON 解析器处理扩展输入.

输入的注释和字符串语法完整时, 保留长度和换行位置, 便于外部解析器定位原始偏移. 本函数不校验 JSON 合法性, 也不修复其他语法错误或未闭合注释. JSON 规范: https://tools.ietf.org/html/rfc8259

func SpecInPlace

func SpecInPlace(src []byte) []byte

SpecInPlace is the same as Spec, but this method reuses the input json buffer to avoid allocations. Do not use the original bytes slice upon return.

func Ugly

func Ugly(json []byte) []byte

Ugly removes insignificant space characters from the input json byte slice and returns the compacted result.

返回值总是新分配的独立副本, 即使输入本来就没有可删除的空白也不会与 json 共用底层数组.

func UglyInPlace

func UglyInPlace(json []byte) []byte

UglyInPlace removes insignificant space characters from the input json byte slice and returns the compacted result. This method reuses the input json buffer to avoid allocations. Do not use the original bytes slice upon return.

输入没有可删除的空白时直接返回 json 本身, 不写内存.

Types

type Options

type Options struct {
	// Width 是单行数组或对象的最大列宽, 小于等于 0 时不尝试单行输出.
	// Default is 80
	Width int
	// Prefix is a prefix for all lines
	// Default is an empty string
	Prefix string
	// Indent is the nested indentation
	// Default is two spaces
	Indent string
	// SortKeys will sort the keys alphabetically
	// Default is false
	SortKeys bool
}

Options is Pretty options

type Style

type Style struct {
	Key, String, Number [2]string
	True, False, Null   [2]string
	Escape              [2]string
	Brackets            [2]string
	Append              func(dst []byte, c byte) []byte
}

Style is the color style

var TerminalStyle *Style

TerminalStyle is for terminals

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL