home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / C++ / Snippets / GNU String⁄Regex / Regex.cc < prev    next >
Encoding:
C/C++ Source or Header  |  1995-07-06  |  3.4 KB  |  137 lines  |  [TEXT/CWIE]

  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 "values.h"
  28. #include <new.h>
  29. #include "builtin.h"
  30.  
  31. // extern "C" {
  32. #include "regexlc.h"
  33. // }
  34.  
  35. #include "Regex.h"
  36.  
  37. Regex::~Regex()
  38. {
  39.   if (buf->buffer) free(buf->buffer);
  40.   if (buf->fastmap) free(buf->fastmap);
  41.   delete(buf);
  42.   delete(reg);
  43. }
  44.  
  45. Regex::Regex(const char* t, int fast, int bufsize, 
  46.                const char* transtable)
  47. {
  48.   int tlen = (t == 0)? 0 : strlen(t);
  49.   buf = new re_pattern_buffer;
  50.   reg = new re_registers;
  51.   if (fast)
  52.     buf->fastmap = (char*)malloc(256);
  53.   else
  54.     buf->fastmap = 0;
  55.   buf->translate = (char*)transtable;
  56.   if (tlen > bufsize)
  57.     bufsize = tlen;
  58.   buf->allocated = bufsize;
  59.   buf->buffer = (char *)malloc(buf->allocated);
  60.   char* msg = re_compile_pattern((const char*)t, tlen, buf);
  61.   if (msg != 0)
  62.     (*lib_error_handler)("Regex", msg);
  63.   else if (fast)
  64.     re_compile_fastmap(buf);
  65. }
  66.  
  67. int Regex::match_info(int& start, int& length, int nth) const
  68. {
  69.   if ((unsigned)(nth) >= RE_NREGS)
  70.     return 0;
  71.   else
  72.   {
  73.     start = reg->start[nth];
  74.     length = reg->end[nth] - start;
  75.     return start >= 0 && length >= 0;
  76.   }
  77. }
  78.  
  79. int Regex::search(const char* s, int len, int& matchlen, int startpos) const
  80. {
  81.   int matchpos, pos, range;
  82.   if (startpos >= 0)
  83.   {
  84.     pos = startpos;
  85.     range = len - startpos;
  86.   }
  87.   else
  88.   {
  89.     pos = len + startpos;
  90.     range = -pos;
  91.   }
  92.   matchpos = re_search_2(buf, 0, 0, (char*)s, len, pos, range, reg, len);
  93.   if (matchpos >= 0)
  94.     matchlen = reg->end[0] - reg->start[0];
  95.   else
  96.     matchlen = 0;
  97.   return matchpos;
  98. }
  99.  
  100. int Regex::match(const char*s, int len, int p) const
  101. {
  102.   if (p < 0)
  103.   {
  104.     p += len;
  105.     if (p > len)
  106.       return -1;
  107.     return re_match_2(buf, 0, 0, (char*)s, p, 0, reg, p);
  108.   }
  109.   else if (p > len)
  110.     return -1;
  111.   else
  112.     return re_match_2(buf, 0, 0, (char*)s, len, p, reg, len);
  113. }
  114.  
  115. int Regex::OK() const
  116. {
  117. // can't verify much, since we've lost the original string
  118.   int v = buf != 0;             // have a regex buf
  119.   v &= buf->buffer != 0;        // with a pat
  120.   if (!v) (*lib_error_handler)("Regex", "invariant failure");
  121.   return v;
  122. }
  123.  
  124. /*
  125.  some built-in Regular expressions
  126. */
  127.  
  128. const Regex RXwhite("[ \n\t\r\v\f]+", 1);
  129. const Regex RXint("-?[0-9]+", 1);
  130. const Regex RXdouble("-?\\(\\([0-9]+\\.[0-9]*\\)\\|\\([0-9]+\\)\\|\\(\\.[0-9]+\\)\\)\\([eE][---+]?[0-9]+\\)?", 1, 200);
  131. const Regex RXalpha("[A-Za-z]+", 1);
  132. const Regex RXlowercase("[a-z]+", 1);
  133. const Regex RXuppercase("[A-Z]+", 1);
  134. const Regex RXalphanum("[0-9A-Za-z]+", 1);
  135. const Regex RXidentifier("[A-Za-z_][A-Za-z0-9_]*", 1);
  136.  
  137.