Changeset 798 in webkit for trunk/JavaScriptCore/kjs/regexp.cpp


Ignore:
Timestamp:
Mar 21, 2002, 4:31:57 PM (23 years ago)
Author:
mjs
Message:

Merged changes from LABYRINTH_KDE_3_MERGE branch.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/JavaScriptCore/kjs/regexp.cpp

    r6 r798  
     1// -*- c-basic-offset: 2 -*-
    12/*
    23 *  This file is part of the KDE libraries
     
    1617 *  License along with this library; if not, write to the Free Software
    1718 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
     19 *
     20 *  $Id$
    1821 */
    1922
    2023#include <stdio.h>
     24#include <stdlib.h>
     25#include <string.h>
    2126
    22 #include "object.h"
    2327#include "regexp.h"
    2428
     
    2630
    2731RegExp::RegExp(const UString &p, int f)
    28  : pattern(p), flags(f)
     32  : pattern(p), flags(f)
    2933{
    30 #ifdef REG_EXTENDED   
    31   regcomp(&preg, p.ascii(), REG_EXTENDED);
    32 #else
    33   regcomp(&preg, p.ascii(), 0); 
    34 #endif 
    35   /* TODO use flags, check for errors */
     34
     35#ifdef HAVE_PCREPOSIX
     36  int pcreflags = 0;
     37  const char *perrormsg;
     38  int errorOffset;
     39
     40  if (flags & IgnoreCase)
     41    pcreflags |= PCRE_CASELESS;
     42
     43  if (flags & Multiline)
     44    pcreflags |= PCRE_MULTILINE;
     45
     46  pcregex = pcre_compile(p.ascii(), pcreflags,
     47                         &perrormsg, &errorOffset, NULL);
     48
     49#ifdef PCRE_INFO_CAPTURECOUNT
     50  // Get number of subpatterns that will be returned
     51  int rc = pcre_fullinfo( pcregex, NULL, PCRE_INFO_CAPTURECOUNT, &nrSubPatterns);
     52  if (rc != 0)
     53#endif
     54    nrSubPatterns = 0; // fallback. We always need the first pair of offsets.
     55
     56#else /* HAVE_PCREPOSIX */
     57
     58  nrSubPatterns = 0; // not implemented with POSIX regex.
     59  int regflags = 0;
     60#ifdef REG_EXTENDED
     61  regflags |= REG_EXTENDED;
     62#endif
     63#ifdef REG_ICASE
     64  if ( f & IgnoreCase )
     65    regflags |= REG_ICASE;
     66#endif
     67
     68  //NOTE: Multiline is not feasible with POSIX regex.
     69  //if ( f & Multiline )
     70  //    ;
     71  // Note: the Global flag is already handled by RegExpProtoFunc::execute
     72
     73  regcomp(&preg, p.ascii(), regflags);
     74  /* TODO check for errors */
     75#endif
     76
    3677}
    3778
    3879RegExp::~RegExp()
    3980{
     81#ifdef HAVE_PCREPOSIX
     82  pcre_free(pcregex);
     83
     84#else
    4085  /* TODO: is this really okay after an error ? */
    4186  regfree(&preg);
     87#endif
    4288}
    4389
    44 UString RegExp::match(const UString &s, int i, int *pos)
     90UString RegExp::match(const UString &s, int i, int *pos, int **ovector)
    4591{
     92
     93#ifdef HAVE_PCREPOSIX
     94  CString buffer(s.cstring());
     95  int ovecsize = (nrSubPatterns+1)*3; // see pcre docu
     96  if (ovector) *ovector = new int[ovecsize];
     97
     98  if (i < 0)
     99    i = 0;
     100
     101  if (i > s.size() || s.isNull() ||
     102      pcre_exec(pcregex, NULL, buffer.c_str(), buffer.size(), i,
     103                0, ovector ? *ovector : 0L, ovecsize) == PCRE_ERROR_NOMATCH) {
     104
     105    if (pos)
     106       *pos = -1;
     107    return UString::null;
     108  }
     109
     110  if (!ovector)
     111    return UString::null; // don't rely on the return value if you pass ovector==0
     112  if (pos)
     113     *pos = (*ovector)[0];
     114  return s.substr((*ovector)[0], (*ovector)[1] - (*ovector)[0]);
     115
     116#else
    46117  regmatch_t rmatch[10];
    47118
     
    49120    i = 0;
    50121
     122  char *str = strdup(s.ascii());
    51123  if (i > s.size() || s.isNull() ||
    52       regexec(&preg, s.ascii() + i, 10, rmatch, 0)) {
     124      regexec(&preg, str + i, 10, rmatch, 0)) {
    53125    if (pos)
    54       *pos = -1;
     126       *pos = -1;
    55127    return UString::null;
    56128  }
     129  free(str);
    57130
    58131  if (pos)
    59     *pos = rmatch[0].rm_so + i;
     132     *pos = rmatch[0].rm_so + i;
     133  // TODO copy from rmatch to ovector
    60134  return s.substr(rmatch[0].rm_so + i, rmatch[0].rm_eo - rmatch[0].rm_so);
     135#endif
    61136}
    62137
     138#if 0 // unused
    63139bool RegExp::test(const UString &s, int)
    64140{
    65   int r = regexec(&preg, s.ascii(), 0, 0, 0);
     141#ifdef HAVE_PCREPOSIX
     142  int ovector[300];
     143  CString buffer(s.cstring());
     144
     145  if (s.isNull() ||
     146      pcre_exec(pcregex, NULL, buffer.c_str(), buffer.size(), 0,
     147                0, ovector, 300) == PCRE_ERROR_NOMATCH)
     148    return false;
     149  else
     150    return true;
     151
     152#else
     153
     154  char *str = strdup(s.ascii());
     155  int r = regexec(&preg, str, 0, 0, 0);
     156  free(str);
    66157
    67158  return r == 0;
     159#endif
    68160}
     161#endif
Note: See TracChangeset for help on using the changeset viewer.