diff options
Diffstat (limited to 'src/common')
-rw-r--r-- | src/common/CMakeLists.txt | 3 | ||||
-rw-r--r-- | src/common/Logger.cpp | 21 | ||||
-rw-r--r-- | src/common/String.cpp | 56 |
3 files changed, 66 insertions, 14 deletions
diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt index 1056a763..e1642da2 100644 --- a/src/common/CMakeLists.txt +++ b/src/common/CMakeLists.txt @@ -30,5 +30,4 @@ else() endif() find_package(Microsoft.GSL CONFIG REQUIRED) -find_package(fmt CONFIG REQUIRED) -target_link_libraries(cru_base PUBLIC Microsoft.GSL::GSL fmt::fmt) +target_link_libraries(cru_base PUBLIC Microsoft.GSL::GSL) diff --git a/src/common/Logger.cpp b/src/common/Logger.cpp index af1dd692..e77e8a85 100644 --- a/src/common/Logger.cpp +++ b/src/common/Logger.cpp @@ -30,7 +30,7 @@ void Logger::RemoveSource(ILogSource *source) { } namespace { -std::u16string_view LogLevelToString(LogLevel level) { +String LogLevelToString(LogLevel level) { switch (level) { case LogLevel::Debug: return u"DEBUG"; @@ -45,36 +45,35 @@ std::u16string_view LogLevelToString(LogLevel level) { } } -std::u16string GetLogTime() { +String GetLogTime() { auto time = std::time(nullptr); auto calendar = std::localtime(&time); - return fmt::format(u"{}:{}:{}", calendar->tm_hour, calendar->tm_min, - calendar->tm_sec); + return Format(u"{}:{}:{}", calendar->tm_hour, calendar->tm_min, + calendar->tm_sec); } } // namespace -void Logger::Log(LogLevel level, std::u16string_view s) { +void Logger::Log(LogLevel level, const String &message) { #ifndef CRU_DEBUG if (level == LogLevel::Debug) { return; } #endif for (const auto &source : sources_) { - source->Write(level, fmt::format(u"[{}] {}: {}\n", GetLogTime(), - LogLevelToString(level), s)); + source->Write(level, Format(u"[{}] {}: {}\n", GetLogTime(), + LogLevelToString(level), message)); } } -void Logger::Log(LogLevel level, std::u16string_view tag, - std::u16string_view s) { +void Logger::Log(LogLevel level, const String &tag, const String &message) { #ifndef CRU_DEBUG if (level == LogLevel::Debug) { return; } #endif for (const auto &source : sources_) { - source->Write(level, fmt::format(u"[{}] {} {}: {}\n", GetLogTime(), - LogLevelToString(level), tag, s)); + source->Write(level, Format(u"[{}] {} {}: {}\n", GetLogTime(), + LogLevelToString(level), tag, message)); } } } // namespace cru::log diff --git a/src/common/String.cpp b/src/common/String.cpp index e9acf984..94eb8f27 100644 --- a/src/common/String.cpp +++ b/src/common/String.cpp @@ -101,6 +101,10 @@ String::~String() { } } +String::String(from_buffer_tag, std::uint16_t* buffer, Index size, + Index capacity) + : buffer_(buffer), size_(size), capacity_(capacity) {} + void String::resize(Index new_size) { Expects(new_size >= 0); @@ -137,7 +141,7 @@ void String::reserve(Index new_capacity) { } } -String::iterator String::insert(const_iterator pos, std::uint16_t* str, +String::iterator String::insert(const_iterator pos, const std::uint16_t* str, Index size) { Index new_size = size_ + size; if (new_size > capacity_) { @@ -239,4 +243,54 @@ int String::Compare(const String& other) const { } } +namespace details { +std::vector<FormatToken> ParseToFormatTokenList(const String& str) { + std::vector<FormatToken> result; + + auto push_char = [&result](std::uint16_t c) { + if (result.empty() || result.back().type == FormatTokenType::PlaceHolder) { + result.push_back(FormatToken{FormatTokenType::Text, String{}}); + } + result.back().data.append(c); + }; + + bool last_is_left_bracket = false; + for (auto c : str) { + if (c == u'{') { + if (last_is_left_bracket) { + push_char(u'{'); + last_is_left_bracket = false; + } else { + last_is_left_bracket = true; + } + } else if (c == u'}') { + if (last_is_left_bracket) { + result.push_back(FormatToken{FormatTokenType::PlaceHolder, String{}}); + } + last_is_left_bracket = false; + } else { + if (last_is_left_bracket) { + push_char(u'{'); + } + push_char(c); + last_is_left_bracket = false; + } + } + return result; +} + +void FormatAppendFromFormatTokenList( + String& current, const std::vector<FormatToken>& format_token_list, + Index index) { + for (Index i = index; i < format_token_list.size(); i++) { + const auto& token = format_token_list[i]; + if (token.type == FormatTokenType::PlaceHolder) { + current += u"{}"; + } else { + current += token.data; + } + } +} +} // namespace details + } // namespace cru |