Line | Branch | Exec | Source |
---|---|---|---|
1 | #ifndef LYTHON_UTILITIES_STRINGS_HEADER | ||
2 | #define LYTHON_UTILITIES_STRINGS_HEADER | ||
3 | |||
4 | #include "dtypes.h" | ||
5 | #include <algorithm> | ||
6 | |||
7 | namespace lython { | ||
8 | |||
9 | String join(String const& sep, Array<String> const& strs); | ||
10 | |||
11 | Set<char> const& strip_defaults(); | ||
12 | |||
13 | String strip(String const& v); | ||
14 | |||
15 | String join(String const& sep, Array<struct ExprNode*> const& exprs); | ||
16 | |||
17 | template <typename T> | ||
18 | String join(String const& sep, Array<T> const& exprs) { | ||
19 | 839 | Array<String> strs; | |
20 |
1/2✓ Branch 2 taken 545 times.
✗ Branch 3 not taken.
|
839 | strs.reserve(exprs.size()); |
21 | |||
22 |
2/4✓ Branch 1 taken 545 times.
✗ Branch 2 not taken.
✓ Branch 6 taken 545 times.
✗ Branch 7 not taken.
|
839 | ::std::transform(::std::begin(exprs), |
23 | ::std::end(exprs), | ||
24 | std::back_inserter(strs), | ||
25 | 309 | [](T const& e) -> String { return str(e); }); | |
26 | |||
27 |
1/2✓ Branch 1 taken 545 times.
✗ Branch 2 not taken.
|
1678 | return join(sep, strs); |
28 | 839 | } | |
29 | |||
30 | Array<String> split(char sep, String const& text); | ||
31 | |||
32 | // template <typename Container, typename S, typename... Args, | ||
33 | // FMT_ENABLE_IF( | ||
34 | // is_contiguous<Container>::value&& internal::is_string<S>::value)> | ||
35 | |||
36 | // Replace a by b in t | ||
37 | template <typename T> | ||
38 | T replace(T const& t, char a, T const& b) { | ||
39 | int n = int(t.size()); | ||
40 | // auto iter = t.rbegin(); | ||
41 | // while (iter != t.rend() && *iter == a){ | ||
42 | // n -= 1; | ||
43 | // iter -= 1; | ||
44 | //} | ||
45 | |||
46 | int count = 0; | ||
47 | for (int i = 0; i < n; ++i) { | ||
48 | if (t[i] == a) { | ||
49 | count += 1; | ||
50 | } | ||
51 | } | ||
52 | |||
53 | auto str = T(n + b.size() * size_t(count), ' '); | ||
54 | int k = 0; | ||
55 | for (int i = 0; i < n; ++i) { | ||
56 | if (t[i] != a) { | ||
57 | str[k] = t[i]; | ||
58 | k += 1; | ||
59 | } else { | ||
60 | for (int j = 0; j < b.size(); ++j) { | ||
61 | str[k] = b[j]; | ||
62 | k += 1; | ||
63 | } | ||
64 | } | ||
65 | } | ||
66 | |||
67 | return str; | ||
68 | } | ||
69 | |||
70 | template <typename T> | ||
71 | inline void print(Array<T> const& obj, std::ostream& out) { | ||
72 |
4/4✓ Branch 3 taken 12 times.
✓ Branch 6 taken 12 times.
✓ Branch 9 taken 12 times.
✓ Branch 12 taken 12 times.
|
12 | out << '[' << join(", ", obj) << ']'; |
73 | 12 | } | |
74 | |||
75 | } // namespace lython | ||
76 | |||
77 | #endif | ||
78 |