diff --git a/src/tests/util/format.cpp b/src/tests/util/format.cpp index dcda82fa9c..21eb7827ff 100644 --- a/src/tests/util/format.cpp +++ b/src/tests/util/format.cpp @@ -34,6 +34,10 @@ static void tst1() { format f10; f10 += f6 + f5 + f3; format f11 = above(f1, above(above(f2, f3), f4)); + format f12 = paren(format{format("a"), + format("b"), + format("c"), + format("d")}); std::vector > v = {{"f1", f1}, @@ -47,6 +51,7 @@ static void tst1() { {"f9", f9}, {"f10", f10}, {"f11", f11}, + {"f12", f12}, }; std::for_each(v.begin(), v.end(), @@ -56,11 +61,17 @@ static void tst1() { << "--------------------" << endl << endl; }); -// std::vector ss = {"Last", "weekend's", "revelation", "that", "J.K.", "Rowling", "is", "the", "author", "of", "the", "critically", "acclaimed", "and", "--", "until", "now", "--", "commercially", "unsuccessful", "crime", "novel", "The", "Cuckoo's", "Calling", "has", "electrified", "the", "book", "world", "and", "solidified", "Rowling's", "reputation", "as", "a", "genuine", "writing", "talent:", "After", "all,", "if", "she", "can", "impress", "the", "critics", "without", "the", "benefit", "of", "her", "towering", "reputation,", "then", "surely", "her", "success", "is", "deserved."}; - std::vector ss = {"Last", "weekend's", "revelation", "that", "J.K.", "Rowling", "is", "the", "author", "of", "the", "critically", "acclaimed"}; + std::vector ss = {"Last", "weekend's", "revelation", "that", "J.K.", "Rowling", "is", "the", "author", "of", "the", "critically", "acclaimed", "and", "--", "until", "now", "--", "commercially", "unsuccessful", "crime", "novel", "The", "Cuckoo's", "Calling", "has", "electrified", "the", "book", "world", "and", "solidified", "Rowling's", "reputation", "as", "a", "genuine", "writing", "talent:", "After", "all,", "if", "she", "can", "impress", "the", "critics", "without", "the", "benefit", "of", "her", "towering", "reputation,", "then", "surely", "her", "success", "is", "deserved."}; - std::vector sl = {f1, f2, f3, f4}; + cout << accumulate(++ss.begin(), + ss.end(), + format(*ss.begin()), + [](format const & result, std::string const & s) { + return wrap(result, format(s)); + }); + + std::vector sl = {f1, f2, f3, f4, f5, f6, f7, f8, f9, f10, f11, f12}; cout << "fill" << endl; cout << std::string(40, '=') << endl; diff --git a/src/util/format.cpp b/src/util/format.cpp index f7d70d70f9..d2c8bfd370 100644 --- a/src/util/format.cpp +++ b/src/util/format.cpp @@ -115,6 +115,9 @@ format bracket(std::string const l, format const & x, std::string const r) { line(), format(r)}); } +format paren(format const & x) { + return bracket("(", x, ")"); +} // wrap = <+/> // wrap x y = x <> (text " " :<|> line) <> y @@ -127,8 +130,9 @@ bool format::fits(int w, sexpr const & s) { lean_assert(is_list(s)); if (is_nil(s)) return true; - if (w < 0) + if (w < 0) { return false; + } sexpr const & x = car(s); switch (sexpr_kind(x)) { @@ -139,10 +143,12 @@ bool format::fits(int w, sexpr const & s) { case format_kind::TEXT: { size_t l = sexpr_text_length(x); - if(l - w > 0) + if(w - static_cast(l) < 0) { return false; - else - return fits(w - l, cdr(s)); + } + else { + return fits(w - static_cast(l), cdr(s)); + } } case format_kind::LINE: return true; @@ -204,13 +210,14 @@ sexpr format::be(unsigned w, unsigned k, sexpr const & s, sexpr const & r) { { sexpr const & x = sexpr_choice_1(v); sexpr const & y = sexpr_choice_2(v);; - sexpr const & s1 = be(w, k, sexpr(sexpr(i, x), z), r); - if (fits(w - k, s1)) { - return s1; - } else { - sexpr const & s2 = be(w, k, sexpr(sexpr(i, y), z), r); - return s2; + int d = static_cast(w) - static_cast(k); + if(d >= 0) { + sexpr const & s1 = be(w, k, sexpr(sexpr(i, x), z), r); + if (fits(d, s1)) + return s1; } + sexpr const & s2 = be(w, k, sexpr(sexpr(i, y), z), r); + return s2; } } lean_unreachable(); diff --git a/src/util/format.h b/src/util/format.h index f8215d839f..6d26319f34 100644 --- a/src/util/format.h +++ b/src/util/format.h @@ -196,6 +196,7 @@ format line(); format group(format const & f); format above(format const & f1, format const & f2); format bracket(std::string const l, format const & x, std::string const r); +format paren(format const & x); format wrap(format const & f1, format const & f2); template