Skip to content

Commit

Permalink
Remove display_width_with_tab variant
Browse files Browse the repository at this point in the history
  • Loading branch information
mtoohey31 committed Oct 30, 2022
1 parent a1ff94e commit 489f3b0
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 65 deletions.
4 changes: 2 additions & 2 deletions benchmarks/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ harness = false
path = "unfill.rs"

[[bench]]
name = "display_width_with_tab"
name = "display_width"
harness = false
path = "display_width_with_tab.rs"
path = "display_width.rs"

[dependencies]
textwrap = { path = "../", features = ["hyphenation"] }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ pub fn benchmark(c: &mut Criterion) {
text.push_str("\n\n\n\n");
assert_eq!(text.len(), 2800); // The size for reference.

c.bench_function("display_width_with_tab", |b| {
b.iter(|| textwrap::core::display_width_with_tab(&text, 2))
c.bench_function("display_width", |b| {
b.iter(|| textwrap::core::display_width(&text, 2))
});
}

Expand Down
20 changes: 10 additions & 10 deletions src/columns.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Functionality for wrapping text into columns.

use crate::core::display_width_with_tab;
use crate::core::display_width;
use crate::{wrap, Options};

/// Wrap text into columns with a given total width.
Expand All @@ -23,9 +23,9 @@ use crate::{wrap, Options};
/// # let columns = 2;
/// # let options = textwrap::Options::new(80);
/// let inner_width = options.width
/// - textwrap::core::display_width(left_gap)
/// - textwrap::core::display_width(right_gap)
/// - textwrap::core::display_width(middle_gap) * (columns - 1);
/// - textwrap::core::display_width(left_gap, options.tab_width)
/// - textwrap::core::display_width(right_gap, options.tab_width)
/// - textwrap::core::display_width(middle_gap, options.tab_width) * (columns - 1);
/// let column_width = inner_width / columns;
/// ```
///
Expand Down Expand Up @@ -77,9 +77,9 @@ where

let inner_width = options
.width
.saturating_sub(display_width_with_tab(left_gap, options.tab_width))
.saturating_sub(display_width_with_tab(right_gap, options.tab_width))
.saturating_sub(display_width_with_tab(middle_gap, options.tab_width) * (columns - 1));
.saturating_sub(display_width(left_gap, options.tab_width))
.saturating_sub(display_width(right_gap, options.tab_width))
.saturating_sub(display_width(middle_gap, options.tab_width) * (columns - 1));

let column_width = std::cmp::max(inner_width / columns, 1);
options.width = column_width;
Expand All @@ -94,9 +94,9 @@ where
match wrapped_lines.get(line_no + column_no * lines_per_column) {
Some(column_line) => {
line.push_str(column_line);
line.push_str(&" ".repeat(
column_width - display_width_with_tab(column_line, options.tab_width),
));
line.push_str(
&" ".repeat(column_width - display_width(column_line, options.tab_width)),
);
}
None => {
line.push_str(&" ".repeat(column_width));
Expand Down
53 changes: 19 additions & 34 deletions src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ fn ch_width(ch: char, tab_width: u8) -> usize {
/// ```
/// use textwrap::core::display_width;
///
/// assert_eq!(display_width("Café Plain"), 10);
/// assert_eq!(display_width("\u{1b}[31mCafé Rouge\u{1b}[0m"), 10);
/// assert_eq!(display_width("Café Plain", 0), 10);
/// assert_eq!(display_width("\u{1b}[31mCafé Rouge\u{1b}[0m", 0), 10);
/// ```
///
/// **Note:** When the `unicode-width` Cargo feature is disabled, the
Expand All @@ -113,11 +113,11 @@ fn ch_width(ch: char, tab_width: u8) -> usize {
/// ```
/// use textwrap::core::display_width;
///
/// assert_eq!(display_width("Cafe Plain"), 10);
/// assert_eq!(display_width("Cafe Plain", 0), 10);
/// #[cfg(feature = "unicode-width")]
/// assert_eq!(display_width("Cafe\u{301} Plain"), 10);
/// assert_eq!(display_width("Cafe\u{301} Plain", 0), 10);
/// #[cfg(not(feature = "unicode-width"))]
/// assert_eq!(display_width("Cafe\u{301} Plain"), 11);
/// assert_eq!(display_width("Cafe\u{301} Plain", 0), 11);
/// ```
///
/// ## Emojis and CJK Characters
Expand All @@ -129,8 +129,8 @@ fn ch_width(ch: char, tab_width: u8) -> usize {
/// ```
/// use textwrap::core::display_width;
///
/// assert_eq!(display_width("😂😭🥺🤣✨😍🙏🥰😊🔥"), 20);
/// assert_eq!(display_width("你好"), 4); // “Nǐ hǎo” or “Hello” in Chinese
/// assert_eq!(display_width("😂😭🥺🤣✨😍🙏🥰😊🔥", 0), 20);
/// assert_eq!(display_width("你好", 0), 4); // “Nǐ hǎo” or “Hello” in Chinese
/// ```
///
/// # Limitations
Expand All @@ -156,9 +156,9 @@ fn ch_width(ch: char, tab_width: u8) -> usize {
///
/// assert_eq!("👨‍🦰".chars().collect::<Vec<char>>(), ['\u{1f468}', '\u{200d}', '\u{1f9b0}']);
/// #[cfg(feature = "unicode-width")]
/// assert_eq!(display_width("👨‍🦰"), 4);
/// assert_eq!(display_width("👨‍🦰", 0), 4);
/// #[cfg(not(feature = "unicode-width"))]
/// assert_eq!(display_width("👨‍🦰"), 6);
/// assert_eq!(display_width("👨‍🦰", 0), 6);
/// ```
///
/// This happens because the grapheme consists of three code points:
Expand All @@ -178,22 +178,7 @@ fn ch_width(ch: char, tab_width: u8) -> usize {
/// [Unicode equivalence]: https://en.wikipedia.org/wiki/Unicode_equivalence
/// [CJK characters]: https://en.wikipedia.org/wiki/CJK_characters
/// [emoji modifier sequences]: https://unicode.org/emoji/charts/full-emoji-modifiers.html
pub fn display_width(text: &str) -> usize {
display_width_with_tab(text, 0)
}

/// Compute the display width of `text` in the same was as
/// `display_width`, while allowing you to customize how wide
/// a tab character will be considered to be.
///
/// # Examples
///
/// ```
/// use textwrap::core::display_width_with_tab;
///
/// assert_eq!(display_width_with_tab("Café \t Plain", 4), 15);
/// ```
pub fn display_width_with_tab(text: &str, tab_width: u8) -> usize {
pub fn display_width(text: &str, tab_width: u8) -> usize {
let mut chars = text.chars();
let mut width = 0;
while let Some(ch) = chars.next() {
Expand Down Expand Up @@ -272,7 +257,7 @@ impl<'a> Word<'a> {
let trimmed = word.trim_end();
Word {
word: trimmed,
width: display_width_with_tab(trimmed, tab_width),
width: display_width(trimmed, tab_width),
whitespace: &word[trimmed.len()..],
penalty: "",
tab_width,
Expand Down Expand Up @@ -342,7 +327,7 @@ impl Fragment for Word<'_> {

#[inline]
fn whitespace_width(&self) -> f64 {
display_width_with_tab(self.whitespace, self.tab_width) as f64
display_width(self.whitespace, self.tab_width) as f64
}

// We assume the penalty is `""` or `"-"`. This allows us to
Expand Down Expand Up @@ -433,32 +418,32 @@ mod tests {
#[test]
fn display_width_works() {
assert_eq!("Café Plain".len(), 11); // “é” is two bytes
assert_eq!(display_width("Café Plain"), 10);
assert_eq!(display_width("\u{1b}[31mCafé Rouge\u{1b}[0m"), 10);
assert_eq!(display_width("Café Plain", 0), 10);
assert_eq!(display_width("\u{1b}[31mCafé Rouge\u{1b}[0m", 0), 10);
}

#[test]
fn display_width_narrow_emojis() {
#[cfg(feature = "unicode-width")]
assert_eq!(display_width("⁉"), 1);
assert_eq!(display_width("⁉", 0), 1);

// The ⁉ character is above DOUBLE_WIDTH_CUTOFF.
#[cfg(not(feature = "unicode-width"))]
assert_eq!(display_width("⁉"), 2);
assert_eq!(display_width("⁉", 0), 2);
}

#[test]
fn display_width_narrow_emojis_variant_selector() {
#[cfg(feature = "unicode-width")]
assert_eq!(display_width("⁉\u{fe0f}"), 1);
assert_eq!(display_width("⁉\u{fe0f}", 0), 1);

// The variant selector-16 is also counted.
#[cfg(not(feature = "unicode-width"))]
assert_eq!(display_width("⁉\u{fe0f}"), 4);
assert_eq!(display_width("⁉\u{fe0f}", 0), 4);
}

#[test]
fn display_width_emojis() {
assert_eq!(display_width("😂😭🥺🤣✨😍🙏🥰😊🔥"), 20);
assert_eq!(display_width("😂😭🥺🤣✨😍🙏🥰😊🔥", 0), 20);
}
}
7 changes: 2 additions & 5 deletions src/refill.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Functionality for unfilling and refilling text.

use crate::core::display_width_with_tab;
use crate::core::display_width;
use crate::line_ending::NonEmptyLines;
use crate::{fill, LineEnding, Options};

Expand Down Expand Up @@ -64,10 +64,7 @@ pub fn unfill(text: &str) -> (String, Options<'_>) {

let mut options = Options::new(0);
for (idx, line) in text.lines().enumerate() {
options.width = std::cmp::max(
options.width,
display_width_with_tab(line, options.tab_width),
);
options.width = std::cmp::max(options.width, display_width(line, options.tab_width));
let without_prefix = line.trim_start_matches(prefix_chars);
let prefix = &line[..line.len() - without_prefix.len()];

Expand Down
6 changes: 3 additions & 3 deletions src/word_splitters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
//! across lines. The [`WordSplitter`] enum defines this
//! functionality.

use crate::core::{display_width_with_tab, Word};
use crate::core::{display_width, Word};

/// The `WordSplitter` enum describes where words can be split.
///
Expand Down Expand Up @@ -193,7 +193,7 @@ where
let need_hyphen = !word[..idx].ends_with('-');
let w = Word {
word: &word.word[prev..idx],
width: display_width_with_tab(&word[prev..idx], word.tab_width),
width: display_width(&word[prev..idx], word.tab_width),
whitespace: "",
penalty: if need_hyphen { "-" } else { "" },
tab_width: word.tab_width,
Expand All @@ -205,7 +205,7 @@ where
if prev < word.word.len() || prev == 0 {
let w = Word {
word: &word.word[prev..],
width: display_width_with_tab(&word[prev..], word.tab_width),
width: display_width(&word[prev..], word.tab_width),
..word
};
prev = word.word.len() + 1;
Expand Down
16 changes: 7 additions & 9 deletions src/wrap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use std::borrow::Cow;

use crate::core::{break_words, display_width_with_tab, Word};
use crate::core::{break_words, display_width, Word};
use crate::word_splitters::split_words;
use crate::Options;

Expand Down Expand Up @@ -217,14 +217,12 @@ pub(crate) fn wrap_single_line_slow_path<'a>(
options: &Options<'_>,
lines: &mut Vec<Cow<'a, str>>,
) {
let initial_width = options.width.saturating_sub(display_width_with_tab(
options.initial_indent,
options.tab_width,
));
let subsequent_width = options.width.saturating_sub(display_width_with_tab(
options.subsequent_indent,
options.tab_width,
));
let initial_width = options
.width
.saturating_sub(display_width(options.initial_indent, options.tab_width));
let subsequent_width = options
.width
.saturating_sub(display_width(options.subsequent_indent, options.tab_width));
let line_widths = [initial_width, subsequent_width];

let words = options.word_separator.find_words(line).map(|mut w| {
Expand Down

0 comments on commit 489f3b0

Please sign in to comment.