Skip to content

Make getopts count (and thus align/linewrap) in terms of codepoints not ... #8710

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 33 additions & 5 deletions src/libextra/getopts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,7 @@ pub enum FailType {
pub mod groups {
use getopts::{HasArg, Long, Maybe, Multi, No, Occur, Opt, Optional, Req};
use getopts::{Short, Yes};
use std::str;

/** one group of options, e.g., both -h and --help, along with
* their shared description and properties
Expand Down Expand Up @@ -689,9 +690,9 @@ pub mod groups {
}
}

// FIXME: #5516
// FIXME: #5516 should be graphemes not codepoints
// here we just need to indent the start of the description
let rowlen = row.len();
let rowlen = str::count_chars(row, 0, row.len());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason that this isn't row.char_len()?

Other than that, r=me

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

none other than count_chars is the first one my searches through str.rs found. Will fix.

if rowlen < 24 {
do (24 - rowlen).times {
row.push_char(' ')
Expand All @@ -707,14 +708,14 @@ pub mod groups {
desc_normalized_whitespace.push_char(' ');
}

// FIXME: #5516
// FIXME: #5516 should be graphemes not codepoints
let mut desc_rows = ~[];
do each_split_within(desc_normalized_whitespace, 54) |substr| {
desc_rows.push(substr.to_owned());
true
};

// FIXME: #5516
// FIXME: #5516 should be graphemes not codepoints
// wrapped description
row.push_str(desc_rows.connect(desc_sep));

Expand Down Expand Up @@ -798,7 +799,7 @@ pub mod groups {
cont
};

ss.iter().enumerate().advance(|x| machine(x));
ss.char_offset_iter().advance(|x| machine(x));

// Let the automaton 'run out' by supplying trailing whitespace
while cont && match state { B | C => true, A => false } {
Expand Down Expand Up @@ -1580,4 +1581,31 @@ Options:
debug!("generated: <<%s>>", usage);
assert!(usage == expected)
}

#[test]
fn test_groups_usage_description_multibyte_handling() {
let optgroups = ~[
groups::optflag("k", "k\u2013w\u2013",
"The word kiwi is normally spelled with two i's"),
groups::optflag("a", "apple",
"This \u201Cdescription\u201D has some characters that could \
confuse the line wrapping; an apple costs 0.51€ in some parts of Europe."),
];

let expected =
~"Usage: fruits

Options:
-k --k–w– The word kiwi is normally spelled with two i's
-a --apple This “description” has some characters that could
confuse the line wrapping; an apple costs 0.51€ in
some parts of Europe.
";

let usage = groups::usage("Usage: fruits", optgroups);

debug!("expected: <<%s>>", expected);
debug!("generated: <<%s>>", usage);
assert!(usage == expected)
}
}