home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 7 / FreshFishVol7.bin / bbs / gnu / libg++-2.6-fsf.lha / libg++-2.6 / libg++ / src / Regex.cc < prev    next >
C/C++ Source or Header  |  1993-07-28  |  3KB  |  136 lines

  1. /* 
  2. Copyright (C) 1988 Free Software Foundation
  3.     written by Doug Lea (dl@rocky.oswego.edu)
  4.  
  5. This file is part of the GNU C++ Library.  This library is free
  6. software; you can redistribute it and/or modify it under the terms of
  7. the GNU Library General Public License as published by the Free
  8. Software Foundation; either version 2 of the License, or (at your
  9. option) any later version.  This library is distributed in the hope
  10. that it will be useful, but WITHOUT ANY WARRANTY; without even the
  11. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  12. PURPOSE.  See the GNU Library General Public License for more details.
  13. You should have received a copy of the GNU Library General Public
  14. License along with this library; if not, write to the Free Software
  15. Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  16. */
  17.  
  18. /* 
  19.   Regex class implementation
  20.  */
  21.  
  22. #ifdef __GNUG__
  23. #pragma implementation
  24. #endif
  25. #include <std.h>
  26. #include <ctype.h>
  27. #include <new.h>
  28. #include <builtin.h>
  29.  
  30. extern "C" {
  31. #include <regex.h>
  32. }
  33.  
  34. #include <Regex.h>
  35.  
  36. Regex::~Regex()
  37. {
  38.   if (buf->buffer) free(buf->buffer);
  39.   if (buf->fastmap) free(buf->fastmap);
  40.   delete(buf);
  41.   delete(reg);
  42. }
  43.  
  44. Regex::Regex(const char* t, int fast, int bufsize, 
  45.                const char* transtable)
  46. {
  47.   int tlen = (t == 0)? 0 : strlen(t);
  48.   buf = new re_pattern_buffer;
  49.   reg = new re_registers;
  50.   if (fast)
  51.     buf->fastmap = (char*)malloc(256);
  52.   else
  53.     buf->fastmap = 0;
  54.   buf->translate = (char*)transtable;
  55.   if (tlen > bufsize)
  56.     bufsize = tlen;
  57.   buf->allocated = bufsize;
  58.   buf->buffer = (char *)malloc(buf->allocated);
  59.   char* msg = re_compile_pattern((const char*)t, tlen, buf);
  60.   if (msg != 0)
  61.     (*lib_error_handler)("Regex", msg);
  62.   else if (fast)
  63.     re_compile_fastmap(buf);
  64. }
  65.  
  66. int Regex::match_info(int& start, int& length, int nth) const
  67. {
  68.   if ((unsigned)(nth) >= RE_NREGS)
  69.     return 0;
  70.   else
  71.   {
  72.     start = reg->start[nth];
  73.     length = reg->end[nth] - start;
  74.     return start >= 0 && length >= 0;
  75.   }
  76. }
  77.  
  78. int Regex::search(const char* s, int len, int& matchlen, int startpos) const
  79. {
  80.   int matchpos, pos, range;
  81.   if (startpos >= 0)
  82.   {
  83.     pos = startpos;
  84.     range = len - startpos;
  85.   }
  86.   else
  87.   {
  88.     pos = len + startpos;
  89.     range = -pos;
  90.   }
  91.   matchpos = re_search_2(buf, 0, 0, (char*)s, len, pos, range, reg, len);
  92.   if (matchpos >= 0)
  93.     matchlen = reg->end[0] - reg->start[0];
  94.   else
  95.     matchlen = 0;
  96.   return matchpos;
  97. }
  98.  
  99. int Regex::match(const char*s, int len, int p) const
  100. {
  101.   if (p < 0)
  102.   {
  103.     p += len;
  104.     if (p > len)
  105.       return -1;
  106.     return re_match_2(buf, 0, 0, (char*)s, p, 0, reg, p);
  107.   }
  108.   else if (p > len)
  109.     return -1;
  110.   else
  111.     return re_match_2(buf, 0, 0, (char*)s, len, p, reg, len);
  112. }
  113.  
  114. int Regex::OK() const
  115. {
  116. // can't verify much, since we've lost the original string
  117.   int v = buf != 0;             // have a regex buf
  118.   v &= buf->buffer != 0;        // with a pat
  119.   if (!v) (*lib_error_handler)("Regex", "invariant failure");
  120.   return v;
  121. }
  122.  
  123. /*
  124.  some built-in Regular expressions
  125. */
  126.  
  127. const Regex RXwhite("[ \n\t\r\v\f]+", 1);
  128. const Regex RXint("-?[0-9]+", 1);
  129. const Regex RXdouble("-?\\(\\([0-9]+\\.[0-9]*\\)\\|\\([0-9]+\\)\\|\\(\\.[0-9]+\\)\\)\\([eE][---+]?[0-9]+\\)?", 1, 200);
  130. const Regex RXalpha("[A-Za-z]+", 1);
  131. const Regex RXlowercase("[a-z]+", 1);
  132. const Regex RXuppercase("[A-Z]+", 1);
  133. const Regex RXalphanum("[0-9A-Za-z]+", 1);
  134. const Regex RXidentifier("[A-Za-z_][A-Za-z0-9_]*", 1);
  135.  
  136.