Repetition operator: repeat the string a specified number of times.
Repetition operator: repeat the string a specified number of times.This method creates a new string consisting of the original string repeated a specified number of times.
#ifndef RUSH_STRING_HPP
#define RUSH_STRING_HPP
#include <cstddef>
#include <string>
#include <type_traits>
namespace rush {
class string : public std::string {
using std::string::string;
public:
explicit string(const std::string &str) : std::string(str) {}
string operator*(int times) const {
string r;
r.reserve(string::size() * times);
while(times-- > 0) {
r += *this;
}
return r;
}
template <typename T>
string operator|(const T x) const {
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);
constexpr size_t s1 = std::char_traits<char>::length(
rush::color::reset);
const std::size_t s2 = string::size();
if(s1 >= s2 || string::substr(s2 - s1) != rush::color::reset) {
return static_cast<string>(rush::color::escape_sequence(static_cast<int>(x)) + *this + rush::color::reset);
}
return static_cast<string>(rush::color::escape_sequence(static_cast<int>(x)) + *this);
}
string replaceSubstr(const std::string &from, const std::string &to) {
const std::size_t lf = from.length();
const std::size_t lt = to.length();
std::size_t index = -lt;
string ret = *this;
while((index = ret.find(from, index + lt)) != std::string::npos) {
ret.replace(index, lf, to);
}
return ret;
}
int countSubstr(const std::string &substr) {
const std::size_t n = substr.length();
int count = 0;
std::size_t index = -n;
while((index = std::string::find(substr, index + n)) != std::string::npos) {
++count;
}
return count;
}
};
}
#endif
This library extends C++ std algorithm.
This library provides text color and style formating utilities.