Chromium Code Reviews| 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 length that string value.substr(0, length) would be with | |
| 30 // trailing zeros removed. | |
| 31 size_t GetLengthWithoutTrailingZeros(const std::string& value, size_t length) { | |
| 32 DCHECK(!value.empty()); | |
| 33 | |
| 34 size_t end_position = value.find_last_not_of(kZeroDigit, length - 1); | |
|
akalin
2011/10/26 18:36:42
if you want to consider the entire string, just om
csharp
2011/10/26 19:59:18
We don't want to always consider the entire string
| |
| 35 | |
| 36 // If no non kZeroDigit is found then the string is a string of all zeros | |
| 37 // digits so we return 0 as the correct length. | |
| 38 if (end_position == std::string::npos) | |
| 39 return 0; | |
| 40 | |
| 41 return end_position + 1; | |
| 42 } | |
| 43 | |
| 44 // Return the digit value at position i, padding with kZeroDigit if required. | |
| 45 int GetPositionValue(const std::string& str, size_t i) { | |
| 46 return (i < str.length()) ? (str[i] - kZeroDigit) : 0; | |
| 47 } | |
| 48 | |
| 49 // Add kMidDigitValue to the value at position index. This returns false if | |
| 50 // adding the half results in an overflow past the first digit, otherwise it | |
| 51 // returns true. This is used by ComputeMidpoint. | |
| 52 bool AddHalf(size_t position, std::string& value) { | |
| 53 DCHECK_GT(position, 0U); | |
| 54 DCHECK_LT(position, value.length()); | |
| 55 | |
| 56 // We can't perform this operation directly on the string because | |
| 57 // overflow can occur and mess up the values. | |
| 58 int new_position_value = value[position] + kMidDigitValue; | |
| 59 | |
| 60 if (new_position_value <= kMaxDigit) { | |
| 61 value[position] = new_position_value; | |
| 62 } else { | |
| 63 value[position] = new_position_value - kRadix; | |
| 64 ++value[position - 1]; | |
| 65 | |
| 66 for (size_t i = position - 1; value[i] > kMaxDigit; --i) { | |
| 67 if (i == 0U) | |
| 68 return false; | |
| 69 value[i] -= kRadix; | |
| 70 ++value[i - 1]; | |
| 71 } | |
| 72 } | |
| 73 | |
| 74 return true; | |
| 75 } | |
| 76 | |
| 77 // Drops off the last digit of value and then all trailing zeros iff that | |
| 78 // doesn't change its ordering as greater than |start|. | |
| 79 void DropUnneededDigits(const std::string& start, std::string* value) { | |
| 80 CHECK_GT(*value, start); | |
| 81 | |
| 82 size_t drop_length = GetLengthWithoutTrailingZeros(*value, value->length()); | |
| 83 // See if the value can have its last digit removed without affecting | |
| 84 // the ordering. | |
| 85 if (drop_length > 1) { | |
| 86 // We must drop the trailing zeros before comparing |shorter_value| to | |
| 87 // |start| because if we don't we may have |shorter_value|=|start|+|a|* | |
| 88 // where |shorter_value| > |start| but not when it drops the trailing |a|s | |
| 89 // to become a valid StringOrdinal value. | |
| 90 int truncated_length = GetLengthWithoutTrailingZeros(*value, | |
| 91 drop_length - 1); | |
| 92 | |
| 93 if (truncated_length != 0 && value->compare(0, truncated_length, start) > 0) | |
| 94 drop_length = truncated_length; | |
| 95 } | |
| 96 | |
| 97 value->resize(drop_length); | |
| 98 } | |
| 99 | |
| 100 // Compute the midpoint string that is between |start| and |end|. | |
| 101 std::string ComputeMidpoint(const std::string& start, | |
| 102 const std::string& end) { | |
| 103 size_t max_size = std::max(start.length(), end.length()) + 1; | |
| 104 std::string midpoint(max_size, kZeroDigit); | |
| 105 | |
| 106 bool add_half = false; | |
| 107 for (size_t i = 0; i < max_size; ++i) { | |
| 108 int char_value = GetPositionValue(start, i); | |
| 109 char_value += GetPositionValue(end, i); | |
| 110 | |
| 111 midpoint[i] += (char_value / 2); | |
| 112 if (add_half) { | |
| 113 // AddHalf only returns false if (midpoint[0] > kMaxDigit) and it | |
|
akalin
2011/10/26 18:36:42
Just say that AddHalf() returning false implies th
csharp
2011/10/26 19:59:18
Done.
| |
| 114 // needs to overflow to the digit before it, which doesn't exist. This | |
| 115 // would mean that the midpoint string would have have a different | |
| 116 // pre-first digit than both |start| and |end|, which means it isn't | |
| 117 // between them anymore. | |
| 118 CHECK(AddHalf(i, midpoint)); | |
| 119 } | |
| 120 | |
| 121 add_half = (char_value % 2 == 1); | |
| 122 } | |
| 123 DCHECK(!add_half); | |
| 124 | |
| 125 return midpoint; | |
| 126 } | |
| 127 | |
| 128 // Create a StringOrdinal that is lexigraphically greater than |start| and | |
| 129 // lexigraphically less than |end|. The returned StringOrdinal will be roughly | |
| 130 // between |start| and |end|. | |
| 131 StringOrdinal CreateStringOrdinalBetween(const StringOrdinal& start, | |
| 132 const StringOrdinal& end) { | |
| 133 CHECK(start.IsValid()); | |
| 134 CHECK(end.IsValid()); | |
| 135 CHECK(start.LessThan(end)); | |
| 136 const std::string& start_string = start.ToString(); | |
| 137 const std::string& end_string = end.ToString(); | |
| 138 DCHECK_LT(start_string, end_string); | |
| 139 | |
| 140 std::string midpoint = ComputeMidpoint(start_string, end_string); | |
| 141 | |
| 142 DropUnneededDigits(start_string, &midpoint); | |
| 143 | |
| 144 DCHECK_GT(midpoint, start_string); | |
| 145 DCHECK_LT(midpoint, end_string); | |
| 146 | |
| 147 StringOrdinal midpoint_ordinal(midpoint); | |
| 148 DCHECK(midpoint_ordinal.IsValid()); | |
| 149 return midpoint_ordinal; | |
| 150 } | |
| 151 | |
| 152 // Returns true iff the input string matches the format [a-z]*[b-z]. | |
| 153 bool IsValidStringOrdinal(const std::string& value) { | |
| 154 if (value.empty()) | |
| 155 return false; | |
| 156 | |
| 157 for (size_t i = 0; i < value.length(); ++i) { | |
| 158 if (value[i] < kZeroDigit || value[i] > kMaxDigit) | |
| 159 return false; | |
| 160 } | |
| 161 | |
| 162 return value[value.length() - 1] != kZeroDigit; | |
| 163 } | |
| 164 | |
| 165 } // namespace | |
| 166 | |
| 167 StringOrdinal::StringOrdinal(const std::string& string_ordinal) | |
| 168 : string_ordinal_(string_ordinal), | |
| 169 is_valid_(IsValidStringOrdinal(string_ordinal_)) { | |
| 170 } | |
| 171 | |
| 172 StringOrdinal::StringOrdinal() : string_ordinal_(""), | |
| 173 is_valid_(false) { | |
| 174 } | |
| 175 | |
| 176 bool StringOrdinal::IsValid() const { | |
| 177 return is_valid_; | |
| 178 } | |
| 179 | |
| 180 bool StringOrdinal::LessThan(const StringOrdinal& other) const { | |
| 181 CHECK(IsValid()); | |
| 182 CHECK(other.IsValid()); | |
| 183 return string_ordinal_ < other.string_ordinal_; | |
| 184 } | |
| 185 | |
| 186 bool StringOrdinal::Equal(const StringOrdinal& other) const { | |
| 187 CHECK(IsValid()); | |
| 188 CHECK(other.IsValid()); | |
| 189 return string_ordinal_ == other.string_ordinal_; | |
| 190 } | |
| 191 | |
| 192 StringOrdinal StringOrdinal::CreateBetween(const StringOrdinal& other) const { | |
| 193 CHECK(IsValid()); | |
| 194 CHECK(other.IsValid()); | |
| 195 CHECK(!Equal(other)); | |
| 196 | |
| 197 if (LessThan(other)) { | |
| 198 return CreateStringOrdinalBetween(*this, other); | |
| 199 } else { | |
| 200 return CreateStringOrdinalBetween(other, *this); | |
| 201 } | |
| 202 } | |
| 203 | |
| 204 StringOrdinal StringOrdinal::CreateBefore() const { | |
| 205 CHECK(IsValid()); | |
| 206 // Create the smallest valid StringOrdinal of the appropriate length | |
| 207 // to be the minimum boundary. | |
| 208 const size_t length = string_ordinal_.length(); | |
| 209 std::string start(length, kZeroDigit); | |
| 210 start[length - 1] = kMinDigit; | |
| 211 if (start == string_ordinal_) { | |
| 212 start[length - 1] = kZeroDigit; | |
| 213 start += kMinDigit; | |
| 214 } | |
| 215 | |
| 216 // Even though |start| is already a valid StringOrdinal that is less | |
| 217 // than |*this|, we don't return it because we wouldn't have much space in | |
| 218 // front of it to insert potential future values. | |
| 219 return CreateBetween(StringOrdinal(start)); | |
| 220 } | |
| 221 | |
| 222 StringOrdinal StringOrdinal::CreateAfter() const { | |
| 223 CHECK(IsValid()); | |
| 224 // Create the largest valid StringOrdinal of the appropriate length to be | |
| 225 // the maximum boundary. | |
| 226 std::string end(string_ordinal_.length(), kMaxDigit); | |
| 227 if (end == string_ordinal_) | |
| 228 end += kMaxDigit; | |
| 229 | |
| 230 // Even though |end| is already a valid StringOrdinal that is greater than | |
| 231 // |*this|, we don't return it because we wouldn't have much space after | |
| 232 // it to insert potential future values. | |
| 233 return CreateBetween(StringOrdinal(end)); | |
| 234 } | |
| 235 | |
| 236 std::string StringOrdinal::ToString() const { | |
| 237 CHECK(IsValid()); | |
| 238 return string_ordinal_; | |
| 239 } | |
| OLD | NEW |