Classes | Public Member Functions | Private Attributes | List of all members
QCString::StringRep Class Reference

Classes

union  ShortOrLongStringSelector
 

Public Member Functions

 StringRep ()
 
void initEmpty ()
 
 ~StringRep ()
 
 StringRep (const StringRep &s)
 
 StringRep (int size)
 
 StringRep (const char *str)
 
 StringRep (const char *str, uint maxlen)
 
StringRepoperator= (const StringRep &s)
 
StringRepoperator= (const char *str)
 
bool isEmpty () const
 
uint length () const
 
const char * data () const
 
char * rawData () const
 
char & at (int i) const
 
bool resize (uint newlen)
 
bool fill (char c, int len)
 

Private Attributes

union QCString::StringRep::ShortOrLongStringSelector u
 

Detailed Description

Definition at line 415 of file qcstring.h.

Constructor & Destructor Documentation

QCString::StringRep::StringRep ( )
inline

Definition at line 418 of file qcstring.h.

419  {
420  initEmpty();
421  }
QCString::StringRep::~StringRep ( )
inline

Definition at line 428 of file qcstring.h.

429  {
430  if (!u.s.isShort)
431  {
432  u.l.d->dispose();
433  }
434  }
void dispose()
Definition: qcstring.h:386
union QCString::StringRep::ShortOrLongStringSelector u
QCString::StringRep::StringRep ( const StringRep s)
inline

Definition at line 435 of file qcstring.h.

436  {
437  if (&s!=this)
438  {
439  u.s.isShort = s.u.s.isShort;
440  if (s.u.s.isShort)
441  {
442  u.s.len = s.u.s.len;
443  memcpy(u.s.str,s.u.s.str,s.u.s.len+1);
444  }
445  else
446  {
447  u.l.d = s.u.l.d;
448  u.l.d->refCount++;
449  }
450  }
451  else // self-assignment
452  {
453  u = s.u; // avoid uninitialized warning from gcc
454  }
455  }
char str[SHORT_STR_CAPACITY]
Definition: qcstring.h:357
union QCString::StringRep::ShortOrLongStringSelector u
static QCString * s
Definition: config.cpp:1042
QCString::StringRep::StringRep ( int  size)
inline

Definition at line 456 of file qcstring.h.

457  {
459  if (size<=SHORT_STR_CAPACITY) // init short string
460  {
461  if (size>0)
462  {
463  u.s.len = size-1;
464  u.s.str[size-1]='\0';
465  }
466  else
467  {
468  u.s.len = 0;
469  }
470  }
471  else // int long string
472  {
473  u.l.d = LSData::create(size);
474  }
475  }
char str[SHORT_STR_CAPACITY]
Definition: qcstring.h:357
uint size() const
Definition: qcstring.h:201
static LSData * create(int size)
Definition: qcstring.h:376
#define SHORT_STR_CAPACITY
Definition: qcstring.h:349
union QCString::StringRep::ShortOrLongStringSelector u
QCString::StringRep::StringRep ( const char *  str)
inline

Definition at line 476 of file qcstring.h.

477  {
478  if (str)
479  {
480  int len = (int)strlen(str);
482  if (len<SHORT_STR_CAPACITY)
483  {
484  u.s.len = len;
485  qstrncpy(u.s.str,str,SHORT_STR_CAPACITY);
486  }
487  else
488  {
489  u.l.d = LSData::create(len+1);
490  memcpy(u.l.d->toStr(),str,u.l.d->len);
491  }
492  }
493  else // create empty string
494  {
495  initEmpty();
496  }
497  }
Q_EXPORT char * qstrncpy(char *src, const char *dst, uint len)
Definition: qcstring.cpp:557
char str[SHORT_STR_CAPACITY]
Definition: qcstring.h:357
static LSData * create(int size)
Definition: qcstring.h:376
char * toStr()
Definition: qcstring.h:369
#define SHORT_STR_CAPACITY
Definition: qcstring.h:349
union QCString::StringRep::ShortOrLongStringSelector u
static QCString str
QCString::StringRep::StringRep ( const char *  str,
uint  maxlen 
)
inline

Definition at line 498 of file qcstring.h.

499  {
500  if (str && maxlen>0)
501  {
502  uint len=(uint)strlen(str);
503  if (len>maxlen) len=maxlen;
504  u.s.isShort = len<=SHORT_STR_MAX_LEN;
505  if (u.s.isShort)
506  {
507  u.s.len = len;
508  memcpy(u.s.str,str,len);
509  u.s.str[len]='\0';
510  }
511  else
512  {
513  u.l.d = LSData::create(len+1);
514  memcpy(u.l.d->toStr(),str,len);
515  }
516  }
517  else // create empty string
518  {
519  initEmpty();
520  }
521  }
char str[SHORT_STR_CAPACITY]
Definition: qcstring.h:357
static const int maxlen
Definition: qregexp.cpp:904
static LSData * create(int size)
Definition: qcstring.h:376
#define SHORT_STR_MAX_LEN
Definition: qcstring.h:350
char * toStr()
Definition: qcstring.h:369
union QCString::StringRep::ShortOrLongStringSelector u
unsigned uint
Definition: qglobal.h:351
static QCString str

Member Function Documentation

char& QCString::StringRep::at ( int  i) const
inline

Definition at line 607 of file qcstring.h.

608  {
609  if (u.s.isShort)
610  {
611  return (char&)u.s.str[i];
612  }
613  else
614  {
615  return u.l.d->toStr()[i];
616  }
617  }
char str[SHORT_STR_CAPACITY]
Definition: qcstring.h:357
char * toStr()
Definition: qcstring.h:369
union QCString::StringRep::ShortOrLongStringSelector u
const char* QCString::StringRep::data ( ) const
inline

Definition at line 584 of file qcstring.h.

585  {
586  if (u.s.isShort)
587  {
588  return u.s.len==0 ? 0 : u.s.str;
589  }
590  else
591  {
592  return u.l.d->len==0 ? 0 : u.l.d->toStr();
593  }
594  }
char str[SHORT_STR_CAPACITY]
Definition: qcstring.h:357
char * toStr()
Definition: qcstring.h:369
union QCString::StringRep::ShortOrLongStringSelector u
bool QCString::StringRep::fill ( char  c,
int  len 
)
inline

Definition at line 668 of file qcstring.h.

669  {
670  if (len<0) len=length();
671  if (!u.s.isShort) // detach from shared string
672  {
673  resize(len+1);
674  }
675  else if (len!=(int)length())
676  {
677  if (len>0)
678  {
679  resize(len+1);
680  }
681  }
682  if (len>0)
683  {
684  memset(rawData(),c,len);
685  }
686  return TRUE;
687  }
bool resize(uint newlen)
Definition: qcstring.h:618
char * rawData() const
Definition: qcstring.h:595
union QCString::StringRep::ShortOrLongStringSelector u
const bool TRUE
Definition: qglobal.h:371
uint length() const
Definition: qcstring.h:579
void QCString::StringRep::initEmpty ( )
inline

Definition at line 422 of file qcstring.h.

423  {
424  u.s.isShort=TRUE;
425  u.s.len=0;
426  //memset(u.s.str,0,SHORT_STR_CAPACITY);
427  }
union QCString::StringRep::ShortOrLongStringSelector u
const bool TRUE
Definition: qglobal.h:371
bool QCString::StringRep::isEmpty ( ) const
inline

Definition at line 575 of file qcstring.h.

576  {
577  return u.s.isShort && u.s.len==0;
578  }
union QCString::StringRep::ShortOrLongStringSelector u
uint QCString::StringRep::length ( ) const
inline

Definition at line 579 of file qcstring.h.

580  {
581  uint l = u.s.isShort ? u.s.len : u.l.d->len;
582  return l;
583  }
static QStrList * l
Definition: config.cpp:1044
union QCString::StringRep::ShortOrLongStringSelector u
unsigned uint
Definition: qglobal.h:351
StringRep& QCString::StringRep::operator= ( const StringRep s)
inline

Definition at line 522 of file qcstring.h.

523  {
524  if (&s!=this)
525  {
526  if (!u.s.isShort)
527  {
528  u.l.d->dispose();
529  }
530  u.s.isShort = s.u.s.isShort;
531  if (u.s.isShort) // copy by value
532  {
533  u.s.len = s.u.s.len;
534  memcpy(u.s.str,s.u.s.str,s.u.s.len+1);
535  }
536  else // copy by reference
537  {
538  u.l.d = s.u.l.d;
539  u.l.d->refCount++;
540  }
541  }
542  else // self-assignment
543  {
544  u = s.u; // avoid uninitialized warning from gcc
545  }
546  return *this;
547  }
void dispose()
Definition: qcstring.h:386
char str[SHORT_STR_CAPACITY]
Definition: qcstring.h:357
union QCString::StringRep::ShortOrLongStringSelector u
static QCString * s
Definition: config.cpp:1042
StringRep& QCString::StringRep::operator= ( const char *  str)
inline

Definition at line 548 of file qcstring.h.

549  {
550  if (!u.s.isShort)
551  {
552  u.l.d->dispose();
553  }
554  if (str)
555  {
556  int len = (int)strlen(str);
558  if (len<SHORT_STR_CAPACITY)
559  {
560  u.s.len = len;
561  qstrncpy(u.s.str,str,SHORT_STR_CAPACITY);
562  }
563  else
564  {
565  u.l.d = LSData::create(len+1);
566  memcpy(u.l.d->toStr(),str,len);
567  }
568  }
569  else
570  {
571  initEmpty();
572  }
573  return *this;
574  }
Q_EXPORT char * qstrncpy(char *src, const char *dst, uint len)
Definition: qcstring.cpp:557
void dispose()
Definition: qcstring.h:386
char str[SHORT_STR_CAPACITY]
Definition: qcstring.h:357
static LSData * create(int size)
Definition: qcstring.h:376
char * toStr()
Definition: qcstring.h:369
#define SHORT_STR_CAPACITY
Definition: qcstring.h:349
union QCString::StringRep::ShortOrLongStringSelector u
static QCString str
char* QCString::StringRep::rawData ( ) const
inline

Definition at line 595 of file qcstring.h.

596  {
597  if (u.s.isShort)
598  {
599  return u.s.len==0 ? 0 : (char*)u.s.str;
600  }
601  else
602  {
603  //assert(u.l.d->refCount==0); // string may not be shared when accessed raw
604  return u.l.d->len==0 ? 0 : u.l.d->toStr();
605  }
606  }
char str[SHORT_STR_CAPACITY]
Definition: qcstring.h:357
char * toStr()
Definition: qcstring.h:369
union QCString::StringRep::ShortOrLongStringSelector u
bool QCString::StringRep::resize ( uint  newlen)
inline

Definition at line 618 of file qcstring.h.

619  {
620  if (u.s.isShort && newlen<=SHORT_STR_CAPACITY) // resize short string
621  {
622  if (newlen>0)
623  {
624  u.s.len = newlen-1;
625  u.s.str[newlen-1]='\0';
626  }
627  else // string becomes empty
628  {
629  initEmpty();
630  }
631  }
632  else if (u.s.isShort) // turn short string into long string
633  {
634  StringRep tmp = *this;
635  u.s.isShort=FALSE;
636  u.l.d = LSData::create(newlen);
637  if (tmp.u.s.len>0)
638  {
639  memcpy(u.l.d->toStr(),tmp.u.s.str,tmp.u.s.len+1);
640  }
641  else
642  {
643  u.l.d->toStr()[0]='\0';
644  }
645  }
646  else if (!u.s.isShort && newlen<=SHORT_STR_CAPACITY) // turn long string into short string
647  {
648  if (newlen>0)
649  {
650  StringRep tmp(newlen); // copy short part into tmp buffer
651  memcpy(tmp.u.s.str,u.l.d->toStr(),newlen-1);
652  tmp.u.s.str[newlen-1]='\0';
653  u.l.d->dispose();
654  u.s = tmp.u.s;
655  }
656  else
657  {
658  u.l.d->dispose();
659  initEmpty();
660  }
661  }
662  else // resize long string
663  {
664  u.l.d = u.l.d->resize(u.l.d,newlen);
665  }
666  return TRUE;
667  }
void dispose()
Definition: qcstring.h:386
char str[SHORT_STR_CAPACITY]
Definition: qcstring.h:357
const bool FALSE
Definition: qglobal.h:370
static LSData * resize(LSData *d, int size)
Definition: qcstring.h:393
static LSData * create(int size)
Definition: qcstring.h:376
string tmp
Definition: languages.py:63
char * toStr()
Definition: qcstring.h:369
#define SHORT_STR_CAPACITY
Definition: qcstring.h:349
union QCString::StringRep::ShortOrLongStringSelector u
const bool TRUE
Definition: qglobal.h:371

Member Data Documentation

union QCString::StringRep::ShortOrLongStringSelector QCString::StringRep::u
private

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