From b40ba0cf91603b695f1f2380cbd39966a458f22f Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Sun, 27 Sep 2009 19:58:24 +0200 Subject: Use Unicode-aware String class instead of std::string --- src/Core/CMakeLists.txt | 2 +- src/Core/ConfigEntry.cpp | 4 +-- src/Core/ConfigEntry.h | 14 ++------ src/Core/ConfigManager.cpp | 6 ++-- src/Core/Exception.h | 2 ++ src/Core/LogManager.cpp | 14 ++++---- src/Core/LogManager.h | 4 +-- src/Core/String.cpp | 44 +++++++++++++++++++++++ src/Core/String.h | 88 ++++++++++++++++++++++++++++++++++++++++++++++ src/Core/Tokenizer.cpp | 14 ++++---- src/Core/Tokenizer.h | 8 ++--- src/Core/UnicodeString.cpp | 44 ----------------------- src/Core/UnicodeString.h | 64 --------------------------------- 13 files changed, 163 insertions(+), 145 deletions(-) create mode 100644 src/Core/String.cpp create mode 100644 src/Core/String.h delete mode 100644 src/Core/UnicodeString.cpp delete mode 100644 src/Core/UnicodeString.h (limited to 'src/Core') diff --git a/src/Core/CMakeLists.txt b/src/Core/CMakeLists.txt index 9828f61..60a5a25 100644 --- a/src/Core/CMakeLists.txt +++ b/src/Core/CMakeLists.txt @@ -20,7 +20,7 @@ mad_library(Core LogManager.cpp LogManager.h Signals.h ThreadManager.cpp ThreadManager.h + String.cpp String.h Tokenizer.cpp Tokenizer.h - UnicodeString.cpp UnicodeString.h ) target_link_libraries(Core ${Boost_LIBRARIES} ${ICU_LIBRARIES}) diff --git a/src/Core/ConfigEntry.cpp b/src/Core/ConfigEntry.cpp index b587043..04ed08f 100644 --- a/src/Core/ConfigEntry.cpp +++ b/src/Core/ConfigEntry.cpp @@ -22,7 +22,7 @@ namespace Mad { namespace Core { -ConfigEntry::String& ConfigEntry::Entry::operator[] (std::size_t i) { +String& ConfigEntry::Entry::operator[] (std::size_t i) { try { return value.at(i); } @@ -32,7 +32,7 @@ ConfigEntry::String& ConfigEntry::Entry::operator[] (std::size_t i) { } } -const ConfigEntry::String& ConfigEntry::Entry::operator[] (std::size_t i) const { +const String& ConfigEntry::Entry::operator[] (std::size_t i) const { try { return value.at(i); } diff --git a/src/Core/ConfigEntry.h b/src/Core/ConfigEntry.h index d4cacc9..26e77d2 100644 --- a/src/Core/ConfigEntry.h +++ b/src/Core/ConfigEntry.h @@ -22,7 +22,7 @@ #include "export.h" -#include "UnicodeString.h" +#include "String.h" #include #include @@ -32,16 +32,6 @@ namespace Core { class MAD_CORE_EXPORT ConfigEntry { public: - class String : public UnicodeString { - public: - String() {} - String(const UnicodeString &str) : UnicodeString(str) {} - - bool matches(const UnicodeString &str) const { - return (str.caseCompare(*this, 0) == 0); - } - }; - class MAD_CORE_EXPORT Entry { private: String key; @@ -51,7 +41,7 @@ class MAD_CORE_EXPORT ConfigEntry { public: Entry() {} - Entry(const std::vector &args) { + Entry(const std::vector &args) { if(args.empty()) return; diff --git a/src/Core/ConfigManager.cpp b/src/Core/ConfigManager.cpp index 51d8647..d79ce32 100644 --- a/src/Core/ConfigManager.cpp +++ b/src/Core/ConfigManager.cpp @@ -26,6 +26,8 @@ #include #include +#include + namespace Mad { namespace Core { @@ -55,9 +57,9 @@ bool ConfigManager::loadFile(const std::string &filename) { std::ifstream file(filename.c_str()); ConfigEntry entry; - UnicodeString line, input; + String line, input; UChar delim; - std::vector splitLine, lastConfigLine; + std::vector splitLine, lastConfigLine; if(!file.good()) return false; diff --git a/src/Core/Exception.h b/src/Core/Exception.h index f12b3df..8e0342d 100644 --- a/src/Core/Exception.h +++ b/src/Core/Exception.h @@ -22,6 +22,8 @@ #include "export.h" +#include "String.h" + #include #include diff --git a/src/Core/LogManager.cpp b/src/Core/LogManager.cpp index 4a4cc0d..c2a90de 100644 --- a/src/Core/LogManager.cpp +++ b/src/Core/LogManager.cpp @@ -42,13 +42,13 @@ void LogManager::ConsoleLogger::logMessageDirect(MessageCategory /*category*/, M } -LogManager::MessageLevel LogManager::parseLevel(const UnicodeString &str) throw (Exception) { - static const UnicodeString DEBUG_LEVEL("debug"); - static const UnicodeString VERBOSE_LEVEL("verbose"); - static const UnicodeString DEFAULT_LEVEL("default"); - static const UnicodeString WARNING_LEVEL("warning"); - static const UnicodeString ERROR_LEVEL("error"); - static const UnicodeString CRITICAL_LEVEL("critical"); +LogManager::MessageLevel LogManager::parseLevel(const String &str) throw (Exception) { + static const String DEBUG_LEVEL("debug"); + static const String VERBOSE_LEVEL("verbose"); + static const String DEFAULT_LEVEL("default"); + static const String WARNING_LEVEL("warning"); + static const String ERROR_LEVEL("error"); + static const String CRITICAL_LEVEL("critical"); if(str.isEmpty()) return Logger::LOG_DEFAULT; diff --git a/src/Core/LogManager.h b/src/Core/LogManager.h index 9b47a7e..4f7c500 100644 --- a/src/Core/LogManager.h +++ b/src/Core/LogManager.h @@ -25,7 +25,7 @@ #include "Configurable.h" #include "Exception.h" #include "Logger.h" -#include "UnicodeString.h" +#include "String.h" #include #include @@ -100,7 +100,7 @@ class MAD_CORE_EXPORT LogManager : public Configurable { virtual void configFinished(); public: - static MessageLevel parseLevel(const UnicodeString &str) throw (Exception); + static MessageLevel parseLevel(const String &str) throw (Exception); void log(MessageCategory category, MessageLevel level, boost::posix_time::ptime timestamp, const std::string &message, const std::string &source = std::string()); diff --git a/src/Core/String.cpp b/src/Core/String.cpp new file mode 100644 index 0000000..d9d357e --- /dev/null +++ b/src/Core/String.cpp @@ -0,0 +1,44 @@ +/* + * String.cpp + * + * Copyright (C) 2009 Matthias Schiffer + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as published by the + * Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License along + * with this program. If not, see . + */ + +#include "String.h" + +namespace Mad { +namespace Core { + +boost::int32_t String::findFirstOf(const String &chars, boost::int32_t start) const { + for(boost::int32_t i = start; i < length(); ++i) { + if(chars.indexOf(charAt(i)) >= 0) + return i; + } + + return -1; +} + +boost::int32_t String::findFirstNotOf(const String &chars, boost::int32_t start) const { + for(boost::int32_t i = start; i < length(); ++i) { + if(chars.indexOf(charAt(i)) < 0) + return i; + } + + return -1; +} + +} +} diff --git a/src/Core/String.h b/src/Core/String.h new file mode 100644 index 0000000..1c93902 --- /dev/null +++ b/src/Core/String.h @@ -0,0 +1,88 @@ +/* + * String.h + * + * Copyright (C) 2009 Matthias Schiffer + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as published by the + * Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License along + * with this program. If not, see . + */ + +#ifndef MAD_CORE_STRING_H_ +#define MAD_CORE_STRING_H_ + +#include "export.h" + +#include +#include +#include +#include + +#define U_USING_ICU_NAMESPACE 0 +#include + +namespace Mad { +namespace Core { + +class MAD_CORE_EXPORT String : public icu::UnicodeString { + private: + std::string extractCodepage(const char *codepage) const { + boost::uint32_t len = static_cast(icu::UnicodeString::extract(0, length(), 0, codepage)); + + boost::scoped_array buf(new char[len]); + icu::UnicodeString::extract(0, length(), buf.get(), len, codepage); + + return std::string(buf.get(), len); + } + + public: + String() {} + String(const icu::UnicodeString &str) : icu::UnicodeString(str) {} + String(const char *str) : icu::UnicodeString(str, -1, US_INV) {} + + String substr(boost::int32_t start, boost::int32_t length = -1) const { + if(length < 0) + return icu::UnicodeString(*this, start); + else + return icu::UnicodeString(*this, start, length); + } + + bool matches(const String &str) const { + return (str.caseCompare(*this, 0) == 0); + } + + boost::int32_t findFirstOf(const String &chars, boost::int32_t start = 0) const; + boost::int32_t findFirstNotOf(const String &chars, boost::int32_t start = 0) const; + + std::string extract() const { + return extractCodepage(0); + } + + std::string extractUTF8() const { + return extractCodepage("UTF-8"); + } + + static String fromUTF8(const char *str) { + return icu::UnicodeString(str, "UTF-8"); + } +}; + +template +std::basic_ostream& operator<<(std::basic_ostream &stream, const String &string) { + stream << string.extract(); + return stream; +} + +} +} + +#endif /* MAD_CORE_STRING_H_ */ diff --git a/src/Core/Tokenizer.cpp b/src/Core/Tokenizer.cpp index 93c853a..9e0553e 100644 --- a/src/Core/Tokenizer.cpp +++ b/src/Core/Tokenizer.cpp @@ -24,11 +24,11 @@ namespace Mad { namespace Core { -const UnicodeString Tokenizer::delimiters(" \t\n\"'\\"); +const String Tokenizer::delimiters(" \t\n\"'\\"); -std::vector Tokenizer::split(const UnicodeString &str) { - std::vector ret; +std::vector Tokenizer::split(const String &str) { + std::vector ret; for(boost::int32_t s = 0; s < str.length();) { boost::int32_t index = str.findFirstOf(delimiters, s); @@ -55,16 +55,16 @@ std::vector Tokenizer::split(const UnicodeString &str) { return ret; } -bool Tokenizer::tokenize(const UnicodeString &str, std::vector &out) { - std::vector splitString = split(str); +bool Tokenizer::tokenize(const String &str, std::vector &out) { + std::vector splitString = split(str); bool singleQuotes = false, doubleQuotes = false, escape = false; - UnicodeString token; + String token; bool forceToken = false; out.clear(); - for(std::vector::iterator s = splitString.begin(); s != splitString.end(); ++s) { + for(std::vector::iterator s = splitString.begin(); s != splitString.end(); ++s) { token += *s; escape = false; diff --git a/src/Core/Tokenizer.h b/src/Core/Tokenizer.h index f65d44c..fbeeeda 100644 --- a/src/Core/Tokenizer.h +++ b/src/Core/Tokenizer.h @@ -22,7 +22,7 @@ #include "export.h" -#include "UnicodeString.h" +#include "String.h" #include namespace Mad { @@ -30,14 +30,14 @@ namespace Core { class MAD_CORE_EXPORT Tokenizer { private: - static const UnicodeString delimiters; + static const String delimiters; Tokenizer(); - static std::vector split(const UnicodeString &str); + static std::vector split(const String &str); public: - static bool tokenize(const UnicodeString &str, std::vector &out); + static bool tokenize(const String &str, std::vector &out); }; } diff --git a/src/Core/UnicodeString.cpp b/src/Core/UnicodeString.cpp deleted file mode 100644 index ccace47..0000000 --- a/src/Core/UnicodeString.cpp +++ /dev/null @@ -1,44 +0,0 @@ -/* - * UnicodeString.cpp - * - * Copyright (C) 2009 Matthias Schiffer - * - * This program is free software: you can redistribute it and/or modify it - * under the terms of the GNU Lesser General Public License as published by the - * Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. - * See the GNU Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License along - * with this program. If not, see . - */ - -#include "UnicodeString.h" - -namespace Mad { -namespace Core { - -boost::int32_t UnicodeString::findFirstOf(const UnicodeString &chars, boost::int32_t start) const { - for(boost::int32_t i = start; i < length(); ++i) { - if(chars.indexOf(charAt(i)) >= 0) - return i; - } - - return -1; -} - -boost::int32_t UnicodeString::findFirstNotOf(const UnicodeString &chars, boost::int32_t start) const { - for(boost::int32_t i = start; i < length(); ++i) { - if(chars.indexOf(charAt(i)) < 0) - return i; - } - - return -1; -} - -} -} diff --git a/src/Core/UnicodeString.h b/src/Core/UnicodeString.h deleted file mode 100644 index aed53b6..0000000 --- a/src/Core/UnicodeString.h +++ /dev/null @@ -1,64 +0,0 @@ -/* - * UnicodeString.h - * - * Copyright (C) 2009 Matthias Schiffer - * - * This program is free software: you can redistribute it and/or modify it - * under the terms of the GNU Lesser General Public License as published by the - * Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. - * See the GNU Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License along - * with this program. If not, see . - */ - -#ifndef MAD_CORE_UNICODESTRING_H_ -#define MAD_CORE_UNICODESTRING_H_ - -#include "export.h" - -#include -#include -#include - -#define U_USING_ICU_NAMESPACE 0 -#include - -namespace Mad { -namespace Core { - -class MAD_CORE_EXPORT UnicodeString : public icu::UnicodeString { - public: - UnicodeString() {} - UnicodeString(const icu::UnicodeString &str) : icu::UnicodeString(str) {} - UnicodeString(const char *str) : icu::UnicodeString(str) {} - - UnicodeString substr(boost::int32_t start, boost::int32_t length = -1) const { - if(length < 0) - return icu::UnicodeString(*this, start); - else - return icu::UnicodeString(*this, start, length); - } - - boost::int32_t findFirstOf(const UnicodeString &chars, boost::int32_t start = 0) const; - boost::int32_t findFirstNotOf(const UnicodeString &chars, boost::int32_t start = 0) const; - - std::string extract() const { - boost::uint32_t len = (boost::uint32_t)icu::UnicodeString::extract(0, length(), (char*)0, 0u); - - boost::scoped_array buf(new char[len]); - icu::UnicodeString::extract(0, length(), buf.get(), len); - - return std::string(buf.get(), len); - } -}; - -} -} - -#endif /* MAD_CORE_UNICODESTRING_H_ */ -- cgit v1.2.3