| OLD | NEW |
| (Empty) |
| 1 // Copyright (C) 2014 Google Inc. | |
| 2 // | |
| 3 // Licensed under the Apache License, Version 2.0 (the "License"); | |
| 4 // you may not use this file except in compliance with the License. | |
| 5 // You may obtain a copy of the License at | |
| 6 // | |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 | |
| 8 // | |
| 9 // Unless required by applicable law or agreed to in writing, software | |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, | |
| 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 12 // See the License for the specific language governing permissions and | |
| 13 // limitations under the License. | |
| 14 | |
| 15 #include "string_util.h" | |
| 16 | |
| 17 #include <libaddressinput/util/scoped_ptr.h> | |
| 18 | |
| 19 #include <algorithm> | |
| 20 #include <cassert> | |
| 21 #include <cctype> | |
| 22 #include <cstddef> | |
| 23 #include <cstdio> | |
| 24 #include <ctime> | |
| 25 #include <string> | |
| 26 #include <vector> | |
| 27 | |
| 28 #include "canonicalize_string.h" | |
| 29 | |
| 30 #ifdef _MSC_VER | |
| 31 // http://msdn.microsoft.com/en-us/library/2ts7cx93%28v=vs.110%29.aspx | |
| 32 #define snprintf _snprintf | |
| 33 #endif // _MSC_VER | |
| 34 | |
| 35 namespace i18n { | |
| 36 namespace addressinput { | |
| 37 | |
| 38 std::string NormalizeLanguageCode(const std::string& language_code) { | |
| 39 std::string lowercase = language_code; | |
| 40 std::transform(lowercase.begin(), lowercase.end(), lowercase.begin(), | |
| 41 tolower); | |
| 42 std::string::size_type pos = lowercase.find('-'); | |
| 43 if (pos == std::string::npos) { | |
| 44 return language_code; | |
| 45 } | |
| 46 static const char kLatinSuffix[] = "-latn"; | |
| 47 static const size_t kLatinSuffixSize = sizeof kLatinSuffix - 1; | |
| 48 if (lowercase.substr(pos, kLatinSuffixSize) == kLatinSuffix) { | |
| 49 return language_code; | |
| 50 } | |
| 51 return lowercase.substr(0, pos); | |
| 52 } | |
| 53 | |
| 54 std::string TimeToString(time_t time) { | |
| 55 char time_string[2 + 3 * sizeof time]; | |
| 56 snprintf(time_string, sizeof time_string, "%ld", time); | |
| 57 return time_string; | |
| 58 } | |
| 59 | |
| 60 bool LooseStringCompare(const std::string& a, const std::string& b) { | |
| 61 scoped_ptr<StringCanonicalizer> canonicalizer(StringCanonicalizer::Build()); | |
| 62 return canonicalizer->CanonicalizeString(a) == | |
| 63 canonicalizer->CanonicalizeString(b); | |
| 64 } | |
| 65 | |
| 66 // The original source code is from: | |
| 67 // http://src.chromium.org/viewvc/chrome/trunk/src/base/strings/string_split.cc?
revision=216633 | |
| 68 void SplitString(const std::string& str, char s, std::vector<std::string>* r) { | |
| 69 assert(r != NULL); | |
| 70 r->clear(); | |
| 71 size_t last = 0; | |
| 72 size_t c = str.size(); | |
| 73 for (size_t i = 0; i <= c; ++i) { | |
| 74 if (i == c || str[i] == s) { | |
| 75 std::string tmp(str, last, i - last); | |
| 76 // Avoid converting an empty or all-whitespace source string into a vector | |
| 77 // of one empty string. | |
| 78 if (i != c || !r->empty() || !tmp.empty()) { | |
| 79 r->push_back(tmp); | |
| 80 } | |
| 81 last = i + 1; | |
| 82 } | |
| 83 } | |
| 84 } | |
| 85 | |
| 86 } // namespace addressinput | |
| 87 } // namespace i18n | |
| OLD | NEW |