~ubuntu-branches/ubuntu/dapper/pantomime/dapper

« back to all changes in this revision

Viewing changes to Source/NSRegEx.m

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2004-11-20 00:25:28 UTC
  • mfrom: (2.1.1 warty)
  • Revision ID: james.westby@ubuntu.com-20041120002528-igjuwyao6gubpig1
Tags: 1.1.2-3
Build depend / depend on libgnustep-base-1.10-dev.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
**  NSRegEx.m
 
3
**
 
4
**  Copyright (c) 2001, 2002
 
5
**
 
6
**  Author: Francis Lachapelle <francis@Sophos.ca>
 
7
**          Ludovic Marcotte <ludovic@Sophos.ca>
 
8
**
 
9
**  This library is free software; you can redistribute it and/or
 
10
**  modify it under the terms of the GNU Lesser General Public
 
11
**  License as published by the Free Software Foundation; either
 
12
**  version 2.1 of the License, or (at your option) any later version.
 
13
**  
 
14
**  This library is distributed in the hope that it will be useful,
 
15
**  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
16
**  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 
17
**  Lesser General Public License for more details.
 
18
**  
 
19
**  You should have received a copy of the GNU Lesser General Public
 
20
**  License along with this library; if not, write to the Free Software
 
21
**  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 
22
*/
 
23
 
 
24
#include <Pantomime/NSRegEx.h>
 
25
 
 
26
#include <Pantomime/Constants.h>
 
27
 
 
28
#import <Foundation/NSArray.h>
 
29
#import <Foundation/NSDebug.h>
 
30
#import <Foundation/NSValue.h>
 
31
 
 
32
#include <stdlib.h>
 
33
 
 
34
@implementation NSRegEx 
 
35
 
 
36
- (id) init
 
37
{
 
38
  return [self initWithPattern:@""];
 
39
}
 
40
 
 
41
- (id) initWithPattern: (NSString *) thePattern
 
42
{
 
43
  return [self initWithPattern:thePattern flags:REG_EXTENDED];
 
44
}
 
45
 
 
46
- (id) initWithPattern: (NSString *) thePattern
 
47
                 flags: (int) theFlags
 
48
{
 
49
  int status;
 
50
  char *error;
 
51
  
 
52
  if ((self = [super init]))
 
53
    {
 
54
      status = regcomp(&re, [thePattern cString], theFlags);
 
55
      if (status != 0)
 
56
        {
 
57
          error = malloc(255*sizeof(char));
 
58
          regerror(status, &re, error, 255);
 
59
          NSDebugLog(@"error: %s\n", error);
 
60
          free(error);
 
61
          
 
62
          [super dealloc];
 
63
          self = nil;
 
64
        }
 
65
    }
 
66
 
 
67
  return self;
 
68
}
 
69
 
 
70
+ (id) regexWithPattern: (NSString *) thePattern
 
71
{
 
72
  return AUTORELEASE([[self alloc] initWithPattern:thePattern]);
 
73
}
 
74
 
 
75
+ (id) regexWithPattern: (NSString *) thePattern
 
76
                  flags: (int) theFlags
 
77
{
 
78
  return AUTORELEASE([[self alloc] initWithPattern:thePattern flags:theFlags]);
 
79
}
 
80
 
 
81
- (void)dealloc
 
82
{
 
83
  regfree(&re);
 
84
  [super dealloc];
 
85
}
 
86
 
 
87
- (NSArray *) matchString: (NSString *) theString
 
88
{
 
89
  NSMutableArray *aMutableArray;
 
90
    
 
91
  int offset, status;
 
92
  char *s, *error;
 
93
  regmatch_t rm[1];
 
94
  
 
95
  s = (char*)[theString lossyCString];
 
96
  aMutableArray = [[NSMutableArray alloc] init];
 
97
  
 
98
  status = regexec(&re, s, 1, rm, 0);
 
99
  offset = 0;
 
100
  
 
101
  while (status == 0)
 
102
    {
 
103
      NSValue *aValue;
 
104
      
 
105
      aValue = [NSValue valueWithRange: NSMakeRange(offset + rm[0].rm_so,  rm[0].rm_eo - rm[0].rm_so)];
 
106
      
 
107
      [aMutableArray addObject: aValue];
 
108
      
 
109
      offset += rm[0].rm_eo;
 
110
      
 
111
      if (rm[0].rm_eo - rm[0].rm_so == 0)
 
112
        {
 
113
          status = 1;
 
114
        }
 
115
      else
 
116
        {
 
117
          status = regexec(&re, s + offset, 1, rm, REG_NOTBOL);
 
118
        }
 
119
    }
 
120
  
 
121
  if (status != REG_NOMATCH)
 
122
    {
 
123
      error = malloc(255*sizeof(char));
 
124
      regerror(status, &re, error, 255);
 
125
      NSDebugLog(@"error: %s\n", error);
 
126
      free(error);
 
127
    }
 
128
  
 
129
  return AUTORELEASE(aMutableArray);
 
130
}
 
131
 
 
132
+ (NSArray *) matchString: (NSString *) theString
 
133
              withPattern: (NSString *) thePattern
 
134
          isCaseSensitive: (BOOL) caseSensitive
 
135
{
 
136
  int flags;
 
137
  NSRegEx *regex;
 
138
  NSArray *result;
 
139
  
 
140
  flags = REG_EXTENDED;
 
141
  
 
142
  if ( !caseSensitive )
 
143
    {
 
144
      flags |= REG_ICASE;
 
145
    }
 
146
  
 
147
  if ( (regex = [NSRegEx regexWithPattern:thePattern flags:flags]) == nil )
 
148
    {
 
149
      result = [NSArray array];
 
150
    }
 
151
  else
 
152
    {
 
153
      result = [regex matchString:theString];
 
154
    }
 
155
  
 
156
  return result;
 
157
}
 
158
 
 
159
@end