Skip to content

Commit

Permalink
Add string
Browse files Browse the repository at this point in the history
  • Loading branch information
raultapia committed Jun 12, 2024
1 parent ac9f6a2 commit 39926af
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ git submodule add -b submodule https://github.com/raultapia/rush include/rush
|Progress Bar|`#include <rush/progress-bar.hpp>`|`rush::progress`|
|ROS-OpenCV Bridge|`#include <rush/ros-cv-bridge.hpp>`|`rush::roscv`|
|ROS Parameter Manager|`#include <rush/ros-parameter-manager.hpp>`|`rush::ros`|
|String|`#include <rush/string.hpp>`|`rush::string`|

## 📚 Documentation
RUSH documentation can be found [here](https://raultapia.github.io/rush).
Expand Down
81 changes: 81 additions & 0 deletions include/rush/string.hpp
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

0 comments on commit 39926af

Please sign in to comment.