Boost logo

Boost :

Subject: Re: [boost] Variadic append for std::string
From: Christof Donat (cd_at_[hidden])
Date: 2017-02-19 08:29:25


Hi,

Am 17.02.2017 13:22, schrieb Olaf van der Spek via Boost:
> So, we've seen a lot of nice ideas, who is going to implement his
> ideas?

I am currently trying to create a proof of concept. The API now looks
like this:

auto s = std::string{};

replace(s).with(to_string(42));
append(hex(42).to(s);
assert(s == "422A");

replace(s).with(concat(42, " "s, hex(42)));
assert(s == "42 2A");

to_string() and hex() is boost::spirit::karma generators and therefore
are probably pretty fast. replace().with() and append().to() both return
a reference to the "string" they have been working on and can take a
rvalue reference to that string. So this will work as well without any
unnecessary copies:

auto s = replace(""s).with(to_string(42));
assert(s == "42");
auto t = append(hex(42)).to("Test test "s);
assert(t == "Test test 2A");

auto the_answer_to_everything() -> std::string {
     return append(to_string(42)).to("The ansert to everything is "s);
}

The functions to_string(), hex(), concat(), join(), format(), etc.
return char ranges that can be iterated as well. e.g. for direct output:

auto r = hex();
std::copy(begin(r), end(r), std::ostream_iterator<char>(std::cout));

The only restrictions, that append() puts on the "string" it appends to
is, that std::back_inserter() has to be available and char has to be
assignable to the value type of that back_inserter_iterator. For
replace(), the "string" also has to provide a member function clear().
It works on e.g. std::vector<char>, or std::list<int> as well.

My current implementation does not care about wide chars, or utf8. I am
happy, when it works with simple chars. At the moment I am struggling
with concat() and I haven't yet started with join() and format()

Plans:

1. get concat() and join() working
2. extend replace().with() and append().to() to implicitly use
to_string() on everything that is not an iterable char range
3. extend append().to() with a variant, that takes an output iterator
instead of a "string" like object
4. add concept checks for better error messages
5. think about wide chars and utf8
6. try with format()
7. think how we can determine the size of the resulting string in
advance and call resize() in the appender and the replacer before we
iterate over the generating range, if available.

Since for me this is a hobby project, things are going slow. I don't
have too much time, I can spend on it, but it will be helpful for an
other, bigger hobby project. If there is someone, who needs such a
library more urgently, I am happy to share my code, so he can help. I
just have to clean it up a little bit before.

Christof


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk