32 #ifndef HERMES_COMMON_STR_H
33 #define HERMES_COMMON_STR_H
59 static const char floating_point_number[];
60 static const char integer_number[];
61 static const char alpha_numeric_word[];
62 static const char c_identifier[];
69 static bool match(
const std::string &s,
const std::string &pattern,
70 std::regex_constants::match_flag_type flags = std::regex_constants::match_default);
76 static bool contains(
const std::string &s,
const std::string &pattern,
77 std::regex_constants::match_flag_type flags = std::regex_constants::match_default);
83 static std::smatch
search(
const std::string &s,
const std::string &pattern,
84 std::regex_constants::match_flag_type flags = std::regex_constants::match_default);
91 static bool search(std::string s,
92 const std::string &pattern,
93 const std::function<
void(
const std::smatch &)> &callback,
94 std::regex_constants::match_flag_type flags = std::regex_constants::match_default);
101 static std::string
replace(
const std::string &s,
const std::string &pattern,
const std::string &
format,
102 std::regex_constants::match_flag_type flags = std::regex_constants::match_default);
112 static bool isPrefix(
const std::string& p,
const std::string& s);
121 static std::string
abbreviate(
const std::string &s,
size_t width,
const char fmt[4] =
"s.s");
129 static std::string
rjust(
const T &value,
size_t width,
char fill_char =
' ') {
132 if (s.
str().size() >= width)
134 return std::string(width - s.
str().size(), fill_char) + s.
str();
143 static std::string
ljust(
const T &value,
size_t width,
char fill_char =
' ') {
146 if (s.
str().size() >= width)
148 return s.
str() + std::string(width - s.
str().size(), fill_char);
157 static std::string
cjust(
const T &value,
size_t width,
char fill_char =
' ') {
160 if (s.
str().size() >= width)
162 size_t pad = (width - s.
str().size()) / 2;
163 return std::string(pad, fill_char) + s.
str() + std::string(pad, fill_char);
169 template<
typename... Ts>
170 static std::string
format(
const std::string &fmt, Ts &&... args) {
173 if constexpr(
sizeof...(args) > 0) {
174 format_r_(s, fmt, 0, std::forward<Ts>(args) ...);
188 static std::string
toHex(T i,
bool leading_zeros =
false,
bool zero_x =
false) {
189 std::stringstream stream;
193 stream << std::setfill(
'0') << std::setw(
sizeof(T) * 2)
196 stream << std::hex << i;
205 static std::string
strip(
const std::string &s,
const std::string &patterns =
" \t\n");
211 template<
class... Args>
212 static std::string
concat(
const Args &... args) {
221 static std::string
join(
const std::vector<std::string> &v,
const std::string &separator =
"");
229 static std::string
join(
const std::vector<T> &v,
const std::string &separator =
"") {
232 for (
const auto &s : v) {
245 static std::vector<std::string>
split(
const std::string &s,
246 const std::string &delimiters =
" ");
253 for (
int i = 31; i >= 0; i--)
267 static std::string
binaryToHex(T input_n,
bool uppercase =
true,
bool strip_leading_zeros =
false) {
268 static const char digits[] =
"0123456789abcdef";
269 static const char DIGITS[] =
"0123456789ABCDEF";
270 unsigned long long n = 0;
271 std::memcpy(&n, &input_n,
sizeof(T));
273 for (
int i =
sizeof(T) - 1; i >= 0; --i) {
274 u8 a = n >> (8 * i + 4) & 0xf;
275 u8 b = (n >> (8 * i)) & 0xf;
277 strip_leading_zeros =
false;
278 if (!strip_leading_zeros)
279 s += (uppercase) ? DIGITS[a] : digits[a];
281 strip_leading_zeros =
false;
282 if (!strip_leading_zeros)
283 s += (uppercase) ? DIGITS[b] : digits[b];
294 for (
i8 i = 7; i >= 0; --i) {
295 auto h =
binaryToHex((ptr >> (i * 8)) & 0xff,
true);
296 s += h.substr(h.size() - 2);
298 return "0x" + s.substr(s.size() - digit_count, digit_count);
305 for (
int i = 7; i >= 0; i--)
306 s += std::to_string((b >> i) & 1);
313 static bool isInteger(
const std::string &s);
318 static bool isNumber(
const std::string &s);
327 return {
str.str() + s};
334 std::stringstream ss;
344 return s.
str() == ss;
353 std::stringstream ss;
355 return s.
str() == ss.str();
373 Str(
Str &&other) noexcept;
381 [[nodiscard]]
inline const std::string &
str()
const {
return s_; }
384 [[nodiscard]]
inline const char *
c_str()
const {
return s_.c_str(); }
387 [[nodiscard]]
inline size_t size()
const {
return s_.size(); }
390 [[nodiscard]]
inline bool empty()
const {
return s_.empty(); }
403 template<
class... Args>
405 std::ostringstream s;
413 template<
class... Args>
415 std::ostringstream s;
417 s_ += s.str() +
'\n';
434 std::stringstream ss;
454 std::stringstream ss;
466 std::stringstream ss;
487 std::stringstream ss;
489 return s_ == ss.str();
496 static void format_r_(std::stringstream &s,
const std::string &fmt,
u32 i,
const T &first) {
498 while (i + 1 < fmt.size() && !(fmt[i] ==
'{' && fmt[i + 1] ==
'}'))
500 if (i + 1 < fmt.size()) {
501 s << fmt.substr(first_i, i - first_i);
503 s << fmt.substr(i + 2, fmt.size() - i - 2);
505 s << fmt.substr(first_i, fmt.size() - first_i);
507 template<
typename T,
typename...Ts>
508 static void format_r_(std::stringstream &s,
const std::string &fmt,
u32 i,
const T &first, Ts &&... rest) {
511 while (i + 1 < fmt.size() && !(fmt[i] ==
'{' && fmt[i + 1] ==
'}'))
513 if (i + 1 < fmt.size()) {
514 s << fmt.substr(first_i, i - first_i);
516 if constexpr(
sizeof ...(rest) > 0)
517 format_r_(s, fmt, i + 2, std::forward<Ts>(rest)...);
521 s << fmt.
substr(first_i, fmt.
size() - first_i);
536 inline std::ostream &operator<<(std::ostream &os, const
Str &s) {
547 std::stringstream ss;
549 return {s + ss.
str()};
556 template<
typename T, std::enable_if_t<std::is_same_v<T, std::
string> == false>>
558 std::stringstream ss;
560 return {s + ss.
str()};
Holds a valid object or an error.
Definition: result.h:56
String class and set of string functions.
Definition: str.h:53
static std::string concat(const Args &... args)
Concatenates multiple elements_ into a single string.
Definition: str.h:212
const char * c_str() const
Get const char* pointer.
Definition: str.h:384
static std::string rjust(const T &value, size_t width, char fill_char=' ')
Right justifies string value.
Definition: str.h:129
static std::string strip(const std::string &s, const std::string &patterns=" \t\n")
Definition: str.cpp:149
Str()
Default constructor.
static std::string join(const std::vector< T > &v, const std::string &separator="")
Concatenate elements together separates by a separator.
Definition: str.h:229
static std::string binaryToHex(T input_n, bool uppercase=true, bool strip_leading_zeros=false)
Get ascii representation of raw bit data of input_n
Definition: str.h:267
size_t size() const
Get the number of characters on the string.
Definition: str.h:387
static std::string join(const std::vector< std::string > &v, const std::string &separator="")
Concatenate strings together separated by a separator.
Definition: str.cpp:137
Result< ConstStrView > substr(size_t pos=0, i64 len=-1)
Get a sub-string view from this object.
Definition: str.cpp:256
static std::string printBits(u32 n)
Print bits in big-endian order.
Definition: str.h:251
static bool isNumber(const std::string &s)
Checks if string represents a number.
Definition: str.cpp:205
void append(const Args &... args)
Append arguments to this Str.
Definition: str.h:404
bool friend operator==(const T &t, const Str &s)
Character-wise comparison.
Definition: str.h:352
Str operator<<(const char *s) const
Generates a copy appended by s
Definition: str.h:473
static std::string abbreviate(const std::string &s, size_t width, const char fmt[4]="s.s")
Abbreviates a string to fit in a string of width characters.
Definition: str.cpp:93
bool operator==(const T &t) const
Performs character comparison with string value of t
Definition: str.h:486
Str & operator+=(const Str &other)
Simple concatenation with other
Definition: str.h:443
static std::vector< std::string > split(const std::string &s, const std::string &delimiters=" ")
Splits a string into tokens separated by delimiters.
Definition: str.cpp:170
Str & operator=(const Str &s)=default
Str & operator+=(const T &t)
Simple concatenation with string of value.
Definition: str.h:453
Str operator+(const T &t) const
Generates a copy appended by t
Definition: str.h:465
static std::string cjust(const T &value, size_t width, char fill_char=' ')
Center justifies string value.
Definition: str.h:157
static std::string addressOf(uintptr_t ptr, u32 digit_count=8)
Generates hexadecimal representation of memory address.
Definition: str.h:291
static std::string format(const std::string &fmt, Ts &&... args)
Definition: str.h:170
static bool isInteger(const std::string &s)
Checks if string represents an integer.
Definition: str.cpp:187
friend bool operator==(const char *ss, const Str &s)
const char* pointer comparison
Definition: str.h:343
const std::string & str() const
Get std::string object.
Definition: str.h:381
void appendLine(const Args &... args)
Append arguments to this Str followed by a breakline.
Definition: str.h:414
static bool isPrefix(const std::string &p, const std::string &s)
Checks if s has prefix p.
Definition: str.cpp:82
static std::string toHex(T i, bool leading_zeros=false, bool zero_x=false)
Generates hexadecimal representation from number.
Definition: str.h:188
Str & operator=(const T &t)
String of value assignment.
Definition: str.h:433
friend Str operator+(const std::string &s, const Str &str)
Concatenate.
Definition: str.h:333
bool empty() const
Checks if string is empty.
Definition: str.h:390
static std::string ljust(const T &value, size_t width, char fill_char=' ')
Left justifies string value.
Definition: str.h:143
static std::string byteToBinary(byte b)
Binary representation of byte.
Definition: str.h:303
Str(const Str &other)
Copy constructor.
bool operator==(const char *ss) const
Performs const char* comparison.
Definition: str.h:480
friend Str operator<<(const char *s, const Str &str)
Concatenate.
Definition: str.h:326
int8_t i8
8 bit size integer type
Definition: defs.h:81
uint32_t u32
32 bit size unsigned integer type
Definition: defs.h:88
int64_t i64
64 bit size integer type
Definition: defs.h:84
uint8_t u8
8 bit size unsigned integer type
Definition: defs.h:86
static bool match(const std::string &s, const std::string &pattern, std::regex_constants::match_flag_type flags=std::regex_constants::match_default)
Checks if a string s matches exactly a regular expression.
Definition: str.cpp:40
static bool contains(const std::string &s, const std::string &pattern, std::regex_constants::match_flag_type flags=std::regex_constants::match_default)
Checks if any substring of s matches a regular expression.
Definition: str.cpp:45
static std::smatch search(const std::string &s, const std::string &pattern, std::regex_constants::match_flag_type flags=std::regex_constants::match_default)
Search the first substrings of s that matches the pattern.
Definition: str.cpp:52
static std::string replace(const std::string &s, const std::string &pattern, const std::string &format, std::regex_constants::match_flag_type flags=std::regex_constants::match_default)
Replaces all matches of pattern in s by format.
Definition: str.cpp:75