This source file includes following definitions.
- NormalizeLanguageCode
- TimeToString
- LooseStringCompare
- SplitString
#include "string_util.h"
#include <libaddressinput/util/scoped_ptr.h>
#include <cassert>
#include <cstddef>
#include <cstdio>
#include <ctime>
#include <string>
#include <vector>
#include "canonicalize_string.h"
#ifdef _MSC_VER
#define snprintf _snprintf
#endif
namespace i18n {
namespace addressinput {
std::string NormalizeLanguageCode(const std::string& language_code) {
std::string::size_type pos = language_code.find('-');
if (pos == std::string::npos) {
return language_code;
}
if (language_code.substr(pos) == "-latn") {
return language_code;
}
return language_code.substr(0, pos);
}
std::string TimeToString(time_t time) {
char time_string[2 + 3 * sizeof time];
snprintf(time_string, sizeof time_string, "%ld", time);
return time_string;
}
bool LooseStringCompare(const std::string& a, const std::string& b) {
scoped_ptr<StringCanonicalizer> canonicalizer(StringCanonicalizer::Build());
return canonicalizer->CanonicalizeString(a) ==
canonicalizer->CanonicalizeString(b);
}
void SplitString(const std::string& str, char s, std::vector<std::string>* r) {
assert(r != NULL);
r->clear();
size_t last = 0;
size_t c = str.size();
for (size_t i = 0; i <= c; ++i) {
if (i == c || str[i] == s) {
std::string tmp(str, last, i - last);
if (i != c || !r->empty() || !tmp.empty()) {
r->push_back(tmp);
}
last = i + 1;
}
}
}
}
}