A portable/cross-platform implementation of wcwidth(3)
with up-to-date Unicode spec.
This project has own Unicode data tables generated from following files.
- https://unicode.org/Public/17.0.0/ucd/UnicodeData.txt
- https://unicode.org/Public/17.0.0/ucd/EastAsianWidth.txt
Instead of this library, there's wcwidth
imported with import Darwin
, import Musl
, or import Glibc
.
- https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man3/wcwidth.3.html
- https://www.gnu.org/software/gnulib/manual/html_node/wcwidth.html
- https://git.musl-libc.org/cgit/musl/tree/src/ctype/wcwidth.c
If that meets your requirements, you should just use it. However, this project has following superior points.
- Portable/Cross-platform implementation that doesn't require a C library nor even Foundation
- Up-to-date Unicode spec
- Better support of Unicode grapheme clusters
- Swift-friendly API
Add to your Package.swift
:
dependencies: [
.package(url: "https://github.com/ainame/swift-displaywidth", from: "0.0.3")
]
Then:
import DisplayWidth
// call as function
let displayWidth = DisplayWidth()
displayWidth("A") // 1
displayWidth("あ") // 2
displayWidth("👩💻") // 2
displayWidth("e\u{0301}") // 1 (e + combining acute)
// If your environment treat ambiguous chars as full-width,
// you can set this option.
let displayWidth = DisplayWidth(treatAmbiguousAsFullWidth: true)
- https://man7.org/linux/man-pages/man3/wcwidth.3.html
- Other langs
- https://emonkak.pages.dev/articles/wcwidth/
- This project (to avoid using
wcwidth(3)
) is against to this blog post but I took ideas around full-width symbols
- This project (to avoid using