Giter Club home page Giter Club logo

unicode-truncate's Introduction

unicode-truncate

Unicode-aware algorithm to pad or truncate str in terms of displayed width.

crates.io Documentation Build Status

Examples

Safely truncate string to display width even not at character boundaries.

use unicode_truncate::UnicodeTruncateStr;

fn main() {
    assert_eq!("你好吗".unicode_truncate(5), ("你好", 4));
}

Making sure the string is displayed in exactly number of columns by combining padding and truncating.

use unicode_truncate::UnicodeTruncateStr;
use unicode_truncate::Alignment;
use unicode_width::UnicodeWidthStr;

fn main() {
    let str = "你好吗".unicode_pad(5, Alignment::Left, true);
    assert_eq!(str, "你好 ");
    assert_eq!(str.width(), 5);
}

Features

unicode-truncate can be built without std by disabling the default feature std. However, in that case unicode_truncate::UnicodeTruncateStr::unicode_pad won't be available because it depends on std::string::String and std::borrow::Cow.

unicode-truncate's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

unicode-truncate's Issues

Action Required: Fix Renovate Configuration

There is an error with this repository's Renovate configuration that needs to be fixed. As a precaution, Renovate will stop PRs until it is resolved.

Error type: Cannot find preset's package (:group:allNonMajor)

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Detected dependencies

cargo
Cargo.toml
  • itertools 0.12
  • unicode-segmentation 1
  • unicode-width 0.1
  • criterion 0.5
github-actions
.github/workflows/publish.yml
  • actions/checkout v4
  • MarcoIeni/release-plz-action v0.5
.github/workflows/rust.yml
  • actions/checkout v4
  • Swatinem/rust-cache v2
  • actions/checkout v4
  • Swatinem/rust-cache v2
  • actions/checkout v4
  • Swatinem/rust-cache v2
  • actions/checkout v4
  • Swatinem/rust-cache v2
  • actions/checkout v4
  • Swatinem/rust-cache v2

  • Check this box to trigger a request for Renovate to run again on this repository

unicode control characters trigger `debug_assert_eq`

Hello,

Thanks for writing the truncation code ^^

During testing of this library, I noticed that carefully crafted inputs containing control caracters can trigger an assertion. This only happens in in debug mode => not a runtime panic for release. The used character is End of Medium.

The assertion is located here

debug_assert_eq!(result.width(), new_width);

A minimal (failing) testcase is the following

#[cfg(test)]
mod tests {
  #[test]
  fn zero_width_control_char() {
      std::assert_eq!("\u{0019}".unicode_truncate(2), ("\u{0019}", 0));
  }
}

This happens because at said point (expanded for better readability)

  • let (byte_index, new_width) = (1, 0);
    
  • let result = "\u{0019}".get(..1).unwrap();
    
  • let result = "\u{0019}";
    
  • => thus the result.width() (=1) is larger than the new_width (=0).

This case happens as (self.len(), 0) is appended to the interator.
This makes the iterator (as intended by the doccomment) eager for trailing zero width characters.
=> The asserted invariant does not hold anymore.

A possible fix would be to change the assertion to

- debug_assert_eq!(result.width(), new_width);
+ debug_assert!(result.width() >= new_width);

I can provide a PR with this fix if you'd prefer this

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.