[0.1.1807] - New ostd::String class + refactor
This commit is contained in:
parent
925d3dc5df
commit
d07117a102
41 changed files with 1628 additions and 705 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
|
@ -1,2 +1,2 @@
|
|||
bin
|
||||
win-release
|
||||
Release
|
||||
|
|
@ -4,6 +4,7 @@ set(PROJ_NAME omnia-framework)
|
|||
set(MAJOR_VER 0)
|
||||
set(MINOR_VER 1)
|
||||
set(OMNIA_STD_LIB ostd)
|
||||
set(TEST_TARGET ostd_test)
|
||||
set(RUN_CONFIG O_F_DEBUG)
|
||||
#-----------------------------------------------------------------------------------------
|
||||
|
||||
|
|
@ -46,17 +47,25 @@ list(APPEND OSTD_SOURCE_FILES
|
|||
${CMAKE_CURRENT_LIST_DIR}/src/ostd/Serial.cpp
|
||||
${CMAKE_CURRENT_LIST_DIR}/src/ostd/SineWave.cpp
|
||||
${CMAKE_CURRENT_LIST_DIR}/src/ostd/StringEditor.cpp
|
||||
${CMAKE_CURRENT_LIST_DIR}/src/ostd/String.cpp
|
||||
${CMAKE_CURRENT_LIST_DIR}/src/ostd/TextStyleParser.cpp
|
||||
${CMAKE_CURRENT_LIST_DIR}/src/ostd/Utils.cpp
|
||||
)
|
||||
list(APPEND TEST_SOURCE_FILES
|
||||
${CMAKE_CURRENT_LIST_DIR}/src/test.cpp
|
||||
)
|
||||
#-----------------------------------------------------------------------------------------
|
||||
|
||||
#Targets
|
||||
#-----------------------------------------------------------------------------------------
|
||||
add_library(${OMNIA_STD_LIB} SHARED ${OSTD_SOURCE_FILES})
|
||||
add_executable(${TEST_TARGET} ${TEST_SOURCE_FILES})
|
||||
|
||||
target_compile_definitions(${OMNIA_STD_LIB} PUBLIC ${RUN_CONFIG} BUILD_NR=${BUILD_NUMBER} MAJ_V=${MAJOR_VER} MIN_V=${MINOR_VER} VERSION_STR="${MAJOR_VER}.${MINOR_VER}.${BUILD_NUMBER}")
|
||||
target_include_directories(${OMNIA_STD_LIB} PUBLIC ${INCLUDE_DIRS})
|
||||
target_include_directories(${TEST_TARGET} PUBLIC ${INCLUDE_DIRS})
|
||||
|
||||
target_link_libraries(${TEST_TARGET} PUBLIC ${OMNIA_STD_LIB})
|
||||
|
||||
#TODO: Different flags for Release/Debug
|
||||
add_compile_options(-O3 -m32 -MMD -MP -Wall -ggdb -fsanitize=address)
|
||||
|
|
|
|||
|
|
@ -32,7 +32,7 @@ namespace ostd
|
|||
|
||||
inline static BaseObject& InvalidRef(void) { return BaseObject::s_invalid_obj; }
|
||||
|
||||
inline void setTypeName(const StringEditor& tn) { m_typeName = tn.str(); }
|
||||
inline void setTypeName(const String& tn) { m_typeName = tn; }
|
||||
inline String getTypeName(void) const { return m_typeName; }
|
||||
String getObjectHeaderString(void) const;
|
||||
|
||||
|
|
|
|||
|
|
@ -25,7 +25,7 @@ namespace ostd
|
|||
Color(void);
|
||||
Color(uint8_t rgb_single_value, uint8_t alpha = 255);
|
||||
Color(uint8_t _r, uint8_t _g, uint8_t _b, uint8_t alpha = 255);
|
||||
Color(const StringEditor& color_string);
|
||||
Color(const String& color_string);
|
||||
Color(const FloatCol& normalized_color);
|
||||
Color(const Color& copy);
|
||||
|
||||
|
|
@ -36,11 +36,11 @@ namespace ostd
|
|||
Color& set(void);
|
||||
Color& set(uint8_t rgb_single_value, uint8_t alpha = 255);
|
||||
Color& set(uint8_t _r, uint8_t _g, uint8_t _b, uint8_t alpha = 255);
|
||||
Color& set(const StringEditor& color_string);
|
||||
Color& set(const String& color_string);
|
||||
Color& set(const FloatCol& normalized_color);
|
||||
|
||||
StringEditor hexString(bool include_alpha = false, StringEditor prefix = "0x") const;
|
||||
StringEditor rgbString(bool include_parenthesis = true, bool include_alpha = false) const;
|
||||
String hexString(bool include_alpha = false, String prefix = "0x") const;
|
||||
String rgbString(bool include_parenthesis = true, bool include_alpha = false) const;
|
||||
uint32_t asInteger(void) const;
|
||||
FloatCol getNormalizedColor(void) const;
|
||||
|
||||
|
|
|
|||
|
|
@ -19,17 +19,17 @@ namespace ostd
|
|||
InteractiveConsole(bool clearOnStart, eMode mode = eMode::Direct);
|
||||
|
||||
OutputHandlerBase& bg(const ConsoleColors::tConsoleColor& color) override;
|
||||
OutputHandlerBase& bg(const StringEditor& color) override;
|
||||
OutputHandlerBase& bg(const String& color) override;
|
||||
OutputHandlerBase& fg(const ConsoleColors::tConsoleColor& color) override;
|
||||
OutputHandlerBase& fg(const StringEditor& color) override;
|
||||
OutputHandlerBase& fg(const String& color) override;
|
||||
|
||||
OutputHandlerBase& pChar(char c) override;
|
||||
OutputHandlerBase& pStyled(const StringEditor& styled) override;
|
||||
OutputHandlerBase& pStyled(const String& styled) override;
|
||||
OutputHandlerBase& pStyled(const TextStyleParser::tStyledString& styled) override;
|
||||
OutputHandlerBase& pStyled(const TextStyleBuilder::IRichStringBase& styled) override;
|
||||
OutputHandlerBase& pObject(const BaseObject& bo) override;
|
||||
|
||||
OutputHandlerBase& p(const StringEditor& se) override;
|
||||
OutputHandlerBase& p(const String& se) override;
|
||||
OutputHandlerBase& p(uint8_t i) override;
|
||||
OutputHandlerBase& p(int8_t i) override;
|
||||
OutputHandlerBase& p(uint16_t i) override;
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@
|
|||
#include <cmath>
|
||||
#include <algorithm>
|
||||
#include <cstdint>
|
||||
#include <ostd/Types.hpp>
|
||||
#include <ostd/String.hpp>
|
||||
|
||||
namespace ostd
|
||||
{
|
||||
|
|
|
|||
|
|
@ -60,46 +60,46 @@ namespace ostd
|
|||
inline static const tConsoleColor OnBrightGray { "brightgray", { 150, 150, 150, 255 }, true};
|
||||
inline static const tConsoleColor OnWhite { "white", { 255, 255, 255, 255 }, true};
|
||||
|
||||
inline static const tConsoleColor getFromName(const StringEditor& name, bool background = false) {
|
||||
StringEditor edit = name;
|
||||
inline static const tConsoleColor getFromName(const String& name, bool background = false) {
|
||||
String edit = name;
|
||||
edit.toLower().trim();
|
||||
if (edit.str() == Red.name) return (background ? OnRed : Red);
|
||||
if (edit.str() == BrightRed.name) return (background ? OnBrightRed : BrightRed);
|
||||
if (edit.str() == Green.name) return (background ? OnGreen : Green);
|
||||
if (edit.str() == BrightGreen.name) return (background ? OnBrightGreen : BrightGreen);
|
||||
if (edit.str() == Blue.name) return (background ? OnBlue : Blue);
|
||||
if (edit.str() == BrightBlue.name) return (background ? OnBrightBlue : BrightBlue);
|
||||
if (edit.str() == Magenta.name) return (background ? OnMagenta : Magenta);
|
||||
if (edit.str() == BrightMagenta.name) return (background ? OnBrightMagenta : BrightMagenta);
|
||||
if (edit.str() == Cyan.name) return (background ? OnCyan : Cyan);
|
||||
if (edit.str() == BrightCyan.name) return (background ? OnBrightCyan : BrightCyan);
|
||||
if (edit.str() == Yellow.name) return (background ? OnYellow : Yellow);
|
||||
if (edit.str() == BrightYellow.name) return (background ? OnBrightYellow : BrightYellow);
|
||||
if (edit.str() == Black.name) return (background ? OnBlack : Black);
|
||||
if (edit.str() == Gray.name) return (background ? OnGray : Gray);
|
||||
if (edit.str() == BrightGray.name) return (background ? OnBrightGray : BrightGray);
|
||||
if (edit.str() == White.name) return (background ? OnWhite : White);
|
||||
if (edit == Red.name) return (background ? OnRed : Red);
|
||||
if (edit == BrightRed.name) return (background ? OnBrightRed : BrightRed);
|
||||
if (edit == Green.name) return (background ? OnGreen : Green);
|
||||
if (edit == BrightGreen.name) return (background ? OnBrightGreen : BrightGreen);
|
||||
if (edit == Blue.name) return (background ? OnBlue : Blue);
|
||||
if (edit == BrightBlue.name) return (background ? OnBrightBlue : BrightBlue);
|
||||
if (edit == Magenta.name) return (background ? OnMagenta : Magenta);
|
||||
if (edit == BrightMagenta.name) return (background ? OnBrightMagenta : BrightMagenta);
|
||||
if (edit == Cyan.name) return (background ? OnCyan : Cyan);
|
||||
if (edit == BrightCyan.name) return (background ? OnBrightCyan : BrightCyan);
|
||||
if (edit == Yellow.name) return (background ? OnYellow : Yellow);
|
||||
if (edit == BrightYellow.name) return (background ? OnBrightYellow : BrightYellow);
|
||||
if (edit == Black.name) return (background ? OnBlack : Black);
|
||||
if (edit == Gray.name) return (background ? OnGray : Gray);
|
||||
if (edit == BrightGray.name) return (background ? OnBrightGray : BrightGray);
|
||||
if (edit == White.name) return (background ? OnWhite : White);
|
||||
return (background ? OnBlack : Black);
|
||||
}
|
||||
inline static bool isConsoleColor(const StringEditor& name) {
|
||||
StringEditor edit = name;
|
||||
inline static bool isConsoleColor(const String& name) {
|
||||
String edit = name;
|
||||
edit.toLower().trim();
|
||||
if (edit.str() == Red.name) return true;
|
||||
if (edit.str() == BrightRed.name) return true;
|
||||
if (edit.str() == Green.name) return true;
|
||||
if (edit.str() == BrightGreen.name) return true;
|
||||
if (edit.str() == Blue.name) return true;
|
||||
if (edit.str() == BrightBlue.name) return true;
|
||||
if (edit.str() == Magenta.name) return true;
|
||||
if (edit.str() == BrightMagenta.name) return true;
|
||||
if (edit.str() == Cyan.name) return true;
|
||||
if (edit.str() == BrightCyan.name) return true;
|
||||
if (edit.str() == Yellow.name) return true;
|
||||
if (edit.str() == BrightYellow.name) return true;
|
||||
if (edit.str() == Black.name) return true;
|
||||
if (edit.str() == Gray.name) return true;
|
||||
if (edit.str() == BrightGray.name) return true;
|
||||
if (edit.str() == White.name) return true;
|
||||
if (edit == Red.name) return true;
|
||||
if (edit == BrightRed.name) return true;
|
||||
if (edit == Green.name) return true;
|
||||
if (edit == BrightGreen.name) return true;
|
||||
if (edit == Blue.name) return true;
|
||||
if (edit == BrightBlue.name) return true;
|
||||
if (edit == Magenta.name) return true;
|
||||
if (edit == BrightMagenta.name) return true;
|
||||
if (edit == Cyan.name) return true;
|
||||
if (edit == BrightCyan.name) return true;
|
||||
if (edit == Yellow.name) return true;
|
||||
if (edit == BrightYellow.name) return true;
|
||||
if (edit == Black.name) return true;
|
||||
if (edit == Gray.name) return true;
|
||||
if (edit == BrightGray.name) return true;
|
||||
if (edit == White.name) return true;
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
|
@ -111,18 +111,18 @@ namespace ostd
|
|||
|
||||
inline virtual OutputHandlerBase& bg(const Color& color) { return *this; }
|
||||
inline virtual OutputHandlerBase& bg(const ConsoleColors::tConsoleColor& color) { return *this; }
|
||||
inline virtual OutputHandlerBase& bg(const StringEditor& color) { return *this; }
|
||||
inline virtual OutputHandlerBase& bg(const String& color) { return *this; }
|
||||
inline virtual OutputHandlerBase& fg(const Color& color) { return *this; }
|
||||
inline virtual OutputHandlerBase& fg(const ConsoleColors::tConsoleColor& color) { return *this; }
|
||||
inline virtual OutputHandlerBase& fg(const StringEditor& color) { return *this; }
|
||||
inline virtual OutputHandlerBase& fg(const String& color) { return *this; }
|
||||
|
||||
inline virtual OutputHandlerBase& pChar(char c) { return *this; }
|
||||
inline virtual OutputHandlerBase& pStyled(const StringEditor& styled) { return *this; }
|
||||
inline virtual OutputHandlerBase& pStyled(const String& styled) { return *this; }
|
||||
inline virtual OutputHandlerBase& pStyled(const TextStyleParser::tStyledString& styled) { return *this; }
|
||||
inline virtual OutputHandlerBase& pStyled(const TextStyleBuilder::IRichStringBase& styled) { return *this; }
|
||||
inline virtual OutputHandlerBase& pObject(const BaseObject& bo) { return *this; }
|
||||
|
||||
inline virtual OutputHandlerBase& p(const StringEditor& se) { return *this; }
|
||||
inline virtual OutputHandlerBase& p(const String& se) { return *this; }
|
||||
inline virtual OutputHandlerBase& p(uint8_t i) { return *this; }
|
||||
inline virtual OutputHandlerBase& p(int8_t i) { return *this; }
|
||||
inline virtual OutputHandlerBase& p(uint16_t i) { return *this; }
|
||||
|
|
@ -157,17 +157,17 @@ namespace ostd
|
|||
{
|
||||
public:
|
||||
OutputHandlerBase& bg(const ConsoleColors::tConsoleColor& color) override;
|
||||
OutputHandlerBase& bg(const StringEditor& color) override;
|
||||
OutputHandlerBase& bg(const String& color) override;
|
||||
OutputHandlerBase& fg(const ConsoleColors::tConsoleColor& color) override;
|
||||
OutputHandlerBase& fg(const StringEditor& color) override;
|
||||
OutputHandlerBase& fg(const String& color) override;
|
||||
|
||||
OutputHandlerBase& pChar(char c) override;
|
||||
OutputHandlerBase& pStyled(const StringEditor& styled) override;
|
||||
OutputHandlerBase& pStyled(const String& styled) override;
|
||||
OutputHandlerBase& pStyled(const TextStyleParser::tStyledString& styled) override;
|
||||
OutputHandlerBase& pStyled(const TextStyleBuilder::IRichStringBase& styled) override;
|
||||
OutputHandlerBase& pObject(const BaseObject& bo) override;
|
||||
|
||||
OutputHandlerBase& p(const StringEditor& se) override;
|
||||
OutputHandlerBase& p(const String& se) override;
|
||||
OutputHandlerBase& p(uint8_t i) override;
|
||||
OutputHandlerBase& p(int8_t i) override;
|
||||
OutputHandlerBase& p(uint16_t i) override;
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
#ifndef __LOGGER_HPP__
|
||||
#define __LOGGER_HPP__
|
||||
|
||||
#include <ostd/Types.hpp>
|
||||
#include <ostd/String.hpp>
|
||||
|
||||
namespace ostd
|
||||
{
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@
|
|||
#define __LOGIC_HPP__
|
||||
|
||||
#include <map>
|
||||
#include <ostd/Types.hpp>
|
||||
#include <ostd/String.hpp>
|
||||
|
||||
namespace ostd
|
||||
{
|
||||
|
|
|
|||
|
|
@ -1,10 +1,11 @@
|
|||
#pragma once
|
||||
|
||||
#include <ostd/Types.hpp>
|
||||
#include <cstring>
|
||||
|
||||
namespace ostd
|
||||
{
|
||||
class StringEditor
|
||||
class String
|
||||
{
|
||||
public: class Tokens
|
||||
{
|
||||
|
|
@ -33,75 +34,228 @@ namespace ostd
|
|||
uint32_t m_current_index;
|
||||
|
||||
public:
|
||||
inline static const String END = "%END%";
|
||||
inline static const cpp_string END = "%END%";
|
||||
|
||||
friend class StringEditor;
|
||||
friend class String;
|
||||
};
|
||||
|
||||
public: enum class ePaddingBehavior { ForceEvenPositive = 1, ForceEvenNegative, AllowOddExtraLeft, AllowOddExtraRight };
|
||||
public:
|
||||
inline StringEditor(void) { m_data = ""; }
|
||||
inline StringEditor(const String& str) { m_data = str; }
|
||||
inline StringEditor(const char* str) { m_data = str; }
|
||||
inline String str(void) const { return m_data; }
|
||||
inline uint32_t len(void) const { return m_data.length(); }
|
||||
inline StringEditor& clr(void) { m_data = ""; return *this; }
|
||||
inline String(void) { m_data = ""; }
|
||||
inline String(const cpp_string& str) { m_data = str; }
|
||||
inline String(const char* str) { m_data = str; }
|
||||
|
||||
inline cpp_string cpp_str(void) const { return m_data; }
|
||||
inline cpp_string& cpp_str_ref(void) { return m_data; }
|
||||
inline const char* c_str(void) const { return m_data.c_str(); }
|
||||
inline char at(uint32_t index) const { return m_data[index]; }
|
||||
inline uint32_t len(void) const { return m_data.length(); }
|
||||
inline char operator[](uint32_t index) const { return m_data[index]; }
|
||||
inline StringEditor& set(String str) { m_data = str; return *this; }
|
||||
inline bool operator== (const char* str2) const { return std::strcmp(c_str(), str2) == 0; }
|
||||
inline bool operator!= (const char* str2) const { return std::strcmp(c_str(), str2) != 0; }
|
||||
// friend bool operator== (const char* str1, const String& str2);
|
||||
// friend bool operator!= (const char* str1, const String& str2);
|
||||
inline String operator+(const String& str2) const { return m_data + str2.cpp_str(); }
|
||||
friend String operator+(const cpp_string& str1, const String& str);
|
||||
inline String& operator+=(const String& str2) { m_data += str2.cpp_str(); return *this; }
|
||||
inline String& operator+=(const char& c) { m_data += c; return *this; }
|
||||
inline operator std::string() const { return m_data; }
|
||||
inline operator const char*() const { return c_str(); }
|
||||
inline String& clr(void) { m_data = ""; return *this; }
|
||||
inline String& set(const cpp_string& str) { m_data = str; return *this; }
|
||||
|
||||
StringEditor& ltrim(void);
|
||||
StringEditor& rtrim(void);
|
||||
StringEditor& trim(void);
|
||||
inline auto begin(void) { return m_data.begin(); }
|
||||
inline auto end(void) { return m_data.end(); }
|
||||
inline auto cbegin(void) const { return m_data.begin(); }
|
||||
inline auto cend(void) const { return m_data.end(); }
|
||||
inline auto begin(void) const { return m_data.begin(); }
|
||||
inline auto end(void) const { return m_data.end(); }
|
||||
|
||||
StringEditor& toLower(void);
|
||||
StringEditor& toUpper(void);
|
||||
//Modifiers
|
||||
String& ltrim(void);
|
||||
String& rtrim(void);
|
||||
String& trim(void);
|
||||
String& toLower(void);
|
||||
String& toUpper(void);
|
||||
String& addPadding(uint32_t new_string_length, char c = ' ', ePaddingBehavior padding_behavior = ePaddingBehavior::ForceEvenPositive);
|
||||
String& addLeftPadding(uint32_t new_string_length, char c = ' ');
|
||||
String& addRightPadding(uint32_t new_string_length, char c = ' ');
|
||||
String& reverse(void);
|
||||
String& replaceAll(const String& what, const String& with);
|
||||
String& replaceFirst(const String& what, const String& with);
|
||||
String& regexReplace(const String& regex_pattern, const String& replace_with, bool case_insensitive = false);
|
||||
String& put(uint32_t index, char c);
|
||||
String& substr(uint32_t start, int32_t end = -1);
|
||||
|
||||
StringEditor& addPadding(char c = ' ', uint32_t count = 10);
|
||||
//StringEditor& add(const StringEditor& se);
|
||||
StringEditor& add(String str);
|
||||
StringEditor& add(char c);
|
||||
StringEditor& addi(uint8_t i);
|
||||
StringEditor& addi(int8_t i);
|
||||
StringEditor& addi(uint16_t i);
|
||||
StringEditor& addi(int16_t i);
|
||||
StringEditor& addi(uint32_t i);
|
||||
StringEditor& addi(int32_t i);
|
||||
StringEditor& addi(uint64_t i);
|
||||
StringEditor& addi(int64_t i);
|
||||
StringEditor& addf(float f);
|
||||
StringEditor& addf(double f);
|
||||
String& addChar(char c);
|
||||
String& add(const String& se);
|
||||
String& add(uint8_t i);
|
||||
String& add(int8_t i);
|
||||
String& add(uint16_t i);
|
||||
String& add(int16_t i);
|
||||
String& add(uint32_t i);
|
||||
String& add(int32_t i);
|
||||
String& add(uint64_t i);
|
||||
String& add(int64_t i);
|
||||
String& add(float f, uint8_t precision = 0);
|
||||
String& add(double f, uint8_t precision = 0);
|
||||
|
||||
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);
|
||||
//New String
|
||||
String new_ltrim(void) const;
|
||||
String new_rtrim(void) const;
|
||||
String new_trim(void) const;
|
||||
String new_toLower(void) const;
|
||||
String new_toUpper(void) const;
|
||||
String new_addPadding(uint32_t new_string_length, char c = ' ', ePaddingBehavior padding_behavior = ePaddingBehavior::ForceEvenPositive) const;
|
||||
String new_addLeftPadding(uint32_t new_string_length, char c = ' ') const;
|
||||
String new_addRightPadding(uint32_t new_string_length, char c = ' ') const;
|
||||
String new_reverse(void) const;
|
||||
String new_replaceAll(const String& what, const String& with) const;
|
||||
String new_replaceFirst(const String& what, const String& with) const;
|
||||
String new_regexReplace(const String& regex_pattern, const String& replace_with, bool case_insensitive = false) const;
|
||||
String new_put(uint32_t index, char c) const;
|
||||
String new_substr(uint32_t start, int32_t end = -1) const;
|
||||
|
||||
StringEditor& put(uint32_t index, char c);
|
||||
String new_addChar(char c);
|
||||
String new_add(const String& se);
|
||||
String new_add(uint8_t i);
|
||||
String new_add(int8_t i);
|
||||
String new_add(uint16_t i);
|
||||
String new_add(int16_t i);
|
||||
String new_add(uint32_t i);
|
||||
String new_add(int32_t i);
|
||||
String new_add(uint64_t i);
|
||||
String new_add(int64_t i);
|
||||
String new_add(float f, uint8_t precision = 0);
|
||||
String new_add(double f, uint8_t precision = 0);
|
||||
|
||||
int64_t toInt(void);
|
||||
float toFloat(void);
|
||||
double toDouble(void);
|
||||
bool isNumeric(bool decimal = false);
|
||||
bool contains(char c);
|
||||
bool contains(String str);
|
||||
bool startsWith(String str);
|
||||
bool endsWith(String str);
|
||||
String getReverse(void);
|
||||
//Utility
|
||||
int64_t toInt(void) const;
|
||||
float toFloat(void) const;
|
||||
double toDouble(void) const;
|
||||
bool isNumeric(bool decimal = false) const;
|
||||
bool contains(char c) const;
|
||||
bool contains(const String& str) const;
|
||||
bool startsWith(const String& str) const;
|
||||
bool endsWith(const String& str) const;
|
||||
uint32_t count(const String& str) const;
|
||||
int32_t indexOf(char c, uint32_t start = 0) const;
|
||||
int32_t indexOf(const String& str, uint32_t start = 0) const;
|
||||
int32_t lastIndexOf(char c) const;
|
||||
int32_t lastIndexOf(const String& str) const;
|
||||
Tokens tokenize(const String& delimiter = " ", bool trim_tokens = true, bool allow_white_space_only_tokens = false) const;
|
||||
|
||||
uint32_t count(String str);
|
||||
|
||||
int32_t indexOf(char c, uint32_t start = 0);
|
||||
int32_t indexOf(String str, uint32_t start = 0);
|
||||
int32_t lastIndexOf(char c);
|
||||
int32_t lastIndexOf(String str);
|
||||
|
||||
String substr(uint32_t start, int32_t end = -1);
|
||||
Tokens tokenize(String delimiter = " ", bool trim_tokens = true, bool allow_white_space_only_tokens = false);
|
||||
|
||||
friend std::ostream& operator<<(std::ostream& out, const StringEditor& val);
|
||||
friend std::ostream& operator<<(std::ostream& out, const String& val);
|
||||
|
||||
private:
|
||||
String m_data;
|
||||
ostd::cpp_string m_data;
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
namespace legacy
|
||||
{
|
||||
class StringEditor
|
||||
{
|
||||
public: class Tokens
|
||||
{
|
||||
public:
|
||||
inline bool hasNext(void) { return m_tokens.size() > 0 && m_current_index < m_tokens.size(); }
|
||||
inline bool hasPrevious(void) { return m_tokens.size() > 0 && m_current_index > 0; }
|
||||
String next(void);
|
||||
String previous(void);
|
||||
inline uint32_t count(void) { return m_tokens.size(); }
|
||||
inline std::vector<String> getRawData(void) { return m_tokens; }
|
||||
inline uint32_t getCurrentIndex(void) { return m_current_index; }
|
||||
inline void cycle(void) { m_current_index = 0; }
|
||||
|
||||
inline auto begin(void) { return m_tokens.begin(); }
|
||||
inline auto end(void) { return m_tokens.end(); }
|
||||
inline auto cbegin(void) const { return m_tokens.begin(); }
|
||||
inline auto cend(void) const { return m_tokens.end(); }
|
||||
inline auto begin(void) const { return m_tokens.begin(); }
|
||||
inline auto end(void) const { return m_tokens.end(); }
|
||||
|
||||
private:
|
||||
inline Tokens(void) { m_current_index = 0; }
|
||||
|
||||
private:
|
||||
std::vector<String> m_tokens;
|
||||
uint32_t m_current_index;
|
||||
|
||||
public:
|
||||
inline static const String END = "%END%";
|
||||
|
||||
friend class StringEditor;
|
||||
};
|
||||
|
||||
public:
|
||||
inline StringEditor(void) { m_data = ""; }
|
||||
inline StringEditor(const String& str) { m_data = str; }
|
||||
inline StringEditor(const char* str) { m_data = str; }
|
||||
inline String str(void) const { return m_data; }
|
||||
inline uint32_t len(void) const { return m_data.length(); }
|
||||
inline StringEditor& clr(void) { m_data = ""; return *this; }
|
||||
inline const char* c_str(void) const { return m_data.c_str(); }
|
||||
inline char at(uint32_t index) const { return m_data[index]; }
|
||||
inline char operator[](uint32_t index) const { return m_data[index]; }
|
||||
inline StringEditor& set(String str) { m_data = str; return *this; }
|
||||
|
||||
StringEditor& ltrim(void);
|
||||
StringEditor& rtrim(void);
|
||||
StringEditor& trim(void);
|
||||
|
||||
StringEditor& toLower(void);
|
||||
StringEditor& toUpper(void);
|
||||
|
||||
StringEditor& addPadding(char c = ' ', uint32_t count = 10);
|
||||
//StringEditor& add(const StringEditor& se);
|
||||
StringEditor& add(String str);
|
||||
StringEditor& add(char c);
|
||||
StringEditor& addi(uint8_t i);
|
||||
StringEditor& addi(int8_t i);
|
||||
StringEditor& addi(uint16_t i);
|
||||
StringEditor& addi(int16_t i);
|
||||
StringEditor& addi(uint32_t i);
|
||||
StringEditor& addi(int32_t i);
|
||||
StringEditor& addi(uint64_t i);
|
||||
StringEditor& addi(int64_t i);
|
||||
StringEditor& addf(float f);
|
||||
StringEditor& addf(double f);
|
||||
|
||||
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);
|
||||
|
||||
int64_t toInt(void);
|
||||
float toFloat(void);
|
||||
double toDouble(void);
|
||||
bool isNumeric(bool decimal = false);
|
||||
bool contains(char c);
|
||||
bool contains(String str);
|
||||
bool startsWith(String str);
|
||||
bool endsWith(String str);
|
||||
String getReverse(void);
|
||||
|
||||
uint32_t count(String str);
|
||||
|
||||
int32_t indexOf(char c, uint32_t start = 0);
|
||||
int32_t indexOf(String str, uint32_t start = 0);
|
||||
int32_t lastIndexOf(char c);
|
||||
int32_t lastIndexOf(String str);
|
||||
|
||||
String substr(uint32_t start, int32_t end = -1);
|
||||
Tokens tokenize(String delimiter = " ", bool trim_tokens = true, bool allow_white_space_only_tokens = false);
|
||||
|
||||
friend std::ostream& operator<<(std::ostream& out, const StringEditor& val);
|
||||
|
||||
private:
|
||||
String m_data;
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -5,7 +5,7 @@
|
|||
|
||||
namespace ostd
|
||||
{
|
||||
class StringEditor;
|
||||
class String;
|
||||
class TextStyleParser
|
||||
{
|
||||
public: enum class eBlockParserReturnValue { CloseBlock = 0, ValidBlock, InvalidBlock };
|
||||
|
|
@ -28,10 +28,10 @@ namespace ostd
|
|||
};
|
||||
|
||||
public:
|
||||
static tStyledString parse(const StringEditor& styledString);
|
||||
static tStyledString parse(const StringEditor& styledString, tColor defaultBackgorundColor, tColor defaultForegroundColor);
|
||||
static tStyledString parse(const String& styledString);
|
||||
static tStyledString parse(const String& styledString, tColor defaultBackgorundColor, tColor defaultForegroundColor);
|
||||
|
||||
static tColor convertColor(const StringEditor& name);
|
||||
static tColor convertColor(const String& name);
|
||||
|
||||
private:
|
||||
static bool test_for_block(const String& block_part);
|
||||
|
|
@ -90,13 +90,13 @@ namespace ostd
|
|||
public: class Regex : public IRichStringBase {
|
||||
public:
|
||||
inline Regex(void) { m_rawString = ""; }
|
||||
inline Regex(const StringEditor& rawString) { setRawString(rawString); }
|
||||
inline Regex(const String& rawString) { setRawString(rawString); }
|
||||
inline String getRawString(void) const { return m_rawString; }
|
||||
Regex& setRawString(const StringEditor& rawString);
|
||||
Regex& setRawString(const String& 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);
|
||||
Regex& fg(const String& regex, const String& foreground_color, bool case_insensitive = false);
|
||||
Regex& bg(const String& regex, const String& background_color, bool case_insensitive = false);
|
||||
Regex& col(const String& regex, const String& foreground_color, const String& background_color, bool case_insensitive = false);
|
||||
|
||||
TextStyleParser::tStyledString getStyledString(void) const override;
|
||||
|
||||
|
|
|
|||
|
|
@ -7,7 +7,11 @@
|
|||
|
||||
namespace ostd
|
||||
{
|
||||
typedef std::string String;
|
||||
namespace legacy
|
||||
{
|
||||
typedef std::string String;
|
||||
}
|
||||
typedef std::string cpp_string;
|
||||
|
||||
typedef int64_t QWord;
|
||||
typedef int32_t DWord;
|
||||
|
|
@ -21,13 +25,6 @@ namespace ostd
|
|||
|
||||
typedef uint32_t StreamIndex;
|
||||
|
||||
typedef uint32_t TextureAtlasIndex;
|
||||
typedef uint32_t ResourceID;
|
||||
|
||||
typedef uint32_t WidgetID;
|
||||
|
||||
typedef uint32_t LayerID;
|
||||
|
||||
typedef union {
|
||||
float val;
|
||||
Byte data[4];
|
||||
|
|
@ -50,12 +47,6 @@ namespace ostd
|
|||
static inline const uint8_t DOUBLE = 8;
|
||||
};
|
||||
|
||||
struct TextureID
|
||||
{
|
||||
ResourceID texture { 0 };
|
||||
TextureAtlasIndex tile { 0 };
|
||||
};
|
||||
|
||||
typedef std::vector<Byte> ByteStream;
|
||||
}
|
||||
|
||||
|
|
|
|||
Binary file not shown.
2
build.nr
2
build.nr
|
|
@ -1 +1 @@
|
|||
1802
|
||||
1808
|
||||
|
|
|
|||
|
|
@ -7,3 +7,9 @@ if [ "$(expr substr $(uname -s) 1 5)" == "Linux" ]; then
|
|||
elif [ "$(expr substr $(uname -s) 1 10)" == "MINGW64_NT" ]; then
|
||||
mingw32-make.exe
|
||||
fi
|
||||
|
||||
if [ $? -eq 0 ]; then
|
||||
printf "\n==============================================[ Running Test ]==============================================\n\n"
|
||||
./ostd_test
|
||||
printf "\n============================================================================================================\n\n"
|
||||
fi
|
||||
|
|
@ -46,7 +46,7 @@ namespace ostd
|
|||
|
||||
String BaseObject::getObjectHeaderString(void) const
|
||||
{
|
||||
return StringEditor(getTypeName()).add("->uid=").addi(getID()).add("/oid=").addi(getCompareOID()).add("/valid=").add(STR_BOOL(isValid())).str();
|
||||
return getTypeName().add("->uid=").add(getID()).add("/oid=").add(getCompareOID()).add("/valid=").add(STR_BOOL(isValid()));
|
||||
}
|
||||
|
||||
void BaseObject::connectSignal(uint32_t signal_id)
|
||||
|
|
|
|||
|
|
@ -32,7 +32,7 @@ namespace ostd
|
|||
|
||||
inline static BaseObject& InvalidRef(void) { return BaseObject::s_invalid_obj; }
|
||||
|
||||
inline void setTypeName(const StringEditor& tn) { m_typeName = tn.str(); }
|
||||
inline void setTypeName(const String& tn) { m_typeName = tn; }
|
||||
inline String getTypeName(void) const { return m_typeName; }
|
||||
String getObjectHeaderString(void) const;
|
||||
|
||||
|
|
|
|||
|
|
@ -28,7 +28,7 @@ namespace ostd
|
|||
BaseObject::setValid(true);
|
||||
}
|
||||
|
||||
Color::Color(const StringEditor& color_string)
|
||||
Color::Color(const String& color_string)
|
||||
{
|
||||
set(color_string);
|
||||
setTypeName("ox::Color");
|
||||
|
|
@ -97,21 +97,21 @@ namespace ostd
|
|||
return *this;
|
||||
}
|
||||
|
||||
Color& Color::set(const StringEditor& color_string)
|
||||
Color& Color::set(const String& color_string)
|
||||
{
|
||||
StringEditor se(color_string);
|
||||
String se(color_string);
|
||||
se.trim();
|
||||
r = g = b = 0;
|
||||
a = 255;
|
||||
if (se.startsWith("#"))
|
||||
{
|
||||
StringEditor tmp = se.substr(1);
|
||||
String tmp = se.new_substr(1);
|
||||
tmp.trim();
|
||||
se = "0x" + tmp.str();
|
||||
se = String("0x") + tmp;
|
||||
}
|
||||
if (se.startsWith("0x"))
|
||||
{
|
||||
int64_t ic = Utils::strToInt(se.str());
|
||||
int64_t ic = Utils::strToInt(se);
|
||||
union uC32 {
|
||||
uint8_t data[4];
|
||||
uint32_t value;
|
||||
|
|
@ -154,7 +154,7 @@ namespace ostd
|
|||
return *this;
|
||||
}
|
||||
|
||||
StringEditor Color::hexString(bool include_alpha, StringEditor prefix) const
|
||||
String Color::hexString(bool include_alpha, String prefix) const
|
||||
{
|
||||
String hex = "";
|
||||
hex += Utils::getHexStr(r, false, 1);
|
||||
|
|
@ -162,23 +162,23 @@ namespace ostd
|
|||
hex += Utils::getHexStr(b, false, 1);
|
||||
if (include_alpha)
|
||||
hex += Utils::getHexStr(a, false, 1);
|
||||
hex = prefix.str() + StringEditor(hex).toUpper().str();
|
||||
hex = prefix + String(hex).toUpper();
|
||||
return hex;
|
||||
}
|
||||
|
||||
StringEditor Color::rgbString(bool include_parenthesis, bool include_alpha) const
|
||||
String Color::rgbString(bool include_parenthesis, bool include_alpha) const
|
||||
{
|
||||
StringEditor rgb = "";
|
||||
String rgb = "";
|
||||
if (include_parenthesis)
|
||||
rgb.add("(");
|
||||
rgb.addi(r).add(", ");
|
||||
rgb.addi(g).add(", ");
|
||||
rgb.addi(b);
|
||||
rgb.add(r).add(", ");
|
||||
rgb.add(g).add(", ");
|
||||
rgb.add(b);
|
||||
if (include_alpha)
|
||||
rgb.add(", ").addi(a);
|
||||
rgb.add(", ").add(a);
|
||||
if (include_parenthesis)
|
||||
rgb.add(")");
|
||||
return rgb.str();
|
||||
return rgb;
|
||||
}
|
||||
|
||||
uint32_t Color::asInteger(void) const
|
||||
|
|
@ -201,7 +201,7 @@ namespace ostd
|
|||
|
||||
String Color::toString(void) const
|
||||
{
|
||||
return hexString(true, "#").str() + " -> rgba" + rgbString(true, true).str();
|
||||
return hexString(true, "#") + " -> rgba" + rgbString(true, true);
|
||||
}
|
||||
|
||||
void Color::print(bool newLine, OutputHandlerBase* __destination) const
|
||||
|
|
|
|||
|
|
@ -25,7 +25,7 @@ namespace ostd
|
|||
Color(void);
|
||||
Color(uint8_t rgb_single_value, uint8_t alpha = 255);
|
||||
Color(uint8_t _r, uint8_t _g, uint8_t _b, uint8_t alpha = 255);
|
||||
Color(const StringEditor& color_string);
|
||||
Color(const String& color_string);
|
||||
Color(const FloatCol& normalized_color);
|
||||
Color(const Color& copy);
|
||||
|
||||
|
|
@ -36,11 +36,11 @@ namespace ostd
|
|||
Color& set(void);
|
||||
Color& set(uint8_t rgb_single_value, uint8_t alpha = 255);
|
||||
Color& set(uint8_t _r, uint8_t _g, uint8_t _b, uint8_t alpha = 255);
|
||||
Color& set(const StringEditor& color_string);
|
||||
Color& set(const String& color_string);
|
||||
Color& set(const FloatCol& normalized_color);
|
||||
|
||||
StringEditor hexString(bool include_alpha = false, StringEditor prefix = "0x") const;
|
||||
StringEditor rgbString(bool include_parenthesis = true, bool include_alpha = false) const;
|
||||
String hexString(bool include_alpha = false, String prefix = "0x") const;
|
||||
String rgbString(bool include_parenthesis = true, bool include_alpha = false) const;
|
||||
uint32_t asInteger(void) const;
|
||||
FloatCol getNormalizedColor(void) const;
|
||||
|
||||
|
|
|
|||
|
|
@ -118,7 +118,7 @@ OutputHandlerBase& InteractiveConsole::bg(const ConsoleColors::tConsoleColor& co
|
|||
return *this;
|
||||
}
|
||||
|
||||
OutputHandlerBase& InteractiveConsole::bg(const StringEditor& color)
|
||||
OutputHandlerBase& InteractiveConsole::bg(const String& color)
|
||||
{
|
||||
std::cout << ConsoleColors::getFromName(color, true);
|
||||
return *this;
|
||||
|
|
@ -130,7 +130,7 @@ OutputHandlerBase& InteractiveConsole::fg(const ConsoleColors::tConsoleColor& co
|
|||
return *this;
|
||||
}
|
||||
|
||||
OutputHandlerBase& InteractiveConsole::fg(const StringEditor& color)
|
||||
OutputHandlerBase& InteractiveConsole::fg(const String& color)
|
||||
{
|
||||
std::cout << ConsoleColors::getFromName(color, false);
|
||||
return *this;
|
||||
|
|
@ -142,9 +142,9 @@ OutputHandlerBase& InteractiveConsole::pChar(char c)
|
|||
return *this;
|
||||
}
|
||||
|
||||
OutputHandlerBase& InteractiveConsole::pStyled(const StringEditor& styled)
|
||||
OutputHandlerBase& InteractiveConsole::pStyled(const String& styled)
|
||||
{
|
||||
return pStyled(TextStyleParser::parse(styled.str()));
|
||||
return pStyled(TextStyleParser::parse(styled));
|
||||
}
|
||||
|
||||
OutputHandlerBase& InteractiveConsole::pStyled(const TextStyleParser::tStyledString& styled)
|
||||
|
|
@ -165,7 +165,7 @@ OutputHandlerBase& InteractiveConsole::pObject(const BaseObject& bo)
|
|||
return *this;
|
||||
}
|
||||
|
||||
OutputHandlerBase& InteractiveConsole::p(const StringEditor& se)
|
||||
OutputHandlerBase& InteractiveConsole::p(const String& se)
|
||||
{
|
||||
std::cout << se;
|
||||
return *this;
|
||||
|
|
|
|||
|
|
@ -19,17 +19,17 @@ namespace ostd
|
|||
InteractiveConsole(bool clearOnStart, eMode mode = eMode::Direct);
|
||||
|
||||
OutputHandlerBase& bg(const ConsoleColors::tConsoleColor& color) override;
|
||||
OutputHandlerBase& bg(const StringEditor& color) override;
|
||||
OutputHandlerBase& bg(const String& color) override;
|
||||
OutputHandlerBase& fg(const ConsoleColors::tConsoleColor& color) override;
|
||||
OutputHandlerBase& fg(const StringEditor& color) override;
|
||||
OutputHandlerBase& fg(const String& color) override;
|
||||
|
||||
OutputHandlerBase& pChar(char c) override;
|
||||
OutputHandlerBase& pStyled(const StringEditor& styled) override;
|
||||
OutputHandlerBase& pStyled(const String& styled) override;
|
||||
OutputHandlerBase& pStyled(const TextStyleParser::tStyledString& styled) override;
|
||||
OutputHandlerBase& pStyled(const TextStyleBuilder::IRichStringBase& styled) override;
|
||||
OutputHandlerBase& pObject(const BaseObject& bo) override;
|
||||
|
||||
OutputHandlerBase& p(const StringEditor& se) override;
|
||||
OutputHandlerBase& p(const String& se) override;
|
||||
OutputHandlerBase& p(uint8_t i) override;
|
||||
OutputHandlerBase& p(int8_t i) override;
|
||||
OutputHandlerBase& p(uint16_t i) override;
|
||||
|
|
|
|||
|
|
@ -23,14 +23,14 @@ namespace ostd
|
|||
errorMessage += extraMessage + "\n";
|
||||
if (userData.isValid())
|
||||
errorMessage += "USER_DATA:\n" + userData.toString() + "\n";
|
||||
StringEditor msgEdit = "";
|
||||
String msgEdit = "";
|
||||
msgEdit.add(Utils::getHexStr(m_errGroup)).add("//");
|
||||
msgEdit.add(Utils::getHexStr(m_errCode, true, 8)).add(" :: ");
|
||||
if (_file_name != "")
|
||||
{
|
||||
msgEdit.add(_file_name).add(" ");
|
||||
if (_line_num > 0)
|
||||
msgEdit.add("[LINE: ").addi(_line_num).add("]\n");
|
||||
msgEdit.add("[LINE: ").add(_line_num).add("]\n");
|
||||
else
|
||||
msgEdit.add("\n");
|
||||
}
|
||||
|
|
@ -43,15 +43,15 @@ namespace ostd
|
|||
}
|
||||
if (m_errLevel == tErrorLevel::Warning)
|
||||
{
|
||||
OX_WARN(msgEdit.str());
|
||||
OX_WARN(msgEdit);
|
||||
}
|
||||
else if (m_errLevel == tErrorLevel::Error)
|
||||
{
|
||||
OX_ERROR(msgEdit.str());
|
||||
OX_ERROR(msgEdit);
|
||||
}
|
||||
else if (m_errLevel == tErrorLevel::Fatal)
|
||||
{
|
||||
OX_FATAL(msgEdit.str());
|
||||
OX_FATAL(msgEdit);
|
||||
}
|
||||
if (tmpOutHndl != nullptr)
|
||||
Logger::setOutputHandler(*tmpOutHndl);
|
||||
|
|
|
|||
|
|
@ -24,18 +24,18 @@ namespace ostd
|
|||
if (!std::filesystem::is_regular_file(filePath)) return *this;
|
||||
std::ifstream file(filePath, std::ios::in | std::ios::binary);
|
||||
if (!file.is_open()) return *this;
|
||||
m_buffer = { std::istreambuf_iterator<char>(file), std::istreambuf_iterator<char>() };
|
||||
m_buffer.set({ std::istreambuf_iterator<char>(file), std::istreambuf_iterator<char>() });
|
||||
|
||||
m_extension = filePath.extension().string();
|
||||
m_fullPath = filePath.string();
|
||||
m_fullName = filePath.filename().string();
|
||||
m_name = filePath.stem().string();
|
||||
StringEditor pathEditor(m_fullPath);
|
||||
String pathEditor(m_fullPath);
|
||||
if (pathEditor.contains('/'))
|
||||
m_directoryPath = pathEditor.substr(0, pathEditor.lastIndexOf('/'));
|
||||
m_directoryPath = pathEditor.new_substr(0, pathEditor.lastIndexOf('/'));
|
||||
pathEditor = m_directoryPath;
|
||||
if (pathEditor.contains('/'))
|
||||
m_directoryName = pathEditor.substr(pathEditor.lastIndexOf('/') + 1);
|
||||
m_directoryName = pathEditor.new_substr(pathEditor.lastIndexOf('/') + 1);
|
||||
validate();
|
||||
return *this;
|
||||
}
|
||||
|
|
@ -43,7 +43,7 @@ namespace ostd
|
|||
std::vector<String> TextFileBuffer::getLines(const String& separator, bool trim_tokens, bool allow_white_space_only_tokens)
|
||||
{
|
||||
if (!exists()) return { };
|
||||
auto st = StringEditor(m_buffer);
|
||||
auto st = String(m_buffer);
|
||||
return st.tokenize(separator, trim_tokens, allow_white_space_only_tokens).getRawData();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ namespace ostd
|
|||
std::vector<std::filesystem::path> Utils::listFilesInDirectory(const String& directoryPath)
|
||||
{
|
||||
std::vector<std::filesystem::path> list;
|
||||
for (const auto& file : std::filesystem::directory_iterator(directoryPath))
|
||||
for (const auto& file : std::filesystem::directory_iterator(directoryPath.cpp_str()))
|
||||
{
|
||||
if (!std::filesystem::is_directory(file.path()))
|
||||
list.push_back(file.path());
|
||||
|
|
@ -16,7 +16,7 @@ namespace ostd
|
|||
std::vector<std::filesystem::path> Utils::listDirectoriesInDirectory(const String& directoryPath)
|
||||
{
|
||||
std::vector<std::filesystem::path> list;
|
||||
for (const auto& file : std::filesystem::directory_iterator(directoryPath))
|
||||
for (const auto& file : std::filesystem::directory_iterator(directoryPath.cpp_str()))
|
||||
{
|
||||
if (std::filesystem::is_directory(file.path()))
|
||||
list.push_back(file.path());
|
||||
|
|
@ -27,7 +27,7 @@ namespace ostd
|
|||
std::vector<std::filesystem::path> Utils::listDirectory(const String& directoryPath)
|
||||
{
|
||||
std::vector<std::filesystem::path> list;
|
||||
for (const auto& file : std::filesystem::directory_iterator(directoryPath))
|
||||
for (const auto& file : std::filesystem::directory_iterator(directoryPath.cpp_str()))
|
||||
list.push_back(file.path());
|
||||
return list;
|
||||
}
|
||||
|
|
@ -35,7 +35,7 @@ namespace ostd
|
|||
std::vector<std::filesystem::path> Utils::listFilesInDirectoryRecursive(const String& directoryPath)
|
||||
{
|
||||
std::vector<std::filesystem::path> list;
|
||||
for (const auto& file : std::filesystem::recursive_directory_iterator(directoryPath))
|
||||
for (const auto& file : std::filesystem::recursive_directory_iterator(directoryPath.cpp_str()))
|
||||
{
|
||||
if (!std::filesystem::is_directory(file.path()))
|
||||
list.push_back(file.path());
|
||||
|
|
@ -46,7 +46,7 @@ namespace ostd
|
|||
std::vector<std::filesystem::path> Utils::listDirectoriesInDirectoryRecursive(const String& directoryPath)
|
||||
{
|
||||
std::vector<std::filesystem::path> list;
|
||||
for (const auto& file : std::filesystem::recursive_directory_iterator(directoryPath))
|
||||
for (const auto& file : std::filesystem::recursive_directory_iterator(directoryPath.cpp_str()))
|
||||
{
|
||||
if (std::filesystem::is_directory(file.path()))
|
||||
list.push_back(file.path());
|
||||
|
|
@ -57,7 +57,7 @@ namespace ostd
|
|||
std::vector<std::filesystem::path> Utils::listDirectoryRecursive(const String& directoryPath)
|
||||
{
|
||||
std::vector<std::filesystem::path> list;
|
||||
for (const auto& file : std::filesystem::recursive_directory_iterator(directoryPath))
|
||||
for (const auto& file : std::filesystem::recursive_directory_iterator(directoryPath.cpp_str()))
|
||||
list.push_back(file.path());
|
||||
return list;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,10 +7,10 @@ namespace ostd
|
|||
{
|
||||
String Vec2::toString(void) const
|
||||
{
|
||||
return StringEditor("{ ").addf(x).add(", ").addf(y).add(" }").str();
|
||||
return String("{ ").add(x).add(", ").add(y).add(" }");
|
||||
}
|
||||
String Vec3::toString(void) const
|
||||
{
|
||||
return StringEditor("{ ").addf(x).add(", ").addf(y).add(", ").addf(z).add(" }").str();
|
||||
return String("{ ").add(x).add(", ").add(y).add(", ").add(z).add(" }");
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@
|
|||
#include <cmath>
|
||||
#include <algorithm>
|
||||
#include <cstdint>
|
||||
#include <ostd/Types.hpp>
|
||||
#include <ostd/String.hpp>
|
||||
|
||||
namespace ostd
|
||||
{
|
||||
|
|
|
|||
|
|
@ -60,46 +60,46 @@ namespace ostd
|
|||
inline static const tConsoleColor OnBrightGray { "brightgray", { 150, 150, 150, 255 }, true};
|
||||
inline static const tConsoleColor OnWhite { "white", { 255, 255, 255, 255 }, true};
|
||||
|
||||
inline static const tConsoleColor getFromName(const StringEditor& name, bool background = false) {
|
||||
StringEditor edit = name;
|
||||
inline static const tConsoleColor getFromName(const String& name, bool background = false) {
|
||||
String edit = name;
|
||||
edit.toLower().trim();
|
||||
if (edit.str() == Red.name) return (background ? OnRed : Red);
|
||||
if (edit.str() == BrightRed.name) return (background ? OnBrightRed : BrightRed);
|
||||
if (edit.str() == Green.name) return (background ? OnGreen : Green);
|
||||
if (edit.str() == BrightGreen.name) return (background ? OnBrightGreen : BrightGreen);
|
||||
if (edit.str() == Blue.name) return (background ? OnBlue : Blue);
|
||||
if (edit.str() == BrightBlue.name) return (background ? OnBrightBlue : BrightBlue);
|
||||
if (edit.str() == Magenta.name) return (background ? OnMagenta : Magenta);
|
||||
if (edit.str() == BrightMagenta.name) return (background ? OnBrightMagenta : BrightMagenta);
|
||||
if (edit.str() == Cyan.name) return (background ? OnCyan : Cyan);
|
||||
if (edit.str() == BrightCyan.name) return (background ? OnBrightCyan : BrightCyan);
|
||||
if (edit.str() == Yellow.name) return (background ? OnYellow : Yellow);
|
||||
if (edit.str() == BrightYellow.name) return (background ? OnBrightYellow : BrightYellow);
|
||||
if (edit.str() == Black.name) return (background ? OnBlack : Black);
|
||||
if (edit.str() == Gray.name) return (background ? OnGray : Gray);
|
||||
if (edit.str() == BrightGray.name) return (background ? OnBrightGray : BrightGray);
|
||||
if (edit.str() == White.name) return (background ? OnWhite : White);
|
||||
if (edit == Red.name) return (background ? OnRed : Red);
|
||||
if (edit == BrightRed.name) return (background ? OnBrightRed : BrightRed);
|
||||
if (edit == Green.name) return (background ? OnGreen : Green);
|
||||
if (edit == BrightGreen.name) return (background ? OnBrightGreen : BrightGreen);
|
||||
if (edit == Blue.name) return (background ? OnBlue : Blue);
|
||||
if (edit == BrightBlue.name) return (background ? OnBrightBlue : BrightBlue);
|
||||
if (edit == Magenta.name) return (background ? OnMagenta : Magenta);
|
||||
if (edit == BrightMagenta.name) return (background ? OnBrightMagenta : BrightMagenta);
|
||||
if (edit == Cyan.name) return (background ? OnCyan : Cyan);
|
||||
if (edit == BrightCyan.name) return (background ? OnBrightCyan : BrightCyan);
|
||||
if (edit == Yellow.name) return (background ? OnYellow : Yellow);
|
||||
if (edit == BrightYellow.name) return (background ? OnBrightYellow : BrightYellow);
|
||||
if (edit == Black.name) return (background ? OnBlack : Black);
|
||||
if (edit == Gray.name) return (background ? OnGray : Gray);
|
||||
if (edit == BrightGray.name) return (background ? OnBrightGray : BrightGray);
|
||||
if (edit == White.name) return (background ? OnWhite : White);
|
||||
return (background ? OnBlack : Black);
|
||||
}
|
||||
inline static bool isConsoleColor(const StringEditor& name) {
|
||||
StringEditor edit = name;
|
||||
inline static bool isConsoleColor(const String& name) {
|
||||
String edit = name;
|
||||
edit.toLower().trim();
|
||||
if (edit.str() == Red.name) return true;
|
||||
if (edit.str() == BrightRed.name) return true;
|
||||
if (edit.str() == Green.name) return true;
|
||||
if (edit.str() == BrightGreen.name) return true;
|
||||
if (edit.str() == Blue.name) return true;
|
||||
if (edit.str() == BrightBlue.name) return true;
|
||||
if (edit.str() == Magenta.name) return true;
|
||||
if (edit.str() == BrightMagenta.name) return true;
|
||||
if (edit.str() == Cyan.name) return true;
|
||||
if (edit.str() == BrightCyan.name) return true;
|
||||
if (edit.str() == Yellow.name) return true;
|
||||
if (edit.str() == BrightYellow.name) return true;
|
||||
if (edit.str() == Black.name) return true;
|
||||
if (edit.str() == Gray.name) return true;
|
||||
if (edit.str() == BrightGray.name) return true;
|
||||
if (edit.str() == White.name) return true;
|
||||
if (edit == Red.name) return true;
|
||||
if (edit == BrightRed.name) return true;
|
||||
if (edit == Green.name) return true;
|
||||
if (edit == BrightGreen.name) return true;
|
||||
if (edit == Blue.name) return true;
|
||||
if (edit == BrightBlue.name) return true;
|
||||
if (edit == Magenta.name) return true;
|
||||
if (edit == BrightMagenta.name) return true;
|
||||
if (edit == Cyan.name) return true;
|
||||
if (edit == BrightCyan.name) return true;
|
||||
if (edit == Yellow.name) return true;
|
||||
if (edit == BrightYellow.name) return true;
|
||||
if (edit == Black.name) return true;
|
||||
if (edit == Gray.name) return true;
|
||||
if (edit == BrightGray.name) return true;
|
||||
if (edit == White.name) return true;
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
|
@ -111,18 +111,18 @@ namespace ostd
|
|||
|
||||
inline virtual OutputHandlerBase& bg(const Color& color) { return *this; }
|
||||
inline virtual OutputHandlerBase& bg(const ConsoleColors::tConsoleColor& color) { return *this; }
|
||||
inline virtual OutputHandlerBase& bg(const StringEditor& color) { return *this; }
|
||||
inline virtual OutputHandlerBase& bg(const String& color) { return *this; }
|
||||
inline virtual OutputHandlerBase& fg(const Color& color) { return *this; }
|
||||
inline virtual OutputHandlerBase& fg(const ConsoleColors::tConsoleColor& color) { return *this; }
|
||||
inline virtual OutputHandlerBase& fg(const StringEditor& color) { return *this; }
|
||||
inline virtual OutputHandlerBase& fg(const String& color) { return *this; }
|
||||
|
||||
inline virtual OutputHandlerBase& pChar(char c) { return *this; }
|
||||
inline virtual OutputHandlerBase& pStyled(const StringEditor& styled) { return *this; }
|
||||
inline virtual OutputHandlerBase& pStyled(const String& styled) { return *this; }
|
||||
inline virtual OutputHandlerBase& pStyled(const TextStyleParser::tStyledString& styled) { return *this; }
|
||||
inline virtual OutputHandlerBase& pStyled(const TextStyleBuilder::IRichStringBase& styled) { return *this; }
|
||||
inline virtual OutputHandlerBase& pObject(const BaseObject& bo) { return *this; }
|
||||
|
||||
inline virtual OutputHandlerBase& p(const StringEditor& se) { return *this; }
|
||||
inline virtual OutputHandlerBase& p(const String& se) { return *this; }
|
||||
inline virtual OutputHandlerBase& p(uint8_t i) { return *this; }
|
||||
inline virtual OutputHandlerBase& p(int8_t i) { return *this; }
|
||||
inline virtual OutputHandlerBase& p(uint16_t i) { return *this; }
|
||||
|
|
@ -157,17 +157,17 @@ namespace ostd
|
|||
{
|
||||
public:
|
||||
OutputHandlerBase& bg(const ConsoleColors::tConsoleColor& color) override;
|
||||
OutputHandlerBase& bg(const StringEditor& color) override;
|
||||
OutputHandlerBase& bg(const String& color) override;
|
||||
OutputHandlerBase& fg(const ConsoleColors::tConsoleColor& color) override;
|
||||
OutputHandlerBase& fg(const StringEditor& color) override;
|
||||
OutputHandlerBase& fg(const String& color) override;
|
||||
|
||||
OutputHandlerBase& pChar(char c) override;
|
||||
OutputHandlerBase& pStyled(const StringEditor& styled) override;
|
||||
OutputHandlerBase& pStyled(const String& styled) override;
|
||||
OutputHandlerBase& pStyled(const TextStyleParser::tStyledString& styled) override;
|
||||
OutputHandlerBase& pStyled(const TextStyleBuilder::IRichStringBase& styled) override;
|
||||
OutputHandlerBase& pObject(const BaseObject& bo) override;
|
||||
|
||||
OutputHandlerBase& p(const StringEditor& se) override;
|
||||
OutputHandlerBase& p(const String& se) override;
|
||||
OutputHandlerBase& p(uint8_t i) override;
|
||||
OutputHandlerBase& p(int8_t i) override;
|
||||
OutputHandlerBase& p(uint16_t i) override;
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
#ifndef __LOGGER_HPP__
|
||||
#define __LOGGER_HPP__
|
||||
|
||||
#include <ostd/Types.hpp>
|
||||
#include <ostd/String.hpp>
|
||||
|
||||
namespace ostd
|
||||
{
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@ namespace ostd
|
|||
{
|
||||
bool LogicEvaluator::eval(String exp)
|
||||
{
|
||||
StringEditor se(exp);
|
||||
String se(exp);
|
||||
se = se.replaceAll("true", "1");
|
||||
se = se.replaceAll("false", "0");
|
||||
se = se.replaceAll("and", "&");
|
||||
|
|
@ -21,12 +21,12 @@ namespace ostd
|
|||
se = se.replaceAll("!1", "0");
|
||||
se = se.replaceAll("!0", "1");
|
||||
se = se.trim();
|
||||
exp = se.str();
|
||||
exp = se;
|
||||
|
||||
std::stack<bool> values;
|
||||
std::stack<eLogicOp> ops;
|
||||
char c = 0;
|
||||
for (unsigned int i = 0; i < exp.length(); i++)
|
||||
for (unsigned int i = 0; i < exp.len(); i++)
|
||||
{
|
||||
c = exp[i];
|
||||
if (c == ' ' || c == '#') continue;
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@
|
|||
#define __LOGIC_HPP__
|
||||
|
||||
#include <map>
|
||||
#include <ostd/Types.hpp>
|
||||
#include <ostd/String.hpp>
|
||||
|
||||
namespace ostd
|
||||
{
|
||||
|
|
|
|||
|
|
@ -62,7 +62,7 @@ namespace ostd
|
|||
return *this;
|
||||
}
|
||||
|
||||
OutputHandlerBase& ConsoleOutputHandler::bg(const StringEditor& color)
|
||||
OutputHandlerBase& ConsoleOutputHandler::bg(const String& color)
|
||||
{
|
||||
std::cout << ConsoleColors::getFromName(color, true);
|
||||
return *this;
|
||||
|
|
@ -74,7 +74,7 @@ namespace ostd
|
|||
return *this;
|
||||
}
|
||||
|
||||
OutputHandlerBase& ConsoleOutputHandler::fg(const StringEditor& color)
|
||||
OutputHandlerBase& ConsoleOutputHandler::fg(const String& color)
|
||||
{
|
||||
std::cout << ConsoleColors::getFromName(color, false);
|
||||
return *this;
|
||||
|
|
@ -86,9 +86,9 @@ namespace ostd
|
|||
return *this;
|
||||
}
|
||||
|
||||
OutputHandlerBase& ConsoleOutputHandler::pStyled(const StringEditor& styled)
|
||||
OutputHandlerBase& ConsoleOutputHandler::pStyled(const String& styled)
|
||||
{
|
||||
return pStyled(TextStyleParser::parse(styled.str()));
|
||||
return pStyled(TextStyleParser::parse(styled));
|
||||
}
|
||||
|
||||
OutputHandlerBase& ConsoleOutputHandler::pStyled(const TextStyleParser::tStyledString& styled)
|
||||
|
|
@ -109,7 +109,7 @@ namespace ostd
|
|||
return *this;
|
||||
}
|
||||
|
||||
OutputHandlerBase& ConsoleOutputHandler::p(const StringEditor& se)
|
||||
OutputHandlerBase& ConsoleOutputHandler::p(const String& se)
|
||||
{
|
||||
std::cout << se;
|
||||
return *this;
|
||||
|
|
|
|||
|
|
@ -413,7 +413,7 @@ namespace ostd
|
|||
else if (!is_validAddr(addr, stream_size)) return false;
|
||||
for (StreamIndex j = addr; j < addr + stream_size; j++)
|
||||
m_data[j] = stream[j - addr];
|
||||
addr += str.length() * tTypeSize::BYTE;
|
||||
addr += str.len() * tTypeSize::BYTE;
|
||||
if (null_terminate)
|
||||
if (!w_Byte(addr, 0x00)) return false;
|
||||
return true;
|
||||
|
|
|
|||
|
|
@ -110,7 +110,7 @@ namespace ostd
|
|||
}
|
||||
int64_t tmpInt = Utils::strToInt(s);
|
||||
//-------------------------------------------------
|
||||
tokens.push_back(Token { Token::Type::Number, StringEditor().addi(tmpInt).str() });
|
||||
tokens.push_back(Token { Token::Type::Number, String().add(tmpInt) });
|
||||
--p;
|
||||
} else {
|
||||
Token::Type t = Token::Type::Unknown;
|
||||
|
|
|
|||
570
src/ostd/String.cpp
Normal file
570
src/ostd/String.cpp
Normal file
|
|
@ -0,0 +1,570 @@
|
|||
#include "Utils.hpp"
|
||||
#include "String.hpp"
|
||||
#include <sstream>
|
||||
#include <algorithm>
|
||||
#include <boost/regex.hpp>
|
||||
|
||||
namespace ostd
|
||||
{
|
||||
String String::Tokens::next(void)
|
||||
{
|
||||
if (!hasNext()) return Tokens::END;
|
||||
return m_tokens[m_current_index++];
|
||||
}
|
||||
|
||||
String String::Tokens::previous(void)
|
||||
{
|
||||
if (!hasPrevious()) return Tokens::END;
|
||||
return m_tokens[--m_current_index];
|
||||
}
|
||||
|
||||
|
||||
String& String::ltrim(void)
|
||||
{
|
||||
m_data.erase(m_data.begin(), std::find_if(m_data.begin(), m_data.end(), [](unsigned char ch) {
|
||||
return !std::isspace(ch);
|
||||
}));
|
||||
return *this;
|
||||
}
|
||||
|
||||
String& String::rtrim(void)
|
||||
{
|
||||
m_data.erase(std::find_if(m_data.rbegin(), m_data.rend(), [](unsigned char ch) {
|
||||
return !std::isspace(ch);
|
||||
}).base(), m_data.end());
|
||||
return *this;
|
||||
}
|
||||
|
||||
String& String::trim(void)
|
||||
{
|
||||
return ltrim().rtrim();
|
||||
}
|
||||
|
||||
String& String::toLower(void)
|
||||
{
|
||||
std::transform(m_data.begin(), m_data.end(), m_data.begin(), [](unsigned char c){ return std::tolower(c); });
|
||||
return *this;
|
||||
}
|
||||
|
||||
String& String::toUpper(void)
|
||||
{
|
||||
std::transform(m_data.begin(), m_data.end(), m_data.begin(), [](unsigned char c){ return std::toupper(c); });
|
||||
return *this;
|
||||
}
|
||||
|
||||
String& String::addPadding(uint32_t new_string_length, char c, ePaddingBehavior padding_behavior)
|
||||
{
|
||||
if (len() - 1 >= new_string_length) return *this;
|
||||
uint32_t diff = new_string_length - len();
|
||||
int32_t leftPadding = 0;
|
||||
int32_t rightPadding = 0;
|
||||
switch (padding_behavior)
|
||||
{
|
||||
case ePaddingBehavior::ForceEvenNegative:
|
||||
if (diff % 2 != 0)
|
||||
diff--;
|
||||
leftPadding = rightPadding = (diff / 2);
|
||||
break;
|
||||
case ePaddingBehavior::ForceEvenPositive:
|
||||
if (diff % 2 != 0)
|
||||
diff++;
|
||||
leftPadding = rightPadding = (diff / 2);
|
||||
break;
|
||||
case ePaddingBehavior::AllowOddExtraLeft:
|
||||
if (diff % 2 != 0)
|
||||
{
|
||||
diff--;
|
||||
leftPadding = rightPadding = (diff / 2);
|
||||
leftPadding++;
|
||||
}
|
||||
else
|
||||
leftPadding = rightPadding = (diff / 2);
|
||||
break;
|
||||
case ePaddingBehavior::AllowOddExtraRight:
|
||||
if (diff % 2 != 0)
|
||||
{
|
||||
diff--;
|
||||
leftPadding = rightPadding = (diff / 2);
|
||||
rightPadding++;
|
||||
}
|
||||
else
|
||||
leftPadding = rightPadding = (diff / 2);
|
||||
break;
|
||||
}
|
||||
if (leftPadding < 0 || rightPadding < 0) return *this;
|
||||
return addLeftPadding(len() + leftPadding, c).addRightPadding(len() + rightPadding, c);
|
||||
}
|
||||
|
||||
String& String::addLeftPadding(uint32_t new_string_length, char c)
|
||||
{
|
||||
reverse();
|
||||
while (len() < new_string_length)
|
||||
m_data += c;
|
||||
reverse();
|
||||
return *this;
|
||||
}
|
||||
|
||||
String& String::addRightPadding(uint32_t new_string_length, char c)
|
||||
{
|
||||
while (len() < new_string_length)
|
||||
m_data += c;
|
||||
return *this;
|
||||
}
|
||||
|
||||
String& String::reverse(void)
|
||||
{
|
||||
std::reverse(m_data.begin(), m_data.end());
|
||||
return *this;
|
||||
}
|
||||
|
||||
String& String::replaceAll(const String& what, const String& with)
|
||||
{
|
||||
while (contains(what))
|
||||
replaceFirst(what, with);
|
||||
return *this;
|
||||
}
|
||||
|
||||
String& String::replaceFirst(const String& what, const String& with)
|
||||
{
|
||||
int32_t index = indexOf(what);
|
||||
if (index == -1) return *this;
|
||||
m_data.replace(index, what.len(), with.cpp_str());
|
||||
return *this;
|
||||
}
|
||||
|
||||
String& String::regexReplace(const String& regex_pattern, const String& replace_with, bool case_insensitive)
|
||||
{
|
||||
try
|
||||
{
|
||||
boost::regex rgx(regex_pattern.cpp_str(), (case_insensitive ? boost::regex_constants::icase : boost::regex_constants::normal));
|
||||
m_data = boost::regex_replace(m_data, rgx, replace_with.cpp_str());
|
||||
return *this;
|
||||
}
|
||||
catch(const boost::regex_error& err)
|
||||
{
|
||||
std::cerr << err.what() << '\n'; //TODO: Better error handling
|
||||
return *this;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
String& String::put(uint32_t index, char c)
|
||||
{
|
||||
if (index < m_data.length())
|
||||
m_data[index] = c;
|
||||
return *this;
|
||||
}
|
||||
|
||||
String& String::substr(uint32_t start, int32_t end)
|
||||
{
|
||||
if (end < 0) m_data = m_data.substr(start);
|
||||
else m_data = m_data.substr(start, end - start);
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
String& String::addChar(char c)
|
||||
{
|
||||
m_data += c;
|
||||
return *this;
|
||||
}
|
||||
|
||||
String& String::add(const String& se)
|
||||
{
|
||||
m_data += se.cpp_str();
|
||||
return *this;
|
||||
}
|
||||
|
||||
String& String::add(uint8_t i)
|
||||
{
|
||||
m_data += std::to_string(i);
|
||||
return *this;
|
||||
}
|
||||
|
||||
String& String::add(int8_t i)
|
||||
{
|
||||
m_data += std::to_string(i);
|
||||
return *this;
|
||||
}
|
||||
|
||||
String& String::add(uint16_t i)
|
||||
{
|
||||
m_data += std::to_string(i);
|
||||
return *this;
|
||||
}
|
||||
|
||||
String& String::add(int16_t i)
|
||||
{
|
||||
m_data += std::to_string(i);
|
||||
return *this;
|
||||
}
|
||||
|
||||
String& String::add(uint32_t i)
|
||||
{
|
||||
m_data += std::to_string(i);
|
||||
return *this;
|
||||
}
|
||||
|
||||
String& String::add(int32_t i)
|
||||
{
|
||||
m_data += std::to_string(i);
|
||||
return *this;
|
||||
}
|
||||
|
||||
String& String::add(uint64_t i)
|
||||
{
|
||||
m_data += std::to_string(i);
|
||||
return *this;
|
||||
}
|
||||
|
||||
String& String::add(int64_t i)
|
||||
{
|
||||
m_data += std::to_string(i);
|
||||
return *this;
|
||||
}
|
||||
|
||||
String& String::add(float f, uint8_t precision)
|
||||
{
|
||||
std::stringstream stream;
|
||||
stream << std::fixed << std::setprecision(precision) << f;
|
||||
cpp_string s = stream.str();
|
||||
return add(s);
|
||||
}
|
||||
|
||||
String& String::add(double f, uint8_t precision)
|
||||
{
|
||||
std::stringstream stream;
|
||||
stream << std::fixed << std::setprecision(precision) << f;
|
||||
cpp_string s = stream.str();
|
||||
return add(s);
|
||||
}
|
||||
|
||||
|
||||
|
||||
//New String
|
||||
String String::new_ltrim(void) const
|
||||
{
|
||||
String __str = m_data;
|
||||
return __str.ltrim();
|
||||
}
|
||||
|
||||
String String::new_rtrim(void) const
|
||||
{
|
||||
String __str = m_data;
|
||||
return __str.rtrim();
|
||||
}
|
||||
|
||||
String String::new_trim(void) const
|
||||
{
|
||||
String __str = m_data;
|
||||
return __str.trim();
|
||||
}
|
||||
|
||||
String String::new_toLower(void) const
|
||||
{
|
||||
String __str = m_data;
|
||||
return __str.toLower();
|
||||
}
|
||||
|
||||
String String::new_toUpper(void) const
|
||||
{
|
||||
String __str = m_data;
|
||||
return __str.toUpper();
|
||||
}
|
||||
|
||||
String String::new_addPadding(uint32_t new_string_length, char c, ePaddingBehavior padding_behavior) const
|
||||
{
|
||||
String __str = m_data;
|
||||
return __str.addPadding(new_string_length, c, padding_behavior);
|
||||
}
|
||||
|
||||
String String::new_addLeftPadding(uint32_t new_string_length, char c) const
|
||||
{
|
||||
String __str = m_data;
|
||||
return __str.addLeftPadding(new_string_length, c);
|
||||
}
|
||||
|
||||
String String::new_addRightPadding(uint32_t new_string_length, char c) const
|
||||
{
|
||||
String __str = m_data;
|
||||
return __str.addRightPadding(new_string_length, c);
|
||||
}
|
||||
|
||||
String String::new_reverse(void) const
|
||||
{
|
||||
String __str = m_data;
|
||||
return __str.reverse();
|
||||
}
|
||||
|
||||
String String::new_replaceAll(const String& what, const String& with) const
|
||||
{
|
||||
String __str = m_data;
|
||||
return __str.replaceAll(what, with);
|
||||
}
|
||||
|
||||
String String::new_replaceFirst(const String& what, const String& with) const
|
||||
{
|
||||
String __str = m_data;
|
||||
return __str.replaceFirst(what, with);
|
||||
}
|
||||
|
||||
String String::new_regexReplace(const String& regex_pattern, const String& replace_with, bool case_insensitive) const
|
||||
{
|
||||
String __str = m_data;
|
||||
return __str.regexReplace(regex_pattern, replace_with, case_insensitive);
|
||||
}
|
||||
|
||||
String String::new_put(uint32_t index, char c) const
|
||||
{
|
||||
String __str = m_data;
|
||||
return __str.put(index, c);
|
||||
}
|
||||
|
||||
String String::new_substr(uint32_t start, int32_t end) const
|
||||
{
|
||||
String __str = m_data;
|
||||
return __str.substr(start, end);
|
||||
}
|
||||
|
||||
|
||||
String String::new_addChar(char c)
|
||||
{
|
||||
String __str = m_data;
|
||||
return __str.addChar(c);
|
||||
}
|
||||
|
||||
String String::new_add(const String& se)
|
||||
{
|
||||
String __str = m_data;
|
||||
return __str.add(se);
|
||||
}
|
||||
|
||||
String String::new_add(uint8_t i)
|
||||
{
|
||||
String __str = m_data;
|
||||
return __str.add(i);
|
||||
}
|
||||
|
||||
String String::new_add(int8_t i)
|
||||
{
|
||||
String __str = m_data;
|
||||
return __str.add(i);
|
||||
}
|
||||
|
||||
String String::new_add(uint16_t i)
|
||||
{
|
||||
String __str = m_data;
|
||||
return __str.add(i);
|
||||
}
|
||||
|
||||
String String::new_add(int16_t i)
|
||||
{
|
||||
String __str = m_data;
|
||||
return __str.add(i);
|
||||
}
|
||||
|
||||
String String::new_add(uint32_t i)
|
||||
{
|
||||
String __str = m_data;
|
||||
return __str.add(i);
|
||||
}
|
||||
|
||||
String String::new_add(int32_t i)
|
||||
{
|
||||
String __str = m_data;
|
||||
return __str.add(i);
|
||||
}
|
||||
|
||||
String String::new_add(uint64_t i)
|
||||
{
|
||||
String __str = m_data;
|
||||
return __str.add(i);
|
||||
}
|
||||
|
||||
String String::new_add(int64_t i)
|
||||
{
|
||||
String __str = m_data;
|
||||
return __str.add(i);
|
||||
}
|
||||
|
||||
String String::new_add(float f, uint8_t precision)
|
||||
{
|
||||
String __str = m_data;
|
||||
return __str.add(f, precision);
|
||||
}
|
||||
|
||||
String String::new_add(double f, uint8_t precision)
|
||||
{
|
||||
String __str = m_data;
|
||||
return __str.add(f, precision);
|
||||
}
|
||||
|
||||
|
||||
|
||||
//Utility
|
||||
int64_t String::toInt(void) const
|
||||
{
|
||||
if (!isNumeric(false)) return 0;
|
||||
return Utils::strToInt(m_data);
|
||||
}
|
||||
|
||||
float String::toFloat(void) const
|
||||
{
|
||||
if (!isNumeric(true)) return 0;
|
||||
return std::stof(m_data);
|
||||
}
|
||||
|
||||
double String::toDouble(void) const
|
||||
{
|
||||
if (!isNumeric(true)) return 0;
|
||||
return std::stod(m_data);
|
||||
}
|
||||
|
||||
bool String::isNumeric(bool decimal) const
|
||||
{
|
||||
if (decimal)
|
||||
{
|
||||
std::istringstream iss(m_data);
|
||||
double f;
|
||||
iss >> std::noskipws >> f;
|
||||
return iss.eof() && !iss.fail();
|
||||
}
|
||||
return Utils::isInt(m_data);
|
||||
}
|
||||
|
||||
bool String::contains(char c) const
|
||||
{
|
||||
return m_data.find(c) != std::string::npos;
|
||||
}
|
||||
|
||||
bool String::contains(const String& str) const
|
||||
{
|
||||
return m_data.find(str.cpp_str()) != std::string::npos;
|
||||
}
|
||||
|
||||
bool String::startsWith(const String& str) const
|
||||
{
|
||||
return m_data.starts_with(str);
|
||||
}
|
||||
|
||||
bool String::endsWith(const String& str) const
|
||||
{
|
||||
return m_data.ends_with(str);
|
||||
}
|
||||
|
||||
uint32_t String::count(const String& str) const
|
||||
{
|
||||
Tokens tok = tokenize(str, false, true);
|
||||
if (tok.count() < 1) return 0;
|
||||
return tok.count() - 1;
|
||||
}
|
||||
|
||||
int32_t String::indexOf(char c, uint32_t start) const
|
||||
{
|
||||
cpp_string cc = "";
|
||||
cc += c;
|
||||
int32_t pos = m_data.find(cc.c_str(), start);
|
||||
if (pos == std::string::npos) return -1;
|
||||
return pos;
|
||||
}
|
||||
|
||||
int32_t String::indexOf(const String& str, uint32_t start) const
|
||||
{
|
||||
int32_t pos = m_data.find(str.c_str(), start);
|
||||
if (pos == std::string::npos) return -1;
|
||||
return pos;
|
||||
}
|
||||
|
||||
int32_t String::lastIndexOf(char c) const
|
||||
{
|
||||
String se(m_data);
|
||||
se.reverse();
|
||||
int32_t pos = se.indexOf(c);
|
||||
if (pos < 0) return -1;
|
||||
return len() - pos - 1;
|
||||
}
|
||||
|
||||
int32_t String::lastIndexOf(const String& str) const
|
||||
{
|
||||
String se(m_data);
|
||||
se.reverse();
|
||||
String se2(str);
|
||||
int32_t pos = se.indexOf(se2.new_reverse());
|
||||
if (pos < 0) return -1;
|
||||
return len() - pos - str.len();
|
||||
}
|
||||
|
||||
String::Tokens String::tokenize(const String& delimiter, bool trim_tokens, bool allow_white_space_only_tokens) const
|
||||
{
|
||||
Tokens tokens;
|
||||
int32_t sindex = 0;
|
||||
int32_t eindex = 0;
|
||||
String __token = "";
|
||||
while ((eindex = indexOf(delimiter, sindex)) != -1)
|
||||
{
|
||||
__token = new_substr(sindex, eindex);
|
||||
if (trim_tokens)
|
||||
{
|
||||
__token.trim();
|
||||
if (__token != "")
|
||||
tokens.m_tokens.push_back(__token.cpp_str());
|
||||
}
|
||||
else
|
||||
{
|
||||
if (allow_white_space_only_tokens && __token != "")
|
||||
tokens.m_tokens.push_back(__token.cpp_str());
|
||||
else if (!allow_white_space_only_tokens && __token.trim() != "")
|
||||
tokens.m_tokens.push_back(__token.cpp_str());
|
||||
}
|
||||
sindex = eindex + delimiter.len();
|
||||
}
|
||||
__token = new_substr(sindex);
|
||||
if (trim_tokens)
|
||||
{
|
||||
__token.trim();
|
||||
if (__token != "")
|
||||
tokens.m_tokens.push_back(__token.cpp_str());
|
||||
}
|
||||
else
|
||||
{
|
||||
if (allow_white_space_only_tokens && __token != "")
|
||||
tokens.m_tokens.push_back(__token.cpp_str());
|
||||
else if (!allow_white_space_only_tokens && __token.trim() != "")
|
||||
tokens.m_tokens.push_back(__token.cpp_str());
|
||||
}
|
||||
return tokens;
|
||||
}
|
||||
|
||||
|
||||
|
||||
String operator+(const cpp_string& str1, const String& str)
|
||||
{
|
||||
return String(str1) + str;
|
||||
}
|
||||
|
||||
// bool operator== (const String& str1, const char* str2)
|
||||
// {
|
||||
// return std::strcmp(str1.c_str(), str2) == 0;
|
||||
// }
|
||||
|
||||
// bool operator!= (const String& str1, const char* str2)
|
||||
// {
|
||||
// return std::strcmp(str1.c_str(), str2) != 0;
|
||||
// }
|
||||
|
||||
// bool operator== (const char* str1, const String& str2)
|
||||
// {
|
||||
// return std::strcmp(str1, str2.c_str()) == 0;
|
||||
// }
|
||||
|
||||
// bool operator!= (const char* str1, const String& str2)
|
||||
// {
|
||||
// return std::strcmp(str1, str2.c_str()) != 0;
|
||||
// }
|
||||
|
||||
std::ostream& operator<<(std::ostream& out, const String& val)
|
||||
{
|
||||
out << val.cpp_str();
|
||||
return out;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,10 +1,11 @@
|
|||
#pragma once
|
||||
|
||||
#include <ostd/Types.hpp>
|
||||
#include <cstring>
|
||||
|
||||
namespace ostd
|
||||
{
|
||||
class StringEditor
|
||||
class String
|
||||
{
|
||||
public: class Tokens
|
||||
{
|
||||
|
|
@ -33,75 +34,228 @@ namespace ostd
|
|||
uint32_t m_current_index;
|
||||
|
||||
public:
|
||||
inline static const String END = "%END%";
|
||||
inline static const cpp_string END = "%END%";
|
||||
|
||||
friend class StringEditor;
|
||||
friend class String;
|
||||
};
|
||||
|
||||
public: enum class ePaddingBehavior { ForceEvenPositive = 1, ForceEvenNegative, AllowOddExtraLeft, AllowOddExtraRight };
|
||||
public:
|
||||
inline StringEditor(void) { m_data = ""; }
|
||||
inline StringEditor(const String& str) { m_data = str; }
|
||||
inline StringEditor(const char* str) { m_data = str; }
|
||||
inline String str(void) const { return m_data; }
|
||||
inline uint32_t len(void) const { return m_data.length(); }
|
||||
inline StringEditor& clr(void) { m_data = ""; return *this; }
|
||||
inline String(void) { m_data = ""; }
|
||||
inline String(const cpp_string& str) { m_data = str; }
|
||||
inline String(const char* str) { m_data = str; }
|
||||
|
||||
inline cpp_string cpp_str(void) const { return m_data; }
|
||||
inline cpp_string& cpp_str_ref(void) { return m_data; }
|
||||
inline const char* c_str(void) const { return m_data.c_str(); }
|
||||
inline char at(uint32_t index) const { return m_data[index]; }
|
||||
inline uint32_t len(void) const { return m_data.length(); }
|
||||
inline char operator[](uint32_t index) const { return m_data[index]; }
|
||||
inline StringEditor& set(String str) { m_data = str; return *this; }
|
||||
inline bool operator== (const char* str2) const { return std::strcmp(c_str(), str2) == 0; }
|
||||
inline bool operator!= (const char* str2) const { return std::strcmp(c_str(), str2) != 0; }
|
||||
// friend bool operator== (const char* str1, const String& str2);
|
||||
// friend bool operator!= (const char* str1, const String& str2);
|
||||
inline String operator+(const String& str2) const { return m_data + str2.cpp_str(); }
|
||||
friend String operator+(const cpp_string& str1, const String& str);
|
||||
inline String& operator+=(const String& str2) { m_data += str2.cpp_str(); return *this; }
|
||||
inline String& operator+=(const char& c) { m_data += c; return *this; }
|
||||
inline operator std::string() const { return m_data; }
|
||||
inline operator const char*() const { return c_str(); }
|
||||
inline String& clr(void) { m_data = ""; return *this; }
|
||||
inline String& set(const cpp_string& str) { m_data = str; return *this; }
|
||||
|
||||
StringEditor& ltrim(void);
|
||||
StringEditor& rtrim(void);
|
||||
StringEditor& trim(void);
|
||||
inline auto begin(void) { return m_data.begin(); }
|
||||
inline auto end(void) { return m_data.end(); }
|
||||
inline auto cbegin(void) const { return m_data.begin(); }
|
||||
inline auto cend(void) const { return m_data.end(); }
|
||||
inline auto begin(void) const { return m_data.begin(); }
|
||||
inline auto end(void) const { return m_data.end(); }
|
||||
|
||||
StringEditor& toLower(void);
|
||||
StringEditor& toUpper(void);
|
||||
//Modifiers
|
||||
String& ltrim(void);
|
||||
String& rtrim(void);
|
||||
String& trim(void);
|
||||
String& toLower(void);
|
||||
String& toUpper(void);
|
||||
String& addPadding(uint32_t new_string_length, char c = ' ', ePaddingBehavior padding_behavior = ePaddingBehavior::ForceEvenPositive);
|
||||
String& addLeftPadding(uint32_t new_string_length, char c = ' ');
|
||||
String& addRightPadding(uint32_t new_string_length, char c = ' ');
|
||||
String& reverse(void);
|
||||
String& replaceAll(const String& what, const String& with);
|
||||
String& replaceFirst(const String& what, const String& with);
|
||||
String& regexReplace(const String& regex_pattern, const String& replace_with, bool case_insensitive = false);
|
||||
String& put(uint32_t index, char c);
|
||||
String& substr(uint32_t start, int32_t end = -1);
|
||||
|
||||
StringEditor& addPadding(char c = ' ', uint32_t count = 10);
|
||||
//StringEditor& add(const StringEditor& se);
|
||||
StringEditor& add(String str);
|
||||
StringEditor& add(char c);
|
||||
StringEditor& addi(uint8_t i);
|
||||
StringEditor& addi(int8_t i);
|
||||
StringEditor& addi(uint16_t i);
|
||||
StringEditor& addi(int16_t i);
|
||||
StringEditor& addi(uint32_t i);
|
||||
StringEditor& addi(int32_t i);
|
||||
StringEditor& addi(uint64_t i);
|
||||
StringEditor& addi(int64_t i);
|
||||
StringEditor& addf(float f);
|
||||
StringEditor& addf(double f);
|
||||
String& addChar(char c);
|
||||
String& add(const String& se);
|
||||
String& add(uint8_t i);
|
||||
String& add(int8_t i);
|
||||
String& add(uint16_t i);
|
||||
String& add(int16_t i);
|
||||
String& add(uint32_t i);
|
||||
String& add(int32_t i);
|
||||
String& add(uint64_t i);
|
||||
String& add(int64_t i);
|
||||
String& add(float f, uint8_t precision = 0);
|
||||
String& add(double f, uint8_t precision = 0);
|
||||
|
||||
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);
|
||||
//New String
|
||||
String new_ltrim(void) const;
|
||||
String new_rtrim(void) const;
|
||||
String new_trim(void) const;
|
||||
String new_toLower(void) const;
|
||||
String new_toUpper(void) const;
|
||||
String new_addPadding(uint32_t new_string_length, char c = ' ', ePaddingBehavior padding_behavior = ePaddingBehavior::ForceEvenPositive) const;
|
||||
String new_addLeftPadding(uint32_t new_string_length, char c = ' ') const;
|
||||
String new_addRightPadding(uint32_t new_string_length, char c = ' ') const;
|
||||
String new_reverse(void) const;
|
||||
String new_replaceAll(const String& what, const String& with) const;
|
||||
String new_replaceFirst(const String& what, const String& with) const;
|
||||
String new_regexReplace(const String& regex_pattern, const String& replace_with, bool case_insensitive = false) const;
|
||||
String new_put(uint32_t index, char c) const;
|
||||
String new_substr(uint32_t start, int32_t end = -1) const;
|
||||
|
||||
StringEditor& put(uint32_t index, char c);
|
||||
String new_addChar(char c);
|
||||
String new_add(const String& se);
|
||||
String new_add(uint8_t i);
|
||||
String new_add(int8_t i);
|
||||
String new_add(uint16_t i);
|
||||
String new_add(int16_t i);
|
||||
String new_add(uint32_t i);
|
||||
String new_add(int32_t i);
|
||||
String new_add(uint64_t i);
|
||||
String new_add(int64_t i);
|
||||
String new_add(float f, uint8_t precision = 0);
|
||||
String new_add(double f, uint8_t precision = 0);
|
||||
|
||||
int64_t toInt(void);
|
||||
float toFloat(void);
|
||||
double toDouble(void);
|
||||
bool isNumeric(bool decimal = false);
|
||||
bool contains(char c);
|
||||
bool contains(String str);
|
||||
bool startsWith(String str);
|
||||
bool endsWith(String str);
|
||||
String getReverse(void);
|
||||
//Utility
|
||||
int64_t toInt(void) const;
|
||||
float toFloat(void) const;
|
||||
double toDouble(void) const;
|
||||
bool isNumeric(bool decimal = false) const;
|
||||
bool contains(char c) const;
|
||||
bool contains(const String& str) const;
|
||||
bool startsWith(const String& str) const;
|
||||
bool endsWith(const String& str) const;
|
||||
uint32_t count(const String& str) const;
|
||||
int32_t indexOf(char c, uint32_t start = 0) const;
|
||||
int32_t indexOf(const String& str, uint32_t start = 0) const;
|
||||
int32_t lastIndexOf(char c) const;
|
||||
int32_t lastIndexOf(const String& str) const;
|
||||
Tokens tokenize(const String& delimiter = " ", bool trim_tokens = true, bool allow_white_space_only_tokens = false) const;
|
||||
|
||||
uint32_t count(String str);
|
||||
|
||||
int32_t indexOf(char c, uint32_t start = 0);
|
||||
int32_t indexOf(String str, uint32_t start = 0);
|
||||
int32_t lastIndexOf(char c);
|
||||
int32_t lastIndexOf(String str);
|
||||
|
||||
String substr(uint32_t start, int32_t end = -1);
|
||||
Tokens tokenize(String delimiter = " ", bool trim_tokens = true, bool allow_white_space_only_tokens = false);
|
||||
|
||||
friend std::ostream& operator<<(std::ostream& out, const StringEditor& val);
|
||||
friend std::ostream& operator<<(std::ostream& out, const String& val);
|
||||
|
||||
private:
|
||||
String m_data;
|
||||
ostd::cpp_string m_data;
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
namespace legacy
|
||||
{
|
||||
class StringEditor
|
||||
{
|
||||
public: class Tokens
|
||||
{
|
||||
public:
|
||||
inline bool hasNext(void) { return m_tokens.size() > 0 && m_current_index < m_tokens.size(); }
|
||||
inline bool hasPrevious(void) { return m_tokens.size() > 0 && m_current_index > 0; }
|
||||
String next(void);
|
||||
String previous(void);
|
||||
inline uint32_t count(void) { return m_tokens.size(); }
|
||||
inline std::vector<String> getRawData(void) { return m_tokens; }
|
||||
inline uint32_t getCurrentIndex(void) { return m_current_index; }
|
||||
inline void cycle(void) { m_current_index = 0; }
|
||||
|
||||
inline auto begin(void) { return m_tokens.begin(); }
|
||||
inline auto end(void) { return m_tokens.end(); }
|
||||
inline auto cbegin(void) const { return m_tokens.begin(); }
|
||||
inline auto cend(void) const { return m_tokens.end(); }
|
||||
inline auto begin(void) const { return m_tokens.begin(); }
|
||||
inline auto end(void) const { return m_tokens.end(); }
|
||||
|
||||
private:
|
||||
inline Tokens(void) { m_current_index = 0; }
|
||||
|
||||
private:
|
||||
std::vector<String> m_tokens;
|
||||
uint32_t m_current_index;
|
||||
|
||||
public:
|
||||
inline static const String END = "%END%";
|
||||
|
||||
friend class StringEditor;
|
||||
};
|
||||
|
||||
public:
|
||||
inline StringEditor(void) { m_data = ""; }
|
||||
inline StringEditor(const String& str) { m_data = str; }
|
||||
inline StringEditor(const char* str) { m_data = str; }
|
||||
inline String str(void) const { return m_data; }
|
||||
inline uint32_t len(void) const { return m_data.length(); }
|
||||
inline StringEditor& clr(void) { m_data = ""; return *this; }
|
||||
inline const char* c_str(void) const { return m_data.c_str(); }
|
||||
inline char at(uint32_t index) const { return m_data[index]; }
|
||||
inline char operator[](uint32_t index) const { return m_data[index]; }
|
||||
inline StringEditor& set(String str) { m_data = str; return *this; }
|
||||
|
||||
StringEditor& ltrim(void);
|
||||
StringEditor& rtrim(void);
|
||||
StringEditor& trim(void);
|
||||
|
||||
StringEditor& toLower(void);
|
||||
StringEditor& toUpper(void);
|
||||
|
||||
StringEditor& addPadding(char c = ' ', uint32_t count = 10);
|
||||
//StringEditor& add(const StringEditor& se);
|
||||
StringEditor& add(String str);
|
||||
StringEditor& add(char c);
|
||||
StringEditor& addi(uint8_t i);
|
||||
StringEditor& addi(int8_t i);
|
||||
StringEditor& addi(uint16_t i);
|
||||
StringEditor& addi(int16_t i);
|
||||
StringEditor& addi(uint32_t i);
|
||||
StringEditor& addi(int32_t i);
|
||||
StringEditor& addi(uint64_t i);
|
||||
StringEditor& addi(int64_t i);
|
||||
StringEditor& addf(float f);
|
||||
StringEditor& addf(double f);
|
||||
|
||||
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);
|
||||
|
||||
int64_t toInt(void);
|
||||
float toFloat(void);
|
||||
double toDouble(void);
|
||||
bool isNumeric(bool decimal = false);
|
||||
bool contains(char c);
|
||||
bool contains(String str);
|
||||
bool startsWith(String str);
|
||||
bool endsWith(String str);
|
||||
String getReverse(void);
|
||||
|
||||
uint32_t count(String str);
|
||||
|
||||
int32_t indexOf(char c, uint32_t start = 0);
|
||||
int32_t indexOf(String str, uint32_t start = 0);
|
||||
int32_t lastIndexOf(char c);
|
||||
int32_t lastIndexOf(String str);
|
||||
|
||||
String substr(uint32_t start, int32_t end = -1);
|
||||
Tokens tokenize(String delimiter = " ", bool trim_tokens = true, bool allow_white_space_only_tokens = false);
|
||||
|
||||
friend std::ostream& operator<<(std::ostream& out, const StringEditor& val);
|
||||
|
||||
private:
|
||||
String m_data;
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -6,288 +6,306 @@
|
|||
|
||||
namespace ostd
|
||||
{
|
||||
String StringEditor::Tokens::next(void)
|
||||
namespace legacy
|
||||
{
|
||||
if (!hasNext()) return Tokens::END;
|
||||
return m_tokens[m_current_index++];
|
||||
}
|
||||
String StringEditor::Tokens::next(void)
|
||||
{
|
||||
if (!hasNext()) return Tokens::END;
|
||||
return m_tokens[m_current_index++];
|
||||
}
|
||||
|
||||
String StringEditor::Tokens::previous(void)
|
||||
{
|
||||
if (!hasPrevious()) return Tokens::END;
|
||||
return m_tokens[--m_current_index];
|
||||
}
|
||||
String StringEditor::Tokens::previous(void)
|
||||
{
|
||||
if (!hasPrevious()) return Tokens::END;
|
||||
return m_tokens[--m_current_index];
|
||||
}
|
||||
|
||||
StringEditor& StringEditor::ltrim(void)
|
||||
{
|
||||
m_data.erase(m_data.begin(), std::find_if(m_data.begin(), m_data.end(), [](unsigned char ch) {
|
||||
return !std::isspace(ch);
|
||||
}));
|
||||
return *this;
|
||||
}
|
||||
StringEditor& StringEditor::rtrim(void)
|
||||
{
|
||||
m_data.erase(std::find_if(m_data.rbegin(), m_data.rend(), [](unsigned char ch) {
|
||||
return !std::isspace(ch);
|
||||
}).base(), m_data.end());
|
||||
return *this;
|
||||
}
|
||||
StringEditor& StringEditor::trim(void)
|
||||
{
|
||||
return ltrim().rtrim();
|
||||
}
|
||||
StringEditor& StringEditor::toLower(void)
|
||||
{
|
||||
std::transform(m_data.begin(), m_data.end(), m_data.begin(), [](unsigned char c){ return std::tolower(c); });
|
||||
return *this;
|
||||
}
|
||||
StringEditor& StringEditor::toUpper(void)
|
||||
{
|
||||
std::transform(m_data.begin(), m_data.end(), m_data.begin(), [](unsigned char c){ return std::toupper(c); });
|
||||
return *this;
|
||||
}
|
||||
StringEditor& StringEditor::addPadding(char c, uint32_t count)
|
||||
{
|
||||
for (uint16_t i = 0; i < count; i++)
|
||||
StringEditor& StringEditor::ltrim(void)
|
||||
{
|
||||
m_data.erase(m_data.begin(), std::find_if(m_data.begin(), m_data.end(), [](unsigned char ch) {
|
||||
return !std::isspace(ch);
|
||||
}));
|
||||
return *this;
|
||||
}
|
||||
StringEditor& StringEditor::rtrim(void)
|
||||
{
|
||||
m_data.erase(std::find_if(m_data.rbegin(), m_data.rend(), [](unsigned char ch) {
|
||||
return !std::isspace(ch);
|
||||
}).base(), m_data.end());
|
||||
return *this;
|
||||
}
|
||||
StringEditor& StringEditor::trim(void)
|
||||
{
|
||||
return ltrim().rtrim();
|
||||
}
|
||||
StringEditor& StringEditor::toLower(void)
|
||||
{
|
||||
std::transform(m_data.begin(), m_data.end(), m_data.begin(), [](unsigned char c){ return std::tolower(c); });
|
||||
return *this;
|
||||
}
|
||||
StringEditor& StringEditor::toUpper(void)
|
||||
{
|
||||
std::transform(m_data.begin(), m_data.end(), m_data.begin(), [](unsigned char c){ return std::toupper(c); });
|
||||
return *this;
|
||||
}
|
||||
StringEditor& StringEditor::addPadding(char c, uint32_t count)
|
||||
{
|
||||
for (uint16_t i = 0; i < count; i++)
|
||||
m_data += c;
|
||||
return *this;
|
||||
}
|
||||
|
||||
// StringEditor& StringEditor::add(const StringEditor& se)
|
||||
// {
|
||||
// m_data += se.m_data;
|
||||
// return *this;
|
||||
// }
|
||||
|
||||
StringEditor& StringEditor::add(String str)
|
||||
{
|
||||
m_data += str;
|
||||
return *this;
|
||||
}
|
||||
|
||||
StringEditor& StringEditor::add(char c)
|
||||
{
|
||||
m_data += c;
|
||||
return *this;
|
||||
}
|
||||
|
||||
// StringEditor& StringEditor::add(const StringEditor& se)
|
||||
// {
|
||||
// m_data += se.m_data;
|
||||
// return *this;
|
||||
// }
|
||||
|
||||
StringEditor& StringEditor::add(String str)
|
||||
{
|
||||
m_data += str;
|
||||
return *this;
|
||||
}
|
||||
|
||||
StringEditor& StringEditor::add(char c)
|
||||
{
|
||||
m_data += c;
|
||||
return *this;
|
||||
}
|
||||
|
||||
StringEditor& StringEditor::addi(uint8_t i)
|
||||
{
|
||||
m_data += std::to_string(i);
|
||||
return *this;
|
||||
}
|
||||
|
||||
StringEditor& StringEditor::addi(int8_t i)
|
||||
{
|
||||
m_data += std::to_string(i);
|
||||
return *this;
|
||||
}
|
||||
|
||||
StringEditor& StringEditor::addi(uint16_t i)
|
||||
{
|
||||
m_data += std::to_string(i);
|
||||
return *this;
|
||||
}
|
||||
|
||||
StringEditor& StringEditor::addi(int16_t i)
|
||||
{
|
||||
m_data += std::to_string(i);
|
||||
return *this;
|
||||
}
|
||||
|
||||
StringEditor& StringEditor::addi(uint32_t i)
|
||||
{
|
||||
m_data += std::to_string(i);
|
||||
return *this;
|
||||
}
|
||||
|
||||
StringEditor& StringEditor::addi(int32_t i)
|
||||
{
|
||||
m_data += std::to_string(i);
|
||||
return *this;
|
||||
}
|
||||
|
||||
StringEditor& StringEditor::addi(uint64_t i)
|
||||
{
|
||||
m_data += std::to_string(i);
|
||||
return *this;
|
||||
}
|
||||
|
||||
StringEditor& StringEditor::addi(int64_t i)
|
||||
{
|
||||
m_data += std::to_string(i);
|
||||
return *this;
|
||||
}
|
||||
|
||||
StringEditor& StringEditor::addf(float f)
|
||||
{
|
||||
m_data += std::to_string(f);
|
||||
return *this;
|
||||
}
|
||||
|
||||
StringEditor& StringEditor::addf(double f)
|
||||
{
|
||||
m_data += std::to_string(f);
|
||||
return *this;
|
||||
}
|
||||
|
||||
StringEditor& StringEditor::reverse(void)
|
||||
{
|
||||
std::reverse(m_data.begin(), m_data.end());
|
||||
return *this;
|
||||
}
|
||||
|
||||
StringEditor& StringEditor::replaceAll(String what, String with)
|
||||
{
|
||||
while (contains(what))
|
||||
replaceFirst(what, with);
|
||||
return *this;
|
||||
}
|
||||
|
||||
StringEditor& StringEditor::replaceFirst(String what, String with)
|
||||
{
|
||||
int32_t index = indexOf(what);
|
||||
if (index == -1) return *this;
|
||||
m_data.replace(index, what.length(), with);
|
||||
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)
|
||||
|
||||
StringEditor& StringEditor::addi(uint8_t i)
|
||||
{
|
||||
std::cerr << err.what() << '\n'; //TODO: Better error handling
|
||||
m_data += std::to_string(i);
|
||||
return *this;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
StringEditor& StringEditor::put(uint32_t index, char c)
|
||||
{
|
||||
if (index < m_data.length())
|
||||
m_data[index] = c;
|
||||
return *this;
|
||||
}
|
||||
|
||||
int64_t StringEditor::toInt(void)
|
||||
{
|
||||
if (!isNumeric(false)) return 0;
|
||||
return Utils::strToInt(m_data);
|
||||
}
|
||||
|
||||
float StringEditor::toFloat(void)
|
||||
{
|
||||
if (!isNumeric(true)) return 0;
|
||||
return std::stof(m_data);
|
||||
}
|
||||
|
||||
double StringEditor::toDouble(void)
|
||||
{
|
||||
if (!isNumeric(true)) return 0;
|
||||
return std::stod(m_data);
|
||||
}
|
||||
|
||||
bool StringEditor::isNumeric(bool decimal)
|
||||
{
|
||||
if (decimal)
|
||||
StringEditor& StringEditor::addi(int8_t i)
|
||||
{
|
||||
std::istringstream iss(m_data);
|
||||
double f;
|
||||
iss >> std::noskipws >> f;
|
||||
return iss.eof() && !iss.fail();
|
||||
m_data += std::to_string(i);
|
||||
return *this;
|
||||
}
|
||||
return Utils::isInt(m_data);
|
||||
}
|
||||
|
||||
bool StringEditor::contains(char c)
|
||||
{
|
||||
return m_data.find(c) != std::string::npos;
|
||||
}
|
||||
|
||||
bool StringEditor::contains(String str)
|
||||
{
|
||||
return m_data.find(str) != std::string::npos;
|
||||
}
|
||||
|
||||
bool StringEditor::startsWith(String str)
|
||||
{
|
||||
return m_data.starts_with(str);
|
||||
}
|
||||
|
||||
bool StringEditor::endsWith(String str)
|
||||
{
|
||||
return m_data.ends_with(str);
|
||||
}
|
||||
|
||||
String StringEditor::getReverse(void)
|
||||
{
|
||||
StringEditor se(m_data);
|
||||
return se.reverse().str();
|
||||
}
|
||||
|
||||
uint32_t StringEditor::count(String str)
|
||||
{
|
||||
Tokens tok = tokenize(str, false, true);
|
||||
if (tok.count() < 1) return 0;
|
||||
return tok.count() - 1;
|
||||
}
|
||||
|
||||
int32_t StringEditor::indexOf(char c, uint32_t start)
|
||||
{
|
||||
String cc = "";
|
||||
cc += c;
|
||||
int32_t pos = m_data.find(cc.c_str(), start);
|
||||
if (pos == std::string::npos) return -1;
|
||||
return pos;
|
||||
}
|
||||
|
||||
int32_t StringEditor::indexOf(String str, uint32_t start)
|
||||
{
|
||||
int32_t pos = m_data.find(str.c_str(), start);
|
||||
if (pos == std::string::npos) return -1;
|
||||
return pos;
|
||||
}
|
||||
|
||||
int32_t StringEditor::lastIndexOf(char c)
|
||||
{
|
||||
StringEditor se(m_data);
|
||||
se.reverse();
|
||||
int32_t pos = se.indexOf(c);
|
||||
if (pos < 0) return -1;
|
||||
return len() - pos - 1;
|
||||
}
|
||||
|
||||
int32_t StringEditor::lastIndexOf(String str)
|
||||
{
|
||||
StringEditor se(m_data);
|
||||
se.reverse();
|
||||
StringEditor se2(str);
|
||||
int32_t pos = se.indexOf(se2.reverse().str());
|
||||
if (pos < 0) return -1;
|
||||
return len() - pos - str.length();
|
||||
}
|
||||
|
||||
String StringEditor::substr(uint32_t start, int32_t end)
|
||||
{
|
||||
if (end < 0) return m_data.substr(start);
|
||||
return m_data.substr(start, end - start);
|
||||
}
|
||||
|
||||
StringEditor::Tokens StringEditor::tokenize(String delimiter, bool trim_tokens, bool allow_white_space_only_tokens)
|
||||
{
|
||||
Tokens tokens;
|
||||
int32_t sindex = 0;
|
||||
int32_t eindex = 0;
|
||||
StringEditor __token = "";
|
||||
while ((eindex = indexOf(delimiter, sindex)) != -1)
|
||||
StringEditor& StringEditor::addi(uint16_t i)
|
||||
{
|
||||
__token = substr(sindex, eindex);
|
||||
m_data += std::to_string(i);
|
||||
return *this;
|
||||
}
|
||||
|
||||
StringEditor& StringEditor::addi(int16_t i)
|
||||
{
|
||||
m_data += std::to_string(i);
|
||||
return *this;
|
||||
}
|
||||
|
||||
StringEditor& StringEditor::addi(uint32_t i)
|
||||
{
|
||||
m_data += std::to_string(i);
|
||||
return *this;
|
||||
}
|
||||
|
||||
StringEditor& StringEditor::addi(int32_t i)
|
||||
{
|
||||
m_data += std::to_string(i);
|
||||
return *this;
|
||||
}
|
||||
|
||||
StringEditor& StringEditor::addi(uint64_t i)
|
||||
{
|
||||
m_data += std::to_string(i);
|
||||
return *this;
|
||||
}
|
||||
|
||||
StringEditor& StringEditor::addi(int64_t i)
|
||||
{
|
||||
m_data += std::to_string(i);
|
||||
return *this;
|
||||
}
|
||||
|
||||
StringEditor& StringEditor::addf(float f)
|
||||
{
|
||||
m_data += std::to_string(f);
|
||||
return *this;
|
||||
}
|
||||
|
||||
StringEditor& StringEditor::addf(double f)
|
||||
{
|
||||
m_data += std::to_string(f);
|
||||
return *this;
|
||||
}
|
||||
|
||||
StringEditor& StringEditor::reverse(void)
|
||||
{
|
||||
std::reverse(m_data.begin(), m_data.end());
|
||||
return *this;
|
||||
}
|
||||
|
||||
StringEditor& StringEditor::replaceAll(String what, String with)
|
||||
{
|
||||
while (contains(what))
|
||||
replaceFirst(what, with);
|
||||
return *this;
|
||||
}
|
||||
|
||||
StringEditor& StringEditor::replaceFirst(String what, String with)
|
||||
{
|
||||
int32_t index = indexOf(what);
|
||||
if (index == -1) return *this;
|
||||
m_data.replace(index, what.length(), with);
|
||||
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())
|
||||
m_data[index] = c;
|
||||
return *this;
|
||||
}
|
||||
|
||||
int64_t StringEditor::toInt(void)
|
||||
{
|
||||
if (!isNumeric(false)) return 0;
|
||||
return Utils::strToInt(m_data);
|
||||
}
|
||||
|
||||
float StringEditor::toFloat(void)
|
||||
{
|
||||
if (!isNumeric(true)) return 0;
|
||||
return std::stof(m_data);
|
||||
}
|
||||
|
||||
double StringEditor::toDouble(void)
|
||||
{
|
||||
if (!isNumeric(true)) return 0;
|
||||
return std::stod(m_data);
|
||||
}
|
||||
|
||||
bool StringEditor::isNumeric(bool decimal)
|
||||
{
|
||||
if (decimal)
|
||||
{
|
||||
std::istringstream iss(m_data);
|
||||
double f;
|
||||
iss >> std::noskipws >> f;
|
||||
return iss.eof() && !iss.fail();
|
||||
}
|
||||
return Utils::isInt(m_data);
|
||||
}
|
||||
|
||||
bool StringEditor::contains(char c)
|
||||
{
|
||||
return m_data.find(c) != std::string::npos;
|
||||
}
|
||||
|
||||
bool StringEditor::contains(String str)
|
||||
{
|
||||
return m_data.find(str) != std::string::npos;
|
||||
}
|
||||
|
||||
bool StringEditor::startsWith(String str)
|
||||
{
|
||||
return m_data.starts_with(str);
|
||||
}
|
||||
|
||||
bool StringEditor::endsWith(String str)
|
||||
{
|
||||
return m_data.ends_with(str);
|
||||
}
|
||||
|
||||
String StringEditor::getReverse(void)
|
||||
{
|
||||
StringEditor se(m_data);
|
||||
return se.reverse().str();
|
||||
}
|
||||
|
||||
uint32_t StringEditor::count(String str)
|
||||
{
|
||||
Tokens tok = tokenize(str, false, true);
|
||||
if (tok.count() < 1) return 0;
|
||||
return tok.count() - 1;
|
||||
}
|
||||
|
||||
int32_t StringEditor::indexOf(char c, uint32_t start)
|
||||
{
|
||||
String cc = "";
|
||||
cc += c;
|
||||
int32_t pos = m_data.find(cc.c_str(), start);
|
||||
if (pos == std::string::npos) return -1;
|
||||
return pos;
|
||||
}
|
||||
|
||||
int32_t StringEditor::indexOf(String str, uint32_t start)
|
||||
{
|
||||
int32_t pos = m_data.find(str.c_str(), start);
|
||||
if (pos == std::string::npos) return -1;
|
||||
return pos;
|
||||
}
|
||||
|
||||
int32_t StringEditor::lastIndexOf(char c)
|
||||
{
|
||||
StringEditor se(m_data);
|
||||
se.reverse();
|
||||
int32_t pos = se.indexOf(c);
|
||||
if (pos < 0) return -1;
|
||||
return len() - pos - 1;
|
||||
}
|
||||
|
||||
int32_t StringEditor::lastIndexOf(String str)
|
||||
{
|
||||
StringEditor se(m_data);
|
||||
se.reverse();
|
||||
StringEditor se2(str);
|
||||
int32_t pos = se.indexOf(se2.reverse().str());
|
||||
if (pos < 0) return -1;
|
||||
return len() - pos - str.length();
|
||||
}
|
||||
|
||||
String StringEditor::substr(uint32_t start, int32_t end)
|
||||
{
|
||||
if (end < 0) return m_data.substr(start);
|
||||
return m_data.substr(start, end - start);
|
||||
}
|
||||
|
||||
StringEditor::Tokens StringEditor::tokenize(String delimiter, bool trim_tokens, bool allow_white_space_only_tokens)
|
||||
{
|
||||
Tokens tokens;
|
||||
int32_t sindex = 0;
|
||||
int32_t eindex = 0;
|
||||
StringEditor __token = "";
|
||||
while ((eindex = indexOf(delimiter, sindex)) != -1)
|
||||
{
|
||||
__token = substr(sindex, eindex);
|
||||
if (trim_tokens)
|
||||
{
|
||||
__token.trim();
|
||||
if (__token.str() != "")
|
||||
tokens.m_tokens.push_back(__token.str());
|
||||
}
|
||||
else
|
||||
{
|
||||
if (allow_white_space_only_tokens && __token.str() != "")
|
||||
tokens.m_tokens.push_back(__token.str());
|
||||
else if (!allow_white_space_only_tokens && StringEditor(__token).trim().str() != "")
|
||||
tokens.m_tokens.push_back(__token.str());
|
||||
}
|
||||
sindex = eindex + delimiter.length();
|
||||
}
|
||||
__token = substr(sindex);
|
||||
if (trim_tokens)
|
||||
{
|
||||
__token.trim();
|
||||
|
|
@ -301,29 +319,13 @@ namespace ostd
|
|||
else if (!allow_white_space_only_tokens && StringEditor(__token).trim().str() != "")
|
||||
tokens.m_tokens.push_back(__token.str());
|
||||
}
|
||||
sindex = eindex + delimiter.length();
|
||||
return tokens;
|
||||
}
|
||||
__token = substr(sindex);
|
||||
if (trim_tokens)
|
||||
{
|
||||
__token.trim();
|
||||
if (__token.str() != "")
|
||||
tokens.m_tokens.push_back(__token.str());
|
||||
}
|
||||
else
|
||||
{
|
||||
if (allow_white_space_only_tokens && __token.str() != "")
|
||||
tokens.m_tokens.push_back(__token.str());
|
||||
else if (!allow_white_space_only_tokens && StringEditor(__token).trim().str() != "")
|
||||
tokens.m_tokens.push_back(__token.str());
|
||||
}
|
||||
return tokens;
|
||||
}
|
||||
|
||||
std::ostream& operator<<(std::ostream& out, const StringEditor& val)
|
||||
{
|
||||
out << val.str();
|
||||
return out;
|
||||
std::ostream& operator<<(std::ostream& out, const StringEditor& val)
|
||||
{
|
||||
out << val.str();
|
||||
return out;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@ namespace ostd
|
|||
{
|
||||
if (!str.validate()) return os;
|
||||
ostd::ConsoleOutputHandler out;
|
||||
for (int32_t i = 0; i < str.text.length(); i++)
|
||||
for (int32_t i = 0; i < str.text.len(); i++)
|
||||
out.bg(str.backgroundColors[i].consoleColor).fg(str.foregroundColors[i].consoleColor).pChar(str.text[i]);
|
||||
out.reset();
|
||||
return os;
|
||||
|
|
@ -34,7 +34,7 @@ namespace ostd
|
|||
text += str;
|
||||
background.background = true;
|
||||
foreground.background = false;
|
||||
for (int32_t i = 0; i < str.length(); i++)
|
||||
for (int32_t i = 0; i < str.len(); i++)
|
||||
{
|
||||
backgroundColors.push_back(background);
|
||||
foregroundColors.push_back(foreground);
|
||||
|
|
@ -43,15 +43,15 @@ namespace ostd
|
|||
|
||||
bool TextStyleParser::tStyledString::validate(void) const
|
||||
{
|
||||
return text.length() > 0 && text.length() == backgroundColors.size() && text.length() == foregroundColors.size();
|
||||
return text.len() > 0 && text.len() == backgroundColors.size() && text.len() == foregroundColors.size();
|
||||
}
|
||||
|
||||
TextStyleParser::tStyledString TextStyleParser::parse(const StringEditor& styledString)
|
||||
TextStyleParser::tStyledString TextStyleParser::parse(const String& styledString)
|
||||
{
|
||||
return parse(styledString, convertColor("black"), convertColor("white"));
|
||||
}
|
||||
|
||||
TextStyleParser::tStyledString TextStyleParser::parse(const StringEditor& styledString, tColor defaultBackgorundColor, tColor defaultForegroundColor)
|
||||
TextStyleParser::tStyledString TextStyleParser::parse(const String& styledString, tColor defaultBackgorundColor, tColor defaultForegroundColor)
|
||||
{
|
||||
tStyledString rstring;
|
||||
bool insideBlock = false;
|
||||
|
|
@ -64,15 +64,15 @@ namespace ostd
|
|||
tColor fgcol = defaultForegroundColor;
|
||||
tColor bgcol = defaultBackgorundColor;
|
||||
String blockText = "";
|
||||
String _styledString = StringEditor(styledString).trim().str();
|
||||
for (int32_t i = 0; i < _styledString.length(); i++)
|
||||
String _styledString = String(styledString).trim();
|
||||
for (int32_t i = 0; i < _styledString.len(); i++)
|
||||
{
|
||||
char c = _styledString[i];
|
||||
if (c == '[')
|
||||
{
|
||||
if (insideBlock)
|
||||
return tStyledString(); //TODO: Error, no nested blocks allowed
|
||||
if (test_for_block(StringEditor(_styledString).substr(i)))
|
||||
if (test_for_block(String(_styledString).substr(i)))
|
||||
{
|
||||
insideBlock = true;
|
||||
continue;
|
||||
|
|
@ -94,7 +94,7 @@ namespace ostd
|
|||
}
|
||||
if (!insideBlock)
|
||||
{
|
||||
rstring.add(StringEditor("").add(c).str(), bgcol, fgcol);
|
||||
rstring.add(String("").addChar(c), bgcol, fgcol);
|
||||
continue;
|
||||
}
|
||||
if (validBlockStart)
|
||||
|
|
@ -119,9 +119,9 @@ namespace ostd
|
|||
return rstring;
|
||||
}
|
||||
|
||||
TextStyleParser::tColor TextStyleParser::convertColor(const StringEditor& name)
|
||||
TextStyleParser::tColor TextStyleParser::convertColor(const String& name)
|
||||
{
|
||||
StringEditor colorStrEditor = name;
|
||||
String colorStrEditor = name;
|
||||
colorStrEditor.trim().toLower();
|
||||
if (ConsoleColors::isConsoleColor(colorStrEditor))
|
||||
{
|
||||
|
|
@ -130,15 +130,15 @@ namespace ostd
|
|||
}
|
||||
tColor col;
|
||||
col.consoleColor = "black";
|
||||
col.fullColor.set(colorStrEditor.str());
|
||||
col.fullColor.set(colorStrEditor);
|
||||
col.background = false;
|
||||
return col;
|
||||
}
|
||||
|
||||
bool TextStyleParser::test_for_block(const String& block_part)
|
||||
{
|
||||
if (block_part.length() < 3) return false;
|
||||
if (block_part.starts_with("[@@"))
|
||||
if (block_part.len() < 3) return false;
|
||||
if (block_part.startsWith("[@@"))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
|
@ -147,9 +147,9 @@ namespace ostd
|
|||
|
||||
TextStyleParser::eBlockParserReturnValue TextStyleParser::parse_block(const String& blockString, tColor& outBackgroundColor, tColor& outForegroundColor)
|
||||
{
|
||||
StringEditor blockEditor = blockString;
|
||||
String blockEditor = blockString;
|
||||
blockEditor.trim().toLower();
|
||||
if (blockEditor.str() == BlockString_Close)
|
||||
if (blockEditor == BlockString_Close)
|
||||
{
|
||||
outBackgroundColor = s_defaultBackgroundColor;
|
||||
outForegroundColor = s_defaultForegroundColor;
|
||||
|
|
@ -157,27 +157,27 @@ namespace ostd
|
|||
}
|
||||
if (blockEditor.startsWith(BlockString_Style))
|
||||
{
|
||||
blockEditor = blockEditor.substr(BlockString_Style.length());
|
||||
blockEditor = blockEditor.substr(BlockString_Style.len());
|
||||
blockEditor.trim();
|
||||
blockEditor.replaceAll(" ", "");
|
||||
auto tokens = blockEditor.tokenize(",");
|
||||
for (auto param : tokens)
|
||||
{
|
||||
StringEditor paramEdit = param;
|
||||
String paramEdit = param;
|
||||
if (!paramEdit.contains(":"))
|
||||
continue; //TODO: Error, invalid param
|
||||
StringEditor name = paramEdit.substr(0, paramEdit.indexOf(":"));
|
||||
String name = paramEdit.new_substr(0, paramEdit.indexOf(":"));
|
||||
name.trim();
|
||||
StringEditor value = paramEdit.substr(paramEdit.indexOf(":") + 1);
|
||||
String value = paramEdit.new_substr(paramEdit.indexOf(":") + 1);
|
||||
value.trim();
|
||||
if (name.str() == "background")
|
||||
if (name == "background")
|
||||
{
|
||||
auto col = convertColor(value.str());
|
||||
auto col = convertColor(value);
|
||||
outBackgroundColor = col;
|
||||
}
|
||||
else if (name.str() == "foreground")
|
||||
else if (name == "foreground")
|
||||
{
|
||||
auto col = convertColor(value.str());
|
||||
auto col = convertColor(value);
|
||||
outForegroundColor = col;
|
||||
}
|
||||
else continue; //TODO: Error, unknown style parameter
|
||||
|
|
@ -226,79 +226,79 @@ namespace ostd
|
|||
|
||||
TextStyleBuilder::Console& TextStyleBuilder::Console::add(uint8_t i)
|
||||
{
|
||||
StringEditor edit("");
|
||||
edit.addi(i);
|
||||
return add(edit.str());
|
||||
String edit("");
|
||||
edit.add(i);
|
||||
return add(edit);
|
||||
}
|
||||
|
||||
TextStyleBuilder::Console& TextStyleBuilder::Console::add(int8_t i)
|
||||
{
|
||||
StringEditor edit("");
|
||||
edit.addi(i);
|
||||
return add(edit.str());
|
||||
String edit("");
|
||||
edit.add(i);
|
||||
return add(edit);
|
||||
}
|
||||
|
||||
TextStyleBuilder::Console& TextStyleBuilder::Console::add(uint16_t i)
|
||||
{
|
||||
StringEditor edit("");
|
||||
edit.addi(i);
|
||||
return add(edit.str());
|
||||
String edit("");
|
||||
edit.add(i);
|
||||
return add(edit);
|
||||
}
|
||||
|
||||
TextStyleBuilder::Console& TextStyleBuilder::Console::add(int16_t i)
|
||||
{
|
||||
StringEditor edit("");
|
||||
edit.addi(i);
|
||||
return add(edit.str());
|
||||
String edit("");
|
||||
edit.add(i);
|
||||
return add(edit);
|
||||
}
|
||||
|
||||
TextStyleBuilder::Console& TextStyleBuilder::Console::add(uint32_t i)
|
||||
{
|
||||
StringEditor edit("");
|
||||
edit.addi(i);
|
||||
return add(edit.str());
|
||||
String edit("");
|
||||
edit.add(i);
|
||||
return add(edit);
|
||||
}
|
||||
|
||||
TextStyleBuilder::Console& TextStyleBuilder::Console::add(int32_t i)
|
||||
{
|
||||
StringEditor edit("");
|
||||
edit.addi(i);
|
||||
return add(edit.str());
|
||||
String edit("");
|
||||
edit.add(i);
|
||||
return add(edit);
|
||||
}
|
||||
|
||||
TextStyleBuilder::Console& TextStyleBuilder::Console::add(uint64_t i)
|
||||
{
|
||||
StringEditor edit("");
|
||||
edit.addi(i);
|
||||
return add(edit.str());
|
||||
String edit("");
|
||||
edit.add(i);
|
||||
return add(edit);
|
||||
}
|
||||
|
||||
TextStyleBuilder::Console& TextStyleBuilder::Console::add(int64_t i)
|
||||
{
|
||||
StringEditor edit("");
|
||||
edit.addi(i);
|
||||
return add(edit.str());
|
||||
String edit("");
|
||||
edit.add(i);
|
||||
return add(edit);
|
||||
}
|
||||
|
||||
TextStyleBuilder::Console& TextStyleBuilder::Console::add(float f)
|
||||
{
|
||||
StringEditor edit("");
|
||||
edit.addf(f);
|
||||
return add(edit.str());
|
||||
String edit("");
|
||||
edit.add(f);
|
||||
return add(edit);
|
||||
}
|
||||
|
||||
TextStyleBuilder::Console& TextStyleBuilder::Console::add(double f)
|
||||
{
|
||||
StringEditor edit("");
|
||||
edit.addf(f);
|
||||
return add(edit.str());
|
||||
String edit("");
|
||||
edit.add(f);
|
||||
return add(edit);
|
||||
}
|
||||
|
||||
TextStyleBuilder::Console& TextStyleBuilder::Console::addc(char c)
|
||||
{
|
||||
StringEditor edit("");
|
||||
edit.add(c);
|
||||
return add(edit.str());
|
||||
String edit("");
|
||||
edit.addChar(c);
|
||||
return add(edit);
|
||||
}
|
||||
|
||||
TextStyleBuilder::Console& TextStyleBuilder::Console::print(OutputHandlerBase& out)
|
||||
|
|
@ -322,34 +322,34 @@ namespace ostd
|
|||
|
||||
|
||||
|
||||
TextStyleBuilder::Regex& TextStyleBuilder::Regex::setRawString(const StringEditor& rawString)
|
||||
TextStyleBuilder::Regex& TextStyleBuilder::Regex::setRawString(const String& rawString)
|
||||
{
|
||||
m_rawString = rawString.str();
|
||||
m_rawString = rawString;
|
||||
return *this;
|
||||
}
|
||||
|
||||
TextStyleBuilder::Regex& TextStyleBuilder::Regex::fg(const StringEditor& regex, const StringEditor& foreground_color, bool case_insensitive)
|
||||
TextStyleBuilder::Regex& TextStyleBuilder::Regex::fg(const String& regex, const String& foreground_color, bool case_insensitive)
|
||||
{
|
||||
String replace_pattern = "[@@ style foreground:" + foreground_color.str();
|
||||
String replace_pattern = "[@@ style foreground:" + foreground_color;
|
||||
replace_pattern += "]$&[@@/]";
|
||||
m_rawString = StringEditor(m_rawString).regexReplace(regex.str(), replace_pattern, case_insensitive).str();
|
||||
m_rawString = String(m_rawString).regexReplace(regex, replace_pattern, case_insensitive);
|
||||
return *this;
|
||||
}
|
||||
|
||||
TextStyleBuilder::Regex& TextStyleBuilder::Regex::bg(const StringEditor& regex, const StringEditor& background_color, bool case_insensitive)
|
||||
TextStyleBuilder::Regex& TextStyleBuilder::Regex::bg(const String& regex, const String& background_color, bool case_insensitive)
|
||||
{
|
||||
String replace_pattern = "[@@ style background:" + background_color.str();
|
||||
String replace_pattern = "[@@ style background:" + background_color;
|
||||
replace_pattern += "]$&[@@/]";
|
||||
m_rawString = StringEditor(m_rawString).regexReplace(regex.str(), replace_pattern, case_insensitive).str();
|
||||
m_rawString = String(m_rawString).regexReplace(regex, replace_pattern, case_insensitive);
|
||||
return *this;
|
||||
}
|
||||
|
||||
TextStyleBuilder::Regex& TextStyleBuilder::Regex::col(const StringEditor& regex, const StringEditor& foreground_color, const StringEditor& background_color, bool case_insensitive)
|
||||
TextStyleBuilder::Regex& TextStyleBuilder::Regex::col(const String& regex, const String& foreground_color, const String& background_color, bool case_insensitive)
|
||||
{
|
||||
String replace_pattern = "[@@ style background:" + background_color.str();
|
||||
replace_pattern += ", foreground:" + foreground_color.str();
|
||||
String replace_pattern = "[@@ style background:" + background_color;
|
||||
replace_pattern += ", foreground:" + foreground_color;
|
||||
replace_pattern += "]$&[@@/]";
|
||||
m_rawString = StringEditor(m_rawString).regexReplace(regex.str(), replace_pattern, case_insensitive).str();
|
||||
m_rawString = String(m_rawString).regexReplace(regex, replace_pattern, case_insensitive);
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@
|
|||
|
||||
namespace ostd
|
||||
{
|
||||
class StringEditor;
|
||||
class String;
|
||||
class TextStyleParser
|
||||
{
|
||||
public: enum class eBlockParserReturnValue { CloseBlock = 0, ValidBlock, InvalidBlock };
|
||||
|
|
@ -28,10 +28,10 @@ namespace ostd
|
|||
};
|
||||
|
||||
public:
|
||||
static tStyledString parse(const StringEditor& styledString);
|
||||
static tStyledString parse(const StringEditor& styledString, tColor defaultBackgorundColor, tColor defaultForegroundColor);
|
||||
static tStyledString parse(const String& styledString);
|
||||
static tStyledString parse(const String& styledString, tColor defaultBackgorundColor, tColor defaultForegroundColor);
|
||||
|
||||
static tColor convertColor(const StringEditor& name);
|
||||
static tColor convertColor(const String& name);
|
||||
|
||||
private:
|
||||
static bool test_for_block(const String& block_part);
|
||||
|
|
@ -90,13 +90,13 @@ namespace ostd
|
|||
public: class Regex : public IRichStringBase {
|
||||
public:
|
||||
inline Regex(void) { m_rawString = ""; }
|
||||
inline Regex(const StringEditor& rawString) { setRawString(rawString); }
|
||||
inline Regex(const String& rawString) { setRawString(rawString); }
|
||||
inline String getRawString(void) const { return m_rawString; }
|
||||
Regex& setRawString(const StringEditor& rawString);
|
||||
Regex& setRawString(const String& 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);
|
||||
Regex& fg(const String& regex, const String& foreground_color, bool case_insensitive = false);
|
||||
Regex& bg(const String& regex, const String& background_color, bool case_insensitive = false);
|
||||
Regex& col(const String& regex, const String& foreground_color, const String& background_color, bool case_insensitive = false);
|
||||
|
||||
TextStyleParser::tStyledString getStyledString(void) const override;
|
||||
|
||||
|
|
|
|||
|
|
@ -7,7 +7,11 @@
|
|||
|
||||
namespace ostd
|
||||
{
|
||||
typedef std::string String;
|
||||
namespace legacy
|
||||
{
|
||||
typedef std::string String;
|
||||
}
|
||||
typedef std::string cpp_string;
|
||||
|
||||
typedef int64_t QWord;
|
||||
typedef int32_t DWord;
|
||||
|
|
@ -21,13 +25,6 @@ namespace ostd
|
|||
|
||||
typedef uint32_t StreamIndex;
|
||||
|
||||
typedef uint32_t TextureAtlasIndex;
|
||||
typedef uint32_t ResourceID;
|
||||
|
||||
typedef uint32_t WidgetID;
|
||||
|
||||
typedef uint32_t LayerID;
|
||||
|
||||
typedef union {
|
||||
float val;
|
||||
Byte data[4];
|
||||
|
|
@ -50,12 +47,6 @@ namespace ostd
|
|||
static inline const uint8_t DOUBLE = 8;
|
||||
};
|
||||
|
||||
struct TextureID
|
||||
{
|
||||
ResourceID texture { 0 };
|
||||
TextureAtlasIndex tile { 0 };
|
||||
};
|
||||
|
||||
typedef std::vector<Byte> ByteStream;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -558,36 +558,36 @@ namespace ostd
|
|||
}
|
||||
bool Utils::isHex(String hex)
|
||||
{
|
||||
hex = StringEditor(hex).trim().toLower().str();
|
||||
return hex.compare(0, 2, "0x") == 0 &&
|
||||
hex.size() > 2 &&
|
||||
hex.find_first_not_of("0123456789abcdef", 2) == std::string::npos;
|
||||
hex = String(hex).trim().toLower();
|
||||
return hex.cpp_str().compare(0, 2, "0x") == 0 &&
|
||||
hex.cpp_str().size() > 2 &&
|
||||
hex.cpp_str().find_first_not_of("0123456789abcdef", 2) == std::string::npos;
|
||||
}
|
||||
bool Utils::isBin(String bin)
|
||||
{
|
||||
bin = StringEditor(bin).trim().toLower().str();
|
||||
return bin.compare(0, 2, "0b") == 0 &&
|
||||
bin.size() > 2 &&
|
||||
bin.find_first_not_of("01", 2) == std::string::npos;
|
||||
bin = String(bin).trim().toLower();
|
||||
return bin.cpp_str().compare(0, 2, "0b") == 0 &&
|
||||
bin.cpp_str().size() > 2 &&
|
||||
bin.cpp_str().find_first_not_of("01", 2) == std::string::npos;
|
||||
}
|
||||
bool Utils::isInt(String str)
|
||||
{
|
||||
str = StringEditor(str).trim().toLower().str();
|
||||
str = String(str).trim().toLower();
|
||||
bool isNumber = std::ranges::all_of(str.begin(), str.end(),
|
||||
[](char c){ return isdigit(c) != 0; });
|
||||
return Utils::isHex(str) || Utils::isBin(str) || isNumber;
|
||||
}
|
||||
int64_t Utils::strToInt(String str)
|
||||
{
|
||||
str = StringEditor(str).trim().toLower().str();
|
||||
str = String(str).trim().toLower();
|
||||
if (!Utils::isInt(str)) return 0;
|
||||
int32_t base = 10;
|
||||
if (str.rfind("0x", 0) == 0)
|
||||
if (str.cpp_str().rfind("0x", 0) == 0)
|
||||
{
|
||||
str = str.substr(2);
|
||||
base = 16;
|
||||
}
|
||||
else if (str.rfind("0b", 0) == 0)
|
||||
else if (str.cpp_str().rfind("0b", 0) == 0)
|
||||
{
|
||||
str = str.substr(2);
|
||||
base = 2;
|
||||
|
|
@ -597,10 +597,10 @@ namespace ostd
|
|||
bool Utils::readFile(String fileName, std::vector<String>& outLines)
|
||||
{
|
||||
String line;
|
||||
std::ifstream file(fileName);
|
||||
std::ifstream file(fileName.cpp_str());
|
||||
if (file.fail()) return false;
|
||||
outLines.clear();
|
||||
while (std::getline(file, line))
|
||||
while (std::getline(file, line.cpp_str_ref()))
|
||||
outLines.push_back(line);
|
||||
return true;
|
||||
}
|
||||
|
|
@ -673,9 +673,9 @@ namespace ostd
|
|||
String ext = "";
|
||||
for (unsigned char i = 0; i < ext_len; i++)
|
||||
ext += (char)(resource_buffer[i + 1]);
|
||||
if (StringEditor(output_file_path).trim().toLower().endsWith(ext))
|
||||
if (String(output_file_path).trim().toLower().endsWith(ext))
|
||||
ext = "";
|
||||
std::fstream bin (output_file_path + ext, std::ios::out | std::ios::binary);
|
||||
std::fstream bin (output_file_path.cpp_str() + ext.cpp_str(), std::ios::out | std::ios::binary);
|
||||
if (!bin.is_open()) return false;
|
||||
bin.write(resource_buffer + ext_len + 1, size - ext_len - 1);
|
||||
bin.close();
|
||||
|
|
@ -685,14 +685,14 @@ namespace ostd
|
|||
{
|
||||
StreamIndex end = start + (n_rows * line_len);
|
||||
if (end > data.size()) end = data.size();
|
||||
StringEditor titleEdit(title);
|
||||
String titleEdit(title);
|
||||
if (titleEdit.len() > 12)
|
||||
titleEdit = titleEdit.substr(0, 12);
|
||||
else if (titleEdit.len() < 12)
|
||||
{
|
||||
int32_t diff = 12 - titleEdit.len();
|
||||
for (int32_t i = 0; i < diff; i++)
|
||||
titleEdit.add(' ');
|
||||
titleEdit.addChar(' ');
|
||||
}
|
||||
bool highlight = addrHighlight >= 0;
|
||||
uint8_t i = 1;
|
||||
|
|
@ -702,7 +702,7 @@ namespace ostd
|
|||
if (line_len <= 0xFF)
|
||||
{
|
||||
out.fg(ConsoleColors::BrightBlue).p("|");
|
||||
out.fg(ConsoleColors::BrightMagenta).p(titleEdit.str());
|
||||
out.fg(ConsoleColors::BrightMagenta).p(titleEdit);
|
||||
out.fg(ConsoleColors::BrightBlue).p("| ");
|
||||
for (int32_t i = 0; i < line_len; i++)
|
||||
out.fg(ConsoleColors::Green).p(getHexStr(i, false, 1)).p(" ");
|
||||
|
|
@ -748,14 +748,14 @@ namespace ostd
|
|||
bool Utils::saveByteStreamToFile(const ByteStream& stream, const String& filePath)
|
||||
{
|
||||
std::ofstream writeFile;
|
||||
writeFile.open(filePath, std::ios::out | std::ios::binary);
|
||||
writeFile.open(filePath.cpp_str(), std::ios::out | std::ios::binary);
|
||||
writeFile.write((char*)(&stream[0]), stream.size());
|
||||
writeFile.close();
|
||||
return true;
|
||||
}
|
||||
bool Utils::loadByteStreamFromFile(const String& filePath, ByteStream& outStream)
|
||||
{
|
||||
std::ifstream rf(filePath, std::ios::out | std::ios::binary);
|
||||
std::ifstream rf(filePath.cpp_str(), std::ios::out | std::ios::binary);
|
||||
if(!rf) return false; //TODO: Error
|
||||
uint8_t cell = 0;
|
||||
while(rf.read((char*)&cell, sizeof(cell)))
|
||||
|
|
@ -772,12 +772,12 @@ namespace ostd
|
|||
}
|
||||
String Utils::byteStreamToString(const ByteStream& data)
|
||||
{
|
||||
StringEditor out_string = "";
|
||||
String out_string = "";
|
||||
for (int64_t i = 0; i < data.size(); i++)
|
||||
{
|
||||
if (data[i] == 0) break;
|
||||
out_string.add((char)data[i]);
|
||||
out_string.addChar((char)data[i]);
|
||||
}
|
||||
return out_string.str();
|
||||
return out_string;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
46
src/test.cpp
Normal file
46
src/test.cpp
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
#include <ostd/String.hpp>
|
||||
#include <ostd/IOHandlers.hpp>
|
||||
#include <ostd/Logger.hpp>
|
||||
|
||||
ostd::ConsoleOutputHandler out;
|
||||
|
||||
void test2(const std::string& str) {}
|
||||
void test3(const ostd::String& str) {}
|
||||
void test4(const char* str) {}
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
out.fg(ostd::ConsoleColors::Red).p("Hello World!!").reset().nl();
|
||||
|
||||
ostd::String str1, str2 = "Hello";
|
||||
bool b = str1 == str2;
|
||||
const ostd::String str3 = "CIAO";
|
||||
b = str2 == str1;
|
||||
b = str3 == "CICCIO";
|
||||
// b = "ciao" == str2;
|
||||
str2 = str3;
|
||||
std::string str = "cc";
|
||||
str1 = str;
|
||||
test2(str1);
|
||||
test3(str);
|
||||
str1 = str2 + str;
|
||||
str1 = str2 + "str";
|
||||
str1 = str2 + str3;
|
||||
str1 = str2 + str1;
|
||||
test4(str1);
|
||||
test4(str3);
|
||||
|
||||
str1 += "ciao";
|
||||
str1 += str;
|
||||
str1 += str1;
|
||||
str1 += str3;
|
||||
str1 += 'c';
|
||||
|
||||
OX_FATAL(str2);
|
||||
|
||||
ostd::RegexRichString rgxrstr("Hello World");
|
||||
rgxrstr.fg("Hello", "Blue");
|
||||
std::cout << rgxrstr << "\n";
|
||||
|
||||
return 0;
|
||||
}
|
||||
Loading…
Reference in a new issue