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 // Remove all trailing zero digits from a value as they provide no value. | |
| 30 void RemoveTrailingZeros(std::string* value) { | |
| 31 DCHECK(!value->empty()); | |
| 32 | |
| 33 int end_position = value->length() - 1; | |
| 34 while ((*value)[end_position] == kZeroDigit) { | |
| 35 --end_position; | |
| 36 } | |
| 37 | |
| 38 value->resize(end_position + 1); | |
| 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 because | |
| 47 // the previous index values had an odd difference, so their correct | |
|
akalin
2011/10/20 19:06:51
difference -> midpoint
their correct -> its corre
| |
| 48 // middle value is x and a half, so the half is now inserted. | |
| 49 void AddHalf(size_t position, std::string& value) { | |
| 50 DCHECK_GT(position, 0U); | |
| 51 | |
| 52 // We can't perform this operation directly on the string because | |
| 53 // overflow can occur and mess up the values. | |
| 54 int new_position_value = value[position] + kMidDigitValue; | |
| 55 | |
| 56 if (new_position_value <= kMaxDigit) { | |
| 57 value[position] = new_position_value; | |
| 58 } else { | |
| 59 value[position] = new_position_value - kRadix; | |
| 60 ++value[position - 1]; | |
| 61 | |
| 62 for (size_t i = position - 1; value[i] > kMaxDigit; --i) { | |
| 63 CHECK_GT(i, 0U); | |
| 64 value[i] -= kRadix; | |
| 65 ++value[i - 1]; | |
| 66 } | |
| 67 } | |
| 68 } | |
| 69 | |
| 70 // Drops off the last digit of value and then all trailing zeros iff that | |
| 71 // doesn't change its ordering as greater than |start|. | |
| 72 void DropUnneededDigits(const std::string& start, | |
| 73 std::string* value) { | |
| 74 CHECK_GT(*value, start); | |
| 75 | |
| 76 RemoveTrailingZeros(value); | |
| 77 // See if the value can have its last digit removed without affecting | |
| 78 // the ordering. | |
| 79 if (value->length() > 1) { | |
| 80 std::string shorter_value = value->substr(0, value->length() - 1); | |
|
akalin
2011/10/20 19:06:51
hmm, i still don't like having to create this temp
| |
| 81 // We must drop the trailing zeros before comparing |shorter_value| to | |
| 82 // |start| because if we don't we may have |shorter_value|=|start|+|a|* | |
| 83 // where |shorter_value| > |start| but not when it drops the trailing |a|s | |
| 84 // to become a valid StringOrdinal value. | |
| 85 RemoveTrailingZeros(&shorter_value); | |
| 86 | |
| 87 if (shorter_value > start) | |
| 88 value->resize(shorter_value.length()); | |
| 89 } | |
| 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::Invalid() { | |
| 158 return StringOrdinal(""); | |
| 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 |