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 <cassert> |
| 20 #include <cstddef> |
| 21 #include <cstdio> |
| 22 #include <ctime> |
| 23 #include <string> |
| 24 #include <vector> |
| 25 |
| 26 #include "canonicalize_string.h" |
| 27 |
| 28 #ifdef _MSC_VER |
| 29 // http://msdn.microsoft.com/en-us/library/2ts7cx93%28v=vs.110%29.aspx |
| 30 #define snprintf _snprintf |
| 31 #endif // _MSC_VER |
| 32 |
| 33 namespace i18n { |
| 34 namespace addressinput { |
| 35 |
| 36 std::string NormalizeLanguageCode(const std::string& language_code) { |
| 37 std::string::size_type pos = language_code.find('-'); |
| 38 if (pos == std::string::npos) { |
| 39 return language_code; |
| 40 } |
| 41 if (language_code.substr(pos) == "-latn") { |
| 42 return language_code; |
| 43 } |
| 44 return language_code.substr(0, pos); |
| 45 } |
| 46 |
| 47 std::string TimeToString(time_t time) { |
| 48 char time_string[2 + 3 * sizeof time]; |
| 49 snprintf(time_string, sizeof time_string, "%ld", time); |
| 50 return time_string; |
| 51 } |
| 52 |
| 53 bool LooseStringCompare(const std::string& a, const std::string& b) { |
| 54 scoped_ptr<StringCanonicalizer> canonicalizer(StringCanonicalizer::Build()); |
| 55 return canonicalizer->CanonicalizeString(a) == |
| 56 canonicalizer->CanonicalizeString(b); |
| 57 } |
| 58 |
| 59 // The original source code is from: |
| 60 // http://src.chromium.org/viewvc/chrome/trunk/src/base/strings/string_split.cc?
revision=216633 |
| 61 void SplitString(const std::string& str, char s, std::vector<std::string>* r) { |
| 62 assert(r != NULL); |
| 63 r->clear(); |
| 64 size_t last = 0; |
| 65 size_t c = str.size(); |
| 66 for (size_t i = 0; i <= c; ++i) { |
| 67 if (i == c || str[i] == s) { |
| 68 std::string tmp(str, last, i - last); |
| 69 // Avoid converting an empty or all-whitespace source string into a vector |
| 70 // of one empty string. |
| 71 if (i != c || !r->empty() || !tmp.empty()) { |
| 72 r->push_back(tmp); |
| 73 } |
| 74 last = i + 1; |
| 75 } |
| 76 } |
| 77 } |
| 78 |
| 79 } // namespace addressinput |
| 80 } // namespace i18n |
OLD | NEW |