Index: components/sync/base/unique_position.cc |
diff --git a/sync/internal_api/public/base/unique_position.cc b/components/sync/base/unique_position.cc |
similarity index 88% |
rename from sync/internal_api/public/base/unique_position.cc |
rename to components/sync/base/unique_position.cc |
index 9924cb4396197527d8386899cc12d13e25db98ec..dc0b310f4d8457917f6c3d169c748bd98706d143 100644 |
--- a/sync/internal_api/public/base/unique_position.cc |
+++ b/components/sync/base/unique_position.cc |
@@ -2,7 +2,7 @@ |
// Use of this source code is governed by a BSD-style license that can be |
// found in the LICENSE file. |
-#include "sync/internal_api/public/base/unique_position.h" |
+#include "components/sync/base/unique_position.h" |
#include <stddef.h> |
#include <stdint.h> |
@@ -14,7 +14,7 @@ |
#include "base/rand_util.h" |
#include "base/stl_util.h" |
#include "base/strings/string_number_conversions.h" |
-#include "sync/protocol/unique_position.pb.h" |
+#include "components/sync/protocol/unique_position.pb.h" |
#include "third_party/zlib/zlib.h" |
namespace syncer { |
@@ -27,8 +27,7 @@ bool UniquePosition::IsValidSuffix(const std::string& suffix) { |
// The suffix must be exactly the specified length, otherwise unique suffixes |
// are not sufficient to guarantee unique positions (because prefix + suffix |
// == p + refixsuffix). |
- return suffix.length() == kSuffixLength |
- && suffix[kSuffixLength-1] != 0; |
+ return suffix.length() == kSuffixLength && suffix[kSuffixLength - 1] != 0; |
} |
// static. |
@@ -38,8 +37,7 @@ bool UniquePosition::IsValidBytes(const std::string& bytes) { |
// prefix + some short suffix == some long suffix. |
// The second condition ensures that FindSmallerWithSuffix can always return a |
// result. |
- return bytes.length() >= kSuffixLength |
- && bytes[bytes.length()-1] != 0; |
+ return bytes.length() >= kSuffixLength && bytes[bytes.length() - 1] != 0; |
} |
// static. |
@@ -77,9 +75,9 @@ UniquePosition UniquePosition::FromProto(const sync_pb::UniquePosition& proto) { |
return UniquePosition::CreateInvalid(); |
} |
if (uncompressed_len != proto.uncompressed_length()) { |
- DLOG(ERROR) |
- << "Uncompressed length " << uncompressed_len |
- << " did not match specified length " << proto.uncompressed_length(); |
+ DLOG(ERROR) << "Uncompressed length " << uncompressed_len |
+ << " did not match specified length " |
+ << proto.uncompressed_length(); |
return UniquePosition::CreateInvalid(); |
} |
return UniquePosition(Compress(un_gzipped)); |
@@ -101,47 +99,41 @@ UniquePosition UniquePosition::FromInt64(int64_t x, const std::string& suffix) { |
} |
// static. |
-UniquePosition UniquePosition::InitialPosition( |
- const std::string& suffix) { |
+UniquePosition UniquePosition::InitialPosition(const std::string& suffix) { |
DCHECK(IsValidSuffix(suffix)); |
return UniquePosition(suffix, suffix); |
} |
// static. |
-UniquePosition UniquePosition::Before( |
- const UniquePosition& x, |
- const std::string& suffix) { |
+UniquePosition UniquePosition::Before(const UniquePosition& x, |
+ const std::string& suffix) { |
DCHECK(IsValidSuffix(suffix)); |
DCHECK(x.IsValid()); |
- const std::string& before = FindSmallerWithSuffix( |
- Uncompress(x.compressed_), suffix); |
+ const std::string& before = |
+ FindSmallerWithSuffix(Uncompress(x.compressed_), suffix); |
return UniquePosition(before + suffix, suffix); |
} |
// static. |
-UniquePosition UniquePosition::After( |
- const UniquePosition& x, |
- const std::string& suffix) { |
+UniquePosition UniquePosition::After(const UniquePosition& x, |
+ const std::string& suffix) { |
DCHECK(IsValidSuffix(suffix)); |
DCHECK(x.IsValid()); |
- const std::string& after = FindGreaterWithSuffix( |
- Uncompress(x.compressed_), suffix); |
+ const std::string& after = |
+ FindGreaterWithSuffix(Uncompress(x.compressed_), suffix); |
return UniquePosition(after + suffix, suffix); |
} |
// static. |
-UniquePosition UniquePosition::Between( |
- const UniquePosition& before, |
- const UniquePosition& after, |
- const std::string& suffix) { |
+UniquePosition UniquePosition::Between(const UniquePosition& before, |
+ const UniquePosition& after, |
+ const std::string& suffix) { |
DCHECK(before.IsValid()); |
DCHECK(after.IsValid()); |
DCHECK(before.LessThan(after)); |
DCHECK(IsValidSuffix(suffix)); |
const std::string& mid = FindBetweenWithSuffix( |
- Uncompress(before.compressed_), |
- Uncompress(after.compressed_), |
- suffix); |
+ Uncompress(before.compressed_), Uncompress(after.compressed_), suffix); |
return UniquePosition(mid + suffix, suffix); |
} |
@@ -224,9 +216,8 @@ std::string UniquePosition::GetSuffixForTest() const { |
return bytes.substr(prefix_len, std::string::npos); |
} |
-std::string UniquePosition::FindSmallerWithSuffix( |
- const std::string& reference, |
- const std::string& suffix) { |
+std::string UniquePosition::FindSmallerWithSuffix(const std::string& reference, |
+ const std::string& suffix) { |
size_t ref_zeroes = reference.find_first_not_of('\0'); |
size_t suffix_zeroes = suffix.find_first_not_of('\0'); |
@@ -257,9 +248,8 @@ std::string UniquePosition::FindSmallerWithSuffix( |
} |
// static |
-std::string UniquePosition::FindGreaterWithSuffix( |
- const std::string& reference, |
- const std::string& suffix) { |
+std::string UniquePosition::FindGreaterWithSuffix(const std::string& reference, |
+ const std::string& suffix) { |
size_t ref_FFs = |
reference.find_first_not_of(std::numeric_limits<uint8_t>::max()); |
size_t suffix_FFs = |
@@ -299,10 +289,9 @@ std::string UniquePosition::FindGreaterWithSuffix( |
} |
// static |
-std::string UniquePosition::FindBetweenWithSuffix( |
- const std::string& before, |
- const std::string& after, |
- const std::string& suffix) { |
+std::string UniquePosition::FindBetweenWithSuffix(const std::string& before, |
+ const std::string& after, |
+ const std::string& suffix) { |
DCHECK(IsValidSuffix(suffix)); |
DCHECK_NE(before, after); |
DCHECK_LT(before, after); |
@@ -315,19 +304,19 @@ std::string UniquePosition::FindBetweenWithSuffix( |
} |
size_t i = 0; |
- for ( ; i < std::min(before.length(), after.length()); ++i) { |
+ for (; i < std::min(before.length(), after.length()); ++i) { |
uint8_t a_digit = before[i]; |
uint8_t b_digit = after[i]; |
if (b_digit - a_digit >= 2) { |
- mid.push_back(a_digit + (b_digit - a_digit)/2); |
+ mid.push_back(a_digit + (b_digit - a_digit) / 2); |
return mid; |
} else if (a_digit == b_digit) { |
mid.push_back(a_digit); |
// Both strings are equal so far. Will appending the suffix at this point |
// give us the comparison we're looking for? |
- if (before.substr(i+1) < suffix && suffix < after.substr(i+1)) { |
+ if (before.substr(i + 1) < suffix && suffix < after.substr(i + 1)) { |
return mid; |
} |
} else { |
@@ -343,17 +332,17 @@ std::string UniquePosition::FindBetweenWithSuffix( |
// appending digits until |mid| > |before|. |
std::string mid_a = mid; |
mid_a.push_back(a_digit); |
- mid_a.append(FindGreaterWithSuffix(before.substr(i+1), suffix)); |
+ mid_a.append(FindGreaterWithSuffix(before.substr(i + 1), suffix)); |
// Option B: Round up the current digit. This makes our |mid| > |before|, |
// no matter what we append afterwards. We then focus on appending digits |
// until |mid| < |after|. Note that this option may not be viable if the |
// current digit is the last one in |after|, so we skip the option in that |
// case. |
- if (after.length() > i+1) { |
+ if (after.length() > i + 1) { |
std::string mid_b = mid; |
mid_b.push_back(b_digit); |
- mid_b.append(FindSmallerWithSuffix(after.substr(i+1), suffix)); |
+ mid_b.append(FindSmallerWithSuffix(after.substr(i + 1), suffix)); |
// Does this give us a shorter position value? If so, use it. |
if (mid_b.length() < mid_a.length()) { |
@@ -380,14 +369,12 @@ std::string UniquePosition::FindBetweenWithSuffix( |
UniquePosition::UniquePosition(const std::string& internal_rep) |
: compressed_(internal_rep), |
- is_valid_(IsValidBytes(Uncompress(internal_rep))) { |
-} |
+ is_valid_(IsValidBytes(Uncompress(internal_rep))) {} |
-UniquePosition::UniquePosition( |
- const std::string& uncompressed, |
- const std::string& suffix) |
- : compressed_(Compress(uncompressed)), |
- is_valid_(IsValidBytes(uncompressed)) { |
+UniquePosition::UniquePosition(const std::string& uncompressed, |
+ const std::string& suffix) |
+ : compressed_(Compress(uncompressed)), |
+ is_valid_(IsValidBytes(uncompressed)) { |
DCHECK(uncompressed.rfind(suffix) + kSuffixLength == uncompressed.length()); |
DCHECK(IsValidSuffix(suffix)); |
DCHECK(IsValid()); |
@@ -518,9 +505,9 @@ static uint32_t ReadEncodedRunLength(const std::string& str, size_t i) { |
// A series of four identical chars at the beginning of a block indicates |
// the beginning of a repeated character block. |
static bool IsRepeatedCharPrefix(const std::string& chars, size_t start_index) { |
- return chars[start_index] == chars[start_index+1] |
- && chars[start_index] == chars[start_index+2] |
- && chars[start_index] == chars[start_index+3]; |
+ return chars[start_index] == chars[start_index + 1] && |
+ chars[start_index] == chars[start_index + 2] && |
+ chars[start_index] == chars[start_index + 3]; |
} |
} // namespace |
@@ -549,7 +536,7 @@ std::string UniquePosition::CompressImpl(const std::string& str) { |
// Each loop iteration will consume 8, or N bytes, where N >= 4 and is the |
// length of a string of identical digits starting at i. |
- for (size_t i = 0; i < str.length(); ) { |
+ for (size_t i = 0; i < str.length();) { |
if (i + 4 <= str.length() && IsRepeatedCharPrefix(str, i)) { |
// Four identical bytes in a row at this position means that we must start |
// a repeated character block. Begin by outputting those four bytes. |
@@ -557,7 +544,7 @@ std::string UniquePosition::CompressImpl(const std::string& str) { |
// Determine the size of the run. |
const char rep_digit = str[i]; |
- const size_t runs_until = str.find_first_not_of(rep_digit, i+4); |
+ const size_t runs_until = str.find_first_not_of(rep_digit, i + 4); |
// Handle the 'runs until end' special case specially. |
size_t run_length; |
@@ -618,7 +605,7 @@ bool UniquePosition::IsValidCompressed(const std::string& str) { |
// characters that started it. |
return false; |
} |
- if (str[i] == str[i+4]) { |
+ if (str[i] == str[i + 4]) { |
// Does the next digit after a count match the repeated character? Then |
// this is not the highest possible count. |
return false; |