Skip to content

ainame/swift-displaywidth

Repository files navigation

swift-displaywidth

Swift Version Swift Package Manager License GitHub Release Build Status

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.

Why use this?

Instead of this library, there's wcwidth imported with import Darwin, import Musl, or import Glibc.

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

Usage

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)

Links

About

wcwidth implementation with the latest Unicode spec

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •  

Languages