textalg

package
v1.3.2 Latest Latest
Warning

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

Go to latest
Published: Nov 30, 2026 License: MIT Imports: 7 Imported by: 0 modules, 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.

Documentation

Overview

Package textalg 为 core/domain/i18n 提供纯字符串、UTF-8、区域标签、模板词法与 JSON null 守卫算法。

架构约束(Plan v2.2 无环叶包):

  • 本包位于 core/domain/i18n/internal/textalg,仅允许由父包 core/domain/i18n 单向导入;
  • 零仓内依赖、仅依赖纯函数标准库;
  • 严守零 I/O、零全局可变 var、零 init()、零反射与零非确定性时钟。

Index

Constants

View Source
const (
	// MaxMessageIDBytes 限制规范消息标识的最大字节长度。
	MaxMessageIDBytes = 128
	// MaxParamKeyBytes 限制参数键名与模板占位符键名的最大字节长度。
	MaxParamKeyBytes = 64
	// MaxTemplateBytes 限制单条消息模板或兜底文本的最大 UTF-8 字节长度。
	MaxTemplateBytes = 8192
)
View Source
const MaxLocaleTagBytes = 64

MaxLocaleTagBytes 限制单条语言/区域标签的最大字节长度。

Variables

This section is empty.

Functions

func BoundDisplayText

func BoundDisplayText(raw string, maxBytes int) string

BoundDisplayText 将任意外部字符串确定性净化为有效 UTF-8 并按完整 rune 边界裁剪至不超过 maxBytes 字节。 非法 UTF-8 字节序列以 Unicode 替换字符 U+FFFD 净化(不附加外层引号);maxBytes <= 0 或空串时返回空串。

func BoundNonBlankText

func BoundNonBlankText(raw string, maxBytes int) (string, bool)

BoundNonBlankText 对外部明细文本执行空白判定、UTF-8 净化与字节裁剪; 若原始或裁剪后文本为空或纯空白则返回 ("", false)。

func CollectFormatPlaceholderKeys

func CollectFormatPlaceholderKeys(tmpl string) []string

CollectFormatPlaceholderKeys 按与 FormatTemplate 100% 同构的宽容词法扫描 tmpl, 依首次出现顺序去重返回所有将被 FormatTemplate 识别的合法占位符键名(正确处理 {{ / }} / {{{k}}} 及前置畸形花括号)。

func FindDuplicateKey

func FindDuplicateKey[T any](items []T, keyOf func(T) (string, bool)) (string, bool)

FindDuplicateKey 在元素序列中按 keyOf 提取有效键名并检测首个重复键(小切片零堆分配)。

func FormatDurationNanos

func FormatDurationNanos(nanos int64) string

FormatDurationNanos 将纳秒整数确定性格式化为标准 Go 时长文本。

func FormatTemplate

func FormatTemplate(tmpl string, resolve func(string) (string, bool)) string

FormatTemplate 对模板执行单遍零反射 {key} 占位符替换,支持 {{ 与 }} 转义为字面花括号。 缺失键值或 resolve 为 nil 时原样保留 "{key}";畸形花括号片段按字面安全输出,永不 panic。

func HasMissingPlaceholder

func HasMissingPlaceholder(tmpl string, hasKey func(string) bool) bool

HasMissingPlaceholder 以 O(n) 零堆分配快路径扫描 tmpl,按与 FormatTemplate 100% 同构的词法规则 检查是否存在任一会被 FormatTemplate 消费但未被 hasKey 绑定的合法 {key} 占位符。

func HasValidLocaleSubtags

func HasValidLocaleSubtags(norm string) bool

HasValidLocaleSubtags 校验归一化后的区域标签非空且每个 "_" 分段均由非空小写 ASCII 字母或数字构成。

func HasValidParamWireShape

func HasValidParamWireShape[M any](w ParamWire[M]) bool

HasValidParamWireShape 校验 ParamWire 恰含 1 个非空变体载荷字段,且非 float64 变体不携带 prec。

func IsValidLocaleSuffix

func IsValidLocaleSuffix(s string) bool

IsValidLocaleSuffix 校验 .codeset 与 @modifier 后缀,严格限定 POSIX 字符集 [A-Za-z0-9_-]。

func IsValidMessageID

func IsValidMessageID(s string) bool

IsValidMessageID 校验消息标识是否为非空规范小写点分/下划线标识符([a-z][a-z0-9_.]* 且不含连续或尾部点号)。

func IsValidParamKey

func IsValidParamKey(k string) bool

IsValidParamKey 校验参数键名与占位符名称:长度 1..MaxParamKeyBytes,首字符为 ASCII 字母/数字/下划线, 其余字符仅限 ASCII 字母、数字、下划线、点或连字符。

func MatchNormalizedPrimary

func MatchNormalizedPrimary(norm string) (string, bool)

MatchNormalizedPrimary 校验归一化标签各子段均由非空小写 ASCII 字母数字构成,并返回主语言段。

func NormalizeLocaleCore

func NormalizeLocaleCore(raw string) (string, bool)

NormalizeLocaleCore 去除首尾空白、先剥离 @modifier 再剥离 .codeset,并将主体转为小写下划线形式。

func ParseDurationNanos

func ParseDurationNanos(raw string) (int64, error)

ParseDurationNanos 解析标准 Go 时长文本并返回纳秒整数。

func ReadCheckedObjectValue

func ReadCheckedObjectValue(dec *jsontext.Decoder, baseErr error) (jsontext.Value, error)

ReadCheckedObjectValue 读取完整 JSON 值并拒绝顶层 null 以及对象内任意显式 null 成员值, 防止类似 {"key":"k","kind":"string","str":"ok","int64":null} 的旁路载荷绕过指针非空校验。

func RejectNullObjectMembers

func RejectNullObjectMembers(raw jsontext.Value, baseErr error) error

RejectNullObjectMembers 扫描原始 JSON 文本,拒绝顶层 null 以及对象内任意显式 null 成员值。

func SanitizeTemplate

func SanitizeTemplate(tmpl string) string

SanitizeTemplate 将超长或包含非法 UTF-8 的模板确定性净化并按完整 rune 边界裁剪至 MaxTemplateBytes。

func UnmarshalCheckedObject

func UnmarshalCheckedObject[T any](dec *jsontext.Decoder, dst *T, baseErr error) error

UnmarshalCheckedObject 读取完整 JSON 对象值、拒绝顶层 null 与对象内任意显式 null 成员键, 随后以 RejectUnknownMembers(true) 严格反序列化至 dst。

Types

type ParamWire

type ParamWire[M any] struct {
	Key      string   `json:"key"`
	Kind     string   `json:"kind"`
	Str      *string  `json:"str,omitzero"`
	Int64    *int64   `json:"int64,omitzero"`
	Uint64   *uint64  `json:"uint64,omitzero"`
	Float64  *float64 `json:"float64,omitzero"`
	Prec     *int     `json:"prec,omitzero"`
	Bool     *bool    `json:"bool,omitzero"`
	Duration *string  `json:"duration,omitzero"`
	Message  *M       `json:"message,omitzero"`
}

ParamWire 是强类型消息参数 Param 的 JSON/v2 确定性线协议表示(泛型载荷 M 避免对父包类型形成循环依赖)。

type TemplateErr

type TemplateErr uint8

TemplateErr 表示模板严格语法解析的分类结果(零值 TemplateErrNone 表示通过)。

const (
	// TemplateErrNone 表示模板语法合法。
	TemplateErrNone TemplateErr = 0
	// TemplateErrInvalid 表示模板超长或包含非法 UTF-8 字节序列。
	TemplateErrInvalid TemplateErr = 1
	// TemplateErrUnclosed 表示模板存在未闭合的占位符左花括号。
	TemplateErrUnclosed TemplateErr = 2
	// TemplateErrUnescaped 表示模板存在未转义的孤立右花括号。
	TemplateErrUnescaped TemplateErr = 3
	// TemplateErrBadKey 表示模板占位符键名不满足参数键规范。
	TemplateErrBadKey TemplateErr = 4
)

func ParsePlaceholderKeys

func ParsePlaceholderKeys(tmpl string) ([]string, TemplateErr, string)

ParsePlaceholderKeys 执行严格模板语法校验与占位符扫描,返回升序去重后的占位符键名切片及错误分类码。

Jump to

Keyboard shortcuts

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