aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
tinystr.cpp
Go to the documentation of this file.
1 /*
2 www.sourceforge.net/projects/tinyxml
3 Original file by Yves Berquin.
4 
5 This software is provided 'as-is', without any express or implied
6 warranty. In no event will the authors be held liable for any
7 damages arising from the use of this software.
8 
9 Permission is granted to anyone to use this software for any
10 purpose, including commercial applications, and to alter it and
11 redistribute it freely, subject to the following restrictions:
12 
13 1. The origin of this software must not be misrepresented; you must
14 not claim that you wrote the original software. If you use this
15 software in a product, an acknowledgment in the product documentation
16 would be appreciated but is not required.
17 
18 2. Altered source versions must be plainly marked as such, and
19 must not be misrepresented as being the original software.
20 
21 3. This notice may not be removed or altered from any source
22 distribution.
23 */
24 
25 /*
26  * THIS FILE WAS ALTERED BY Tyge L�vset, 7. April 2005.
27  */
28 
29 #ifndef TIXML_USE_STL
30 
31 #include "tinystr.h"
32 
33 // Error value for find primitive
35  static_cast< TiXmlString::size_type >(-1);
36 
37 // Null rep.
38 TiXmlString::Rep TiXmlString::nullrep_ = {0, 0, {'\0'}};
39 
41  if (cap > capacity()) {
42  TiXmlString tmp;
43  tmp.init(length(), cap);
44  memcpy(tmp.start(), data(), length());
45  swap(tmp);
46  }
47 }
48 
49 TiXmlString& TiXmlString::assign(const char* str, size_type len) {
50  size_type cap = capacity();
51 
52  if (len > cap || cap > 3 * (len + 8)) {
53  TiXmlString tmp;
54  tmp.init(len);
55  memcpy(tmp.start(), str, len);
56  swap(tmp);
57  } else {
58  memmove(start(), str, len);
59  set_size(len);
60  }
61 
62  return *this;
63 }
64 
65 TiXmlString& TiXmlString::append(const char* str, size_type len) {
66  size_type newsize = length() + len;
67 
68  if (newsize > capacity()) {
69  reserve(newsize + capacity());
70  }
71 
72  memmove(finish(), str, len);
73  set_size(newsize);
74  return *this;
75 }
76 
78  TiXmlString tmp;
80  tmp += a;
81  tmp += b;
82  return tmp;
83 }
84 
85 TiXmlString operator+(const TiXmlString& a, const char* b) {
86  TiXmlString tmp;
87  TiXmlString::size_type b_len = static_cast< TiXmlString::size_type >(strlen(b));
88  tmp.reserve(a.length() + b_len);
89  tmp += a;
90  tmp.append(b, b_len);
91  return tmp;
92 }
93 
94 TiXmlString operator+(const char* a, const TiXmlString& b) {
95  TiXmlString tmp;
96  TiXmlString::size_type a_len = static_cast< TiXmlString::size_type >(strlen(a));
97  tmp.reserve(a_len + b.length());
98  tmp.append(a, a_len);
99  tmp += b;
100  return tmp;
101 }
102 
103 #endif // TIXML_USE_STL
static const size_type npos
Definition: tinystr.h:77
void swap(TiXmlString &other)
Definition: tinystr.h:195
TiXmlString operator+(const TiXmlString &a, const TiXmlString &b)
Definition: tinystr.cpp:77
size_type capacity() const
Definition: tinystr.h:146
TiXmlString & assign(const char *str, size_type len)
Definition: tinystr.cpp:49
void reserve(size_type cap)
Definition: tinystr.cpp:40
void init(size_type sz)
Definition: tinystr.h:202
TiXmlString operator+(const char *a, const TiXmlString &b)
Definition: tinystr.cpp:94
void set_size(size_type sz)
Definition: tinystr.h:203
size_t size_type
Definition: tinystr.h:74
size_type length() const
Definition: tinystr.h:137
TiXmlString & append(const char *str, size_type len)
Definition: tinystr.cpp:65
TiXmlString operator+(const TiXmlString &a, const char *b)
Definition: tinystr.cpp:85
static Rep nullrep_
Definition: tinystr.h:240
void init(size_type sz, size_type cap)
Definition: tinystr.h:212
TiXmlString & operator+=(const TiXmlString &suffix)
Definition: tinystr.h:126