Geant4 11.4.0
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
MCGIDI::String Class Reference

#include <MCGIDI_string.hpp>

Public Types

typedef size_t size_type

Public Member Functions

LUPI_HOST_DEVICE String ()
LUPI_HOST_DEVICE ~String ()
LUPI_HOST_DEVICE String (const String &)
LUPI_HOST_DEVICE String (const char *)
LUPI_HOST_DEVICE Stringoperator= (const char *)
LUPI_HOST_DEVICE Stringoperator= (const String &)
LUPI_HOST_DEVICE Stringoperator+= (const String &)
LUPI_HOST_DEVICE Stringoperator+= (const char *)
LUPI_HOST_DEVICE Stringoperator+= (char)
LUPI_HOST_DEVICE void push_back (char)
LUPI_HOST_DEVICE bool operator== (const char *) const
LUPI_HOST_DEVICE bool operator== (const String &) const
LUPI_HOST_DEVICE void clear ()
LUPI_HOST_DEVICE void clearMemory ()
LUPI_HOST_DEVICE size_type size () const
 size without terminating NUL
LUPI_HOST_DEVICE size_type length () const
 as size()
LUPI_HOST_DEVICE size_type capacity () const
LUPI_HOST_DEVICE size_t internalSize () const
LUPI_HOST_DEVICE bool empty () const
LUPI_HOST_DEVICE const char * c_str () const
 raw data
LUPI_HOST_DEVICE void reserve (size_type n, char **address=nullptr)
LUPI_HOST_DEVICE void resize (size_type n, char **address=nullptr)
LUPI_HOST_DEVICE void resize (size_type n, char c, char **address=nullptr)
LUPI_HOST_DEVICE void swap (String &)
 swap contents
LUPI_HOST_DEVICE String substr (const size_type pos, size_type length) const
LUPI_HOST_DEVICE char & operator[] (const size_type i)
LUPI_HOST_DEVICE char operator[] (const size_type i) const
LUPI_HOST_DEVICE char & at (const size_type i)
LUPI_HOST_DEVICE char at (const size_type i) const
LUPI_HOST_DEVICE Stringerase (size_type pos, size_type len)
 erase len characters at position pos
LUPI_HOST_DEVICE Stringappend (const char *str, size_type n)
 Append n characters of a string.
LUPI_HOST_DEVICE int compare (size_type pos, size_type len, const String &str) const
LUPI_HOST_DEVICE int compare (size_type pos, size_type len, const char *str) const

Static Public Attributes

static const size_type npos = static_cast<size_t>(-1)

Friends

String LUPI_HOST_DEVICE operator+ (const String &lhs, const String &rhs)

Detailed Description

Definition at line 72 of file MCGIDI_string.hpp.

Member Typedef Documentation

◆ size_type

typedef size_t MCGIDI::String::size_type

Definition at line 80 of file MCGIDI_string.hpp.

Constructor & Destructor Documentation

◆ String() [1/3]

LUPI_HOST_DEVICE MCGIDI::String::String ( )

Definition at line 69 of file MCGIDI_string.cc.

69: p( nullptr ), allocated_(0), size_(0) { }

Referenced by append(), clearMemory(), compare(), erase(), operator+, operator+=(), operator+=(), operator+=(), operator=(), operator=(), operator==(), String(), substr(), and swap().

◆ ~String()

LUPI_HOST_DEVICE MCGIDI::String::~String ( )

Definition at line 71 of file MCGIDI_string.cc.

72 {
73 free(p);
74 }
void free(voidpf ptr)

◆ String() [2/3]

LUPI_HOST_DEVICE MCGIDI::String::String ( const String & s)

Definition at line 76 of file MCGIDI_string.cc.

77 : p(nullptr)
78 {
79 p = malloc_never_null( s.size_ + 1 ); // copy only used part
80 allocated_ = s.size_ + 1;
81 size_ = s.size_;
82 memcpy(p, s.p, size_ + 1);
83 }

◆ String() [3/3]

LUPI_HOST_DEVICE MCGIDI::String::String ( const char * s)

Definition at line 85 of file MCGIDI_string.cc.

86 : p(strdup_never_null(s))
87 {
88 }

Member Function Documentation

◆ append()

LUPI_HOST_DEVICE String & MCGIDI::String::append ( const char * str,
size_type n )

Append n characters of a string.

Definition at line 251 of file MCGIDI_string.cc.

251 {
252 if (str && n > 0) {
253 size_t lens = MCGIDI_strlen(str);
254 if (n > lens)
255 n = lens;
256 size_t newlen = size_ + n;
257 this->reserve( newlen );
258 MCGIDI_memmove(p+size_, str, n); // p and s.p MAY overlap
259 p[newlen] = 0; // add NUL termination
260 size_ = newlen;
261 }
262 return *this;
263 }
LUPI_HOST_DEVICE void reserve(size_type n, char **address=nullptr)
LUPI_HOST_DEVICE size_t MCGIDI_strlen(const char *str)
LUPI_HOST_DEVICE void MCGIDI_memmove(char *dest, const char *src, size_t n)

◆ at() [1/2]

LUPI_HOST_DEVICE char & MCGIDI::String::at ( const size_type i)

Definition at line 212 of file MCGIDI_string.cc.

213 {
214 if ( i > MCGIDI_strlen(p) )
215 LUPI_THROW( "MCGIDI::String::at(): index out_of_range");
216
217 return p[i];
218 }
#define LUPI_THROW(arg)

◆ at() [2/2]

LUPI_HOST_DEVICE char MCGIDI::String::at ( const size_type i) const

Definition at line 219 of file MCGIDI_string.cc.

220 {
221 if ( i > MCGIDI_strlen(p) )
222 LUPI_THROW("MCGIDI::String::at(): index out_of_range");
223
224 return p[i];
225 }

◆ c_str()

◆ capacity()

LUPI_HOST_DEVICE size_type MCGIDI::String::capacity ( ) const
inline

Definition at line 109 of file MCGIDI_string.hpp.

109{ return allocated_-1; }

◆ clear()

LUPI_HOST_DEVICE void MCGIDI::String::clear ( )

Definition at line 177 of file MCGIDI_string.cc.

178 {
179 size_ = 0;
180 p[0] = 0;
181 }

◆ clearMemory()

LUPI_HOST_DEVICE void MCGIDI::String::clearMemory ( )

Definition at line 171 of file MCGIDI_string.cc.

172 {
173 String s;
174 this->swap( s );
175 }
LUPI_HOST_DEVICE void swap(String &)
swap contents
LUPI_HOST_DEVICE String()

◆ compare() [1/2]

LUPI_HOST_DEVICE int MCGIDI::String::compare ( size_type pos,
size_type len,
const char * str ) const

Definition at line 280 of file MCGIDI_string.cc.

280 {
281 if (pos > size_)
282 LUPI_THROW("MCGIDI::String::compare: pos index out of range");
283
284 if ( len > size_ - pos)
285 len = size_ - pos; // limit len to available length
286
287 const size_type osize = MCGIDI_strlen(str);
288 const size_type len2 = MCGIDI_MIN(len, osize);
289 int r = MCGIDI_strncmp( p + pos, str, len2);
290 if (r==0) // equal so far, now compare sizes
291 r = len < osize ? -1 : ( len == osize ? 0 : +1 );
292 return r;
293 }
#define MCGIDI_MIN(x, y)
LUPI_HOST_DEVICE int MCGIDI_strncmp(const char *s1, const char *s2, size_t n)

◆ compare() [2/2]

LUPI_HOST_DEVICE int MCGIDI::String::compare ( size_type pos,
size_type len,
const String & str ) const

Definition at line 265 of file MCGIDI_string.cc.

265 {
266 if (pos > size_)
267 LUPI_THROW("MCGIDI::String::compare: pos index out of range");
268
269 if ( len > size_ - pos)
270 len = size_ - pos; // limit len to available length
271
272 const size_type osize = str.size();
273 const size_type len2 = MCGIDI_MIN(len, osize);
274 int r = MCGIDI_strncmp( p + pos, str.p, len2);
275 if (r==0) // equal so far, now compare sizes
276 r = len < osize ? -1 : ( len == osize ? 0 : +1 );
277 return r;
278 }

◆ empty()

LUPI_HOST_DEVICE bool MCGIDI::String::empty ( ) const
inline

Definition at line 119 of file MCGIDI_string.hpp.

119{ return size_ == 0; }

◆ erase()

LUPI_HOST_DEVICE String & MCGIDI::String::erase ( size_type pos,
size_type len )

erase len characters at position pos

Definition at line 227 of file MCGIDI_string.cc.

228 {
229 if (len > 0) {
230
231 if ( pos >= size_ ) // user must not remove trailing 0
232 LUPI_THROW("MCGIDI::String::erase: pos index out_of_range");
233
234 long s2 = (long) size_;
235 long remain = s2 - (long) ( pos - len );
236
237 if (remain > 0) {
238 // erase by overwriting
239 MCGIDI_memmove(p + pos, p + pos + len, (size_t) remain);
240 }
241
242 if ( remain < 0 ) remain = 0;
243
244 // remove unused space
245 this->resize( pos + (size_t) remain );
246
247 }
248 return *this;
249 }
LUPI_HOST_DEVICE void resize(size_type n, char **address=nullptr)

◆ internalSize()

LUPI_HOST_DEVICE size_t MCGIDI::String::internalSize ( ) const
inline

Definition at line 112 of file MCGIDI_string.hpp.

112 {
113 size_t delta = allocated_;
114 size_t sub = delta % 8;
115 if (sub != 0) delta += (8-sub);
116 return delta * sizeof(char);
117 }

◆ length()

LUPI_HOST_DEVICE size_type MCGIDI::String::length ( ) const
inline

as size()

Definition at line 106 of file MCGIDI_string.hpp.

◆ operator+=() [1/3]

LUPI_HOST_DEVICE String & MCGIDI::String::operator+= ( char c)

Definition at line 141 of file MCGIDI_string.cc.

142 {
143 push_back(c);
144 return *this;
145 }
LUPI_HOST_DEVICE void push_back(char)

◆ operator+=() [2/3]

LUPI_HOST_DEVICE String & MCGIDI::String::operator+= ( const char * s)

Definition at line 122 of file MCGIDI_string.cc.

123 {
124 const size_type lens = MCGIDI_strlen(s);
125 if (lens > 0){
126 if (size_ + lens + 1 <= allocated_) {
127 MCGIDI_memmove(p+size_, s, lens+1); // trailing 0
128 size_ += lens;
129 }
130 else {
131 String s2( *this ); // copy own data
132 s2.reserve(size_ + lens);
133 MCGIDI_memmove(s2.p+size_, s, lens+1); // trailing 0
134 s2.size_ = size_ + lens;
135 this->swap( s2 );
136 }
137 }
138 return *this;
139 }

◆ operator+=() [3/3]

LUPI_HOST_DEVICE String & MCGIDI::String::operator+= ( const String & s)

Definition at line 111 of file MCGIDI_string.cc.

112 {
113 if (s.size_ > 0){
114 this->reserve(size_ + s.size_);
115 MCGIDI_memmove(p+size_, s.p, s.size_+1); // trailing 0
116 size_ += s.size_;
117 }
118 return *this;
119 }

◆ operator=() [1/2]

LUPI_HOST_DEVICE String & MCGIDI::String::operator= ( const char * s)

Definition at line 90 of file MCGIDI_string.cc.

91 {
92 if ( p != s ) {
93 // s could point into our own string, so we have to allocate a new string
94 const size_t len = MCGIDI_strlen(s);
95 char* copy = (char*) malloc( len + 1);
96 MCGIDI_memmove(copy, s, len+1); // trailing 0
97 free( p );
98 p = copy;
99 size_ = len;
100 allocated_ = len+1;
101 }
102
103 return *this;
104 }
voidp malloc(uInt size)
void copy(G4double dst[], const G4double src[], std::size_t size=G4FieldTrack::ncompSVEC)

Referenced by operator=().

◆ operator=() [2/2]

LUPI_HOST_DEVICE String & MCGIDI::String::operator= ( const String & s)

Definition at line 106 of file MCGIDI_string.cc.

107 {
108 return operator=(s.p);
109 }
LUPI_HOST_DEVICE String & operator=(const char *)

◆ operator==() [1/2]

LUPI_HOST_DEVICE bool MCGIDI::String::operator== ( const char * s) const

Definition at line 161 of file MCGIDI_string.cc.

162 {
163 return !MCGIDI_strcmp(p, s);
164 }
LUPI_HOST_DEVICE int MCGIDI_strcmp(const char *p1, const char *p2)

◆ operator==() [2/2]

LUPI_HOST_DEVICE bool MCGIDI::String::operator== ( const String & s) const

Definition at line 166 of file MCGIDI_string.cc.

167 {
168 return !MCGIDI_strcmp(p, s.p);
169 }

◆ operator[]() [1/2]

LUPI_HOST_DEVICE char & MCGIDI::String::operator[] ( const size_type i)
inline

Definition at line 148 of file MCGIDI_string.hpp.

148{ return p[i]; }

◆ operator[]() [2/2]

LUPI_HOST_DEVICE char MCGIDI::String::operator[] ( const size_type i) const
inline

Definition at line 149 of file MCGIDI_string.hpp.

149{ return p[i]; }

◆ push_back()

LUPI_HOST_DEVICE void MCGIDI::String::push_back ( char c)

Definition at line 148 of file MCGIDI_string.cc.

148 {
149
150 if (size_ == allocated_ - 1) {
151 size_t more = (allocated_* 3) / 2; // factor 1.5
152 if ( more < 4 ) more = 4;
153 reserve( size_ + more );
154 }
155
156 p[size_] = c;
157 size_++;
158 p[size_] = 0;
159 }

Referenced by operator+=().

◆ reserve()

LUPI_HOST_DEVICE void MCGIDI::String::reserve ( size_type n,
char ** address = nullptr )

Reserve internal string memory so that n characters can be put into the string (plus 1 for the NUL char). If there is already enough memory, nothing happens, if not, the memory will be realloated to exactly this amount.

Definition at line 317 of file MCGIDI_string.cc.

317 {
318 if (n >= allocated_ ) {
319 this->my_realloc(n + 1, address);
320 allocated_ = n + 1;
321 }
322 }

Referenced by append(), operator+=(), operator+=(), push_back(), and resize().

◆ resize() [1/2]

LUPI_HOST_DEVICE void MCGIDI::String::resize ( size_type n,
char ** address = nullptr )

Resize string. If n is less than the current size, the string will be truncated. If n is larger, then the memory will be reallocated to exactly this amount, and the additional characters will be NUL characters.

Definition at line 324 of file MCGIDI_string.cc.

324 {
325 this->resize( n, 0, address );
326 }

Referenced by erase(), and resize().

◆ resize() [2/2]

LUPI_HOST_DEVICE void MCGIDI::String::resize ( size_type n,
char c,
char ** address = nullptr )

Resize string. If n is less than the current size, the string will be truncated. If n is larger, then the memory will be reallocated to exactly this amount, and the additional characters will be c characters.

Definition at line 328 of file MCGIDI_string.cc.

328 {
329 if (n < allocated_ ) {
330 p[n] = 0;
331 size_ = n;
332 }
333 else if (n >= allocated_ ) {
334 this->reserve( n, address );
335 for (size_type i=size_; i < n; ++i )
336 p[i] = c;
337 p[n] = 0;
338 size_ = n;
339 }
340 }

◆ size()

LUPI_HOST_DEVICE size_type MCGIDI::String::size ( ) const
inline

size without terminating NUL

Definition at line 105 of file MCGIDI_string.hpp.

Referenced by compare().

◆ substr()

LUPI_HOST_DEVICE String MCGIDI::String::substr ( const size_type pos,
size_type length ) const

Definition at line 188 of file MCGIDI_string.cc.

189 {
190 String s;
191 const size_type len = size_;
192
193 if ( pos > len )
194 LUPI_THROW("MCGIDI::String::substr: pos index out of range");
195
196 size_type remain = len - pos;
197
198 if ( in_length > remain )
199 in_length = remain;
200
201 s.reserve( in_length );
202
203 memcpy(s.p, p + pos, in_length);
204 s.p[in_length] = '\0';
205 s.size_ = in_length;
206
207 return s;
208 }

◆ swap()

LUPI_HOST_DEVICE void MCGIDI::String::swap ( String & s)

swap contents

Definition at line 342 of file MCGIDI_string.cc.

342 {
343 MCGIDI_SWAP( allocated_, s.allocated_, size_t );
344 MCGIDI_SWAP( size_, s.size_, size_t );
345 MCGIDI_SWAP( p, s.p, char * );
346 }
#define MCGIDI_SWAP(a, b, type)

Referenced by clearMemory(), and operator+=().

◆ operator+

String LUPI_HOST_DEVICE operator+ ( const String & lhs,
const String & rhs )
friend

Definition at line 183 of file MCGIDI_string.cc.

184 {
185 return String(lhs) += rhs;
186 }

Member Data Documentation

◆ npos

const String::size_type MCGIDI::String::npos = static_cast<size_t>(-1)
static

Definition at line 81 of file MCGIDI_string.hpp.


The documentation for this class was generated from the following files: