home *** CD-ROM | disk | FTP | other *** search
/ GEMini Atari / GEMini_Atari_CD-ROM_Walnut_Creek_December_1993.iso / files / gnu / g__lib / string.h < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-23  |  29.4 KB  |  1,208 lines

  1. // This may look like C code, but it is really -*- C++ -*-
  2. /* 
  3. Copyright (C) 1988 Free Software Foundation
  4.     written by Doug Lea (dl@rocky.oswego.edu)
  5.  
  6. This file is part of GNU CC.
  7.  
  8. GNU CC is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY.  No author or distributor
  10. accepts responsibility to anyone for the consequences of using it
  11. or for whether it serves any particular purpose or works at all,
  12. unless he says so in writing.  Refer to the GNU CC General Public
  13. License for full details.
  14.  
  15. Everyone is granted permission to copy, modify and redistribute
  16. GNU CC, but only under the conditions described in the
  17. GNU CC General Public License.   A copy of this license is
  18. supposed to have been given to you along with GNU CC so you
  19. can know your rights and responsibilities.  It should be in a
  20. file named COPYING.  Among other things, the copyright notice
  21. and this notice must be preserved on all copies.  
  22. */
  23.  
  24.  
  25. #ifndef _String_h
  26. #pragma once
  27. #define _String_h 1
  28.  
  29. #include <stream.h>
  30.  
  31. struct StrRep                     // internal String representations
  32. {
  33.   unsigned short    len;         // string length 
  34.   unsigned short    sz;          // allocated space
  35.   char              s[1];        // the string starts here 
  36.                                  // (at least 1 char for trailing null)
  37.                                  // allocated & expanded via non-public fcts
  38.  
  39.   friend StrRep*     Salloc(StrRep*, const char*, int, int);
  40.   friend StrRep*     Scopy(StrRep*, StrRep*);
  41.   friend StrRep*     Sresize(StrRep*, int);
  42.   friend StrRep*     Scat(StrRep*, const char*, int, const char*, int);
  43.   friend StrRep*     Sprepend(StrRep*, const char*, int);
  44.   friend StrRep*     Sreverse(StrRep*, StrRep*);
  45.   friend StrRep*     Supcase(StrRep*, StrRep*);
  46.   friend StrRep*     Sdowncase(StrRep*, StrRep*);
  47.   friend StrRep*     Scapitalize(StrRep*, StrRep*);
  48.  
  49. };
  50.  
  51.  
  52. class String;
  53. class SubString;
  54. class StrTmp;
  55.  
  56.  
  57. struct re_pattern_buffer;       // defined elsewhere
  58. struct re_registers;
  59.  
  60. class Regex
  61. {
  62.   friend class       String;
  63.   friend class       SubString;
  64. protected:
  65.   re_pattern_buffer* buf;
  66.   re_registers*      reg;
  67.  
  68.   void               initialize(const char* t, int tlen, int fast, 
  69.                                 int bufsize, const char* transtable);
  70.  
  71. public:
  72.                      Regex(const char* t, 
  73.                            int fast = 0, 
  74.                            int bufsize = 40, 
  75.                            const char* transtable = 0);
  76.  
  77.                      Regex(String& x, 
  78.                            int fast = 0, 
  79.                            int bufsize = 40, 
  80.                            const char* transtable = 0);
  81.  
  82.                      ~Regex();
  83.  
  84.   int                match(const char* s, int len, int pos = 0);
  85.   int                search(const char* s, int len, 
  86.                             int& matchlen, int startpos = 0);
  87.   int                match_info(int& start, int& length, int nth = 0);
  88.  
  89.   int                OK();  // representation invariant
  90. };
  91.  
  92.  
  93.  
  94. class SubString
  95. {
  96.   friend class      String;
  97.   friend class      StrTmp;
  98. protected:
  99.   String*           S;
  100.   unsigned short    pos;
  101.   unsigned short    len;
  102.  
  103.   void              assign(StrRep*, const char*, int = -1);
  104.                     SubString(String* x, int p, int l);
  105.                     SubString(const SubString& x);
  106.  
  107. public:
  108.                     ~SubString();
  109.  
  110.   void              operator =  (String&     y);
  111.   void              operator =  (SubString&  y);
  112.   void              operator =  (const char* t);
  113.   void              operator =  (char        c);
  114.   
  115.   StrTmp            operator +  (String&     y);
  116.   StrTmp            operator +  (SubString&  y);
  117.   StrTmp            operator +  (const char* t);
  118.   StrTmp            operator +  (char        c);
  119.   StrTmp            operator +  (StrTmp& y);
  120.   friend StrTmp     operator +  (const char* t,SubString&  y);
  121.   friend StrTmp     operator +  (char       c, SubString&  x);
  122.  
  123.   friend int        operator == (SubString& x, String&     y);
  124.   friend int        operator == (String&    x, SubString&  y);
  125.   friend int        operator == (SubString& x, SubString&  y);
  126.   friend int        operator == (SubString& x, const char* t);
  127.   
  128.   friend int        operator != (SubString& x, String&     y);
  129.   friend int        operator != (String&    x, SubString&  y);
  130.   friend int        operator != (SubString& x, SubString&  y);
  131.   friend int        operator != (SubString& x, const char* t);
  132.   
  133.   friend int        operator <= (SubString& x, String&     y);
  134.   friend int        operator <= (String&    x, SubString&  y);
  135.   friend int        operator <= (SubString& x, SubString&  y);
  136.   friend int        operator <= (SubString& x, const char* t);
  137.   
  138.   friend int        operator <  (SubString& x, String&     y);
  139.   friend int        operator <  (String&    x, SubString&  y);
  140.   friend int        operator <  (SubString& x, SubString&  y);
  141.   friend int        operator <  (SubString& x, const char* t);
  142.   
  143.   friend int        operator >= (SubString& x, String&     y);
  144.   friend int        operator >= (String&    x, SubString&  y);
  145.   friend int        operator >= (SubString& x, SubString&  y);
  146.   friend int        operator >= (SubString& x, const char* t);
  147.   
  148.   friend int        operator >  (SubString& x, String&     y);
  149.   friend int        operator >  (String&    x, SubString&  y);
  150.   friend int        operator >  (SubString& x, SubString&  y);
  151.   friend int        operator >  (SubString& x, const char* t);
  152.  
  153.   int               contains(char        c);
  154.   int               contains(String&     y);
  155.   int               contains(SubString&  y);
  156.   int               contains(const char* t);
  157.   int               contains(Regex&       r);
  158.  
  159.   int               matches(Regex&  r);
  160.  
  161. // misc
  162.  
  163.   int               length();
  164.   int               empty();
  165.  
  166.   friend ostream&   operator<<(ostream& s, SubString& x);
  167.  
  168.   int               OK(); 
  169.  
  170. // non-operator versions
  171.  
  172.   friend int        compare(SubString& x, String&     y);
  173.   friend int        compare(String&    x, SubString&  y);
  174.   friend int        compare(SubString& x, SubString&  y);
  175.   friend int        compare(SubString& x, const char* y);
  176.  
  177. };
  178.  
  179.  
  180. class String
  181. {
  182.   friend class      SubString;
  183.   friend class      Regex;
  184.   friend class      StrTmp;
  185.  
  186. protected:
  187.   StrRep*           rep;
  188.  
  189.   int               search(int, int, const char*, int = -1);
  190.   int               search(int, int, char);
  191.   int               match(int, int, int, const char*, int = -1);
  192.   int               _gsub(const char*, int, const char* ,int);
  193.   int               _gsub(Regex&, const char*, int);
  194.  
  195. public:
  196.  
  197. // constructors & assignment
  198.  
  199.                     String();
  200.                     String(String& x);
  201.                     String(SubString&  x);
  202.                     String(const char* t);
  203.                     String(const char* t, int len);
  204.                     String(char c);
  205.  
  206.                     ~String();
  207.  
  208.   void              operator =  (String&     y);
  209.   void              operator =  (StrTmp&     y);
  210.   void              operator =  (const char* y);
  211.   void              operator =  (char        c);
  212.   void              operator =  (SubString&  y);
  213.  
  214. // concatenation
  215.  
  216.   StrTmp            operator +  (String&     y);     
  217.   StrTmp            operator +  (const char* t);
  218.   StrTmp            operator +  (char        c);
  219.   StrTmp            operator +  (SubString&  y);     
  220.   StrTmp            operator +  (StrTmp& y);
  221.   friend StrTmp     operator +  (const char* t, String& y);
  222.   friend StrTmp     operator +  (char        c, String& x);
  223.  
  224.   void              operator += (String&     y); 
  225.   void              operator += (SubString&  y);
  226.   void              operator += (const char* t);
  227.   void              operator += (char        c);
  228.  
  229.   void              prepend(String&     y); 
  230.   void              prepend(SubString&  y);
  231.   void              prepend(const char* t);
  232.   void              prepend(char        c);
  233.  
  234.   
  235. // relational operators
  236.  
  237.   friend int        operator == (String&     x, String&     y);
  238.   friend int        operator == (String&     x, const char* t);
  239.   friend int        operator == (String&     x, SubString&  y);
  240.   friend int        operator == (SubString&  x, String&     y);
  241.   
  242.   friend int        operator != (String&     x, String&     y);
  243.   friend int        operator != (String&     x, const char* t);
  244.   friend int        operator != (String&     x, SubString&  y);
  245.   friend int        operator != (SubString&  x, String&     y);
  246.   
  247.   friend int        operator <= (String&     x, String&     y);
  248.   friend int        operator <= (String&     x, const char* t);
  249.   friend int        operator <= (String&     x, SubString&  y);
  250.   friend int        operator <= (SubString&  x, String&     y);
  251.   
  252.   friend int        operator <  (String&     x, String&     y);
  253.   friend int        operator <  (String&     x, const char* t);
  254.   friend int        operator <  (String&     x, SubString&  y);
  255.   friend int        operator <  (SubString&  x, String&     y);
  256.   
  257.   friend int        operator >= (String&     x, String&     y);
  258.   friend int        operator >= (String&     x, const char* t);
  259.   friend int        operator >= (String&     x, SubString&  y);
  260.   friend int        operator >= (SubString&  x, String&     y);
  261.   
  262.   friend int        operator >  (String&     x, String&     y);
  263.   friend int        operator >  (String&     x, const char* t);
  264.   friend int        operator >  (String&     x, SubString&  y);
  265.   friend int        operator >  (SubString&  x, String&     y);
  266.  
  267.   friend int        fcompare(String&   x, String&     y); // ignore case
  268.  
  269. // searching & matching
  270.  
  271.   int               index(char        c, int startpos = 0);      
  272.   int               index(String&     y, int startpos = 0);      
  273.   int               index(SubString&  y, int startpos = 0);      
  274.   int               index(const char* t, int startpos = 0);  
  275.   int               index(Regex&      r, int startpos = 0);       
  276.  
  277.   int               contains(char        c);
  278.   int               contains(String&     y);
  279.   int               contains(SubString&  y);
  280.   int               contains(const char* t);
  281.   int               contains(Regex&      r);
  282.  
  283.   int               contains(char        c, int pos);
  284.   int               contains(String&     y, int pos);
  285.   int               contains(SubString&  y, int pos);
  286.   int               contains(const char* t, int pos);
  287.   int               contains(Regex&      r, int pos);
  288.  
  289.   int               matches(char        c, int pos = 0);
  290.   int               matches(String&     y, int pos = 0);
  291.   int               matches(SubString&  y, int pos = 0);
  292.   int               matches(const char* t, int pos = 0);
  293.   int               matches(Regex&      r, int pos = 0);
  294.  
  295.   int               freq(char        c); // return number of c's in string
  296.   int               freq(String&     y);
  297.   int               freq(SubString&  y);
  298.   int               freq(const char* t);
  299.  
  300. // substring extraction
  301.  
  302.   SubString         at(int         pos, int len);
  303.   SubString         at(String&     x, int startpos = 0); 
  304.   SubString         at(SubString&  x, int startpos = 0); 
  305.   SubString         at(const char* t, int startpos = 0);
  306.   SubString         at(char        c, int startpos = 0);
  307.   SubString         at(Regex&      r, int startpos = 0); 
  308.  
  309.   SubString         before(int          pos);
  310.   SubString         before(String&      x, int startpos = 0);
  311.   SubString         before(SubString&   x, int startpos = 0);
  312.   SubString         before(const char*  t, int startpos = 0);
  313.   SubString         before(char         c, int startpos = 0);
  314.   SubString         before(Regex&       r, int startpos = 0);
  315.  
  316.   SubString         through(int          pos);
  317.   SubString         through(String&      x, int startpos = 0);
  318.   SubString         through(SubString&   x, int startpos = 0);
  319.   SubString         through(const char*  t, int startpos = 0);
  320.   SubString         through(char         c, int startpos = 0);
  321.   SubString         through(Regex&       r, int startpos = 0);
  322.  
  323.   SubString         from(int          pos);
  324.   SubString         from(String&      x, int startpos = 0);
  325.   SubString         from(SubString&   x, int startpos = 0);
  326.   SubString         from(const char*  t, int startpos = 0);
  327.   SubString         from(char         c, int startpos = 0);
  328.   SubString         from(Regex&       r, int startpos = 0);
  329.  
  330.   SubString         after(int         pos);
  331.   SubString         after(String&     x, int startpos = 0);
  332.   SubString         after(SubString&  x, int startpos = 0);
  333.   SubString         after(const char* t, int startpos = 0);
  334.   SubString         after(char        c, int startpos = 0);
  335.   SubString         after(Regex&      r, int startpos = 0);
  336.  
  337. // modification
  338.  
  339.   void              del(int         pos, int len);
  340.   void              del(String&     y, int startpos = 0);
  341.   void              del(SubString&  y, int startpos = 0);
  342.   void              del(const char* t, int startpos = 0);
  343.   void              del(char        c, int startpos = 0);
  344.   void              del(Regex&      r, int startpos = 0);
  345.  
  346.   int               gsub(String&     pat, String&     repl);
  347.   int               gsub(SubString&  pat, String&     repl);
  348.   int               gsub(const char* pat, String&     repl);
  349.   int               gsub(const char* pat, const char* repl);
  350.   int               gsub(Regex&      pat, String&     repl);
  351.  
  352. // friends & utilities
  353.  
  354.   friend int        split(String& x, String res[], int maxn, String& sep);
  355.   friend int        split(String& x, String res[], int maxn, Regex&  sep);
  356.  
  357.   friend StrTmp     join(String src[], int n, String& sep);
  358.  
  359.   friend StrTmp     replicate(char        c, int n);
  360.   friend StrTmp     replicate(String&     y, int n);
  361.  
  362.   friend StrTmp     common_prefix(String& x, String& y, int startpos = 0);
  363.   friend StrTmp     common_suffix(String& x, String& y, int startpos = -1);
  364.  
  365.   friend StrTmp     reverse(String& x);
  366.   friend StrTmp     upcase(String& x);
  367.   friend StrTmp     downcase(String& x);
  368.   friend StrTmp     capitalize(String& x);
  369.  
  370. // in-place versions of above
  371.  
  372.   void              reverse();
  373.   void              upcase();
  374.   void              downcase();
  375.   void              capitalize();
  376.  
  377. // conversion
  378.  
  379.   char&             operator [] (int i);
  380.  
  381.   const char*       operator char*();
  382.  
  383. // IO
  384.  
  385.   friend ostream&   operator<<(ostream& s, String& x);
  386.   friend ostream&   operator<<(ostream& s, SubString& x);
  387.   friend istream&   operator>>(istream& s, String& x);
  388.  
  389.   friend int        readline(istream& s, String& x, 
  390.                              char terminator = '\n',
  391.                              int discard_terminator = 1);
  392.  
  393. // status
  394.  
  395.   int               length();
  396.   int               empty();
  397.  
  398.   void              alloc(int newsize);
  399.  
  400.   void              error(char* msg);
  401.   int               OK();
  402.  
  403. // non-operator versions of operator functions
  404.  
  405.   friend int        compare(String&    x, String&     y);
  406.   friend int        compare(String&    x, SubString&  y);
  407.   friend int        compare(String&    x, const char* y);
  408.   friend int        compare(SubString& x, String&     y);
  409. };
  410.  
  411. class StrTmp : public String
  412. {
  413. public:
  414.                     StrTmp(StrRep* p);
  415.                     StrTmp(String& x);
  416.                     StrTmp(StrTmp& x);
  417.                     ~StrTmp();
  418.  
  419.   StrTmp            operator + (String& y); 
  420.   StrTmp            operator + (SubString& y); 
  421.   StrTmp            operator + (const char* y); 
  422.   StrTmp            operator + (char y); 
  423.   friend StrTmp     operator + (const char* x, StrTmp& y);
  424.   friend StrTmp     operator + (char x, StrTmp& y);
  425.  
  426.   friend StrTmp     reverse(StrTmp& x);
  427.   friend StrTmp     upcase(StrTmp& x);
  428.   friend StrTmp     downcase(StrTmp& x);
  429.   friend StrTmp     capitalize(StrTmp& x);
  430.  
  431. };
  432.  
  433. // some built in regular expressions
  434.  
  435. extern Regex RXwhite;          // = "[ \n\t]+"
  436. extern Regex RXint;            // = "-?[0-9]+"
  437. extern Regex RXdouble;         // = "-?\\(\\([0-9]+\\.[0-9]*\\)\\|
  438.                                //    \\([0-9]+\\)\\|\\(\\.[0-9]+\\)\\)
  439.                                //    \\([eE][---+]?[0-9]+\\)?"
  440. extern Regex RXalpha;          // = "[A-Za-z]+"
  441. extern Regex RXlowercase;      // = "[a-z]+"
  442. extern Regex RXuppercase;      // = "[A-Z]+"
  443. extern Regex RXalphanum;       // = "[0-9A-Za-z]+"
  444. extern Regex RXidentifier;     // = "[A-Za-z_][A-Za-z0-9_]*"
  445.  
  446.  
  447. //#ifdef __OPTIMIZE__
  448.  
  449.  
  450. extern StrRep  _nilStrRep;
  451. extern String _nilString;
  452.  
  453.  
  454. inline String::String()
  455.   rep = &_nilStrRep;
  456. }
  457.  
  458. inline String::String(String& x)
  459.   rep = Scopy(0, x.rep);
  460. }
  461.  
  462. inline String::String(const char* t)
  463. {
  464.   rep = Salloc(0, t, -1, -1);
  465. }
  466.  
  467. inline StrTmp::StrTmp(StrRep* r) 
  468. {
  469.   rep = r;
  470. }
  471.  
  472. inline StrTmp::StrTmp(String& x) 
  473. {
  474.   rep = x.rep; x.rep = &_nilStrRep;
  475. }
  476.  
  477. inline StrTmp::StrTmp(StrTmp& x) 
  478. {
  479.   rep = x.rep; x.rep = &_nilStrRep;
  480. }
  481.  
  482. inline String::String(const char* t, int tlen)
  483. {
  484.   rep = Salloc(0, t, tlen, tlen);
  485. }
  486.  
  487. inline String::String(SubString& y)
  488. {
  489.   rep = Salloc(0, &(y.S->rep->s[y.pos]), y.len, y.len);
  490. }
  491.  
  492. inline String::String(char c)
  493. {
  494.   rep = Salloc(0, &c, 1, 1);
  495. }
  496.  
  497. inline String::~String()
  498.   if (rep != &_nilStrRep) delete rep;
  499. }
  500.  
  501. inline StrTmp::~StrTmp() {} 
  502.  
  503. inline void String::operator =  (String& y)
  504.   rep = Scopy(rep, y.rep);
  505. }
  506.  
  507. inline void String::operator =  (StrTmp& y)
  508.   if (rep != &_nilStrRep) delete rep; 
  509.   rep = y.rep; y.rep = &_nilStrRep;
  510. }
  511.  
  512. inline void String::operator=(const char* t)
  513. {
  514.   rep = Salloc(rep, t, -1, -1); 
  515. }
  516.  
  517. inline void String::operator=(SubString&  y)
  518. {
  519.   rep = Salloc(rep, &(y.S->rep->s[y.pos]), y.len, y.len);
  520. }
  521.  
  522. inline void String::operator=(char c)
  523. {
  524.   rep = Salloc(rep, &c, 1, 1); 
  525. }
  526.  
  527. inline void String::operator +=(String& y)
  528. {
  529.   rep = Scat(rep, rep->s, rep->len, y.rep->s, y.rep->len);
  530. }
  531.  
  532. inline void String::operator +=(SubString& y)
  533. {
  534.   rep = Scat(rep, rep->s, rep->len, &(y.S->rep->s[y.pos]),y.len);
  535. }
  536.  
  537. inline void String::operator += (const char* y)
  538. {
  539.   rep = Scat(rep, rep->s, rep->len, y, -1);
  540. }
  541.  
  542. inline void String:: operator +=(char y)
  543. {
  544.   rep = Scat(rep, rep->s, rep->len, &y, 1); 
  545. }
  546.  
  547. inline StrTmp String::operator + (String& y)
  548. {
  549.   return(Scat(0, rep->s, rep->len, y.rep->s, y.rep->len));
  550. }
  551.  
  552. inline StrTmp String::operator +(SubString& y)
  553. {
  554.   return(Scat(0, rep->s, rep->len, &(y.S->rep->s[y.pos]),y.len));
  555. }
  556.  
  557. inline StrTmp String::operator + (const char* y)
  558. {
  559.   return(Scat(0, rep->s, rep->len, y, -1));
  560. }
  561.  
  562. inline StrTmp String::operator + (char y)
  563. {
  564.   return(Scat(0, rep->s, rep->len, &y, 1));
  565. }
  566.  
  567. inline StrTmp SubString::operator + (String& y)
  568. {
  569.   return(Scat(0, &(S->rep->s[pos]), len, y.rep->s, y.rep->len));
  570. }
  571.  
  572. inline StrTmp SubString::operator + (SubString& y)
  573. {
  574.   return(Scat(0, &(S->rep->s[pos]), len, &(y.S->rep->s[y.pos]), y.len));
  575. }
  576.  
  577. inline StrTmp SubString::operator + (const char* y)
  578. {
  579.   return(Scat(0, &(S->rep->s[pos]), len, y, -1));
  580. }
  581.  
  582. inline StrTmp SubString::operator + (char y)
  583. {
  584.   return(Scat(0, &(S->rep->s[pos]), len, &y, 1));
  585. }
  586.  
  587. inline StrTmp operator +(const char* t, String& y)
  588. {
  589.   return(Scat(0, t, -1, y.rep->s, y.rep->len));
  590. }
  591.  
  592. inline StrTmp operator + (const char* t, SubString& y)
  593. {
  594.   return(Scat(0, t, -1, &(y.S->rep->s[y.pos]), y.len));
  595. }
  596.  
  597. inline StrTmp operator + (char c, String& y)
  598. {
  599.   return(Scat(0, &c, 1, y.rep->s, y.rep->len));
  600. }
  601.  
  602. inline StrTmp operator + (char c, SubString& y)
  603. {
  604.   return(Scat(0, &c, 1, &(y.S->rep->s[y.pos]), y.len));
  605. }
  606.  
  607. inline StrTmp StrTmp::operator + (String& y)
  608. {
  609.   rep = Scat(rep, rep->s, rep->len, y.rep->s, y.rep->len); return *this;
  610. }
  611.  
  612. inline StrTmp StrTmp::operator + (SubString& y)
  613. {
  614.   rep = Scat(rep, rep->s, rep->len, &(y.S->rep->s[y.pos]),y.len); return *this;
  615. }
  616.  
  617. inline StrTmp StrTmp::operator + (const char* y)
  618. {
  619.   rep = Scat(rep, rep->s, rep->len, y, -1); return *this;
  620. }
  621.  
  622. inline StrTmp StrTmp::operator + (char y)
  623. {
  624.   rep = Scat(rep, rep->s, rep->len, &y, 1); return *this;
  625. }
  626.  
  627. inline StrTmp String::operator + (StrTmp& y)
  628. {
  629.   y.rep = Sprepend(y.rep, rep->s, rep->len); return y;
  630. }
  631.  
  632. inline StrTmp SubString::operator + (StrTmp& y)
  633. {
  634.   y.rep = Sprepend(y.rep, &(S->rep->s[pos]), len); return y;
  635. }
  636.  
  637. inline StrTmp operator + (const char* x, StrTmp& y)
  638. {
  639.   y.rep = Sprepend(y.rep, x, -1); return y;
  640. }
  641.  
  642. inline StrTmp operator + (char x, StrTmp& y)
  643. {
  644.   y.rep = Sprepend(y.rep, &x, 1); return y;
  645. }
  646.  
  647. inline void String::prepend(String& y)
  648. {
  649.   rep = Sprepend(rep, y.rep->s, y.rep->len);
  650. }
  651.  
  652. inline void String::prepend(const char* y)
  653. {
  654.   rep = Sprepend(rep, y, -1); 
  655. }
  656.  
  657. inline void String::prepend(char y)
  658. {
  659.   rep = Sprepend(rep, &y, 1); 
  660. }
  661.  
  662. inline void String::prepend(SubString& y)
  663. {
  664.   rep = Sprepend(rep, &(y.S->rep->s[y.pos]), y.len);
  665. }
  666.  
  667. inline StrTmp reverse(String& x)
  668. {
  669.   return(Sreverse(x.rep, 0));
  670. }
  671.  
  672. inline StrTmp reverse(StrTmp& x)
  673. {
  674.   x.rep = Sreverse(x.rep, x.rep); return x;
  675. }
  676.  
  677. inline void String::reverse()
  678. {
  679.   rep = Sreverse(rep, rep);
  680. }
  681.  
  682. inline StrTmp upcase(String& x)
  683. {
  684.   return(Supcase(x.rep, 0));
  685. }
  686.  
  687. inline StrTmp upcase(StrTmp& x)
  688. {
  689.   x.rep = Supcase(x.rep, x.rep); return x;
  690. }
  691.  
  692. inline void String::upcase()
  693. {
  694.   rep = Supcase(rep, rep);
  695. }
  696.  
  697. inline StrTmp downcase(String& x)
  698. {
  699.   return(Sdowncase(x.rep, 0));
  700. }
  701.  
  702. inline StrTmp downcase(StrTmp& x)
  703. {
  704.   x.rep = Sdowncase(x.rep, x.rep); return x;
  705. }
  706.  
  707. inline void String::downcase()
  708. {
  709.   rep = Sdowncase(rep, rep);
  710. }
  711.  
  712. inline StrTmp capitalize(String& x)
  713. {
  714.   return(Scapitalize(x.rep, 0));
  715. }
  716.  
  717. inline StrTmp capitalize(StrTmp& x)
  718. {
  719.   x.rep = Scapitalize(x.rep, x.rep); return x;
  720. }
  721.  
  722. inline void String::capitalize()
  723. {
  724.   rep = Scapitalize(rep, rep);
  725. }
  726.  
  727. inline void String::alloc(int newsize)
  728. {
  729.   rep = Sresize(rep, newsize);
  730. }
  731.  
  732. inline SubString::SubString(const SubString& x)
  733.   S = x.S; pos = x.pos;   len = x.len; 
  734. }
  735.  
  736. inline SubString::SubString(String* x, int first, int l)
  737. {
  738.   if (first < 0 || (unsigned)(first + l) > x->rep->len)
  739.   {
  740.     S = &_nilString; pos = len = 0;
  741.   }
  742.   else
  743.   {
  744.     S = x; pos = first; len = l;
  745.   }
  746. }
  747.  
  748. inline SubString::~SubString() {}
  749.  
  750. inline void SubString::operator = (const char* ys)
  751. {
  752.   assign(0, ys);
  753. }
  754.  
  755. inline void SubString::operator = (char ch)
  756. {
  757.   assign(0, &ch, 1);
  758. }
  759.  
  760. inline void SubString::operator = (String& y)
  761. {
  762.   assign(y.rep, y.rep->s, y.rep->len);
  763. }
  764.  
  765. inline void SubString::operator = (SubString& y)
  766. {
  767.   assign(y.S->rep, &(y.S->rep->s[y.pos]), y.len);
  768. }
  769.  
  770. inline int String::length()
  771.   return rep->len;
  772. }
  773.  
  774. inline int String::empty()
  775.   return rep->len == 0;
  776. }
  777.  
  778. inline char&  String::operator [] (int i) 
  779.   if (((unsigned)i) >= rep->len) error("invalid index");
  780.   return rep->s[i];
  781. }
  782.  
  783. inline int String::index(char c, int startpos = 0)
  784. {
  785.   return search(startpos, rep->len, c);
  786. }
  787.  
  788. inline int String::index(const char* t, int startpos = 0)
  789. {   
  790.   return search(startpos, rep->len, t);
  791. }
  792.  
  793. inline int String::index(String& y, int startpos = 0)
  794. {   
  795.   return search(startpos, rep->len, y.rep->s, y.rep->len);
  796. }
  797.  
  798. inline int String::index(SubString& y, int startpos = 0)
  799. {   
  800.   return search(startpos, rep->len, &(y.S->rep->s[y.pos]), y.len);
  801. }
  802.  
  803. inline int String::contains(char c)
  804. {
  805.   return search(0, rep->len, c) >= 0;
  806. }
  807.  
  808. inline int SubString::contains(char c)
  809. {
  810.   return S->search(pos, pos+len, 0, c) >= 0;
  811. }
  812.  
  813. inline int String::contains(const char* t)
  814. {   
  815.   return search(0, rep->len, t) >= 0;
  816. }
  817.  
  818. inline int String::contains(String& y)
  819. {   
  820.   return search(0, rep->len, y.rep->s, y.rep->len) >= 0;
  821. }
  822.  
  823. inline int String::contains(SubString& y)
  824. {   
  825.   return search(0, rep->len, &(y.S->rep->s[y.pos]), y.len) >= 0;
  826. }
  827.  
  828. inline int SubString::contains(const char* t)
  829. {   
  830.   return S->search(pos, pos+len, t) >= 0;
  831. }
  832.  
  833. inline int SubString::contains(String& y)
  834. {   
  835.   return S->search(pos, pos+len, y.rep->s, y.rep->len) >= 0;
  836. }
  837.  
  838. inline int SubString::contains(SubString&  y)
  839. {   
  840.   return S->search(pos, pos+len, &(y.S->rep->s[y.pos]), y.len) >= 0;
  841. }
  842.  
  843. inline int String::contains(char c, int p)
  844. {
  845.   return match(p, rep->len, 0, &c, 1);
  846. }
  847.  
  848. inline int String::matches(char c, int p = 0)
  849. {
  850.   return match(p, rep->len, 1, &c, 1);
  851. }
  852.  
  853. inline int String::contains(const char* t, int p)
  854. {
  855.   return match(p, rep->len, 0, t);
  856. }
  857.  
  858. inline int String::matches(const char* t, int p = 0)
  859. {
  860.   return match(p, rep->len, 1, t);
  861. }
  862.  
  863. inline int String::contains(String& y, int p)
  864. {
  865.   return match(p, rep->len, 0, y.rep->s, y.rep->len);
  866. }
  867.  
  868. inline int String::matches(String& y, int p = 0)
  869. {
  870.   return match(p, rep->len, 1, y.rep->s, y.rep->len);
  871. }
  872.  
  873. inline int String::contains(SubString& y, int p)
  874. {
  875.   return match(p, rep->len, 0, &(y.S->rep->s[y.pos]), y.len);
  876. }
  877.  
  878. inline int String::matches(SubString& y, int p = 0)
  879. {
  880.   return match(p, rep->len, 1, &(y.S->rep->s[y.pos]), y.len);
  881. }
  882.  
  883. inline int String::contains(Regex& r)
  884. {
  885.   int unused;  return r.search(rep->s, rep->len, unused, 0) >= 0;
  886. }
  887.  
  888. inline int SubString::contains(Regex& r)
  889. {
  890.   int unused;  return r.search(&(S->rep->s[pos]), len, unused, 0) >= 0;
  891. }
  892.  
  893. inline int String::contains(Regex& r, int p)
  894. {
  895.   return r.match(rep->s, rep->len, p) >= 0;
  896. }
  897.  
  898. inline int String::matches(Regex& r, int p = 0)
  899. {
  900.   int l = (p < 0)? -p : rep->len - p;
  901.   return r.match(rep->s, rep->len, p) == l;
  902. }
  903.  
  904. inline int SubString::matches(Regex& r)
  905. {
  906.   return r.match(&(S->rep->s[pos]), len, 0) == len;
  907. }
  908.  
  909. inline const char* String::operator char*()
  910.   return rep->s;
  911. }
  912.  
  913. inline int String::index(Regex& r, int startpos = 0)
  914. {
  915.   int unused;  return r.search(rep->s, rep->len, unused, startpos);
  916. }
  917.  
  918.  
  919. inline  int SubString::length()
  920.   return len;
  921. }
  922.  
  923. inline  int SubString::empty()
  924.   return len == 0;
  925. }
  926.  
  927. inline  ostream& operator<<(ostream& s, String& x)
  928.   s.put(x.rep->s); return s;
  929. }
  930.  
  931. inline int operator==(String& x, String& y) 
  932. {
  933.   return compare(x, y) == 0; 
  934. }
  935.  
  936. inline int operator!=(String& x, String& y)
  937. {
  938.   return compare(x, y) != 0; 
  939. }
  940.  
  941. inline int operator>(String& x, String& y)
  942. {
  943.   return compare(x, y) > 0; 
  944. }
  945.  
  946. inline int operator>=(String& x, String& y)
  947. {
  948.   return compare(x, y) >= 0; 
  949. }
  950.  
  951. inline int operator<(String& x, String& y)
  952. {
  953.   return compare(x, y) < 0; 
  954. }
  955.  
  956. inline int operator<=(String& x, String& y)
  957. {
  958.   return compare(x, y) <= 0; 
  959. }
  960.  
  961. inline int operator==(String& x, SubString&  y) 
  962. {
  963.   return compare(x, y) == 0; 
  964. }
  965.  
  966. inline int operator!=(String& x, SubString&  y)
  967. {
  968.   return compare(x, y) != 0; 
  969. }
  970.  
  971. inline int operator>(String& x, SubString&  y)      
  972. {
  973.   return compare(x, y) > 0; 
  974. }
  975.  
  976. inline int operator>=(String& x, SubString&  y)
  977. {
  978.   return compare(x, y) >= 0; 
  979. }
  980.  
  981. inline int operator<(String& x, SubString&  y) 
  982. {
  983.   return compare(x, y) < 0; 
  984. }
  985.  
  986. inline int operator<=(String& x, SubString&  y)
  987. {
  988.   return compare(x, y) <= 0; 
  989. }
  990.  
  991. inline int operator==(String& x, const char* t) 
  992. {
  993.   return compare(x, t) == 0; 
  994. }
  995.  
  996. inline int operator!=(String& x, const char* t) 
  997. {
  998.   return compare(x, t) != 0; 
  999. }
  1000.  
  1001. inline int operator>(String& x, const char* t)  
  1002. {
  1003.   return compare(x, t) > 0; 
  1004. }
  1005.  
  1006. inline int operator>=(String& x, const char* t) 
  1007. {
  1008.   return compare(x, t) >= 0; 
  1009. }
  1010.  
  1011. inline int operator<(String& x, const char* t)  
  1012. {
  1013.   return compare(x, t) < 0; 
  1014. }
  1015.  
  1016. inline int operator<=(String& x, const char* t) 
  1017. {
  1018.   return compare(x, t) <= 0; 
  1019. }
  1020.  
  1021. inline int operator==(SubString& x, String& y) 
  1022. {
  1023.   return compare(y, x) == 0; 
  1024. }
  1025.  
  1026. inline int operator!=(SubString& x, String& y)
  1027. {
  1028.   return compare(y, x) != 0;
  1029. }
  1030.  
  1031. inline int operator>(SubString& x, String& y)      
  1032. {
  1033.   return compare(y, x) < 0;
  1034. }
  1035.  
  1036. inline int operator>=(SubString& x, String& y)     
  1037. {
  1038.   return compare(y, x) <= 0;
  1039. }
  1040.  
  1041. inline int operator<(SubString& x, String& y)      
  1042. {
  1043.   return compare(y, x) > 0;
  1044. }
  1045.  
  1046. inline int operator<=(SubString& x, String& y)     
  1047. {
  1048.   return compare(y, x) >= 0;
  1049. }
  1050.  
  1051. inline int operator==(SubString& x, SubString&  y) 
  1052. {
  1053.   return compare(x, y) == 0; 
  1054. }
  1055.  
  1056. inline int operator!=(SubString& x, SubString&  y)
  1057. {
  1058.   return compare(x, y) != 0;
  1059. }
  1060.  
  1061. inline int operator>(SubString& x, SubString&  y)      
  1062. {
  1063.   return compare(x, y) > 0;
  1064. }
  1065.  
  1066. inline int operator>=(SubString& x, SubString&  y)
  1067. {
  1068.   return compare(x, y) >= 0;
  1069. }
  1070.  
  1071. inline int operator<(SubString& x, SubString&  y) 
  1072. {
  1073.   return compare(x, y) < 0;
  1074. }
  1075.  
  1076. inline int operator<=(SubString& x, SubString&  y)
  1077. {
  1078.   return compare(x, y) <= 0;
  1079. }
  1080.  
  1081. inline int operator==(SubString& x, const char* t) 
  1082. {
  1083.   return compare(x, t) == 0; 
  1084. }
  1085.  
  1086. inline int operator!=(SubString& x, const char* t) 
  1087. {
  1088.   return compare(x, t) != 0;
  1089. }
  1090.  
  1091. inline int operator>(SubString& x, const char* t)  
  1092. {
  1093.   return compare(x, t) > 0; 
  1094. }
  1095.  
  1096. inline int operator>=(SubString& x, const char* t) 
  1097. {
  1098.   return compare(x, t) >= 0; 
  1099. }
  1100.  
  1101. inline int operator<(SubString& x, const char* t)  
  1102. {
  1103.   return compare(x, t) < 0; 
  1104. }
  1105.  
  1106. inline int operator<=(SubString& x, const char* t) 
  1107. {
  1108.   return compare(x, t) <= 0; 
  1109. }
  1110.  
  1111.  
  1112. inline SubString String::at(int first, int len)
  1113. {
  1114.   return SubString(this, first, len);
  1115. }
  1116.  
  1117. inline SubString String::before(int pos)
  1118. {
  1119.   return SubString(this, 0, pos);
  1120. }
  1121.  
  1122. inline SubString String::through(int pos)
  1123. {
  1124.   return SubString(this, 0, pos+1);
  1125. }
  1126.  
  1127. inline SubString String::after(int pos)
  1128. {
  1129.   return SubString(this, pos + 1, rep->len - (pos + 1));
  1130. }
  1131.  
  1132. inline SubString String::from(int pos)
  1133. {
  1134.   return SubString(this, pos, rep->len - pos);
  1135. }
  1136.  
  1137. inline int String::gsub(String& pat, String& r)
  1138. {
  1139.   return _gsub(pat.rep->s, pat.rep->len, r.rep->s, r.rep->len);
  1140. }
  1141.  
  1142. inline int String::gsub(SubString&  pat, String& r)
  1143. {
  1144.   return _gsub(&(pat.S->rep->s[pat.pos]), pat.len, r.rep->s, r.rep->len);
  1145. }
  1146.  
  1147. inline int String::gsub(Regex& pat, String& r)
  1148. {
  1149.   return _gsub(pat, r.rep->s, r.rep->len);
  1150. }
  1151.  
  1152. inline int String::gsub(const char* pat, String& r)
  1153. {
  1154.   return _gsub(pat, -1, r.rep->s, r.rep->len);
  1155. }
  1156.  
  1157. inline int String::gsub(const char* pat, const char* r)
  1158. {
  1159.   return _gsub(pat, -1, r, -1);
  1160. }
  1161.  
  1162. inline void String::del(String& y, int startpos = 0)
  1163. {
  1164.   del(search(startpos, rep->len, y.rep->s, y.rep->len), y.rep->len);
  1165. }
  1166.  
  1167. inline void String::del(SubString& y, int startpos = 0)
  1168. {
  1169.   del(search(startpos, rep->len, &(y.S->rep->s[y.pos]), y.len), y.len);
  1170. }
  1171.  
  1172. inline void String::del(char c, int startpos = 0)
  1173. {
  1174.   del(search(startpos, rep->len, c), 1);
  1175. }
  1176.  
  1177.  
  1178. inline Regex::Regex(String& x, int fast = 0, int bufsize = 40, 
  1179.              const char* transtable = 0)
  1180. {
  1181.   initialize(x.rep->s, x.rep->len, fast, bufsize, transtable);
  1182. }
  1183.  
  1184. inline Regex::Regex(const char* t, int fast = 0, int bufsize = 40, 
  1185.              const char* transtable = 0)
  1186. {
  1187.   initialize(t, -1, fast, bufsize, transtable);
  1188. }
  1189.  
  1190.   
  1191.  
  1192. //#endif
  1193.  
  1194. #endif
  1195.