Skip to content

Commit 0a03aa9

Browse files
committed
xo-flatstring: undercap flastring_concat instead of char arrays
1 parent 6b43254 commit 0a03aa9

3 files changed

Lines changed: 34 additions & 4 deletions

File tree

example/ex1/ex1.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,12 +136,12 @@ main() {
136136
static_assert(sizeof(s14) == 7);
137137

138138
constexpr flatstring s15 = flatstring_concat(flatstring("hello"),
139-
", ",
140-
flatstring("world"));
139+
flatstring(", "),
140+
flatstring("world"));
141141
static_assert(s15.fixed_capacity == 13);
142142
static_assert(sizeof(s15) == 13);
143143

144-
constexpr auto s16 = xo::flatstring_concat("foo", "bar");
144+
constexpr auto s16 = xo::flatstring_concat(flatstring("foo"), flatstring("bar"));
145145

146146
static_assert(s16.fixed_capacity == 7);
147147

include/xo/flatstring/flatstring.hpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,18 @@ namespace xo {
452452
std::size_t pos = 0;
453453

454454
auto detail_concat = [ &pos, &result ](auto && arg) {
455-
constexpr auto count = (sizeof(arg) - sizeof(value_type)) / sizeof(value_type);
455+
/* tradeoff here:
456+
* 1. flatstring::size() is constexpr, so we can concat strings with size() < capacity().
457+
* (note flatstring::from_int() likely creates such strings)
458+
* 2. ..but no size() method on char arrays.
459+
* 3. std::size() not suitable: size of char array includes null terminator,
460+
* while flatstring.size() excludes it, and flatstring behavior is consistent with
461+
* std::string.size()
462+
* Consequence of using arg.size() here; have to wrap char arrays with
463+
* flatstring() to use them with flatstring_concat()
464+
*/
465+
auto count = arg.size();
466+
//constexpr auto count = (sizeof(arg) - sizeof(value_type)) / sizeof(value_type);
456467

457468
std::copy_n(/*arg.c_str()*/ static_cast<const char *>(arg), count, result.value_ + pos);
458469
pos += count;

utest/flatstring.test.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,25 @@ namespace xo {
231231

232232
REQUIRE(::strcmp(concat.c_str(), req_str.c_str()) == 0);
233233
}
234+
235+
#ifdef NOT_USING
236+
{
237+
auto concat4 = flatstring_concat(str,
238+
flatstring(text2),
239+
str,
240+
flatstring(text2));
241+
auto req_str = string(text) + string(text2) + string(text) + string(text2);
242+
243+
REQUIRE(::strcmp(concat4.c_str(), req_str.c_str()) == 0);
244+
}
245+
#endif
246+
247+
{
248+
auto concat4 = flatstring_concat(str, str2, str, str2);
249+
auto req_str = string(text) + string(text2) + string(text) + string(text2);
250+
251+
REQUIRE(::strcmp(concat4.c_str(), req_str.c_str()) == 0);
252+
}
234253
}
235254

236255
template <typename String>

0 commit comments

Comments
 (0)