1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
|
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/sync/model/string_ordinal.h"
#include <algorithm>
#include <utility>
#include "base/check.h"
#include "base/check_op.h"
#include "base/json/string_escape.h"
namespace syncer {
const uint8_t StringOrdinal::kZeroDigit;
const uint8_t StringOrdinal::kMaxDigit;
const size_t StringOrdinal::kMinLength;
const uint8_t StringOrdinal::kOneDigit;
const uint8_t StringOrdinal::kMidDigit;
const unsigned int StringOrdinal::kMidDigitValue;
const unsigned int StringOrdinal::kMaxDigitValue;
const unsigned int StringOrdinal::kRadix;
StringOrdinal::LessThanFn::LessThanFn() = default;
bool StringOrdinal::LessThanFn::operator()(const StringOrdinal& lhs,
const StringOrdinal& rhs) const {
return lhs.LessThan(rhs);
}
StringOrdinal::EqualsFn::EqualsFn() = default;
bool StringOrdinal::EqualsFn::operator()(const StringOrdinal& lhs,
const StringOrdinal& rhs) const {
return lhs.Equals(rhs);
}
bool operator==(const StringOrdinal& lhs, const StringOrdinal& rhs) {
return lhs.EqualsOrBothInvalid(rhs);
}
StringOrdinal::StringOrdinal(std::string bytes)
: bytes_(std::move(bytes)), is_valid_(IsValidOrdinalBytes(bytes_)) {}
StringOrdinal::StringOrdinal() : is_valid_(false) {}
StringOrdinal StringOrdinal::CreateInitialOrdinal() {
std::string bytes(kMinLength, kZeroDigit);
bytes[0] = kMidDigit;
return StringOrdinal(bytes);
}
bool StringOrdinal::IsValid() const {
DCHECK_EQ(IsValidOrdinalBytes(bytes_), is_valid_);
return is_valid_;
}
bool StringOrdinal::EqualsOrBothInvalid(const StringOrdinal& other) const {
if (!IsValid() && !other.IsValid()) {
return true;
}
if (!IsValid() || !other.IsValid()) {
return false;
}
return Equals(other);
}
std::string StringOrdinal::ToDebugString() const {
std::string debug_string =
base::EscapeBytesAsInvalidJSONString(bytes_, false /* put_in_quotes */);
if (!is_valid_) {
debug_string = "INVALID[" + debug_string + "]";
}
return debug_string;
}
bool StringOrdinal::LessThan(const StringOrdinal& other) const {
CHECK(IsValid());
CHECK(other.IsValid());
return bytes_ < other.bytes_;
}
bool StringOrdinal::GreaterThan(const StringOrdinal& other) const {
CHECK(IsValid());
CHECK(other.IsValid());
return bytes_ > other.bytes_;
}
bool StringOrdinal::Equals(const StringOrdinal& other) const {
CHECK(IsValid());
CHECK(other.IsValid());
return bytes_ == other.bytes_;
}
StringOrdinal StringOrdinal::CreateBetween(const StringOrdinal& other) const {
CHECK(IsValid());
CHECK(other.IsValid());
CHECK(!Equals(other));
if (LessThan(other)) {
return CreateOrdinalBetween(*this, other);
} else {
return CreateOrdinalBetween(other, *this);
}
}
StringOrdinal StringOrdinal::CreateBefore() const {
CHECK(IsValid());
// Create the smallest valid StringOrdinal of the appropriate length
// to be the minimum boundary.
const size_t length = bytes_.length();
std::string start(length, kZeroDigit);
start[length - 1] = kOneDigit;
if (start == bytes_) {
start[length - 1] = kZeroDigit;
start += kOneDigit;
}
// Even though `start` is already a valid StringOrdinal that is less
// than `*this`, we don't return it because we wouldn't have much space in
// front of it to insert potential future values.
return CreateBetween(StringOrdinal(start));
}
StringOrdinal StringOrdinal::CreateAfter() const {
CHECK(IsValid());
// Create the largest valid StringOrdinal of the appropriate length to be
// the maximum boundary.
std::string end(bytes_.length(), kMaxDigit);
if (end == bytes_) {
end += kMaxDigit;
}
// Even though `end` is already a valid StringOrdinal that is greater than
// `*this`, we don't return it because we wouldn't have much space after
// it to insert potential future values.
return CreateBetween(StringOrdinal(end));
}
std::string StringOrdinal::ToInternalValue() const {
CHECK(IsValid());
return bytes_;
}
bool StringOrdinal::IsValidOrdinalBytes(const std::string& bytes) {
const size_t length = bytes.length();
if (length < kMinLength) {
return false;
}
bool found_non_zero = false;
for (size_t i = 0; i < length; ++i) {
const uint8_t byte = bytes[i];
if (byte < kZeroDigit || byte > kMaxDigit) {
return false;
}
if (byte > kZeroDigit) {
found_non_zero = true;
}
}
if (!found_non_zero) {
return false;
}
if (length > kMinLength) {
const uint8_t last_byte = bytes[length - 1];
if (last_byte == kZeroDigit) {
return false;
}
}
return true;
}
size_t StringOrdinal::GetLengthWithoutTrailingZeroDigits(
const std::string& bytes,
size_t length) {
DCHECK(!bytes.empty());
DCHECK_GT(length, 0U);
size_t end_position =
bytes.find_last_not_of(static_cast<char>(kZeroDigit), length - 1);
// If no non kZeroDigit is found then the string is a string of all zeros
// digits so we return 0 as the correct length.
if (end_position == std::string::npos) {
return 0;
}
return end_position + 1;
}
uint8_t StringOrdinal::GetDigit(const std::string& bytes, size_t i) {
return (i < bytes.length()) ? bytes[i] : kZeroDigit;
}
int StringOrdinal::GetDigitValue(const std::string& bytes, size_t i) {
return GetDigit(bytes, i) - kZeroDigit;
}
int StringOrdinal::AddDigitValue(std::string* bytes,
size_t i,
int digit_value) {
DCHECK_LT(i, bytes->length());
for (int j = static_cast<int>(i); j >= 0 && digit_value > 0; --j) {
int byte_j_value = GetDigitValue(*bytes, j) + digit_value;
digit_value = byte_j_value / kRadix;
DCHECK_LE(digit_value, 1);
byte_j_value %= kRadix;
(*bytes)[j] = static_cast<char>(kZeroDigit + byte_j_value);
}
return digit_value;
}
size_t StringOrdinal::GetProperLength(const std::string& lower_bound,
const std::string& bytes) {
CHECK_GT(bytes, lower_bound);
size_t drop_length =
GetLengthWithoutTrailingZeroDigits(bytes, bytes.length());
// See if the `ordinal` can be truncated after its last non-zero
// digit without affecting the ordering.
if (drop_length > kMinLength) {
size_t truncated_length =
GetLengthWithoutTrailingZeroDigits(bytes, drop_length - 1);
if (truncated_length > 0 &&
bytes.compare(0, truncated_length, lower_bound) > 0) {
drop_length = truncated_length;
}
}
return std::max(drop_length, kMinLength);
}
std::string StringOrdinal::ComputeMidpoint(const std::string& start,
const std::string& end) {
size_t max_size = std::max(start.length(), end.length()) + 1;
std::string midpoint(max_size, kZeroDigit);
// Perform the operation (start + end) / 2 left-to-right by
// maintaining a "forward carry" which is either 0 or
// kMidDigitValue. AddDigitValue() is in general O(n), but this
// operation is still O(n) despite that; calls to AddDigitValue()
// will overflow at most to the last position where AddDigitValue()
// last overflowed.
int forward_carry = 0;
for (size_t i = 0; i < max_size; ++i) {
const int sum_value = GetDigitValue(start, i) + GetDigitValue(end, i);
const int digit_value = sum_value / 2 + forward_carry;
// AddDigitValue returning a non-zero carry would imply that
// midpoint[0] >= kMaxDigit, which one can show is impossible.
CHECK_EQ(AddDigitValue(&midpoint, i, digit_value), 0);
forward_carry = (sum_value % 2 == 1) ? kMidDigitValue : 0;
}
DCHECK_EQ(forward_carry, 0);
return midpoint;
}
StringOrdinal StringOrdinal::CreateOrdinalBetween(const StringOrdinal& start,
const StringOrdinal& end) {
CHECK(start.IsValid());
CHECK(end.IsValid());
CHECK(start.LessThan(end));
const std::string& start_bytes = start.ToInternalValue();
const std::string& end_bytes = end.ToInternalValue();
DCHECK_LT(start_bytes, end_bytes);
std::string midpoint = ComputeMidpoint(start_bytes, end_bytes);
const size_t proper_length = GetProperLength(start_bytes, midpoint);
midpoint.resize(proper_length, kZeroDigit);
DCHECK_GT(midpoint, start_bytes);
DCHECK_LT(midpoint, end_bytes);
StringOrdinal midpoint_ordinal(midpoint);
DCHECK(midpoint_ordinal.IsValid());
return midpoint_ordinal;
}
} // namespace syncer
|