syncable_id.cc revision ddb351dbec246cf1fab5ec20d2d5520909041de1
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/browser/sync/syncable/syncable_id.h"
6
7#include <iosfwd>
8
9#include "base/string_util.h"
10#include "base/values.h"
11
12using std::ostream;
13using std::string;
14
15namespace syncable {
16const Id kNullId;  // Currently == root.
17
18ostream& operator<<(ostream& out, const Id& id) {
19  out << id.s_;
20  return out;
21}
22
23StringValue* Id::ToValue() const {
24  return Value::CreateStringValue(s_);
25}
26
27string Id::GetServerId() const {
28  // Currently root is the string "0". We need to decide on a true value.
29  // "" would be convenient here, as the IsRoot call would not be needed.
30  if (IsRoot())
31    return "0";
32  return s_.substr(1);
33}
34
35Id Id::CreateFromServerId(const string& server_id) {
36  Id id;
37  if (server_id == "0")
38    id.s_ = "r";
39  else
40    id.s_ = string("s") + server_id;
41  return id;
42}
43
44Id Id::CreateFromClientString(const string& local_id) {
45  Id id;
46  if (local_id == "0")
47    id.s_ = "r";
48  else
49    id.s_ = string("c") + local_id;
50  return id;
51}
52
53Id Id::GetLexicographicSuccessor() const {
54  // The successor of a string is given by appending the least
55  // character in the alphabet.
56  Id id = *this;
57  id.s_.push_back(0);
58  return id;
59}
60
61// static
62Id Id::GetLeastIdForLexicographicComparison() {
63  Id id;
64  id.s_.clear();
65  return id;
66}
67
68}  // namespace syncable
69