diff options
Diffstat (limited to 'source/core')
-rw-r--r-- | source/core/CMakeLists.txt | 2 | ||||
-rw-r--r-- | source/core/StarText.cpp | 89 | ||||
-rw-r--r-- | source/core/StarText.hpp | 26 | ||||
-rw-r--r-- | source/core/StarTime.cpp | 2 |
4 files changed, 118 insertions, 1 deletions
diff --git a/source/core/CMakeLists.txt b/source/core/CMakeLists.txt index 1fd2aac..8c2964e 100644 --- a/source/core/CMakeLists.txt +++ b/source/core/CMakeLists.txt @@ -112,6 +112,7 @@ SET (star_core_HEADERS StarStringView.hpp StarStrongTypedef.hpp StarTcp.hpp + StarText.hpp StarThread.hpp StarTickRateMonitor.hpp StarTime.hpp @@ -171,6 +172,7 @@ SET (star_core_SOURCES StarString.cpp StarStringView.cpp StarTcp.cpp + StarText.cpp StarThread.cpp StarTime.cpp StarTickRateMonitor.cpp diff --git a/source/core/StarText.cpp b/source/core/StarText.cpp new file mode 100644 index 0000000..11fca27 --- /dev/null +++ b/source/core/StarText.cpp @@ -0,0 +1,89 @@ +#include "StarText.hpp" + +#include <regex> + +namespace Star { + +namespace Text { + static auto stripEscapeRegex = std::regex(strf("\\{:c}[^;]*{:c}", CmdEsc, EndEsc)); + String stripEscapeCodes(String const& s) { + return std::regex_replace(s.utf8(), stripEscapeRegex, ""); + } + + static std::string escapeChars = strf("{:c}{:c}", CmdEsc, StartEsc); + + bool processText(StringView text, TextCallback textFunc, CommandsCallback commandsFunc, bool includeCommandSides) { + std::string_view escChars(escapeChars); + + std::string_view str = text.utf8(); + while (true) { + size_t escape = str.find_first_of(escChars); + if (escape != NPos) { + escape = str.find_first_not_of(escChars, escape) - 1; // jump to the last ^ + + size_t end = str.find_first_of(EndEsc, escape); + if (end != NPos) { + if (escape && !textFunc(str.substr(0, escape))) + return false; + if (commandsFunc) { + StringView commands = includeCommandSides + ? str.substr(escape, end - escape + 1) + : str.substr(escape + 1, end - escape - 1); + if (!commands.empty() && !commandsFunc(commands)) + return false; + } + str = str.substr(end + 1); + continue; + } + } + + if (!str.empty()) + return textFunc(str); + + return true; + } + } + + // The below two functions aren't used anymore, not bothering with StringView for them + String preprocessEscapeCodes(String const& s) { + bool escape = false; + std::string result = s.utf8(); + + size_t escapeStartIdx = 0; + for (size_t i = 0; i < result.size(); i++) { + auto& c = result[i]; + if (isEscapeCode(c)) { + escape = true; + escapeStartIdx = i; + } + if ((c <= SpecialCharLimit) && !(c == StartEsc)) + escape = false; + if ((c == EndEsc) && escape) + result[escapeStartIdx] = StartEsc; + } + return {result}; + } + + String extractCodes(String const& s) { + bool escape = false; + StringList result; + String escapeCode; + for (auto c : preprocessEscapeCodes(s)) { + if (c == StartEsc) + escape = true; + if (c == EndEsc) { + escape = false; + for (auto command : escapeCode.split(',')) + result.append(command); + escapeCode = ""; + } + if (escape && (c != StartEsc)) + escapeCode.append(c); + } + if (!result.size()) + return ""; + return "^" + result.join(",") + ";"; + } +} + +}
\ No newline at end of file diff --git a/source/core/StarText.hpp b/source/core/StarText.hpp new file mode 100644 index 0000000..e318484 --- /dev/null +++ b/source/core/StarText.hpp @@ -0,0 +1,26 @@ +#ifndef STAR_TEXT_HPP +#define STAR_TEXT_HPP +#include "StarString.hpp" +#include "StarStringView.hpp" + +namespace Star { + +namespace Text { + unsigned char const StartEsc = '\x1b'; + unsigned char const EndEsc = ';'; + unsigned char const CmdEsc = '^'; + unsigned char const SpecialCharLimit = ' '; + + String stripEscapeCodes(String const& s); + inline bool isEscapeCode(char c) { return c == CmdEsc || c == StartEsc; } + + typedef function<bool(StringView text)> TextCallback; + typedef function<bool(StringView commands)> CommandsCallback; + bool processText(StringView text, TextCallback textFunc, CommandsCallback commandsFunc = CommandsCallback(), bool includeCommandSides = false); + String preprocessEscapeCodes(String const& s); + String extractCodes(String const& s); +} + +} + +#endif
\ No newline at end of file diff --git a/source/core/StarTime.cpp b/source/core/StarTime.cpp index 622ea50..f64b850 100644 --- a/source/core/StarTime.cpp +++ b/source/core/StarTime.cpp @@ -43,7 +43,7 @@ String Time::printDuration(double time) { seconds = strf("{} second{}", numSeconds, numSeconds == 1 ? "" : "s"); } - int numMilliseconds = round(time * 1000); + int numMilliseconds = round(fmod(time, 1.0) * 1000); milliseconds = strf("{} millisecond{}", numMilliseconds, numMilliseconds == 1 ? "" : "s"); return String::joinWith(", ", hours, minutes, seconds, milliseconds); |