-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
82 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/** | ||
* @file string.hpp | ||
* @brief This library provides string-related utilities. | ||
* @author Raul Tapia (raultapia.com) | ||
* @copyright GNU General Public License v3.0 | ||
* @see https://github.com/raultapia/rush | ||
*/ | ||
#ifndef RUSH_STRING_HPP | ||
#define RUSH_STRING_HPP | ||
|
||
#include "rush/algorithm.hpp" | ||
#include "rush/color.hpp" | ||
#include <string> | ||
|
||
namespace rush { | ||
|
||
/** | ||
* @brief This class extends std::string | ||
*/ | ||
class string : public std::string { | ||
using std::string::string; | ||
|
||
public: | ||
explicit string(const std::string &str) : std::string(str) {} | ||
|
||
/** | ||
* @brief 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. | ||
* | ||
* @param times The number of times to repeat the string. | ||
* @return A new string containing the repeated sequence. | ||
* | ||
* @example | ||
* @code | ||
* rush::string s("abc"); | ||
* rush::string result = s * 3; // result is "abcabcabc" | ||
* @endcode | ||
*/ | ||
string operator*(int times) const { | ||
rush::clampl(times, 0); | ||
string r; | ||
r.reserve(string::size() * times); | ||
while(times-- > 0) { | ||
r += *this; | ||
} | ||
return r; | ||
} | ||
|
||
/** | ||
* @brief Format operator: Apply a color or style to the string. | ||
* | ||
* This method formats the string with a given color or style | ||
* by adding the appropriate escape sequences. | ||
* | ||
* @tparam T The type of the color or style, which must be one of rush::color::fg, rush::color::bg, or rush::color::st. | ||
* @param x The color or style to apply. | ||
* @return A new string formatted with the specified color or style. | ||
* | ||
* @example | ||
* @code | ||
* rush::string s("Hello"); | ||
* rush::string result = s | rush::color::fg::red; // result is the string "Hello" in red color | ||
* @endcode | ||
*/ | ||
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); | ||
} | ||
}; | ||
|
||
} // namespace rush | ||
|
||
#endif // RUSH_STRING_HPP |