Added basic RegEx functionality + RegexRichString

This commit is contained in:
OmniaX 2023-12-03 23:29:35 +01:00
parent 37f33e5a77
commit 9ab0cad97f
8 changed files with 135 additions and 15 deletions

View file

@ -9,7 +9,7 @@ download MSYS2 from https://www.msys2.org/ and install it
run MSYS2, and in the terminal run:
```
pacman -Syuu
pacman -S --needed base-devel mingw-w64-ucrt-x86_64-clang mingw-w64-ucrt-x86_64-gdb mingw-w64-ucrt-x86_64-cmake mingw-w64-ucrt-x86_64-sfml mingw-w64-ucrt-x86_64-make
pacman -S --needed base-devel mingw-w64-ucrt-x86_64-clang mingw-w64-ucrt-x86_64-gdb mingw-w64-ucrt-x86_64-cmake mingw-w64-ucrt-x86_64-sfml mingw-w64-ucrt-x86_64-make mingw-w64-ucrt-x86_64-boost
```
**Step 3:**

View file

@ -1,4 +1,4 @@
#/bin/bash
#!/bin/bash
rm -r bin

View file

@ -165,9 +165,9 @@ namespace ostd
return *this;
}
IOutputHandler& ConsoleOutputHandler::pStyled(const String& styled)
IOutputHandler& ConsoleOutputHandler::pStyled(const StringEditor& styled)
{
return pStyled(TextStyleParser::parse(styled));
return pStyled(TextStyleParser::parse(styled.str()));
}
IOutputHandler& ConsoleOutputHandler::pStyled(const TextStyleParser::tStyledString& styled)
@ -176,6 +176,12 @@ namespace ostd
return *this;
}
IOutputHandler& ConsoleOutputHandler::pStyled(const TextStyleBuilder::IRichStringBase& styled)
{
std::cout << styled.getStyledString();
return *this;
}
IOutputHandler& ConsoleOutputHandler::nl(void)
{
std::cout << "\n";

View file

@ -1,6 +1,7 @@
#include "Utils.hpp"
#include <sstream>
#include <algorithm>
#include <boost/regex.hpp>
namespace ostd
{
@ -150,6 +151,22 @@ namespace ostd
return *this;
}
StringEditor& StringEditor::regexReplace(String regex_pattern, String replace_with, bool case_insensitive)
{
try
{
boost::regex rgx(regex_pattern, (case_insensitive ? boost::regex_constants::icase : boost::regex_constants::normal));
m_data = boost::regex_replace(m_data, rgx, replace_with);
return *this;
}
catch(const boost::regex_error& err)
{
std::cerr << err.what() << '\n'; //TODO: Better error handling
return *this;
}
return *this;
}
StringEditor& StringEditor::put(uint32_t index, char c)
{
if (index < m_data.length())

View file

@ -18,6 +18,16 @@ namespace ostd
return os << builder.getStyledString();
}
std::ostream &operator<<(std::ostream &os, TextStyleBuilder::Regex const &regex)
{
return os << regex.getStyledString();
}
std::ostream &operator<<(std::ostream &os, TextStyleBuilder::IRichStringBase const &rstr)
{
return os << rstr.getStyledString();
}
void TextStyleParser::tColor::convertToBackground(void)
{
StringEditor edit(consoleColor);
@ -49,12 +59,12 @@ namespace ostd
return text.length() > 0 && text.length() == backgroundColors.size() && text.length() == foregroundColors.size();
}
TextStyleParser::tStyledString TextStyleParser::parse(const String& styledString)
TextStyleParser::tStyledString TextStyleParser::parse(const StringEditor& styledString)
{
return parse(styledString, ConsoleColors["black"], ConsoleColors["white"]);
}
TextStyleParser::tStyledString TextStyleParser::parse(const String& styledString, tColor defaultBackgorundColor, tColor defaultForegroundColor)
TextStyleParser::tStyledString TextStyleParser::parse(const StringEditor& styledString, tColor defaultBackgorundColor, tColor defaultForegroundColor)
{
tStyledString rstring;
bool insideBlock = false;
@ -313,4 +323,57 @@ namespace ostd
return TextStyleParser::ConsoleColors["black"];
}
TextStyleBuilder::Regex& TextStyleBuilder::Regex::setRawString(const StringEditor& rawString)
{
m_rawString = rawString.str();
return *this;
}
TextStyleBuilder::Regex& TextStyleBuilder::Regex::fg(const StringEditor& regex, const StringEditor& foreground_color, bool case_insensitive)
{
String replace_pattern = "[@@ style foreground:" + foreground_color.str();
replace_pattern += "]$&[@@/]";
m_rawString = StringEditor(m_rawString).regexReplace(regex.str(), replace_pattern, case_insensitive).str();
return *this;
}
TextStyleBuilder::Regex& TextStyleBuilder::Regex::bg(const StringEditor& regex, const StringEditor& background_color, bool case_insensitive)
{
String replace_pattern = "[@@ style background:" + background_color.str();
replace_pattern += "]$&[@@/]";
m_rawString = StringEditor(m_rawString).regexReplace(regex.str(), replace_pattern, case_insensitive).str();
return *this;
}
TextStyleBuilder::Regex& TextStyleBuilder::Regex::col(const StringEditor& regex, const StringEditor& foreground_color, const StringEditor& background_color, bool case_insensitive)
{
String replace_pattern = "[@@ style background:" + background_color.str();
replace_pattern += ", foreground:" + foreground_color.str();
replace_pattern += "]$&[@@/]";
m_rawString = StringEditor(m_rawString).regexReplace(regex.str(), replace_pattern, case_insensitive).str();
return *this;
}
TextStyleParser::tStyledString TextStyleBuilder::Regex::getStyledString(void) const
{
return TextStyleParser::parse(m_rawString);
}
TextStyleBuilder::Regex& TextStyleBuilder::Regex::print(void)
{
ConsoleOutputHandler out;
out.pStyled(m_rawString);
return *this;
}
TextStyleBuilder::Regex& TextStyleBuilder::Regex::print(IOutputHandler& out)
{
out.pStyled(m_rawString);
return *this;
}
}

View file

@ -5,6 +5,7 @@
namespace ostd
{
class StringEditor;
class TextStyleParser
{
public: enum class eBlockParserReturnValue { CloseBlock = 0, ValidBlock, InvalidBlock };
@ -29,8 +30,8 @@ namespace ostd
};
public:
static tStyledString parse(const String& styledString);
static tStyledString parse(const String& styledString, tColor defaultBackgorundColor, tColor defaultForegroundColor);
static tStyledString parse(const StringEditor& styledString);
static tStyledString parse(const StringEditor& styledString, tColor defaultBackgorundColor, tColor defaultForegroundColor);
private:
static eBlockParserReturnValue parse_block(const String& blockString, tColor& outBackgroundColor, tColor& outForegroundColor);
@ -65,7 +66,13 @@ namespace ostd
class TextStyleBuilder
{
public: class Console {
public: class IRichStringBase {
public:
virtual TextStyleParser::tStyledString getStyledString(void) const = 0;
friend std::ostream& operator<<(std::ostream&, IRichStringBase const&);
};
public: class Console : public IRichStringBase {
public:
Console(void);
Console& bg(const String& consoleColor);
@ -87,7 +94,7 @@ namespace ostd
Console& print(IOutputHandler& out);
Console& print(void);
inline TextStyleParser::tStyledString getStyledString(void) const { return m_styledString; }
inline TextStyleParser::tStyledString getStyledString(void) const override { return m_styledString; }
friend std::ostream& operator<<(std::ostream&, Console const&);
@ -100,6 +107,28 @@ namespace ostd
TextStyleParser::tColor m_foregroundColor;
};
public: class Regex : public IRichStringBase {
public:
inline Regex(void) { m_rawString = ""; }
inline Regex(const StringEditor& rawString) { setRawString(rawString); }
inline String getRawString(void) const { return m_rawString; }
Regex& setRawString(const StringEditor& rawString);
Regex& fg(const StringEditor& regex, const StringEditor& foreground_color, bool case_insensitive = false);
Regex& bg(const StringEditor& regex, const StringEditor& background_color, bool case_insensitive = false);
Regex& col(const StringEditor& regex, const StringEditor& foreground_color, const StringEditor& background_color, bool case_insensitive = false);
TextStyleParser::tStyledString getStyledString(void) const override;
Regex& print(void);
Regex& print(IOutputHandler& out);
friend std::ostream& operator<<(std::ostream&, Regex const&);
private:
String m_rawString { "" };
};
//TODO: Implement
// public: class FullColor {
@ -107,4 +136,5 @@ namespace ostd
};
typedef TextStyleBuilder::Console ConsoleRichString;
typedef TextStyleBuilder::Regex RegexRichString;
}

View file

@ -247,6 +247,7 @@ namespace ostd
StringEditor& reverse(void);
StringEditor& replaceAll(String what, String with);
StringEditor& replaceFirst(String what, String with);
StringEditor& regexReplace(String regex_pattern, String replace_with, bool case_insensitive = false);
StringEditor& put(uint32_t index, char c);
@ -341,8 +342,9 @@ namespace ostd
virtual IOutputHandler& pi(int64_t i) = 0;
virtual IOutputHandler& pf(float f, uint8_t precision = 0) = 0;
virtual IOutputHandler& pf(double f, uint8_t precision = 0) = 0;
virtual IOutputHandler& pStyled(const String& styled) = 0;
virtual IOutputHandler& pStyled(const StringEditor& styled) = 0;
virtual IOutputHandler& pStyled(const TextStyleParser::tStyledString& styled) = 0;
virtual IOutputHandler& pStyled(const TextStyleBuilder::IRichStringBase& styled) = 0;
virtual IOutputHandler& nl(void) = 0;
virtual IOutputHandler& flush(void) = 0;
virtual IOutputHandler& reset(void) = 0;
@ -375,8 +377,9 @@ namespace ostd
IOutputHandler& pi(int64_t i) override;
IOutputHandler& pf(float f, uint8_t precision = 0) override;
IOutputHandler& pf(double f, uint8_t precision = 0) override;
IOutputHandler& pStyled(const String& styled) override;
IOutputHandler& pStyled(const StringEditor& styled) override;
IOutputHandler& pStyled(const TextStyleParser::tStyledString& styled) override;
IOutputHandler& pStyled(const TextStyleBuilder::IRichStringBase& styled) override;
IOutputHandler& nl(void) override;
IOutputHandler& flush(void) override;
IOutputHandler& reset(void) override;
@ -403,8 +406,9 @@ namespace ostd
IOutputHandler& pi(int64_t i) override;
IOutputHandler& pf(float f, uint8_t precision = 0) override;
IOutputHandler& pf(double f, uint8_t precision = 0) override;
IOutputHandler& pStyled(const String& styled) override { return *this; };
IOutputHandler& pStyled(const TextStyleParser::tStyledString& styled) override { return *this; };
IOutputHandler& pStyled(const StringEditor& styled) override { return *this; };
inline IOutputHandler& pStyled(const TextStyleParser::tStyledString& styled) override { return *this; }
inline IOutputHandler& pStyled(const TextStyleBuilder::IRichStringBase& styled) override { return *this; };
IOutputHandler& nl(void) override;
inline IOutputHandler& flush(void) override { return *this; }
inline IOutputHandler& reset(void) override { return *this; }

View file

@ -1 +1 @@
1725
1743