working text utils

Former-commit-id: fa44e4578a007291948e4709a0cfd4278fd3af66
This commit is contained in:
Rafał Jaworski 2014-04-24 14:26:35 +02:00
parent 93c3f50b14
commit 5eaf981bc0
4 changed files with 30 additions and 23 deletions

View File

@ -1,7 +1,7 @@
add_executable(concordia-console concordia-console.cpp) add_executable(concordia-console concordia-console.cpp)
target_link_libraries(concordia-console concordia ${Boost_LIBRARIES} ${LIBCONFIG_LIB} ${LIBSTEMMER_LIB}) target_link_libraries(concordia-console concordia utf8case ${Boost_LIBRARIES} ${LIBCONFIG_LIB} ${LIBSTEMMER_LIB})
if (WITH_RE2) if (WITH_RE2)
target_link_libraries(concordia-console re2) target_link_libraries(concordia-console re2)

View File

@ -3,27 +3,17 @@
#include <boost/algorithm/string/case_conv.hpp> #include <boost/algorithm/string/case_conv.hpp>
#include <boost/locale.hpp> #include <boost/locale.hpp>
TextUtils::TextUtils() {
using namespace boost::locale; _lowerConverter =
StringCaseConverterManager::getInstance().getLowerCaseConverter("pl");
_upperConverter =
StringCaseConverterManager::getInstance().getUpperCaseConverter("pl");
}
string TextUtils::toLowerCase(const string & text) { string TextUtils::toLowerCase(const string & text) {
generator gen; return simpleConvert(*_lowerConverter, text);
locale loc=gen("pl_PL.UTF-8");
locale::global(loc);
cout.imbue(loc);
string result = text;
boost::locale::to_lower(result);
return result;
} }
string TextUtils::toUpperCase(const string & text) { string TextUtils::toUpperCase(const string & text) {
generator gen; return simpleConvert(*_upperConverter, text);
locale loc=gen("pl_PL.UTF-8");
locale::global(loc);
cout.imbue(loc);
string result = text;
boost::locale::to_upper(result);
return result;
} }

View File

@ -2,6 +2,10 @@
#define TEXT_UTILS_HDR #define TEXT_UTILS_HDR
#include <string> #include <string>
#include <boost/shared_ptr.hpp>
#include "utf8case/simple_convert.hpp"
#include "utf8case/case_converter_factory.hpp"
#include "utf8case/string_case_converter_manager.hpp"
using namespace std; using namespace std;
@ -10,21 +14,34 @@ using namespace std;
*/ */
class TextUtils { class TextUtils {
public: public:
TextUtils();
static TextUtils & getInstance() {
static TextUtils instance; // Guaranteed to be destroyed.
// Instantiated on first use.
return instance;
}
/*! A method for converting all string letters to lower case. /*! A method for converting all string letters to lower case.
\param text input string \param text input string
\returns lower case version of the input string. \returns lower case version of the input string.
*/ */
static string toLowerCase(const string & text); string toLowerCase(const string & text);
/*! A method for converting all string letters to upper case. /*! A method for converting all string letters to upper case.
\param text input string \param text input string
\returns upper case version of the input string. \returns upper case version of the input string.
*/ */
static string toUpperCase(const string & text); string toUpperCase(const string & text);
private: private:
TextUtils(TextUtils const&); // Don't Implement
void operator=(TextUtils const&); // Don't implement
boost::shared_ptr<StringGeneralCaseConverter> _lowerConverter;
boost::shared_ptr<StringGeneralCaseConverter> _upperConverter;
}; };
#endif #endif

View File

@ -9,13 +9,13 @@ BOOST_AUTO_TEST_SUITE(text_utils)
BOOST_AUTO_TEST_CASE( ToLower ) BOOST_AUTO_TEST_CASE( ToLower )
{ {
string str = "ZAŻÓŁĆ GĘŚLĄ JAŹŃ"; string str = "ZAŻÓŁĆ GĘŚLĄ JAŹŃ";
BOOST_CHECK_EQUAL(TextUtils::toLowerCase(str),"zażółć gęślą jaźń"); BOOST_CHECK_EQUAL(TextUtils::getInstance().toLowerCase(str),"zażółć gęślą jaźń");
} }
BOOST_AUTO_TEST_CASE( ToUpper ) BOOST_AUTO_TEST_CASE( ToUpper )
{ {
string str = "zażółć gęślą jaźń"; string str = "zażółć gęślą jaźń";
BOOST_CHECK_EQUAL(TextUtils::toUpperCase(str),"ZAŻÓŁĆ GĘŚLĄ JAŹŃ"); BOOST_CHECK_EQUAL(TextUtils::getInstance().toUpperCase(str),"ZAŻÓŁĆ GĘŚLĄ JAŹŃ");
} }
BOOST_AUTO_TEST_SUITE_END() BOOST_AUTO_TEST_SUITE_END()