home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c083 / 14.ddi / GENINC.PAK / CSTRING.H < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-02  |  35.4 KB  |  1,134 lines

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