RUSH
Reusable Utilities in Single Headers is a collection of header-only utilities for C++
Loading...
Searching...
No Matches
/github/workspace/include/rush/string.hpp

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.

Parameters
timesThe number of times to repeat the string.
Returns
A new string containing the repeated sequence.
rush::string s("abc");
rush::string result = s * 3; // result is "abcabcabc"
This class extends std::string.
Definition string.hpp:22
#ifndef RUSH_STRING_HPP
#define RUSH_STRING_HPP
#include "rush/color.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 {
rush::clampl(times, 0);
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;
}
};
} // namespace rush
#endif // RUSH_STRING_HPP
This library extends C++ std algorithm.
This library provides text color and style formating utilities.