OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/version.h" | 5 #include "base/version.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/string_number_conversions.h" |
8 #include "base/string_util.h" | 9 #include "base/string_util.h" |
| 10 #include "base/utf_string_conversions.h" |
9 | 11 |
10 // static | 12 // static |
11 Version* Version::GetVersionFromString(const std::wstring& version_str) { | 13 Version* Version::GetVersionFromString(const std::wstring& version_str) { |
12 if (!IsStringASCII(version_str)) | 14 if (!IsStringASCII(version_str)) |
13 return NULL; | 15 return NULL; |
14 return GetVersionFromString(WideToASCII(version_str)); | 16 return GetVersionFromString(WideToUTF8(version_str)); |
15 } | 17 } |
16 | 18 |
17 // static | 19 // static |
18 Version* Version::GetVersionFromString(const std::string& version_str) { | 20 Version* Version::GetVersionFromString(const std::string& version_str) { |
19 Version* vers = new Version(); | 21 Version* vers = new Version(); |
20 if (vers->InitFromString(version_str)) { | 22 if (vers->InitFromString(version_str)) { |
21 DCHECK(vers->is_valid_); | 23 DCHECK(vers->is_valid_); |
22 return vers; | 24 return vers; |
23 } | 25 } |
24 delete vers; | 26 delete vers; |
(...skipping 28 matching lines...) Expand all Loading... |
53 return -1; | 55 return -1; |
54 } | 56 } |
55 return 0; | 57 return 0; |
56 } | 58 } |
57 | 59 |
58 const std::string Version::GetString() const { | 60 const std::string Version::GetString() const { |
59 DCHECK(is_valid_); | 61 DCHECK(is_valid_); |
60 std::string version_str; | 62 std::string version_str; |
61 int count = components_.size(); | 63 int count = components_.size(); |
62 for (int i = 0; i < count - 1; ++i) { | 64 for (int i = 0; i < count - 1; ++i) { |
63 version_str.append(IntToString(components_[i])); | 65 version_str.append(base::IntToString(components_[i])); |
64 version_str.append("."); | 66 version_str.append("."); |
65 } | 67 } |
66 version_str.append(IntToString(components_[count - 1])); | 68 version_str.append(base::IntToString(components_[count - 1])); |
67 return version_str; | 69 return version_str; |
68 } | 70 } |
69 | 71 |
70 bool Version::InitFromString(const std::string& version_str) { | 72 bool Version::InitFromString(const std::string& version_str) { |
71 DCHECK(!is_valid_); | 73 DCHECK(!is_valid_); |
72 std::vector<std::string> numbers; | 74 std::vector<std::string> numbers; |
73 SplitString(version_str, '.', &numbers); | 75 SplitString(version_str, '.', &numbers); |
74 if (numbers.empty()) | 76 if (numbers.empty()) |
75 return false; | 77 return false; |
76 for (std::vector<std::string>::iterator i = numbers.begin(); | 78 for (std::vector<std::string>::iterator i = numbers.begin(); |
77 i != numbers.end(); ++i) { | 79 i != numbers.end(); ++i) { |
78 int num; | 80 int num; |
79 if (!StringToInt(*i, &num)) | 81 if (!base::StringToInt(*i, &num)) |
80 return false; | 82 return false; |
81 if (num < 0) | 83 if (num < 0) |
82 return false; | 84 return false; |
83 const uint16 max = 0xFFFF; | 85 const uint16 max = 0xFFFF; |
84 if (num > max) | 86 if (num > max) |
85 return false; | 87 return false; |
86 // This throws out things like +3, or 032. | 88 // This throws out things like +3, or 032. |
87 if (IntToString(num) != *i) | 89 if (base::IntToString(num) != *i) |
88 return false; | 90 return false; |
89 uint16 component = static_cast<uint16>(num); | 91 uint16 component = static_cast<uint16>(num); |
90 components_.push_back(component); | 92 components_.push_back(component); |
91 } | 93 } |
92 is_valid_ = true; | 94 is_valid_ = true; |
93 return true; | 95 return true; |
94 } | 96 } |
OLD | NEW |