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)
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)
target_link_libraries(concordia-console re2)

View File

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

View File

@ -2,6 +2,10 @@
#define TEXT_UTILS_HDR
#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;
@ -10,21 +14,34 @@ using namespace std;
*/
class TextUtils {
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.
\param text 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.
\param text input string
\returns upper case version of the input string.
*/
static string toUpperCase(const string & text);
string toUpperCase(const string & text);
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

View File

@ -9,13 +9,13 @@ BOOST_AUTO_TEST_SUITE(text_utils)
BOOST_AUTO_TEST_CASE( ToLower )
{
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 )
{
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()