Home | History | Annotate | Download | only in syncable
      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 
     12 using std::ostream;
     13 using std::string;
     14 
     15 namespace syncable {
     16 const Id kNullId;  // Currently == root.
     17 
     18 ostream& operator<<(ostream& out, const Id& id) {
     19   out << id.s_;
     20   return out;
     21 }
     22 
     23 StringValue* Id::ToValue() const {
     24   return Value::CreateStringValue(s_);
     25 }
     26 
     27 string 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 
     35 Id 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 
     44 Id 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 
     53 Id 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
     62 Id Id::GetLeastIdForLexicographicComparison() {
     63   Id id;
     64   id.s_.clear();
     65   return id;
     66 }
     67 
     68 }  // namespace syncable
     69