Doxygen
growbuf.h
浏览该文件的文档.
1 #ifndef GROWBUF_H
2 #define GROWBUF_H
3 
4 #include <utility>
5 #include <stdlib.h>
6 #include <string.h>
7 #include <string>
8 
9 #define GROW_AMOUNT 1024*4
10 
11 /** Class representing a string buffer optimised for growing. */
12 class GrowBuf
13 {
14  public:
15  GrowBuf() : m_str(0), m_pos(0), m_len(0) {}
16  GrowBuf(uint initialSize) : m_pos(0), m_len(initialSize) { m_str=(char*)malloc(m_len); }
17  ~GrowBuf() { free(m_str); }
18  GrowBuf(const GrowBuf &other)
19  {
20  m_len = other.m_len;
21  m_pos = other.m_pos;
22  m_str = (char*)malloc(m_len);
23  memcpy(m_str,other.m_str,m_len);
24  }
25  GrowBuf &operator=(const GrowBuf &other)
26  {
27  if (this!=&other)
28  {
29  free(m_str);
30  m_len = other.m_len;
31  m_pos = other.m_pos;
32  m_str = (char*)malloc(m_len);
33  memcpy(m_str,other.m_str,m_len);
34  }
35  return *this;
36  }
37  GrowBuf(GrowBuf &&other)
38  : m_str(std::exchange(other.m_str,(char*)0))
39  , m_pos(std::exchange(other.m_pos,0))
40  , m_len(std::exchange(other.m_len,0))
41  {
42  }
44  {
45  if (this==&other)
46  return *this;
47  m_len = std::exchange(other.m_len,0);
48  m_pos = std::exchange(other.m_pos,0);
49  m_str = std::exchange(other.m_str,(char*)0);
50  return *this;
51  }
52  void reserve(uint size) { if (m_len<size) { m_len = size; m_str = (char*)realloc(m_str,m_len); } }
53  void clear() { m_pos=0; }
54  void addChar(char c) { if (m_pos>=m_len) { m_len+=GROW_AMOUNT; m_str = (char*)realloc(m_str,m_len); }
55  m_str[m_pos++]=c;
56  }
57  void addStr(const QCString &s) {
58  if (!s.isEmpty())
59  {
60  uint l=s.length();
61  if (m_pos+l>=m_len) { m_len+=l+GROW_AMOUNT; m_str = (char*)realloc(m_str,m_len); }
62  strcpy(&m_str[m_pos],s.data());
63  m_pos+=l;
64  }
65  }
66  void addStr(const std::string &s) {
67  if (!s.empty())
68  {
69  uint l=(uint)s.length();
70  if (m_pos+l>=m_len) { m_len+=l+GROW_AMOUNT; m_str = (char*)realloc(m_str,m_len); }
71  strcpy(&m_str[m_pos],s.c_str());
72  m_pos+=l;
73  }
74  }
75  void addStr(const char *s) {
76  if (s)
77  {
78  uint l=(uint)strlen(s);
79  if (m_pos+l>=m_len) { m_len+=l+GROW_AMOUNT; m_str = (char*)realloc(m_str,m_len); }
80  strcpy(&m_str[m_pos],s);
81  m_pos+=l;
82  }
83  }
84  void addStr(const char *s,uint n) {
85  if (s)
86  {
87  uint l=(uint)strlen(s);
88  if (n<l) l=n;
89  if (m_pos+l>=m_len) { m_len+=l+GROW_AMOUNT; m_str = (char*)realloc(m_str,m_len); }
90  strncpy(&m_str[m_pos],s,n);
91  m_pos+=l;
92  }
93  }
94  char *get() { return m_str; }
95  const char *get() const { return m_str; }
96  uint getPos() const { return m_pos; }
97  void setPos(uint newPos) { m_pos = newPos; }
98  char at(uint i) const { return m_str[i]; }
99  bool empty() const { return m_pos==0; }
100  private:
101  char *m_str;
104 };
105 
106 #endif
GrowBuf::m_len
uint m_len
Definition: growbuf.h:103
GrowBuf::m_str
char * m_str
Definition: growbuf.h:101
GrowBuf::setPos
void setPos(uint newPos)
Definition: growbuf.h:97
QCString::length
uint length() const
Returns the length of the string, not counting the 0-terminator.
Definition: qcstring.h:147
GrowBuf::get
char * get()
Definition: growbuf.h:94
QCString::isEmpty
bool isEmpty() const
Returns TRUE iff the string is empty
Definition: qcstring.h:144
GROW_AMOUNT
#define GROW_AMOUNT
Definition: growbuf.h:9
GrowBuf::get
const char * get() const
Definition: growbuf.h:95
GrowBuf::addStr
void addStr(const QCString &s)
Definition: growbuf.h:57
GrowBuf::addStr
void addStr(const char *s, uint n)
Definition: growbuf.h:84
GrowBuf::at
char at(uint i) const
Definition: growbuf.h:98
GrowBuf::addStr
void addStr(const char *s)
Definition: growbuf.h:75
GrowBuf::GrowBuf
GrowBuf(const GrowBuf &other)
Definition: growbuf.h:18
GrowBuf::addChar
void addChar(char c)
Definition: growbuf.h:54
GrowBuf
Class representing a string buffer optimised for growing.
Definition: growbuf.h:12
uint
unsigned uint
Definition: qcstring.h:40
GrowBuf::getPos
uint getPos() const
Definition: growbuf.h:96
GrowBuf::GrowBuf
GrowBuf()
Definition: growbuf.h:15
GrowBuf::operator=
GrowBuf & operator=(const GrowBuf &other)
Definition: growbuf.h:25
GrowBuf::reserve
void reserve(uint size)
Definition: growbuf.h:52
GrowBuf::operator=
GrowBuf & operator=(GrowBuf &&other)
Definition: growbuf.h:43
GrowBuf::GrowBuf
GrowBuf(uint initialSize)
Definition: growbuf.h:16
GrowBuf::~GrowBuf
~GrowBuf()
Definition: growbuf.h:17
GrowBuf::m_pos
uint m_pos
Definition: growbuf.h:102
QCString::data
const char * data() const
Returns a pointer to the contents of the string in the form of a 0-terminated C string
Definition: qcstring.h:153
GrowBuf::addStr
void addStr(const std::string &s)
Definition: growbuf.h:66
GrowBuf::clear
void clear()
Definition: growbuf.h:53
GrowBuf::GrowBuf
GrowBuf(GrowBuf &&other)
Definition: growbuf.h:37
GrowBuf::empty
bool empty() const
Definition: growbuf.h:99
QCString
This is an alternative implementation of QCString.
Definition: qcstring.h:108