在Go语言中是否有与wcwidth()函数等效的函数?

huangapple go评论70阅读模式
英文:

Is there an equivalent to the wcwidth() function in Go?

问题

POSIX函数wcwidth()用于计算在终端上打印时给定wchar_t的宽度。例如,wcwidth(L'A')返回1wcwidth(L'字')返回2,等等。还有一个函数wcswidth()用于计算整个字符串的宽度,如果存在组合重音符号,则这个函数非常有用。

Go标准库或附加库中是否存在类似的函数?如果不存在,是否有一种简单的方法来创建一个足够相似的函数?

英文:

The POSIX function wcwidth() computes the width of a given wchar_t when printed on a terminal. For instance, wcwidth(L'A') returns 1, wcwidth(L'字') returns 2, etc. There is also a function wcswidth() which computes the width of an entire string—this is useful if combining accents are present.

Does a similar function exist in the Go standard library or the supplementary libraries? If not, is there an easy way to make something sufficiently similar?

答案1

得分: 8

在Go标准库或附加库中是否存在类似的功能?

我认为最流行的库是go-runewidth

示例代码:

package main

import (
  "github.com/mattn/go-runewidth"
)

func main() {
  println(runewidth.StringWidth("A"))   // 输出 1
  println(runewidth.StringWidth("字"))  // 输出 2
}

希望对你有帮助!

英文:

> Does a similar function exist in the Go standard library or the supplementary libraries?

I believe the most popular library for this is go-runewidth.

Example:

package main

import (
  "github.com/mattn/go-runewidth"
)

func main() {
  println(runewidth.StringWidth("A"))   // prints 1
  println(runewidth.StringWidth("字"))  // prints 2
}

huangapple
  • 本文由 发表于 2015年1月1日 11:52:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/27728157.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定