RUSH
Reusable Utilities in Single Headers is a collection of header-only utilities for C++
Toggle main menu visibility
Loading...
Searching...
No Matches
string.hpp
Go to the documentation of this file.
1
8
#ifndef RUSH_STRING_HPP
9
#define RUSH_STRING_HPP
10
11
#include "
rush/algorithm.hpp
"
12
#include "
rush/color.hpp
"
13
#include <cstddef>
14
#include <string>
15
#include <type_traits>
16
17
namespace
rush {
18
22
class
string :
public
std::string {
23
using
std::string::string;
24
25
public
:
26
explicit
string(
const
std::string &str) : std::string(str) {}
27
43
string
operator*(
int
times)
const
{
44
rush::clampl
(times, 0);
45
string
r;
46
r.reserve(string::size() * times);
47
while
(times-- > 0) {
48
r += *
this
;
49
}
50
return
r;
51
}
52
69
template
<
typename
T>
70
string
operator|(
const
T x)
const
{
71
static_assert
(std::is_same<T, rush::color::fg>::value || std::is_same<T, rush::color::bg>::value || std::is_same<T, rush::color::st>::value);
72
constexpr
size_t
s1 = std::char_traits<char>::length(
rush::color::reset
);
73
const
std::size_t s2 = string::size();
74
if
(s1 >= s2 || string::substr(s2 - s1) !=
rush::color::reset
) {
75
return
static_cast<
string
>
(rush::color::escape_sequence(
static_cast<
int
>
(x)) + *
this
+
rush::color::reset
);
76
}
77
return
static_cast<
string
>
(rush::color::escape_sequence(
static_cast<
int
>
(x)) + *
this
);
78
}
79
96
string
replaceSubstr(
const
std::string &from,
const
std::string &to) {
97
const
std::size_t lf = from.length();
98
const
std::size_t lt = to.length();
99
std::size_t index = -lt;
100
string
ret = *
this
;
101
while
((index = ret.find(from, index + lt)) != std::string::npos) {
102
ret.replace(index, lf, to);
103
}
104
return
ret;
105
}
106
122
int
countSubstr(
const
std::string &substr) {
123
const
std::size_t n = substr.length();
124
int
count = 0;
125
std::size_t index = -n;
126
while
((index = std::string::find(substr, index + n)) != std::string::npos) {
127
++count;
128
}
129
return
count;
130
}
131
};
132
133
}
// namespace rush
134
135
#endif
// RUSH_STRING_HPP
algorithm.hpp
This library extends C++ std algorithm.
rush::clampl
constexpr const T & clampl(const T &v, const T &lo)
Clamps a value to be not less than a specified lower bound.
Definition
algorithm.hpp:45
color.hpp
This library provides text color and style formating utilities.
rush::color::reset
constexpr char reset[]
Definition
color.hpp:68
include
rush
string.hpp
Generated by
1.17.0