scstring.h 4.85 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
/******************************************************************************
 *
 * Copyright (C) 1997-2004 by Dimitri van Heesch.
 *
 * Permission to use, copy, modify, and distribute this software and its
 * documentation under the terms of the GNU General Public License is hereby 
 * granted. No representations are made about the suitability of this software 
 * for any purpose. It is provided "as is" without express or implied warranty.
 * See the GNU General Public License for more details.
 *
 * Documents produced by Doxygen are derivative works derived from the
 * input used in their production; they are not affected by this license.
 *
 */

#ifndef SCSTRING_H
#define SCSTRING_H

#include <stdlib.h>

class QRegExp;

/** This is an alternative implementation of QCString. It provides basically
 *  the same functions but uses less memory for administration. This class
 *  is just a wrapper around a plain C string requiring only 4 bytes "overhead".
 *  QCString features sharing of data and stores the string length, but 
 *  requires 4 + 12 bytes for this (even for the empty string). As doxygen 
 *  uses a LOT of string during a run it saves a lot of memory to use a 
 *  more memory efficient implementation at the cost of relatively low
 *  runtime overhead.
 */
class SCString 
{
public:
    SCString() : m_data(0) {}		// make null string
    SCString( const SCString &s );
    SCString( int size );
    SCString( const char *str );
    SCString( const char *str, uint maxlen );
    ~SCString();

    SCString    &operator=( const SCString &s );// deep copy
    SCString    &operator=( const char *str );	// deep copy

    bool        isNull()        const;
    bool	isEmpty()	const;
    uint	length()	const;
    uint        size()          const { return m_data ? length()+1 : 0; }
    char *      data()          const { return m_data; }
    bool	resize( uint newlen );
    bool	truncate( uint pos );
    bool	fill( char c, int len = -1 );

    SCString	copy()	const;

    SCString    &sprintf( const char *format, ... );

    int		find( char c, int index=0, bool cs=TRUE ) const;
    int		find( const char *str, int index=0, bool cs=TRUE ) const;
    int		find( const QRegExp &, int index=0 ) const;
Dimitri van Heesch's avatar
Dimitri van Heesch committed
61
    int         find( const QCString &str, int index, bool cs ) const;
62 63 64 65 66 67
    int		findRev( char c, int index=-1, bool cs=TRUE) const;
    int		findRev( const char *str, int index=-1, bool cs=TRUE) const;
    int		findRev( const QRegExp &, int index=-1 ) const;
    int		contains( char c, bool cs=TRUE ) const;
    int		contains( const char *str, bool cs=TRUE ) const;
    int		contains( const QRegExp & ) const;
68
    bool        stripPrefix(const char *prefix);
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148

    SCString	left( uint len )  const;
    SCString	right( uint len ) const;
    SCString	mid( uint index, uint len=0xffffffff) const;

    SCString	lower() const;
    SCString	upper() const;

    SCString	stripWhiteSpace()	const;
    SCString	simplifyWhiteSpace()	const;

    SCString    &assign( const char *str );
    SCString    &insert( uint index, const char * );
    SCString    &insert( uint index, char );
    SCString    &append( const char *s );
    SCString    &prepend( const char *s );
    SCString    &remove( uint index, uint len );
    SCString    &replace( uint index, uint len, const char * );
    SCString    &replace( const QRegExp &, const char * );

    short	toShort( bool *ok=0 )	const;
    ushort	toUShort( bool *ok=0 )	const;
    int		toInt( bool *ok=0 )	const;
    uint	toUInt( bool *ok=0 )	const;
    long	toLong( bool *ok=0 )	const;
    ulong	toULong( bool *ok=0 )	const;

    SCString    &setNum( short );
    SCString    &setNum( ushort );
    SCString    &setNum( int );
    SCString    &setNum( uint );
    SCString    &setNum( long );
    SCString    &setNum( ulong );
    QCString    &setNum( float, char f='g', int prec=6 );
    QCString    &setNum( double, char f='g', int prec=6 );

		operator const char *() const;
    SCString    &operator+=( const char *str );
    SCString    &operator+=( char c );
    char &at( uint index ) const;
    char &operator[]( int i ) const { return at(i); }
    
  private:
    static void msg_index( uint );
    void duplicate( const SCString &s );
    void duplicate( const char *str);
    SCString &duplicate( const char *str, int);

    char *      m_data;
};

inline char &SCString::at( uint index ) const
{
  return m_data[index];
}

inline void SCString::duplicate( const SCString &s )
{
  if (!s.isEmpty()) 
  {
    uint l = strlen(s.data());
    m_data = (char *)malloc(l+1);
    if (m_data) memcpy(m_data,s.data(),l+1);
  }
  else 
    m_data=0; 
}
inline void SCString::duplicate( const char *str)
{
  if (str && str[0]!='\0') 
  {
    uint l = strlen(str);
    m_data = (char *)malloc(l+1);
    if (m_data) memcpy(m_data,str,l+1);
  }
  else 
    m_data=0;
}
inline SCString &SCString::duplicate( const char *str, int)
{
149
  if (m_data) free(m_data);
150 151 152 153 154 155
  duplicate(str);
  return *this;
}

#endif