## modules/up/src/gnug++/Regex.cc

/* [<][>][^][v][top][bottom][index][help] */

## FUNCTIONS

This source file includes following functions.

1. Regex
2. match_info
3. search
4. match
5. OK

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

/* [<][>][^][v][top][bottom][index][help] */