OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "sync/internal_api/public/base/node_ordinal.h" | 5 #include "sync/internal_api/public/base/node_ordinal.h" |
6 | 6 |
| 7 #include <stddef.h> |
| 8 #include <stdint.h> |
| 9 |
7 #include <algorithm> | 10 #include <algorithm> |
8 #include <string> | 11 #include <string> |
9 | 12 |
10 namespace syncer { | 13 namespace syncer { |
11 | 14 |
12 NodeOrdinal Int64ToNodeOrdinal(int64 x) { | 15 NodeOrdinal Int64ToNodeOrdinal(int64_t x) { |
13 uint64 y = static_cast<uint64>(x); | 16 uint64_t y = static_cast<uint64_t>(x); |
14 y ^= 0x8000000000000000ULL; | 17 y ^= 0x8000000000000000ULL; |
15 std::string bytes(NodeOrdinal::kMinLength, '\x00'); | 18 std::string bytes(NodeOrdinal::kMinLength, '\x00'); |
16 if (y == 0) { | 19 if (y == 0) { |
17 // 0 is a special case since |bytes| must not be all zeros. | 20 // 0 is a special case since |bytes| must not be all zeros. |
18 bytes.push_back('\x80'); | 21 bytes.push_back('\x80'); |
19 } else { | 22 } else { |
20 for (int i = 7; i >= 0; --i) { | 23 for (int i = 7; i >= 0; --i) { |
21 bytes[i] = static_cast<uint8>(y); | 24 bytes[i] = static_cast<uint8_t>(y); |
22 y >>= 8; | 25 y >>= 8; |
23 } | 26 } |
24 } | 27 } |
25 NodeOrdinal ordinal(bytes); | 28 NodeOrdinal ordinal(bytes); |
26 DCHECK(ordinal.IsValid()); | 29 DCHECK(ordinal.IsValid()); |
27 return ordinal; | 30 return ordinal; |
28 } | 31 } |
29 | 32 |
30 int64 NodeOrdinalToInt64(const NodeOrdinal& ordinal) { | 33 int64_t NodeOrdinalToInt64(const NodeOrdinal& ordinal) { |
31 uint64 y = 0; | 34 uint64_t y = 0; |
32 const std::string& s = ordinal.ToInternalValue(); | 35 const std::string& s = ordinal.ToInternalValue(); |
33 size_t l = NodeOrdinal::kMinLength; | 36 size_t l = NodeOrdinal::kMinLength; |
34 if (s.length() < l) { | 37 if (s.length() < l) { |
35 NOTREACHED(); | 38 NOTREACHED(); |
36 l = s.length(); | 39 l = s.length(); |
37 } | 40 } |
38 for (size_t i = 0; i < l; ++i) { | 41 for (size_t i = 0; i < l; ++i) { |
39 const uint8 byte = s[l - i - 1]; | 42 const uint8_t byte = s[l - i - 1]; |
40 y |= static_cast<uint64>(byte) << (i * 8); | 43 y |= static_cast<uint64_t>(byte) << (i * 8); |
41 } | 44 } |
42 y ^= 0x8000000000000000ULL; | 45 y ^= 0x8000000000000000ULL; |
43 // This is technically implementation-defined if y > INT64_MAX, so | 46 // This is technically implementation-defined if y > INT64_MAX, so |
44 // we're assuming that we're on a twos-complement machine. | 47 // we're assuming that we're on a twos-complement machine. |
45 return static_cast<int64>(y); | 48 return static_cast<int64_t>(y); |
46 } | 49 } |
47 | 50 |
48 } // namespace syncer | 51 } // namespace syncer |
OLD | NEW |