OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/common/string_ordinal.h" | |
6 | |
7 #include <algorithm> | |
8 #include <cstddef> | |
9 | |
10 #include "base/basictypes.h" | |
11 #include "base/logging.h" | |
12 | |
13 namespace { | |
14 | |
15 // Constants for StringOrdinal digits. | |
16 const char kZeroDigit = 'a'; | |
17 const char kMinDigit = 'b'; | |
18 const char kMidDigit = 'n'; | |
19 const char kMaxDigit = 'z'; | |
20 const int kMidDigitValue = kMidDigit - kZeroDigit; | |
21 const int kMaxDigitValue = kMaxDigit - kZeroDigit; | |
22 const int kRadix = kMaxDigitValue + 1; | |
23 COMPILE_ASSERT(kMidDigitValue == 13, kMidDigitValue_incorrect); | |
24 COMPILE_ASSERT(kMaxDigitValue == 25, kMaxDigitValue_incorrect); | |
25 COMPILE_ASSERT(kRadix == 26, kRadix_incorrect); | |
26 | |
27 // Helper Functions | |
28 | |
29 // Returns the last position before the given length that has a non-zero digit. | |
30 size_t GetLastNonZeroPosition(const std::string& value, size_t length) { | |
31 DCHECK(!value.empty()); | |
32 | |
33 int end_position = length - 1; | |
34 while (value[end_position] == kZeroDigit) { | |
35 --end_position; | |
36 } | |
37 | |
38 return end_position + 1; | |
akalin
2011/10/20 19:47:54
ah I was confused, this should be renamed to "GetN
| |
39 } | |
40 | |
41 // Return the digit value at position i, padding with kZeroDigit if required. | |
42 int GetPositionValue(const std::string& str, size_t i) { | |
43 return (i < str.length()) ? (str[i] - kZeroDigit) : 0; | |
44 } | |
45 | |
46 // Add kMidDigitValue to the value at position index. This is used by | |
47 // ComputeMidpoint. | |
48 void AddHalf(size_t position, std::string& value) { | |
49 DCHECK_GT(position, 0U); | |
50 | |
51 // We can't perform this operation directly on the string because | |
52 // overflow can occur and mess up the values. | |
53 int new_position_value = value[position] + kMidDigitValue; | |
54 | |
55 if (new_position_value <= kMaxDigit) { | |
56 value[position] = new_position_value; | |
57 } else { | |
58 value[position] = new_position_value - kRadix; | |
59 ++value[position - 1]; | |
60 | |
61 for (size_t i = position - 1; value[i] > kMaxDigit; --i) { | |
62 CHECK_GT(i, 0U); | |
63 value[i] -= kRadix; | |
64 ++value[i - 1]; | |
65 } | |
66 } | |
67 } | |
68 | |
69 // Drops off the last digit of value and then all trailing zeros iff that | |
70 // doesn't change its ordering as greater than |start|. | |
71 void DropUnneededDigits(const std::string& start, | |
72 std::string* value) { | |
akalin
2011/10/20 19:47:54
can't this fit on previous line?
| |
73 CHECK_GT(*value, start); | |
74 | |
75 size_t drop_length = GetLastNonZeroPosition(*value, value->length()); | |
76 // See if the value can have its last digit removed without affecting | |
77 // the ordering. | |
78 if (drop_length > 1) { | |
79 // We must drop the trailing zeros before comparing |shorter_value| to | |
80 // |start| because if we don't we may have |shorter_value|=|start|+|a|* | |
81 // where |shorter_value| > |start| but not when it drops the trailing |a|s | |
82 // to become a valid StringOrdinal value. | |
83 int truncated_length = GetLastNonZeroPosition(*value, drop_length - 1); | |
84 | |
85 if (value->compare(0, truncated_length, start) > 0) | |
86 drop_length = truncated_length; | |
87 } | |
88 | |
89 value->resize(drop_length); | |
90 } | |
91 | |
92 // Compute the midpoint string that is between |start| and |end|. | |
93 std::string ComputeMidpoint(const std::string& start, | |
94 const std::string& end) { | |
95 size_t max_size = std::max(start.length(), end.length()) + 1; | |
96 std::string midpoint(max_size, kZeroDigit); | |
97 | |
98 bool add_half = false; | |
99 for (size_t i = 0; i < max_size; ++i) { | |
100 int char_value = GetPositionValue(start, i); | |
101 char_value += GetPositionValue(end, i); | |
102 | |
103 midpoint[i] += (char_value / 2); | |
104 if (add_half) | |
105 AddHalf(i, midpoint); | |
106 add_half = (char_value % 2 == 1); | |
107 } | |
108 DCHECK(!add_half); | |
109 | |
110 return midpoint; | |
111 } | |
112 | |
113 // Create a StringOrdinal that is lexigraphically greater than |start| and | |
114 // lexigraphically less than |end|. The returned StringOrdinal will be roughly | |
115 // between |start| and |end|. | |
116 StringOrdinal CreateStringOrdinalBetween(const StringOrdinal& start, | |
117 const StringOrdinal& end) { | |
118 CHECK(start.IsValid()); | |
119 CHECK(end.IsValid()); | |
120 CHECK(start.LessThan(end)); | |
121 const std::string& start_string = start.ToString(); | |
122 const std::string& end_string = end.ToString(); | |
123 DCHECK_LT(start_string, end_string); | |
124 | |
125 std::string midpoint = ComputeMidpoint(start_string, end_string); | |
126 | |
127 DropUnneededDigits(start_string, &midpoint); | |
128 | |
129 DCHECK_GT(midpoint, start_string); | |
130 DCHECK_LT(midpoint, end_string); | |
131 | |
132 StringOrdinal midpoint_ordinal(midpoint); | |
133 DCHECK(midpoint_ordinal.IsValid()); | |
134 return midpoint_ordinal; | |
135 } | |
136 | |
137 // Returns true iff the input string matches the format [a-z]*[b-z]. | |
138 bool IsValidStringOrdinal(const std::string& value) { | |
139 if (value.empty()) | |
140 return false; | |
141 | |
142 for (size_t i = 0; i < value.length(); ++i) { | |
143 if (value[i] < kZeroDigit || value[i] > kMaxDigit) | |
144 return false; | |
145 } | |
146 | |
147 return value[value.length() - 1] != kZeroDigit; | |
148 } | |
149 | |
150 } // namespace | |
151 | |
152 StringOrdinal::StringOrdinal(const std::string& string_ordinal) | |
153 : string_ordinal_(string_ordinal), | |
154 is_valid_(IsValidStringOrdinal(string_ordinal_)) { | |
155 } | |
156 | |
157 StringOrdinal::StringOrdinal() : string_ordinal_(""), | |
158 is_valid_(false) { | |
159 } | |
160 | |
161 bool StringOrdinal::IsValid() const { | |
162 return is_valid_; | |
163 } | |
164 | |
165 bool StringOrdinal::LessThan(const StringOrdinal& other) const { | |
166 CHECK(IsValid()); | |
167 CHECK(other.IsValid()); | |
168 return string_ordinal_ < other.string_ordinal_; | |
169 } | |
170 | |
171 bool StringOrdinal::Equal(const StringOrdinal& other) const { | |
172 CHECK(IsValid()); | |
173 CHECK(other.IsValid()); | |
174 return string_ordinal_ == other.string_ordinal_; | |
175 } | |
176 | |
177 StringOrdinal StringOrdinal::CreateBetween(const StringOrdinal& other) const { | |
178 CHECK(IsValid()); | |
179 CHECK(other.IsValid()); | |
180 CHECK(!Equal(other)); | |
181 | |
182 if (LessThan(other)) { | |
183 return CreateStringOrdinalBetween(*this, other); | |
184 } else { | |
185 return CreateStringOrdinalBetween(other, *this); | |
186 } | |
187 } | |
188 | |
189 StringOrdinal StringOrdinal::CreateBefore() const { | |
190 CHECK(IsValid()); | |
191 // Create the smallest valid StringOrdinal of the appropriate length | |
192 // to be the minimum boundary. | |
193 const size_t length = string_ordinal_.length(); | |
194 std::string start(length, kZeroDigit); | |
195 start[length - 1] = kMinDigit; | |
196 if (start == string_ordinal_) { | |
197 start[length - 1] = kZeroDigit; | |
198 start += kMinDigit; | |
199 } | |
200 | |
201 // Even though |start| is already a valid StringOrdinal that is less | |
202 // than |*this|, we don't return it because we wouldn't have much space in | |
203 // front of it to insert potential future values. | |
204 return CreateBetween(StringOrdinal(start)); | |
205 } | |
206 | |
207 StringOrdinal StringOrdinal::CreateAfter() const { | |
208 CHECK(IsValid()); | |
209 // Create the largest valid StringOrdinal of the appropriate length to be | |
210 // the maximum boundary. | |
211 std::string end(string_ordinal_.length(), kMaxDigit); | |
212 if (end == string_ordinal_) | |
213 end += kMaxDigit; | |
214 | |
215 // Even though |end| is already a valid StringOrdinal that is greater than | |
216 // |*this|, we don't return it because we wouldn't have much space after | |
217 // it to insert potential future values. | |
218 return CreateBetween(StringOrdinal(end)); | |
219 } | |
220 | |
221 std::string StringOrdinal::ToString() const { | |
222 CHECK(IsValid()); | |
223 return string_ordinal_; | |
224 } | |
OLD | NEW |