Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(34)

Side by Side Diff: sync/internal_api/public/base/node_ordinal.cc

Issue 10920017: [Sync] Generalize StringOrdinal to handle ordinal_in_parent field (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Relax tests Created 8 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2012 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 "sync/internal_api/public/base/node_ordinal.h"
6
7 #include <algorithm>
8
9 namespace syncer {
10
11 NodeOrdinal Int64ToNodeOrdinal(int64 x) {
12 uint64 y = static_cast<uint64>(x);
13 y ^= 0x8000000000000000ULL;
14 std::string bytes(NodeOrdinal::kMinLength, '\x00');
15 if (y == 0) {
16 // 0 is a special case since |bytes| must not be all zeros.
17 bytes.push_back('\x80');
18 } else {
19 for (int i = 7; i >= 0; --i) {
20 bytes[i] = static_cast<uint8>(y);
21 y >>= 8;
22 }
23 }
24 NodeOrdinal ordinal(bytes);
25 DCHECK(ordinal.IsValid());
26 return ordinal;
27 }
28
29 int64 NodeOrdinalToInt64(const NodeOrdinal& ordinal) {
30 uint64 y = 0;
31 const std::string& s = ordinal.ToInternalValue();
32 size_t l = NodeOrdinal::kMinLength;
33 if (s.length() < l) {
34 NOTREACHED();
35 l = s.length();
36 }
37 for (size_t i = 0; i < l; ++i) {
38 const uint8 byte = s[l - i - 1];
39 y |= static_cast<uint64>(byte) << (i * 8);
40 }
41 y ^= 0x8000000000000000ULL;
42 // This is technically implementation-defined if y > INT64_MAX, so
43 // we're assuming that we're on a twos-complement machine.
44 return static_cast<int64>(y);
45 }
46
47 } // namespace syncer
OLDNEW
« no previous file with comments | « sync/internal_api/public/base/node_ordinal.h ('k') | sync/internal_api/public/base/node_ordinal_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698