summaryrefslogtreecommitdiffstats
path: root/kopete/protocols/jabber/jingle/libjingle/talk/xmllite/qname.cpp
blob: e20b57e32d44ef84bfc80ca38ce0371d7c21d567 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
 * libjingle
 * Copyright 2004--2005, Google Inc.
 *
 * Redistribution and use in source and binary forms, with or without 
 * modification, are permitted provided that the following conditions are met:
 *
 *  1. Redistributions of source code must retain the above copyright notice, 
 *     this list of conditions and the following disclaimer.
 *  2. Redistributions in binary form must reproduce the above copyright notice,
 *     this list of conditions and the following disclaimer in the documentation
 *     and/or other materials provided with the distribution.
 *  3. The name of the author may not be used to endorse or promote products 
 *     derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <string>
#include "talk/base/common.h"
#include "talk/xmllite/xmlelement.h"
#include "talk/xmllite/qname.h"
#include "talk/xmllite/xmlconstants.h"

//#define new TRACK_NEW

namespace buzz {

static int TQName_Hash(const std::string & ns, const char * local) {
  int result = ns.size() * 101;
  while (*local) {
    result *= 19;
    result += *local;
    local += 1;
  }
  return result;
}

static const int bits = 9;
static TQName::Data * get_qname_table() {
  static TQName::Data qname_table[1 << bits];
  return qname_table;
}

static TQName::Data *
AllocateOrFind(const std::string & ns, const char * local) {
  int index = TQName_Hash(ns, local);
  int increment = index >> (bits - 1) | 1;
  TQName::Data * qname_table = get_qname_table();
  for (;;) {
    index &= ((1 << bits) - 1);
    if (!qname_table[index].Occupied()) {
      return new TQName::Data(ns, local);
    }
    if (qname_table[index].localPart_ == local &&
        qname_table[index].namespace_ == ns) {
      qname_table[index].AddRef();
      return qname_table + index;
    }
    index += increment;
  }
}

static TQName::Data *
Add(const std::string & ns, const char * local) {
  int index = TQName_Hash(ns, local);
  int increment = index >> (bits - 1) | 1;
  TQName::Data * qname_table = get_qname_table();
  for (;;) {
    index &= ((1 << bits) - 1);
    if (!qname_table[index].Occupied()) {
      qname_table[index].namespace_ = ns;
      qname_table[index].localPart_ = local;
      qname_table[index].AddRef(); // AddRef twice so it's never deleted
      qname_table[index].AddRef();
      return qname_table + index;
    }
    if (qname_table[index].localPart_ == local &&
        qname_table[index].namespace_ == ns) {
      qname_table[index].AddRef();
      return qname_table + index;
    }
    index += increment;
  }
}

TQName::~TQName() {
  data_->Release();
}

TQName::TQName() : data_(TQN_EMPTY.data_) {
  data_->AddRef();
}

TQName::TQName(bool add, const std::string & ns, const char * local) :
  data_(add ? Add(ns, local) : AllocateOrFind(ns, local)) {}
  
TQName::TQName(bool add, const std::string & ns, const std::string & local) :
  data_(add ? Add(ns, local.c_str()) : AllocateOrFind(ns, local.c_str())) {}
  
TQName::TQName(const std::string & ns, const char * local) :
  data_(AllocateOrFind(ns, local)) {}

static std::string
TQName_LocalPart(const std::string & name) {
  size_t i = name.rfind(':');
  if (i == std::string::npos)
    return name;
  return name.substr(i + 1);
}

static std::string
TQName_Namespace(const std::string & name) {
  size_t i = name.rfind(':');
  if (i == std::string::npos)
    return STR_EMPTY;
  return name.substr(0, i);
}

TQName::TQName(const std::string & mergedOrLocal) :
  data_(AllocateOrFind(TQName_Namespace(mergedOrLocal),
                 TQName_LocalPart(mergedOrLocal).c_str())) {}

std::string
TQName::Merged() const {
  if (data_->namespace_ == STR_EMPTY)
    return data_->localPart_;

  std::string result(data_->namespace_);
  result.reserve(result.length() + 1 + data_->localPart_.length());
  result += ':';
  result += data_->localPart_;
  return result;
}

bool
TQName::operator==(const TQName & other) const {
  return other.data_ == data_ ||
    data_->localPart_ == other.data_->localPart_ &&
    data_->namespace_ == other.data_->namespace_;
}

int
TQName::Compare(const TQName & other) const {
  if (data_ == other.data_)
    return 0;
  
  int result = data_->localPart_.compare(other.data_->localPart_);
  if (result)
    return result;

  return data_->namespace_.compare(other.data_->namespace_);
}

}