Skip to content

Commit

Permalink
wrap string without a need to append extra space
Browse files Browse the repository at this point in the history
  • Loading branch information
artpaul committed Jun 9, 2022
1 parent c1b8f43 commit 593f883
Showing 1 changed file with 26 additions and 16 deletions.
42 changes: 26 additions & 16 deletions include/cxxopts.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1767,7 +1767,7 @@ class options {
cxx_string result;

if (!empty(help_string_)) {
result += wrap_string(help_string_ + " ", 0, width_);
result += wrap_string(help_string_, 0, width_);
result += '\n';
}

Expand All @@ -1791,7 +1791,7 @@ class options {

if (!empty(footer_)) {
result += "\n";
result += wrap_string(to_local_string(footer_ + " "), 0, width_);
result += wrap_string(to_local_string(footer_), 0, width_);
}

return to_utf8_string(result);
Expand Down Expand Up @@ -1905,7 +1905,7 @@ class options {
size_t start,
size_t allowed,
bool tab_expansion) const {
auto desc = o.desc;
cxx_string desc = o.desc;

if (o.has_default && (!o.is_boolean || o.default_value != "false")) {
if (!o.default_value.empty()) {
Expand All @@ -1919,9 +1919,7 @@ class options {
desc = expand_tab_character(desc);
}

desc += " ";

return wrap_string(desc, start, allowed);
return wrap_string(std::move(desc), start, allowed);
}

cxx_string help_one_group(const std::string& group_name) const {
Expand Down Expand Up @@ -2027,18 +2025,24 @@ class options {
}

cxx_string wrap_string(const cxx_string& desc,
size_t start,
size_t allowed) const {
const size_t start,
const size_t allowed) const {
cxx_string result;

// Nothing to wrap for empty string.
if (std::begin(desc) == std::end(desc)) {
return result;
}

auto current = std::begin(desc);
auto previous = current;
auto start_line = current;
auto last_space = current;

auto size = size_t{};

bool only_whitespace = true;

result.reserve(desc.size());

for (; current != std::end(desc); ++current) {
bool append_new_line = false;

Expand All @@ -2049,11 +2053,13 @@ class options {
if (!std::isblank(*current)) {
only_whitespace = false;
}

while (*current == '\n') {
previous = current;
++current;
// Skip all line feed characters.
if (*current == '\n') {
append_new_line = true;
do {
previous = current;
++current;
} while (current != std::end(desc) && *current == '\n');
}

if (!append_new_line && size >= allowed) {
Expand Down Expand Up @@ -2083,13 +2089,17 @@ class options {
size = 0;
}

if (current == std::end(desc)) {
break;
}

previous = current;
++size;
}

// append whatever is left but ignore whitespace
// Append whatever is left but ignore whitespace.
if (!only_whitespace) {
string_append(result, start_line, previous);
string_append(result, start_line, current);
}

return result;
Expand Down

0 comments on commit 593f883

Please sign in to comment.