1/*
2 * libjingle
3 * Copyright 2004--2005, Google Inc.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 *  1. Redistributions of source code must retain the above copyright notice,
9 *     this list of conditions and the following disclaimer.
10 *  2. Redistributions in binary form must reproduce the above copyright notice,
11 *     this list of conditions and the following disclaimer in the documentation
12 *     and/or other materials provided with the distribution.
13 *  3. The name of the author may not be used to endorse or promote products
14 *     derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#include <string>
29#include "talk/base/common.h"
30#include "talk/xmllite/xmlelement.h"
31#include "talk/xmllite/qname.h"
32#include "talk/xmllite/xmlconstants.h"
33
34namespace buzz {
35
36static int QName_Hash(const std::string & ns, const char * local) {
37  int result = static_cast<int>(ns.size()) * 101;
38  while (*local) {
39    result *= 19;
40    result += *local;
41    local += 1;
42  }
43  return result;
44}
45
46static const int bits = 9;
47static QName::Data * get_qname_table() {
48  static QName::Data qname_table[1 << bits];
49  return qname_table;
50}
51
52static QName::Data *
53AllocateOrFind(const std::string & ns, const char * local) {
54  int index = QName_Hash(ns, local);
55  int increment = index >> (bits - 1) | 1;
56  QName::Data * qname_table = get_qname_table();
57  for (;;) {
58    index &= ((1 << bits) - 1);
59    if (!qname_table[index].Occupied()) {
60      return new QName::Data(ns, local);
61    }
62    if (qname_table[index].localPart_ == local &&
63        qname_table[index].namespace_ == ns) {
64      qname_table[index].AddRef();
65      return qname_table + index;
66    }
67    index += increment;
68  }
69}
70
71static QName::Data *
72Add(const std::string & ns, const char * local) {
73  int index = QName_Hash(ns, local);
74  int increment = index >> (bits - 1) | 1;
75  QName::Data * qname_table = get_qname_table();
76  for (;;) {
77    index &= ((1 << bits) - 1);
78    if (!qname_table[index].Occupied()) {
79      qname_table[index].namespace_ = ns;
80      qname_table[index].localPart_ = local;
81      qname_table[index].AddRef(); // AddRef twice so it's never deleted
82      qname_table[index].AddRef();
83      return qname_table + index;
84    }
85    if (qname_table[index].localPart_ == local &&
86        qname_table[index].namespace_ == ns) {
87      qname_table[index].AddRef();
88      return qname_table + index;
89    }
90    index += increment;
91  }
92}
93
94QName::~QName() {
95  data_->Release();
96}
97
98QName::QName() : data_(QN_EMPTY.data_) {
99  data_->AddRef();
100}
101
102QName::QName(bool add, const std::string & ns, const char * local) :
103  data_(add ? Add(ns, local) : AllocateOrFind(ns, local)) {}
104
105QName::QName(bool add, const std::string & ns, const std::string & local) :
106  data_(add ? Add(ns, local.c_str()) : AllocateOrFind(ns, local.c_str())) {}
107
108QName::QName(const std::string & ns, const char * local) :
109  data_(AllocateOrFind(ns, local)) {}
110
111static std::string
112QName_LocalPart(const std::string & name) {
113  size_t i = name.rfind(':');
114  if (i == std::string::npos)
115    return name;
116  return name.substr(i + 1);
117}
118
119static std::string
120QName_Namespace(const std::string & name) {
121  size_t i = name.rfind(':');
122  if (i == std::string::npos)
123    return STR_EMPTY;
124  return name.substr(0, i);
125}
126
127QName::QName(const std::string & mergedOrLocal) :
128  data_(AllocateOrFind(QName_Namespace(mergedOrLocal),
129                 QName_LocalPart(mergedOrLocal).c_str())) {}
130
131std::string
132QName::Merged() const {
133  if (data_->namespace_ == STR_EMPTY)
134    return data_->localPart_;
135
136  std::string result(data_->namespace_);
137  result.reserve(result.length() + 1 + data_->localPart_.length());
138  result += ':';
139  result += data_->localPart_;
140  return result;
141}
142
143bool
144QName::operator==(const QName & other) const {
145  return other.data_ == data_ ||
146      (data_->localPart_ == other.data_->localPart_ &&
147       data_->namespace_ == other.data_->namespace_);
148}
149
150int
151QName::Compare(const QName & other) const {
152  if (data_ == other.data_)
153    return 0;
154
155  int result = data_->localPart_.compare(other.data_->localPart_);
156  if (result)
157    return result;
158
159  return data_->namespace_.compare(other.data_->namespace_);
160}
161
162}
163