Small, testable display width implementation in Swift. Provides simple functions to measure display width of Unicode scalars, characters, and strings with an option to treat East Asian "Ambiguous" width as full-width.
Add to your Package.swift
:
dependencies: [
.package(url: "https://github.com/ainame/swift-displaywidth", from: "0.0.2")
]
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)
- Naming reference https://man7.org/linux/man-pages/man3/wcwidth.3.html
- Other langs