home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / tlx501.zip / STRINGS.H < prev    next >
C/C++ Source or Header  |  1996-01-05  |  6KB  |  176 lines

  1. /****************************************************************************
  2.     $Id: strings.h 501.0 1995/03/07 12:26:46 RON Exp $
  3.  
  4.     Copyright (c) 1991-95 Tarma Software Research. All rights reserved.
  5.  
  6.     Project:    Tarma Library for C++ V5.0
  7.     Author:    Ron van der Wal
  8.  
  9.     Declaration of class TLString. This class represents a character string,
  10.     i.e. a sequence of characters, terminated by '\0'. The string can be
  11.     instructed to retain its capacity, or to resize itself each time an
  12.     expansion is necessary.
  13.  
  14.     Character indexing starts at 0, just as with C-style strings.
  15.  
  16.     $Log: strings.h $
  17.     Revision 501.0  1995/03/07 12:26:46  RON
  18.     Updated for TLX 5.01
  19.     Revision 1.7  1995/01/31 16:29:26  RON
  20.     Update for release 012
  21.     Added partial support for SunPro C++ compiler
  22.     Revision 1.6  1994/10/05  18:24:56  ron
  23.     Renamed TLx...() functions to tl...()
  24.  
  25.     Revision 1.5  1994/09/28  14:29:14  ron
  26.     Removed Macintosh-style #include references
  27.  
  28.     Revision 1.4  1994/09/27  20:25:43  ron
  29.     Changed path separator from / to \
  30.  
  31.     Revision 1.3  1994/09/26  15:22:34  ron
  32.     Renamed SetSize() to Resize()
  33.  
  34.     Revision 1.2  1994/09/06  13:59:10  ron
  35.     Adapted to changes in tlx.h
  36.  
  37.     Revision 1.1  1994/08/16  18:06:53  ron
  38.     Initial revision
  39.  
  40. ****************************************************************************/
  41.  
  42. #ifndef _TLX_STRINGS_H
  43. #define _TLX_STRINGS_H
  44.  
  45. //-----    System headers
  46.  
  47. //-----    Project headers
  48.  
  49. #ifndef _TLX_TLX_H
  50. #include <tlx\501\tlx.h>
  51. #endif
  52.  
  53. class _RTLCLASS ostream;
  54.  
  55. /*---------------------------------------------------------------------------
  56.     TLString -
  57.  
  58.     This class represents a character string, i.e. a sequence of characters
  59.     of known length. It stores the characters in an internal buffer,
  60.     and expands the buffer if necessary. The buffer is only contracted on
  61.     request.
  62. ---------------------------------------------------------------------------*/
  63.  
  64. class _TLXCLASS TLString
  65. {
  66.     char *        mString;    // Actual contents
  67.     size_t        mSize;        // Buffer size
  68.     size_t         mLen;        // Logical string length
  69.  
  70. public:
  71.     // Constructors and destructor
  72.  
  73.     TLString(size_t = 0);
  74.     TLString(char, size_t = 0);
  75.     TLString(const char *, size_t = 0);
  76.     TLString(const TLString &);
  77.     ~TLString();
  78.  
  79.     // Assignment operators
  80.  
  81.     TLString &        operator =(char);
  82.     TLString &        operator =(const char *);
  83.     TLString &        operator =(const TLString &);
  84.     TLString &        operator +=(char);
  85.     TLString &        operator +=(const char *);
  86.     TLString &        operator +=(const TLString &);
  87.  
  88.     // Conversion operators
  89.                 operator char *() { return mString; }
  90.                 operator const char *() const { return mString; }
  91.                 operator char() const { return *mString; }
  92.     int         operator !() const { return mLen == 0; }
  93.  
  94.     // Access to individual characters
  95.  
  96.     char &        operator [](size_t);
  97.     char         operator [](size_t) const;
  98.     char &        PeekAt(size_t i) { return mString[i]; }
  99.     char         PeekAt(size_t i) const { return mString[i]; }
  100.  
  101.     // Modifications to the string's contents
  102.  
  103.     void         Clear();
  104.     void         ToLower();
  105.     void         ToUpper();
  106.     void         Reverse();
  107.  
  108.     // Substring insertion & extraction
  109.  
  110.     void         Fill(char, size_t = 0, size_t = kMaxAlloc);
  111.     void         Append(char c) { Insert(c, mLen); }
  112.     void         Append(const char *s) { Insert(s, mLen); }
  113.     void         Append(const TLString &s) { Insert(s, mLen); }
  114.     void         Insert(char, size_t);
  115.     void         Insert(const char *, size_t);
  116.     void         Insert(const TLString &, size_t);
  117.     void         Prepend(char c) { Insert(c, 0); }
  118.     void         Prepend(const char *s) { Insert(s, 0); }
  119.     void         Prepend(const TLString &s) { Insert(s, 0); }
  120.     void         Remove(size_t, size_t);
  121.     TLString         Copy(size_t, size_t);
  122.     TLString         Extract(size_t, size_t);
  123.  
  124.     // String searching
  125.  
  126.     size_t         IndexOf(char, size_t = 0);
  127.     size_t         IndexOf(const char *, size_t = 0);
  128.     size_t         IndexOf(const TLString &, size_t = 0);
  129.  
  130.     // Length & integrity functions
  131.  
  132.     void         CompactStorage();
  133.     size_t         Length() const { return mLen; }
  134.     size_t         Size() const { return mSize; }
  135.     void        Reserve(size_t);
  136.     static size_t     MaxSize() { return kMaxAlloc - 1; }
  137.     void         Sync();
  138.     void        Terminate();
  139.     bool         IsValidIndex(size_t aIndex) const
  140.                     { return aIndex < mLen; }
  141.  
  142.     // Comparison operators
  143.  
  144.     friend int _TLXFUNC    operator ==(const TLString &, const TLString &);
  145.     friend int _TLXFUNC operator ==(const TLString &, const char *);
  146.     friend int _TLXFUNC operator ==(const char *, const TLString &);
  147.     friend int _TLXFUNC    operator !=(const TLString &, const TLString &);
  148.     friend int _TLXFUNC operator !=(const TLString &, const char *);
  149.     friend int _TLXFUNC operator !=(const char *, const TLString &);
  150.     friend int _TLXFUNC    operator <=(const TLString &, const TLString &);
  151.     friend int _TLXFUNC operator <=(const TLString &, const char *);
  152.     friend int _TLXFUNC operator <=(const char *, const TLString &);
  153.     friend int _TLXFUNC    operator < (const TLString &, const TLString &);
  154.     friend int _TLXFUNC operator < (const TLString &, const char *);
  155.     friend int _TLXFUNC operator < (const char *, const TLString &);
  156.     friend int _TLXFUNC    operator >=(const TLString &, const TLString &);
  157.     friend int _TLXFUNC operator >=(const TLString &, const char *);
  158.     friend int _TLXFUNC operator >=(const char *, const TLString &);
  159.     friend int _TLXFUNC    operator > (const TLString &, const TLString &);
  160.     friend int _TLXFUNC operator > (const TLString &, const char *);
  161.     friend int _TLXFUNC operator > (const char *, const TLString &);
  162.  
  163.     friend ostream & _TLXFUNC operator <<(ostream &, const TLString &);
  164.     friend void _TLXFUNC tlSwap(TLString &, TLString &);
  165.  
  166. private:
  167.     TLString &        Assign(const char *, size_t);
  168.     TLString &        Append(const char *, size_t);
  169.     void         Create(const char *, size_t, size_t);
  170.     void         Insert(const char *, size_t, size_t);
  171.     void        Resize(size_t);
  172. };
  173.  
  174. #endif    // _TLX_STRINGS_H
  175.  
  176.