Skip to content

Commit 599d78b

Browse files
committed
utils.h: added helpers for efficient string concatination [skip ci]
1 parent 8cb66cf commit 599d78b

3 files changed

Lines changed: 174 additions & 2 deletions

File tree

lib/tokenize.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2418,8 +2418,7 @@ namespace {
24182418
while (scope && scope->parent) {
24192419
if (scope->name.empty())
24202420
break;
2421-
fullName.insert(0, " :: ");
2422-
fullName.insert(0, scope->name);
2421+
utils::string::prepend(fullName, scope->name, " :: ");
24232422
scope = scope->parent;
24242423
}
24252424
}

lib/utils.h

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,57 @@ namespace utils {
436436
return result;
437437
};
438438
}
439+
440+
namespace string
441+
{
442+
template<size_t size>
443+
void concat(std::string &str, const char (&data)[size])
444+
{
445+
str.append(data, size-1);
446+
}
447+
448+
inline void concat(std::string &str, char c)
449+
{
450+
str.append(1, c);
451+
}
452+
453+
template<typename T>
454+
void concat(std::string &str, T t)
455+
{
456+
str.append(t);
457+
}
458+
459+
template<typename T, typename... Args>
460+
void concat(std::string& str, T t, Args... args)
461+
{
462+
concat(str, t);
463+
concat(str, args...);
464+
}
465+
466+
template<size_t size>
467+
void prepend(std::string &str, const char (&data)[size])
468+
{
469+
str.insert(0, data, size-1);
470+
}
471+
472+
inline void prepend(std::string &str, char c)
473+
{
474+
str.insert(0, 1, c);
475+
}
476+
477+
template<typename T>
478+
void prepend(std::string &str, T t)
479+
{
480+
str.insert(0, t);
481+
}
482+
483+
template<typename T, typename... Args>
484+
void prepend(std::string& str, T t, Args... args)
485+
{
486+
prepend(str, args...);
487+
prepend(str, t);
488+
}
489+
}
439490
}
440491

441492
#endif

test/testutils.cpp

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ class TestUtils : public TestFixture {
4545
TEST_CASE(as_const);
4646
TEST_CASE(memoize);
4747
TEST_CASE(endsWith);
48+
TEST_CASE(stringConcat);
49+
TEST_CASE(stringPrepend);
4850
}
4951

5052
void isValidGlobPattern() const {
@@ -575,6 +577,126 @@ class TestUtils : public TestFixture {
575577
ASSERT(!::endsWith("tes", "test"));
576578
ASSERT(!::endsWith("2test", "2"));
577579
}
580+
581+
void stringConcat() const
582+
{
583+
{
584+
std::string s = "0";
585+
utils::string::concat(s, "a");
586+
utils::string::concat(s, "b");
587+
utils::string::concat(s, "c");
588+
ASSERT_EQUALS("0abc", s);
589+
}
590+
{
591+
std::string s = "0";
592+
utils::string::concat(s, "a", "b", "c");
593+
ASSERT_EQUALS("0abc", s);
594+
}
595+
{
596+
const std::string a_str = "a";
597+
const std::string b_str = "b";
598+
const std::string c_str = "c";
599+
{
600+
std::string s = "0";
601+
utils::string::concat(s, a_str);
602+
utils::string::concat(s, b_str);
603+
utils::string::concat(s, c_str);
604+
ASSERT_EQUALS("0abc", s);
605+
}
606+
{
607+
std::string s = "0";
608+
utils::string::concat(s, a_str, b_str, c_str);
609+
ASSERT_EQUALS("0abc", s);
610+
}
611+
}
612+
{
613+
std::string s = "0";
614+
utils::string::concat(s, 'a');
615+
utils::string::concat(s, 'b');
616+
utils::string::concat(s, 'c');
617+
ASSERT_EQUALS("0abc", s);
618+
}
619+
{
620+
std::string s = "0";
621+
utils::string::concat(s, 'a', 'b', 'c');
622+
ASSERT_EQUALS("0abc", s);
623+
}
624+
{
625+
const std::string a_str = "a";
626+
{
627+
std::string s = "0";
628+
utils::string::concat(s, a_str);
629+
utils::string::concat(s, 'b');
630+
utils::string::concat(s, "c");
631+
ASSERT_EQUALS("0abc", s);
632+
}
633+
{
634+
std::string s = "0";
635+
utils::string::concat(s, a_str, 'b', "c");
636+
ASSERT_EQUALS("0abc", s);
637+
}
638+
}
639+
}
640+
641+
void stringPrepend() const
642+
{
643+
{
644+
std::string s = "0";
645+
utils::string::prepend(s, "a");
646+
utils::string::prepend(s, "b");
647+
utils::string::prepend(s, "c");
648+
ASSERT_EQUALS("cba0", s);
649+
}
650+
{
651+
std::string s = "0";
652+
utils::string::prepend(s, "a", "b", "c");
653+
ASSERT_EQUALS("abc0", s);
654+
}
655+
{
656+
const std::string a_str = "a";
657+
const std::string b_str = "b";
658+
const std::string c_str = "c";
659+
{
660+
std::string s = "0";
661+
utils::string::prepend(s, a_str);
662+
utils::string::prepend(s, b_str);
663+
utils::string::prepend(s, c_str);
664+
ASSERT_EQUALS("cba0", s);
665+
}
666+
{
667+
std::string s = "0";
668+
utils::string::prepend(s, a_str, b_str, c_str);
669+
ASSERT_EQUALS("abc0", s);
670+
}
671+
}
672+
{
673+
std::string s = "0";
674+
utils::string::prepend(s, 'a');
675+
utils::string::prepend(s, 'b');
676+
utils::string::prepend(s, 'c');
677+
ASSERT_EQUALS("cba0", s);
678+
}
679+
{
680+
std::string s = "0";
681+
utils::string::prepend(s, 'a', 'b', 'c');
682+
ASSERT_EQUALS("abc0", s);
683+
}
684+
{
685+
const std::string a_str = "a";
686+
{
687+
std::string s = "0";
688+
utils::string::prepend(s, a_str);
689+
utils::string::prepend(s, 'b');
690+
utils::string::prepend(s, "c");
691+
ASSERT_EQUALS("cba0", s);
692+
}
693+
{
694+
std::string s = "0";
695+
utils::string::prepend(s, a_str, 'b', "c");
696+
ASSERT_EQUALS("abc0", s);
697+
}
698+
}
699+
}
578700
};
579701

580702
REGISTER_TEST(TestUtils)

0 commit comments

Comments
 (0)