home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / borhead.zip / CSTRING.H < prev    next >
C/C++ Source or Header  |  1994-11-09  |  36KB  |  1,153 lines

  1. /*------------------------------------------------------------------------*/
  2. /*                                                                        */
  3. /*  CSTRING.H                                                             */
  4. /*                                                                        */
  5. /*                                                                        */
  6. /*------------------------------------------------------------------------*/
  7.  
  8. /*
  9.  *      C/C++ Run Time Library - Version 1.5
  10.  *
  11.  *      Copyright (c) 1993, 1994 by Borland International
  12.  *      All Rights Reserved.
  13.  *
  14.  */
  15.  
  16. #ifndef __cplusplus
  17. #error Must use C++ for CSTRING.H
  18. #endif
  19.  
  20. #ifndef __CSTRING_H
  21. #define __CSTRING_H
  22.  
  23. /*------------------------------------------------------------------------*/
  24. /*                                                                        */
  25. /*  This class uses a technique called "copy-on-write".                   */
  26. /*  Multiple instances of a string can refer to the same piece of data    */
  27. /*  so long as it is in a "readonly" situation.  If a string writes to    */
  28. /*  the data, then a copy is automatically made if more than one string   */
  29. /*  is referring to it.                                                   */
  30. /*                                                                        */
  31. /*------------------------------------------------------------------------*/
  32.  
  33. #ifndef OS2DEF_INCLUDED
  34. typedef unsigned long    LHANDLE;
  35. typedef LHANDLE HAB;
  36. typedef LHANDLE HMODULE;
  37. #endif
  38.  
  39. #if !defined(__STRING_H)
  40. #include <string.h>
  41. #endif
  42.  
  43. #if !defined(__REF_H)
  44. #include <ref.h>
  45. #endif
  46.  
  47. #if !defined(__CTYPE_H)
  48. #include <ctype.h>
  49. #endif
  50.  
  51. #if !defined(__STDDEF_H)
  52. #include <stddef.h>
  53. #endif
  54.  
  55. #if !defined(__EXCEPT_H)
  56. #include <except.h>
  57. #endif
  58.  
  59.  
  60. #if !defined(RC_INVOKED)
  61.  
  62. #pragma option -a-      // byte packing
  63.  
  64. #if defined(__BCOPT__)
  65. #endif
  66.  
  67. #if !defined(__TINY__)
  68. #pragma option -RT
  69. #endif
  70.  
  71. #pragma option -Vo-     // set standard C++ options
  72.  
  73. #if defined(__STDC__)
  74. #pragma warn -nak
  75. #endif
  76.  
  77. #endif  /* !RC_INVOKED */
  78.  
  79.  
  80. class _EXPCLASS string;
  81. class _EXPCLASS TRegexp;
  82. class _EXPCLASS TSubString;
  83. class _EXPCLASS TStringRef;
  84. class _EXPCLASS istream;
  85. class _EXPCLASS ostream;
  86.  
  87. /*------------------------------------------------------------------------*/
  88. /*                                                                        */
  89. /*  Since inline functions that use throw specifiers                      */
  90. /*  currently end up rather large, we don't use throw                     */
  91. /*  specifiers on them unless you #define USE_THROW_SPECIFIERS.           */
  92. /*                                                                        */
  93. /*------------------------------------------------------------------------*/
  94.  
  95. #if defined( USE_THROW_SPECIFIERS )
  96. #define THROW_NONE                  throw()
  97. #define THROW_XALLOC                throw(xalloc)
  98. #define THROW_OUTOFRANGE            throw( string::outofrange )
  99. #define THROW_XALLOC_LENGTHERROR    throw( xalloc, string::lengtherror )
  100. #define THROW_XALLOC_OUTOFRANGE     throw( xalloc, string::outofrange )
  101. #define THROW_XALLOC_RANGE_LENGTH   \
  102.         throw( xalloc, string::lengtherror, string::outofrange )
  103. #else
  104. #define THROW_NONE
  105. #define THROW_XALLOC
  106. #define THROW_OUTOFRANGE
  107. #define THROW_XALLOC_LENGTHERROR
  108. #define THROW_XALLOC_OUTOFRANGE
  109. #define THROW_XALLOC_RANGE_LENGTH
  110. #endif
  111.  
  112.  
  113. /*------------------------------------------------------------------------*/
  114. /*                                                                        */
  115. /*  string                                                                */
  116. /*                                                                        */
  117. /*------------------------------------------------------------------------*/
  118.  
  119. const size_t NPOS = size_t(-1);
  120.  
  121. class _EXPCLASS string
  122. {
  123.  
  124. public:
  125.  
  126.     //
  127.     // Exceptions
  128.     //
  129.     class outofrange : public xmsg
  130.     {
  131.     public:
  132.         _RTLENTRY outofrange();
  133.     };
  134.  
  135.     class lengtherror : public xmsg
  136.     {
  137.     public:
  138.         _RTLENTRY lengtherror();
  139.     };
  140.  
  141.     //
  142.     // Constructors
  143.     //
  144.  
  145.     _RTLENTRY string() throw( xalloc );
  146.  
  147.     _RTLENTRY string( const string _FAR &s ) throw( xalloc );
  148.     _RTLENTRY string( const string _FAR &s, size_t orig, size_t n = NPOS )
  149.         throw( xalloc );
  150.  
  151.     _RTLENTRY string( const char _FAR *cp ) throw( xalloc, lengtherror );
  152.     _RTLENTRY string( const char _FAR *cp, size_t orig, size_t n = NPOS )
  153.         throw( xalloc, lengtherror );
  154.  
  155.     _RTLENTRY string( char c ) THROW_XALLOC_LENGTHERROR;
  156.     _RTLENTRY string( char c, size_t n ) THROW_XALLOC_LENGTHERROR;
  157.  
  158.     _RTLENTRY string( signed char c ) THROW_XALLOC_LENGTHERROR;
  159.     _RTLENTRY string( signed char c, size_t n ) THROW_XALLOC_LENGTHERROR;
  160.  
  161.     _RTLENTRY string( unsigned char c ) THROW_XALLOC_LENGTHERROR;
  162.     _RTLENTRY string( unsigned char c, size_t n ) THROW_XALLOC_LENGTHERROR;
  163.  
  164.     // non-standard constructors
  165.     _RTLENTRY string( const TSubString _FAR &ss ) throw( xalloc );
  166.  
  167.     // Special far string ctors for small & medium model
  168.     #if (defined( __TINY__ ) || defined( __SMALL__ ) || defined( __MEDIUM__ )) && !defined( __DLL__ )
  169.     _RTLENTRY string( const char __far *cp ) THROW_XALLOC_LENGTHERROR;
  170.     _RTLENTRY string( const char __far *cp, size_t orig, size_t n = NPOS )
  171.         throw( xalloc, lengtherror );
  172.     #endif
  173.  
  174.     // Ctor to make a string from a resource
  175.     #if defined( _Windows )
  176.     _RTLENTRY string( HINSTANCE instance, UINT id, int len = 255 )
  177.         throw( xalloc, lengtherror );
  178.     #endif
  179.     #if defined( __OS2__ )
  180.     _RTLENTRY string( HAB hab, HMODULE resource, unsigned id, int len = 255 )
  181.         throw( xalloc, lengtherror );
  182.     #endif
  183.  
  184.     //
  185.     // Destructor
  186.     //
  187.     _RTLENTRY ~string() throw();
  188.  
  189.     //
  190.     // Assignment
  191.     //
  192.     string _FAR & _RTLENTRY operator = ( const string _FAR &s ) THROW_XALLOC;
  193.     string _FAR & _RTLENTRY assign( const string _FAR &s ) THROW_XALLOC;
  194.     string _FAR & _RTLENTRY assign( const string _FAR &s,
  195.                                     size_t orig,
  196.                                     size_t n = NPOS ) throw( xalloc );
  197.  
  198.     //
  199.     // Concatenation
  200.     //
  201.     string _FAR & _RTLENTRY operator += ( const string _FAR &s )
  202.         THROW_XALLOC_LENGTHERROR;
  203.     string _FAR & _RTLENTRY append( const string _FAR &s )
  204.         THROW_XALLOC_LENGTHERROR;
  205.     string _FAR & _RTLENTRY append( const string _FAR &s,
  206.                                     size_t orig,
  207.                                     size_t n = NPOS ) throw( xalloc, lengtherror );
  208.  
  209.     string _FAR & _RTLENTRY operator += ( const char _FAR *cp )
  210.         THROW_XALLOC_LENGTHERROR;
  211.     friend string _RTLENTRY _FARFUNC operator + ( const string _FAR &s,
  212.                                                const char _FAR *cp )
  213.         THROW_XALLOC_LENGTHERROR;
  214.     string _FAR & _RTLENTRY append( const char _FAR *cp )
  215.         throw( xalloc, lengtherror );
  216.     string _FAR & _RTLENTRY append( const char _FAR *cp,
  217.                                     size_t orig,
  218.                                     size_t n = NPOS ) throw( xalloc, lengtherror );
  219.  
  220.     string _FAR & _RTLENTRY prepend( const string _FAR &s )
  221.         THROW_XALLOC_LENGTHERROR;
  222.     string _FAR & _RTLENTRY prepend( const string _FAR &s,
  223.                                      size_t orig,
  224.                                      size_t n = NPOS ) THROW_XALLOC_LENGTHERROR;
  225.     string _FAR & _RTLENTRY prepend( const char _FAR *cp )
  226.         THROW_XALLOC_LENGTHERROR;
  227.     string _FAR & _RTLENTRY prepend( const char _FAR *cp,
  228.                                      size_t orig,
  229.                                      size_t n = NPOS ) throw( xalloc, lengtherror );
  230.  
  231.     //
  232.     // Comparison
  233.     //
  234.     int _RTLENTRY compare(const string _FAR &s) const throw();
  235.     int _RTLENTRY compare(const string _FAR &s,
  236.                           size_t orig,
  237.                           size_t n = NPOS ) const throw();
  238.  
  239.     friend int _RTLENTRY operator == ( const string _FAR &s1, const string _FAR &s2 )
  240.         THROW_NONE;
  241.  
  242.     friend int _RTLENTRY operator != ( const string _FAR &s1, const string _FAR &s2 )
  243.         THROW_NONE;
  244.  
  245.     friend int _RTLENTRY operator == ( const string _FAR &s, const char _FAR *cp )
  246.         THROW_NONE;
  247.     friend int _RTLENTRY operator == ( const char _FAR *cp, const string _FAR &s )
  248.         THROW_NONE;
  249.  
  250.     friend int _RTLENTRY operator != ( const string _FAR &s, const char _FAR *cp )
  251.         THROW_NONE;
  252.     friend int _RTLENTRY operator != ( const char _FAR *cp, const string _FAR &s )
  253.         THROW_NONE;
  254.  
  255.     friend int _RTLENTRY operator <  ( const string _FAR &s1, const string _FAR &s2 )
  256.         THROW_NONE;
  257.     friend int _RTLENTRY operator <  ( const string _FAR &s, const char _FAR *cp )
  258.         THROW_NONE;
  259.     friend int _RTLENTRY operator <  ( const char _FAR *cp, const string _FAR &s )
  260.         THROW_NONE;
  261.  
  262.     friend int _RTLENTRY operator <= ( const string _FAR &s1, const string _FAR &s2 )
  263.         THROW_NONE;
  264.     friend int _RTLENTRY operator <= ( const string _FAR &s, const char _FAR *cp )
  265.         THROW_NONE;
  266.     friend int _RTLENTRY operator <= ( const char _FAR *cp, const string _FAR &s )
  267.         THROW_NONE;
  268.  
  269.     friend int _RTLENTRY operator >  ( const string _FAR &s1, const string _FAR &s2 )
  270.         THROW_NONE;
  271.     friend int _RTLENTRY operator >  ( const string _FAR &s, const char _FAR *cp )
  272.         THROW_NONE;
  273.     friend int _RTLENTRY operator >  ( const char _FAR *cp, const string _FAR &s )
  274.         THROW_NONE;
  275.  
  276.     friend int _RTLENTRY operator >= ( const string _FAR &s1, const string _FAR &s2 )
  277.         THROW_NONE;
  278.     friend int _RTLENTRY operator >= ( const string _FAR &s, const char _FAR *cp )
  279.         THROW_NONE;
  280.     friend int _RTLENTRY operator >= ( const char _FAR *cp, const string _FAR &s )
  281.         THROW_NONE;
  282.  
  283.     //
  284.     // Insertion at some position
  285.     //
  286.     string _FAR & _RTLENTRY insert( size_t pos, const string _FAR &s )
  287.         throw( xalloc, outofrange, lengtherror );
  288.     string _FAR & _RTLENTRY insert( size_t pos,
  289.                                     const string _FAR &s,
  290.                                     size_t orig,
  291.                                     size_t n = NPOS ) throw( xalloc, outofrange, lengtherror );
  292.  
  293.     //
  294.     // Removal
  295.     //
  296.     string _FAR & _RTLENTRY remove( size_t pos ) THROW_XALLOC_OUTOFRANGE;
  297.     string _FAR & _RTLENTRY remove( size_t pos, size_t n )
  298.         throw( xalloc, outofrange );
  299.  
  300.     //
  301.     // Replacement at some position
  302.     //
  303.     string _FAR & _RTLENTRY replace( size_t pos, size_t n, const string _FAR &s )
  304.         THROW_XALLOC_RANGE_LENGTH;
  305.     string _FAR & _RTLENTRY replace( size_t pos,
  306.                                      size_t n1,
  307.                                      const string _FAR &s,
  308.                                      size_t orig,
  309.                                      size_t n2 = NPOS ) throw( xalloc, outofrange, lengtherror );
  310.  
  311.     //
  312.     // Subscripting
  313.     //
  314.     char _RTLENTRY get_at( size_t pos ) const THROW_OUTOFRANGE;
  315.     void _RTLENTRY put_at( size_t pos, char c ) THROW_OUTOFRANGE;
  316.  
  317.     char _FAR & _RTLENTRY operator[]( size_t pos ) THROW_OUTOFRANGE;
  318.     char _FAR & _RTLENTRY operator()( size_t pos ) throw( outofrange );
  319.     TSubString _RTLENTRY operator()( size_t start, size_t len ) THROW_NONE;
  320.     TSubString _RTLENTRY operator()( const TRegexp _FAR &re ) THROW_NONE;
  321.     TSubString _RTLENTRY operator()( const TRegexp _FAR &re, size_t start ) throw();
  322.  
  323.     char _RTLENTRY operator[]( size_t pos ) const THROW_OUTOFRANGE;
  324.     char _RTLENTRY operator()( size_t pos ) const THROW_OUTOFRANGE;
  325.     const TSubString _RTLENTRY operator()( size_t start, size_t len ) const throw();
  326.     const TSubString _RTLENTRY operator()( const TRegexp _FAR &pat ) const THROW_NONE;
  327.     const TSubString _RTLENTRY operator()( const TRegexp _FAR &pat, size_t start )
  328.         const throw();
  329.  
  330.     //
  331.     // Searching
  332.     //
  333.     size_t _RTLENTRY find( const string _FAR &s ) const THROW_NONE;
  334.     size_t _RTLENTRY find( const string _FAR &s, size_t pos ) const throw();
  335.     size_t _RTLENTRY rfind( const string _FAR &s ) const THROW_NONE;
  336.     size_t _RTLENTRY rfind( const string _FAR &s, size_t pos ) const throw();
  337.  
  338.     int _RTLENTRY contains( const char _FAR *pat ) const throw();
  339.     int _RTLENTRY contains(const string _FAR &s) const THROW_NONE;
  340.     size_t _RTLENTRY find( const TRegexp _FAR &pat, size_t i = 0 ) const throw();
  341.     size_t _RTLENTRY find( const TRegexp _FAR &pat, size_t _FAR *ext, size_t i = 0 )
  342.         const throw();
  343.  
  344.     //
  345.     // Substring
  346.     //
  347.     string _RTLENTRY substr( size_t pos ) const
  348.         throw( xalloc, outofrange );
  349.     string _RTLENTRY substr( size_t pos, size_t n ) const
  350.         throw( xalloc, outofrange );
  351.  
  352.     TSubString _RTLENTRY substring( const char _FAR *cp ) THROW_NONE;
  353.     const TSubString _RTLENTRY substring( const char _FAR *cp )
  354.         const THROW_NONE;
  355.     TSubString _RTLENTRY substring( const char _FAR *cp, size_t start ) throw();
  356.     const TSubString _RTLENTRY substring( const char _FAR *cp, size_t start )
  357.         const throw();
  358.  
  359.     //
  360.     // Character set searching
  361.     //
  362.     size_t _RTLENTRY find_first_of( const string _FAR &s ) const THROW_NONE;
  363.     size_t _RTLENTRY find_first_of( const string _FAR &s, size_t pos ) const throw();
  364.     size_t _RTLENTRY find_first_not_of( const string _FAR &s ) const THROW_NONE;
  365.     size_t _RTLENTRY find_first_not_of( const string _FAR &s, size_t pos )
  366.         const throw();
  367.     size_t _RTLENTRY find_last_of( const string _FAR &s ) const THROW_NONE;
  368.     size_t _RTLENTRY find_last_of( const string _FAR &s, size_t pos ) const throw();
  369.     size_t _RTLENTRY find_last_not_of( const string _FAR &s ) const THROW_NONE;
  370.     size_t _RTLENTRY find_last_not_of( const string _FAR &s, size_t pos )
  371.         const throw();
  372.  
  373.     //
  374.     // Miscellaneous
  375.     //
  376.     size_t _RTLENTRY length() const THROW_NONE;
  377.     size_t _RTLENTRY copy( char _FAR *cb, size_t n ) throw( outofrange );
  378.     size_t _RTLENTRY copy( char _FAR *cb, size_t n, size_t pos ) throw( outofrange );
  379.     const char _FAR * _RTLENTRY c_str() const THROW_XALLOC;
  380.     size_t _RTLENTRY reserve() const THROW_NONE;
  381.     void _RTLENTRY reserve( size_t ic ) throw( xalloc, outofrange );
  382.  
  383.     string _RTLENTRY copy() const throw( xalloc );    // Distinct copy of self.
  384.  
  385.  
  386.     // Static member functions:
  387.     static int _RTLENTRY set_case_sensitive( int tf = 1 );
  388.     static int _RTLENTRY set_paranoid_check( int ck = 1 );
  389.     static int _RTLENTRY skip_whitespace( int sk = 1 );
  390.     static size_t _RTLENTRY initial_capacity( size_t ic = 63 );
  391.     static size_t _RTLENTRY resize_increment( size_t ri = 64 );
  392.     static size_t _RTLENTRY max_waste( size_t mw = 63 );
  393.  
  394.     static int _RTLENTRY get_case_sensitive_flag();
  395.     static int _RTLENTRY get_paranoid_check_flag();
  396.     static int _RTLENTRY get_skip_whitespace_flag();
  397.     static size_t _RTLENTRY get_initial_capacity();
  398.     static size_t _RTLENTRY get_resize_increment();
  399.     static size_t _RTLENTRY get_max_waste();
  400.  
  401.     enum StripType { Leading, Trailing, Both };
  402.  
  403.     // Non-static member functions:
  404.     unsigned _RTLENTRY hash() const;
  405.     int      _RTLENTRY is_null() const;
  406.     istream _FAR & _RTLENTRY read_file( istream _FAR &is );
  407.     istream _FAR & _RTLENTRY read_string( istream _FAR &is );
  408.     istream _FAR & _RTLENTRY read_line( istream _FAR &is );
  409.     istream _FAR & _RTLENTRY read_to_delim( istream _FAR &is, char delim = '\n' );
  410.     istream _FAR & _RTLENTRY read_token( istream _FAR &is );
  411.     void       _RTLENTRY resize( size_t m );
  412.     TSubString _RTLENTRY strip( StripType s = Trailing, char c = ' ' );
  413.     void       _RTLENTRY to_lower();
  414.     void       _RTLENTRY to_upper();
  415.  
  416.  
  417. protected:
  418.  
  419.     int  _RTLENTRY valid_element( size_t pos ) const THROW_NONE;
  420.     int  _RTLENTRY valid_index( size_t pos ) const THROW_NONE;
  421.  
  422.     void _RTLENTRY assert_element( size_t pos ) const throw( outofrange );
  423.     void _RTLENTRY assert_index( size_t pos ) const throw( outofrange );
  424.  
  425.     _RTLENTRY string( const string _FAR &s, const char _FAR *cb );
  426.     void _RTLENTRY cow();
  427.  
  428. private:
  429.  
  430.     TStringRef _FAR *p;
  431.  
  432.     static int case_sensitive;
  433.     static int paranoid_check;
  434.     static int skip_white;
  435.     static size_t initial_capac;
  436.     static size_t resize_inc;
  437.     static size_t freeboard;
  438.  
  439. private:
  440.  
  441.     friend class _EXPCLASS TSubString;
  442.     friend class _EXPCLASS TStringRef;
  443.  
  444.     void _RTLENTRY clone();
  445.     size_t _RTLENTRY find_case_index( const char _FAR *cb,
  446.                             size_t start,
  447.                             size_t _FAR &patl) const;
  448.     size_t _RTLENTRY rfind_case_index( const char _FAR *cb,
  449.                              size_t start,
  450.                              size_t _FAR &patl) const;
  451.     size_t _RTLENTRY find_index(const char _FAR *,
  452.                       size_t start,
  453.                       size_t _FAR & patl) const;
  454.     size_t _RTLENTRY rfind_index(const char _FAR *,
  455.                        size_t start,
  456.                        size_t _FAR & patl) const;
  457.     unsigned _RTLENTRY hash_case() const;
  458.  
  459. };
  460.  
  461. #if defined( BI_OLDNAMES )
  462. #define TString string
  463. #define BI_String string
  464. #endif
  465.  
  466. /*------------------------------------------------------------------------*/
  467. /*                                                                        */
  468. /*  Related global functions                                              */
  469. /*                                                                        */
  470. /*------------------------------------------------------------------------*/
  471.  
  472. istream _FAR &
  473. _RTLENTRY _FARFUNC operator >> ( istream _FAR &is, string _FAR &s );
  474.  
  475. ostream _FAR &
  476. _RTLENTRY _FARFUNC operator << ( ostream _FAR &os, const string _FAR &s );
  477.  
  478. istream _FAR &
  479. _RTLENTRY _FARFUNC getline( istream _FAR &is, string _FAR &s );
  480.  
  481. istream _FAR &
  482. _RTLENTRY _FARFUNC getline( istream _FAR &is, string _FAR &s, char c );
  483.  
  484. string _RTLENTRY _FARFUNC to_lower( const string _FAR &s ) throw();
  485. string _RTLENTRY _FARFUNC to_upper( const string _FAR &s ) throw();
  486. string _RTLENTRY _FARFUNC operator + ( const char _FAR *cp,
  487.                                     const string _FAR & s)
  488.                                     throw( xalloc, string::lengtherror );
  489. string _RTLENTRY _FARFUNC operator + ( const string _FAR &s1,
  490.                                     const string _FAR &s2 )
  491.                                     THROW_XALLOC_LENGTHERROR;
  492.  
  493. /*------------------------------------------------------------------------*/
  494. /*                                                                        */
  495. /*  TStringRef                                                            */
  496. /*                                                                        */
  497. /*  This is the dynamically allocated part of a string.                   */
  498. /*  It maintains a reference count.                                       */
  499. /*  There are no public member functions.                                 */
  500. /*                                                                        */
  501. /*------------------------------------------------------------------------*/
  502.  
  503. class _EXPCLASS TStringRef : public TReference
  504. {
  505.  
  506.     friend class _EXPCLASS string;
  507.     friend class _EXPCLASS TSubString;
  508.  
  509.     //
  510.     // Data
  511.     //
  512.     char _FAR *array;
  513.     size_t nchars;
  514.     size_t capacity;
  515.  
  516.     //
  517.     // State flags
  518.     //
  519.     enum {
  520.         MemReserved = 1     // indicates that reserve() has been
  521.                             // called on this string
  522.         };
  523.     unsigned flags;
  524.  
  525.     //
  526.     // Constructors
  527.     //
  528.     _RTLENTRY TStringRef( char c, size_t n );
  529.     _RTLENTRY TStringRef( const char _FAR *str1, size_t count1,
  530.                 const char _FAR *str2, size_t count2,
  531.                 size_t extra );
  532.  
  533.     // Special far TStringRef ctor for small data models
  534.     #if (defined( __TINY__ ) || defined( __SMALL__ ) || defined( __MEDIUM__ )) && !defined( __DLL__ )
  535.     _RTLENTRY TStringRef( const char __far*, size_t n = NPOS );
  536.     #endif
  537.  
  538.     //
  539.     // Ctor to make a TStringRef from a resource
  540.     //
  541.     #if defined( _Windows )
  542.     _RTLENTRY TStringRef( HINSTANCE instance, UINT id, int len = 255 )
  543.          throw( xalloc, string::lengtherror );
  544.     #endif
  545.     #if defined( __OS2__ )
  546.     _RTLENTRY TStringRef( HAB hab, HMODULE resource, unsigned int id, int len = 255 )
  547.          throw( xalloc, string::lengtherror );
  548.     #endif
  549.  
  550.     //
  551.     // Destructor
  552.     //
  553.     _RTLENTRY ~TStringRef() throw();
  554.  
  555.     //
  556.     // Miscellaneous
  557.     //
  558.     void _RTLENTRY reserve( size_t ic ) throw( xalloc, string::outofrange );
  559.     void _RTLENTRY check_freeboard() throw();
  560.     void _RTLENTRY grow_to( size_t n ) throw( xalloc, string::lengtherror );
  561.     void _RTLENTRY read_to_delim( istream _FAR &is, char delim ) throw( xalloc );
  562.     void _RTLENTRY read_token( istream _FAR &is ) throw( xalloc );
  563.     static size_t _RTLENTRY round_capacity( size_t cap ) throw();
  564.     void _RTLENTRY splice( size_t start, size_t extent,
  565.                  const char _FAR *cp, size_t n )
  566.         throw( xalloc, string::lengtherror );
  567.  
  568. };
  569.  
  570. #if defined( BI_OLDNAMES )
  571. #define BI_StringRef TStringRef
  572. #endif
  573.  
  574. /*------------------------------------------------------------------------*/
  575. /*                                                                        */
  576. /*  TSubString                                                            */
  577. /*                                                                        */
  578. /*  The TSubString class allows selected elements to be addressed.        */
  579. /*  There are no public constructors.                                     */
  580. /*                                                                        */
  581. /*------------------------------------------------------------------------*/
  582.  
  583. class _EXPCLASS TSubString
  584. {
  585.  
  586. public:
  587.  
  588.     //
  589.     // Assignment
  590.     //
  591.     TSubString _FAR & _RTLENTRY operator = ( const string _FAR &s ) throw();
  592.  
  593.     //
  594.     // Comparison
  595.     //
  596.     int _RTLENTRY operator == ( const char _FAR *cp ) const throw();
  597.     int _RTLENTRY operator == ( const string _FAR &s ) const THROW_NONE;
  598.     int _RTLENTRY operator != ( const char _FAR *cp ) const THROW_NONE;
  599.     int _RTLENTRY operator != ( const string _FAR & str ) const THROW_NONE;
  600.  
  601.     //
  602.     // Subscripting
  603.     //
  604.     char _RTLENTRY get_at( size_t pos ) const THROW_OUTOFRANGE;
  605.     void _RTLENTRY put_at( size_t pos, char c ) THROW_OUTOFRANGE;
  606.  
  607.     char _FAR & _RTLENTRY operator[]( size_t pos ) THROW_OUTOFRANGE;
  608.     char _FAR & _RTLENTRY operator()( size_t pos ) throw( string::outofrange );
  609.     char _RTLENTRY operator[]( size_t pos ) const THROW_OUTOFRANGE;
  610.     char _RTLENTRY operator()( size_t pos ) const THROW_OUTOFRANGE;
  611.     size_t _RTLENTRY length() const THROW_NONE;
  612.     int _RTLENTRY start() const THROW_NONE;
  613.     void _RTLENTRY to_lower() throw();
  614.     void _RTLENTRY to_upper() throw();
  615.  
  616.     //
  617.     // Detecting empty strings
  618.     //
  619.     int _RTLENTRY is_null() const THROW_NONE;
  620.     int _RTLENTRY operator!() const THROW_NONE;
  621.  
  622. protected:
  623.  
  624.     void _RTLENTRY assert_element( size_t pos ) const throw( string::outofrange );
  625.     int _RTLENTRY valid_element( size_t pos ) const;
  626.  
  627. private:
  628.  
  629.     friend class _EXPCLASS string;
  630.  
  631.     //
  632.     // Data
  633.     //
  634.     string _FAR *s;
  635.     size_t begin;
  636.     size_t extent;
  637.  
  638.     //
  639.     // Constructor
  640.     //
  641.     _RTLENTRY TSubString( const string _FAR *cp, size_t start, size_t len );
  642.  
  643. };
  644.  
  645. #if defined( BI_OLDNAMES )
  646. #define BI_SubString TSubString
  647. #endif
  648.  
  649. /*------------------------------------------------------------------------*/
  650. /*                                                                        */
  651. /*  string inlines                                                        */
  652. /*                                                                        */
  653. /*------------------------------------------------------------------------*/
  654.  
  655. inline _RTLENTRY string::outofrange::outofrange() :
  656.     xmsg( "String reference out of range" )
  657. {
  658. }
  659.  
  660. inline _RTLENTRY string::lengtherror::lengtherror() :
  661.     xmsg( "String length error" )
  662. {
  663. }
  664.  
  665. inline _RTLENTRY string::string( char c ) THROW_XALLOC_LENGTHERROR
  666. {
  667.     p = new TStringRef(c,1);
  668. }
  669.  
  670. inline _RTLENTRY string::string( char c, size_t n ) THROW_XALLOC_LENGTHERROR
  671. {
  672.     p = new TStringRef(c,n);
  673. }
  674.  
  675. inline _RTLENTRY string::string( signed char c ) THROW_XALLOC_LENGTHERROR
  676. {
  677.     p = new TStringRef(c,1);
  678. }
  679.  
  680. inline _RTLENTRY string::string( signed char c, size_t n ) THROW_XALLOC_LENGTHERROR
  681. {
  682.     p = new TStringRef(c,n);
  683. }
  684.  
  685. inline _RTLENTRY string::string( unsigned char c ) THROW_XALLOC_LENGTHERROR
  686. {
  687.     p = new TStringRef(c,1);
  688. }
  689.  
  690. inline _RTLENTRY string::string( unsigned char c, size_t n ) THROW_XALLOC_LENGTHERROR
  691. {
  692.     p = new TStringRef(c,n);
  693. }
  694.  
  695. #if (defined(__TINY__)|| defined(__SMALL__)|| defined(__MEDIUM__)) && !defined(__DLL__)
  696. // Far string ctors make a near string from a far string
  697.  
  698. inline _RTLENTRY string::string( const char __far *cp ) THROW_XALLOC_LENGTHERROR
  699. {
  700.     p = new TStringRef(cp);
  701. }
  702.  
  703. #endif
  704.  
  705. inline string _FAR & _RTLENTRY string::operator = ( const string _FAR &s )
  706.     THROW_NONE
  707. {
  708.     return assign( s, 0, NPOS );
  709. }
  710.  
  711. inline string _FAR & _RTLENTRY string::assign( const string _FAR &s )
  712.     THROW_NONE
  713. {
  714.     return assign( s, 0, NPOS );
  715. }
  716.  
  717. inline string _FAR & _RTLENTRY string::operator += ( const string _FAR &s )
  718.     THROW_XALLOC_LENGTHERROR
  719. {
  720.     return append( s, 0, NPOS );
  721. }
  722.  
  723. inline string _FAR & _RTLENTRY string::append( const string _FAR &s )
  724.     THROW_XALLOC_LENGTHERROR
  725. {
  726.     return append(s, 0, NPOS);
  727. }
  728.  
  729. inline string _FAR & _RTLENTRY string::prepend( const char _FAR *cp )
  730.     THROW_XALLOC_LENGTHERROR
  731. {
  732.     return prepend( cp, 0, strlen(cp) );
  733. }
  734.  
  735. inline int _RTLENTRY operator == ( const string _FAR &s1, const string _FAR &s2 )
  736.     THROW_NONE
  737. {
  738.     return s1.compare( s2 ) == 0;
  739. }
  740.  
  741. inline int _RTLENTRY operator != ( const string _FAR &s1, const string _FAR &s2 )
  742.     THROW_NONE
  743. {
  744.     return !(s1==s2);
  745. }
  746.  
  747. inline string _FAR & _RTLENTRY string::remove( size_t pos )
  748.     THROW_XALLOC_OUTOFRANGE
  749. {
  750.     return remove( pos, length() );
  751. }
  752.  
  753. inline string _FAR & _RTLENTRY string::replace( size_t pos,
  754.                                      size_t n,
  755.                                      const string _FAR &s )
  756.     THROW_XALLOC_RANGE_LENGTH
  757. {
  758.     return replace( pos, n, s, 0, NPOS );
  759. }
  760.  
  761. inline char _RTLENTRY string::get_at( size_t pos ) const THROW_OUTOFRANGE
  762. {
  763.     return (*this)[pos];
  764. }
  765.  
  766. inline void _RTLENTRY string::put_at( size_t pos, char c ) THROW_OUTOFRANGE
  767. {
  768.     (*this)[pos] = c;
  769. }
  770.  
  771. inline char _FAR & _RTLENTRY string::operator[]( size_t pos ) THROW_OUTOFRANGE
  772. {
  773.     return (*this)(pos);    // use operator()
  774. }
  775.  
  776. inline TSubString _RTLENTRY string::operator()( size_t start, size_t len ) THROW_NONE
  777. {
  778.     return TSubString( this, start, len );
  779. }
  780.  
  781. inline size_t _RTLENTRY string::find( const string _FAR &s ) const THROW_NONE
  782. {
  783.     return find( s, 0 );
  784. }
  785.  
  786. inline size_t _RTLENTRY string::rfind( const string _FAR &s ) const THROW_NONE
  787. {
  788.     return rfind( s, length() );
  789. }
  790.  
  791. inline size_t _RTLENTRY string::length() const THROW_NONE
  792. {
  793.     return p->nchars;
  794. }
  795.  
  796. inline const char _FAR * _RTLENTRY string::c_str() const THROW_NONE
  797. {
  798.     return p->array;
  799. }
  800.  
  801. inline size_t _RTLENTRY string::reserve() const THROW_NONE
  802. {
  803.     return p->capacity;
  804. }
  805.  
  806. inline void _RTLENTRY string::cow()
  807. {
  808.     if( p->References() > 1 )
  809.         clone();
  810. }
  811.  
  812. inline string _FAR & _RTLENTRY string::operator += ( const char _FAR *cp )
  813.     THROW_XALLOC_LENGTHERROR
  814. {
  815.     return append( cp, 0, strlen(cp) );
  816. }
  817.  
  818. inline string _FAR & _RTLENTRY string::prepend( const string _FAR &s )
  819.     THROW_XALLOC_LENGTHERROR
  820. {
  821.     return prepend( s.c_str() );
  822. }
  823.  
  824. inline string _FAR & _RTLENTRY string::prepend( const string _FAR &s,
  825.                                                 size_t orig,
  826.                                                 size_t n ) THROW_XALLOC_LENGTHERROR
  827. {
  828.     return prepend( s.c_str(), orig, n );
  829. }
  830.  
  831. inline int _RTLENTRY operator == ( const string _FAR &s1, const char _FAR *s2 ) THROW_NONE
  832. {
  833.     return s1.compare(s2) == 0;
  834. }
  835.  
  836. inline int _RTLENTRY operator == ( const char _FAR *cp, const string _FAR &s ) THROW_NONE
  837. {
  838.     return string(cp).compare(s) == 0;
  839. }
  840.  
  841. inline int _RTLENTRY operator != ( const string _FAR &s, const char _FAR *cp ) THROW_NONE
  842. {
  843.     return !(s==cp);
  844. }
  845.  
  846. inline int _RTLENTRY operator != ( const char _FAR *cp, const string _FAR &s ) THROW_NONE
  847. {
  848.     return !(cp==s);
  849. }
  850.  
  851. inline int _RTLENTRY operator <  ( const string _FAR &s1, const string _FAR &s2 )
  852.     THROW_NONE
  853. {
  854.     return s1.compare(s2) < 0;
  855. }
  856.  
  857. inline int _RTLENTRY operator <  ( const string _FAR &s1, const char _FAR *s2 ) THROW_NONE
  858. {
  859.     return s1.compare(s2) < 0;
  860. }
  861.  
  862. inline int _RTLENTRY operator <  ( const char _FAR *cp, const string _FAR &s ) THROW_NONE
  863. {
  864.     return string(cp).compare(s) < 0;
  865. }
  866.  
  867. inline int _RTLENTRY operator <= ( const string _FAR &s1, const string _FAR &s2 )
  868.     THROW_NONE
  869. {
  870.     return s1.compare(s2) <= 0;
  871. }
  872.  
  873. inline int _RTLENTRY operator <= ( const string _FAR &s, const char _FAR *cp ) THROW_NONE
  874. {
  875.     return s.compare(string(cp)) <= 0;
  876. }
  877.  
  878. inline int _RTLENTRY operator <= ( const char _FAR *cp, const string _FAR &s ) THROW_NONE
  879. {
  880.     return string(cp).compare(s) <= 0;
  881. }
  882.  
  883. inline int _RTLENTRY operator >  ( const string _FAR &s1, const string _FAR &s2 )
  884.     THROW_NONE
  885. {
  886.     return s1.compare(s2) > 0;
  887. }
  888.  
  889. inline int _RTLENTRY operator >  ( const string _FAR &s, const char _FAR *cp ) THROW_NONE
  890. {
  891.     return s.compare(cp) > 0;
  892. }
  893.  
  894. inline int _RTLENTRY operator >  ( const char _FAR *cp, const string _FAR &s ) THROW_NONE
  895. {
  896.     return string(cp).compare(s) > 0;
  897. }
  898.  
  899. inline int _RTLENTRY operator >= ( const string _FAR &s1, const string _FAR &s2 )
  900.     THROW_NONE
  901. {
  902.     return s1.compare(s2) >= 0;
  903. }
  904.  
  905. inline int _RTLENTRY operator >= ( const string _FAR &s, const char _FAR *cp ) THROW_NONE
  906. {
  907.     return s.compare(cp) >= 0;
  908. }
  909.  
  910. inline int _RTLENTRY operator >= ( const char _FAR *cp, const string _FAR &s ) THROW_NONE
  911. {
  912.     return string(cp).compare(s) >= 0;
  913. }
  914.  
  915. inline char _RTLENTRY string::operator[]( size_t pos ) const THROW_OUTOFRANGE
  916. {
  917.     assert_element(pos);
  918.     return p->array[pos];
  919. }
  920.  
  921. inline char _RTLENTRY string::operator()( size_t pos ) const THROW_OUTOFRANGE
  922. {
  923. #if defined( BOUNDS_CHECK )
  924.     assert_element(pos);
  925. #endif
  926.     return p->array[pos];
  927. }
  928.  
  929. inline int _RTLENTRY string::contains( const string _FAR &s ) const THROW_NONE
  930. {
  931.     return contains( s.c_str() );
  932. }
  933.  
  934. inline TSubString _RTLENTRY string::substring( const char _FAR *cp ) THROW_NONE
  935. {
  936.     return substring( cp, 0 );
  937. }
  938.  
  939. inline const TSubString _RTLENTRY string::substring( const char _FAR *cp ) const
  940.     THROW_NONE
  941. {
  942.     return substring( cp, 0 );
  943. }
  944.  
  945. inline size_t _RTLENTRY string::find_first_of( const string _FAR &s ) const THROW_NONE
  946. {
  947.     return find_first_of( s, 0 );
  948. }
  949.  
  950. inline size_t _RTLENTRY string::find_first_not_of( const string _FAR &s ) const THROW_NONE
  951. {
  952.     return find_first_not_of( s, 0 );
  953. }
  954.  
  955. inline size_t _RTLENTRY string::find_last_of( const string _FAR &s ) const THROW_NONE
  956. {
  957.     return find_last_of( s, NPOS );
  958. }
  959.  
  960. inline size_t _RTLENTRY string::find_last_not_of( const string _FAR &s ) const THROW_NONE
  961. {
  962.     return find_last_not_of( s, NPOS );
  963. }
  964.  
  965. inline int _RTLENTRY string::get_case_sensitive_flag()
  966. {
  967.     return case_sensitive;
  968. }
  969.  
  970. inline int _RTLENTRY string::get_paranoid_check_flag()
  971. {
  972.     return paranoid_check;
  973. }
  974.  
  975. inline int _RTLENTRY string::get_skip_whitespace_flag()
  976. {
  977.     return skip_white;
  978. }
  979.  
  980. inline size_t _RTLENTRY string::get_initial_capacity()
  981. {
  982.     return initial_capac;
  983. }
  984.  
  985. inline size_t _RTLENTRY string::get_resize_increment()
  986. {
  987.     return resize_inc;
  988. }
  989.  
  990. inline size_t _RTLENTRY string::get_max_waste()
  991. {
  992.     return freeboard;
  993. }
  994.  
  995. inline int _RTLENTRY string::is_null() const
  996. {
  997.     return *p->array==0;
  998. }
  999.  
  1000.  
  1001. // Check to make sure a string index refers to a valid element
  1002. inline int _RTLENTRY string::valid_element( size_t n ) const THROW_NONE
  1003. {
  1004.     return n < length();
  1005. }
  1006.  
  1007. // Check to make sure a string index is in range
  1008. inline int _RTLENTRY string::valid_index( size_t n ) const THROW_NONE
  1009. {
  1010.     return n <= length();
  1011. }
  1012.  
  1013. // Constructor for internal use only
  1014. inline _RTLENTRY string::string( const string _FAR &s, const char _FAR *cp )
  1015. {
  1016.     p = new TStringRef( s.c_str(), s.length(), cp, cp?strlen(cp):0, 0 );
  1017. }
  1018.  
  1019. inline string _RTLENTRY operator + ( const string _FAR &s,
  1020.                                   const char _FAR *cp )
  1021.     THROW_XALLOC_LENGTHERROR
  1022. {
  1023.     return string(s,cp);
  1024. }
  1025.  
  1026. inline string _RTLENTRY operator + ( const string _FAR &s1,
  1027.                                   const string _FAR &s2 )
  1028.     THROW_XALLOC_LENGTHERROR
  1029. {
  1030.     return s1 + s2.c_str();
  1031. }
  1032.  
  1033. /*------------------------------------------------------------------------*/
  1034. /*                                                                        */
  1035. /*  TSubString inlines                                                    */
  1036. /*                                                                        */
  1037. /*------------------------------------------------------------------------*/
  1038.  
  1039. inline int _RTLENTRY TSubString::operator == ( const string _FAR &s ) const THROW_NONE
  1040. {
  1041.     return operator==(s.c_str());
  1042. }
  1043.  
  1044. inline int _RTLENTRY TSubString::operator != ( const char _FAR *cp ) const THROW_NONE
  1045. {
  1046.     return !operator==(cp);
  1047. }
  1048.  
  1049. inline int _RTLENTRY TSubString::operator != ( const string _FAR &s ) const THROW_NONE
  1050. {
  1051.     return !operator==(s.c_str());
  1052. }
  1053.  
  1054. inline char _RTLENTRY TSubString::get_at( size_t pos ) const THROW_OUTOFRANGE
  1055. {
  1056.     return (*this)[pos];
  1057. }
  1058.  
  1059. inline void _RTLENTRY TSubString::put_at( size_t pos, char c ) THROW_OUTOFRANGE
  1060. {
  1061.     (*this)[pos] = c;
  1062. }
  1063.  
  1064. inline char _FAR & _RTLENTRY TSubString::operator[]( size_t pos ) THROW_OUTOFRANGE
  1065. {
  1066.     return (*this)(pos);    // use operator()
  1067. }
  1068.  
  1069. inline char _RTLENTRY TSubString::operator[]( size_t pos ) const
  1070.     THROW_OUTOFRANGE
  1071. {
  1072.     assert_element(pos);
  1073.     return s->p->array[begin+pos];
  1074. }
  1075.  
  1076. inline char _RTLENTRY TSubString::operator()( size_t pos ) const
  1077.     THROW_OUTOFRANGE
  1078. {
  1079. #if defined( BOUNDS_CHECK )
  1080.     assert_element(pos);
  1081. #endif
  1082.     return s->p->array[begin+pos];
  1083. }
  1084.  
  1085. inline size_t _RTLENTRY TSubString::length() const THROW_NONE
  1086. {
  1087.     return extent;
  1088. }
  1089.  
  1090. inline int _RTLENTRY TSubString::start() const THROW_NONE
  1091. {
  1092.     return begin;
  1093. }
  1094.  
  1095. inline int _RTLENTRY TSubString::is_null() const THROW_NONE
  1096. {
  1097.     return begin == NPOS;
  1098. }
  1099.  
  1100. inline int _RTLENTRY TSubString::operator!() const THROW_NONE
  1101. {
  1102.     return begin == NPOS;
  1103. }
  1104.  
  1105. inline int _RTLENTRY TSubString::valid_element( size_t n ) const THROW_NONE
  1106. {
  1107.     return n < length();
  1108. }
  1109.  
  1110. // Private constructor
  1111. inline _RTLENTRY TSubString::TSubString(const string _FAR *sp,
  1112.                               size_t start,
  1113.                               size_t len ) :
  1114.     begin(start),
  1115.     extent(len),
  1116.     s((string _FAR *)sp)
  1117. {
  1118. }
  1119.  
  1120. inline TSubString _RTLENTRY string::operator()( const TRegexp _FAR & r ) THROW_NONE
  1121. {
  1122.     return (*this)(r,0);
  1123. }
  1124.  
  1125. inline const TSubString _RTLENTRY string::operator()( const TRegexp _FAR &r ) const THROW_NONE
  1126. {
  1127.     return (*this)(r,0);
  1128. }
  1129.  
  1130.  
  1131. #if !defined(RC_INVOKED)
  1132.  
  1133. #if defined(__STDC__)
  1134. #pragma warn .nak
  1135. #endif
  1136.  
  1137. #pragma option -Vo.     // restore user C++ options
  1138.  
  1139. #if !defined(__TINY__)
  1140. #pragma option -RT.
  1141. #endif
  1142.  
  1143. #if defined(__BCOPT__)
  1144. #endif
  1145.  
  1146. #pragma option -a.      // restore default packing
  1147.  
  1148. #endif  /* !RC_INVOKED */
  1149.  
  1150.  
  1151. #endif  // __CSTRING_H
  1152.  
  1153.