This source file includes following definitions.
- collator_
- CanonicalizeString
- Build
#include "cpp/src/util/canonicalize_string.h"
#include "base/logging.h"
#include "cpp/include/libaddressinput/util/scoped_ptr.h"
#include "third_party/icu/source/i18n/unicode/coll.h"
namespace i18n {
namespace addressinput {
namespace {
class ChromeStringCanonicalizer : public StringCanonicalizer {
public:
ChromeStringCanonicalizer()
: error_code_(U_ZERO_ERROR),
collator_(icu::Collator::createInstance(error_code_)) {
collator_->setStrength(icu::Collator::PRIMARY);
DCHECK(U_SUCCESS(error_code_));
}
virtual ~ChromeStringCanonicalizer() {}
virtual std::string CanonicalizeString(const std::string& original) {
icu::UnicodeString icu_str(
original.c_str(), static_cast<int32_t>(original.length()));
int32_t buffer_size = collator_->getSortKey(icu_str, NULL, 0);
scoped_ptr<uint8_t[]> buffer(new uint8_t[buffer_size]);
DCHECK(buffer.get());
int32_t filled_size =
collator_->getSortKey(icu_str, buffer.get(), buffer_size);
DCHECK_EQ(buffer_size, filled_size);
return std::string(reinterpret_cast<const char*>(buffer.get()));
}
private:
UErrorCode error_code_;
scoped_ptr<icu::Collator> collator_;
DISALLOW_COPY_AND_ASSIGN(ChromeStringCanonicalizer);
};
}
scoped_ptr<StringCanonicalizer> StringCanonicalizer::Build() {
return scoped_ptr<StringCanonicalizer>(new ChromeStringCanonicalizer);
}
}
}