unicode

package module
v0.0.0-...-8b45bad Latest Latest
Warning

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

Go to latest
Published: Jan 13, 2026 License: MIT Imports: 7 Imported by: 1

Documentation

Overview

Package unicode 提供与 unicode 相关的部分功能

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func CutSetWithMaxDisplayWidth

func CutSetWithMaxDisplayWidth(s string, maxWidth int) string

CutSetWithMaxDisplayWidth 返回一个字符串,该字符串在东亚字符集中的展示宽度不超过 maxWidth

func EastAsianDisplayWidth

func EastAsianDisplayWidth(s string, opts ...Option) int

EastAsianDisplayWidth 返回一段包含东亚字符和 emoji 字符串在等宽字符集中的展示宽度。 其中 opts 暂时只支持 WithTabWidth

func EastAsianStringer

func EastAsianStringer(v interface{}, asciiWidth int, opts ...Option) fmt.Stringer

EastAsianStringer 返回一个 fmt.Stringer 用于按照东亚字符真正的字符宽度进行展示

Reference:

- golang获取字符的宽度(East_Asian_Width) - http://www.nbtuan.vip/2017/05/10/golang-char-width/ - Unicode中文和特殊字符的编码范围 - https://www.cnblogs.com/sosoft/p/3456631.html

func TrimUTF8

func TrimUTF8(orig string, tail string, maxRunes, maxBytes int, opts ...Option) string

TrimUTF8 按照 maxRunes 和 maxBytes 值限制字符串的长度,如果字符串长度超过 maxRunes 或 maxBytes, 则截取字符串的末尾, 并添加 tail 字符串。其中 maxXXX 取 <= 0 表示不限制, tail 取 "" 表示不添加尾部。本函数确保加了 tail 之后也不超 maxXXX 值。

请确保传入的 orig 和 tail 均为 UTF8 字符串, 否则逻辑会出现意想不到的错误

Options 暂时只支持 WithDebugger

Example

ExampleTrimUTF8 示例函数

// 基本用法:限制字符数
result := TrimUTF8("Hello World", "...", 8, 0)
fmt.Println(result)

// 限制字节数
result = TrimUTF8("你好世界测试", "...", 0, 14)
fmt.Println(result)

// 同时限制字符数和字节数
result = TrimUTF8("Hello 世界", "...", 10, 15)
fmt.Println(result)
Output:

Hello...
你好世...
Hello 世界

Types

type Align

type Align int

Align 表示对齐方式

const (
	AlignRight Align = iota
	AlignLeft
	AlignCenter
)

type Option

type Option func(*option)

func WithAlign

func WithAlign(align Align) Option

WithAlign 返回关于对齐方式的选项

func WithBlank

func WithBlank(blank string) Option

WithBlank 表示空格填充符号

func WithDebugger

func WithDebugger(f func(string, ...any)) Option

WithDebugger 表示指定调试器

func WithTabWidth

func WithTabWidth(width int) Option

WithTabWidth 表示将 tab 替换成的空格数。如果不指定则视为一个字符, 显示效果取决于终端

Jump to

Keyboard shortcuts

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