rapidyaml  0.11.0
parse and emit YAML, and do it fast
c4::yml::extra::string Struct Reference

an owning string class used by the yaml std event handler (and the YamlScript handler). More...

#include <string.hpp>

Public Types

enum  : id_type { sso_size = RYML_STRING_SSO_SIZE }
 

Public Member Functions

 string ()
 
 ~string () noexcept
 
 string (string const &that) RYML_NOEXCEPT
 
 string (string &&that) noexcept
 
stringoperator= (string const &that) RYML_NOEXCEPT
 
stringoperator= (string &&that) noexcept
 
 operator csubstr () const noexcept
 
 operator substr () noexcept
 
const char * data () const noexcept
 
id_type size () const noexcept
 
id_type capacity () const noexcept
 
void clear ()
 
void resize (id_type sz)
 
void reserve (id_type sz)
 
void append (char c)
 
void append (csubstr cs)
 
void insert (char c, id_type pos)
 
void insert (csubstr cs, id_type pos)
 
size_t find_last (csubstr pattern) RYML_NOEXCEPT
 
void _free ()
 
void _cp (string const *that)
 
void _mv (string *that)
 

Public Attributes

char m_buf [sso_size]
 
char * m_str
 
id_type m_size
 
id_type m_capacity
 

Detailed Description

an owning string class used by the yaml std event handler (and the YamlScript handler).

we use this instead of std::string because: 1) this spares the dependency on the standard library 2) enables possibility of adding borrowing semantics (in the future)

Definition at line 32 of file string.hpp.

Member Enumeration Documentation

◆ anonymous enum

anonymous enum : id_type
Enumerator
sso_size 

Definition at line 34 of file string.hpp.

RYML_ID_TYPE id_type
The type of a node id in the YAML tree; to override the default type, define the macro RYML_ID_TYPE t...
Definition: common.hpp:244
#define RYML_STRING_SSO_SIZE
Definition: string.hpp:17

Constructor & Destructor Documentation

◆ string() [1/3]

c4::yml::extra::string::string ( )
inline

Definition at line 42 of file string.hpp.

43  : m_buf()
44  , m_str(m_buf)
45  , m_size(0)
47  {}
char m_buf[sso_size]
Definition: string.hpp:35

◆ ~string()

c4::yml::extra::string::~string ( )
inlinenoexcept

Definition at line 48 of file string.hpp.

49  {
50  _free();
51  }

References _free().

◆ string() [2/3]

c4::yml::extra::string::string ( string const &  that)
inline

Definition at line 53 of file string.hpp.

53  : string()
54  {
55  resize(that.m_size);
56  _cp(&that);
57  }
void resize(id_type sz)
Definition: string.hpp:93
void _cp(string const *that)
Definition: string.hpp:217

References _cp(), and resize().

◆ string() [3/3]

c4::yml::extra::string::string ( string &&  that)
inlinenoexcept

Definition at line 59 of file string.hpp.

59  : string()
60  {
61  _mv(&that);
62  }
void _mv(string *that)
Definition: string.hpp:236

References _mv().

Member Function Documentation

◆ operator=() [1/2]

string& c4::yml::extra::string::operator= ( string const &  that)
inline

Definition at line 64 of file string.hpp.

65  {
66  resize(that.m_size);
67  _cp(&that);
68  return *this;
69  }

References _cp(), and resize().

◆ operator=() [2/2]

string& c4::yml::extra::string::operator= ( string &&  that)
inlinenoexcept

Definition at line 71 of file string.hpp.

72  {
73  _mv(&that);
74  return *this;
75  }

References _mv().

◆ operator csubstr()

c4::yml::extra::string::operator csubstr ( ) const
inlinenoexcept

Definition at line 79 of file string.hpp.

79 { return {m_str, m_size}; }

References m_size, and m_str.

◆ operator substr()

c4::yml::extra::string::operator substr ( )
inlinenoexcept

Definition at line 80 of file string.hpp.

80 { return {m_str, m_size}; }

References m_size, and m_str.

◆ data()

const char* c4::yml::extra::string::data ( ) const
inlinenoexcept

Definition at line 84 of file string.hpp.

84 { return m_str; }

References m_str.

◆ size()

id_type c4::yml::extra::string::size ( ) const
inlinenoexcept

Definition at line 85 of file string.hpp.

85 { return m_size; }

References m_size.

◆ capacity()

id_type c4::yml::extra::string::capacity ( ) const
inlinenoexcept

Definition at line 86 of file string.hpp.

86 { return m_capacity; }

References m_capacity.

◆ clear()

void c4::yml::extra::string::clear ( )
inline

Definition at line 88 of file string.hpp.

89  {
90  m_size = 0;
91  }

References m_size.

◆ resize()

void c4::yml::extra::string::resize ( id_type  sz)
inline

Definition at line 93 of file string.hpp.

94  {
95  reserve(sz);
96  m_size = sz;
97  }
void reserve(id_type sz)
Definition: string.hpp:99

References m_size, and reserve().

◆ reserve()

void c4::yml::extra::string::reserve ( id_type  sz)
inline

Definition at line 99 of file string.hpp.

100  {
101  if(sz <= m_capacity)
102  return;
104  cap = cap > sz ? cap : sz;
105  if(cap <= sso_size)
106  return;
107  Callbacks cb = get_callbacks();
108  char *buf = (char*) _RYML_CB_ALLOC(cb, char, cap);
109  if(m_size)
110  memcpy(buf, m_str, (size_t)m_size);
111  if(m_str != m_buf)
112  {
113  _RYML_CB_FREE(cb, m_str, char, m_size);
114  }
115  m_str = buf;
116  m_capacity = cap;
117  }
Callbacks const & get_callbacks()
get the global callbacks
Definition: common.cpp:94

References c4::yml::get_callbacks(), m_buf, m_capacity, m_size, m_str, and sso_size.

◆ append() [1/2]

void c4::yml::extra::string::append ( char  c)
inline

Definition at line 121 of file string.hpp.

122  {
123  if(C4_UNLIKELY(m_size == m_capacity))
124  reserve(m_size + 1);
125  m_str[m_size++] = c;
126  }

References m_capacity, m_size, m_str, and reserve().

◆ append() [2/2]

void c4::yml::extra::string::append ( csubstr  cs)
inline

Definition at line 127 of file string.hpp.

128  {
129  if(cs.len)
130  {
131  const id_type ilen = (id_type)cs.len;
132  if(C4_UNLIKELY(m_size + ilen > m_capacity))
133  reserve(m_size + ilen);
134  memcpy(m_str + m_size, cs.str, cs.len);
135  m_size += ilen;
136  }
137  }

References m_capacity, m_size, m_str, and reserve().

◆ insert() [1/2]

void c4::yml::extra::string::insert ( char  c,
id_type  pos 
)
inline

Definition at line 138 of file string.hpp.

139  {
140  _RYML_ASSERT_BASIC(pos <= m_size);
141  if(pos < m_size)
142  {
143  if(C4_UNLIKELY(m_size == m_capacity))
144  reserve(m_size + 1);
145  char *C4_RESTRICT src = m_str + pos;
146  memmove(src + 1, src, m_size - pos);
147  *src = c;
148  ++m_size;
149  }
150  else
151  {
152  append(c);
153  }
154  }
void append(char c)
Definition: string.hpp:121

References append(), m_capacity, m_size, m_str, and reserve().

◆ insert() [2/2]

void c4::yml::extra::string::insert ( csubstr  cs,
id_type  pos 
)
inline

Definition at line 155 of file string.hpp.

156  {
157  _RYML_ASSERT_BASIC(pos <= m_size);
158  if(cs.len)
159  {
160  if(pos < m_size)
161  {
162  const id_type ilen = (id_type)cs.len;
163  if(C4_UNLIKELY(m_size + ilen > m_capacity))
164  reserve(m_size + ilen);
165  char *C4_RESTRICT src = m_str + pos;
166  memmove(src + cs.len, src, m_size - pos);
167  memcpy(src, cs.str, cs.len);
168  m_size += ilen;
169  }
170  else
171  {
172  append(cs);
173  }
174  }
175  }

References append(), m_capacity, m_size, m_str, and reserve().

◆ find_last()

size_t c4::yml::extra::string::find_last ( csubstr  pattern)
inline

Definition at line 176 of file string.hpp.

177  {
178  _RYML_ASSERT_BASIC(pattern.len);
179  if(m_size >= pattern.len)
180  {
181  for(size_t i = m_size - pattern.len; i != (size_t)-1; --i)
182  {
183  if(m_str[i] == pattern[0])
184  {
185  bool gotit = true;
186  for(size_t j = 1; j < pattern.len; ++j)
187  {
188  if(m_str[i + j] != pattern[j])
189  {
190  gotit = false;
191  break;
192  }
193  }
194  if(gotit)
195  return i;
196  }
197  }
198  }
199  return npos;
200  }
@ npos
a null string position
Definition: common.hpp:258

References m_size, m_str, and c4::yml::npos.

◆ _free()

void c4::yml::extra::string::_free ( )
inline

Definition at line 204 of file string.hpp.

205  {
206  _RYML_ASSERT_BASIC(m_str != nullptr); // this structure cannot be memset() to zero
207  if(m_str != m_buf)
208  {
209  _RYML_CB_FREE(get_callbacks(), m_str, char, (size_t)m_capacity);
210  m_str = m_buf;
212  }
213  _RYML_ASSERT_BASIC(m_capacity == sso_size);
214  m_size = 0;
215  }

References c4::yml::get_callbacks(), m_buf, m_capacity, m_size, m_str, and sso_size.

◆ _cp()

void c4::yml::extra::string::_cp ( string const *  that)
inline

Definition at line 217 of file string.hpp.

218  {
219  #if RYML_USE_ASSERT
220  if(that->m_str != that->m_buf)
221  {
222  _RYML_ASSERT_BASIC(that->m_capacity > sso_size);
223  _RYML_ASSERT_BASIC(that->m_size <= that->m_capacity);
224  }
225  else
226  {
227  _RYML_ASSERT_BASIC(that->m_capacity <= sso_size);
228  _RYML_ASSERT_BASIC(that->m_size <= that->m_capacity);
229  }
230  #endif
231  memcpy(m_str, that->m_str, that->m_size);
232  m_size = that->m_size;
233  m_capacity = that->m_size < sso_size ? sso_size : that->m_size;
234  }

References m_capacity, m_size, m_str, and sso_size.

◆ _mv()

void c4::yml::extra::string::_mv ( string that)
inline

Definition at line 236 of file string.hpp.

237  {
238  if(that->m_str != that->m_buf)
239  {
240  _RYML_ASSERT_BASIC(that->m_capacity > sso_size);
241  _RYML_ASSERT_BASIC(that->m_size <= that->m_capacity);
242  m_str = that->m_str;
243  }
244  else
245  {
246  _RYML_ASSERT_BASIC(that->m_capacity <= sso_size);
247  _RYML_ASSERT_BASIC(that->m_size <= that->m_capacity);
248  memcpy(m_buf, that->m_buf, that->m_size);
249  m_str = m_buf;
250  }
251  m_size = that->m_size;
252  m_capacity = that->m_capacity;
253  // make sure no deallocation happens on destruction
254  _RYML_ASSERT_BASIC(that->m_str != this->m_buf);
255  that->m_str = that->m_buf;
256  that->m_capacity = sso_size;
257  that->m_size = 0;
258  }

References m_buf, m_capacity, m_size, m_str, and sso_size.

Member Data Documentation

◆ m_buf

char c4::yml::extra::string::m_buf[sso_size]

Definition at line 35 of file string.hpp.

◆ m_str

char* c4::yml::extra::string::m_str

Definition at line 36 of file string.hpp.

◆ m_size

id_type c4::yml::extra::string::m_size

Definition at line 37 of file string.hpp.

◆ m_capacity

id_type c4::yml::extra::string::m_capacity

Definition at line 38 of file string.hpp.


The documentation for this struct was generated from the following file: