Oolite 1.91.0.7604-240417-a536cbe
Loading...
Searching...
No Matches
OORegExpMatcher.m
Go to the documentation of this file.
1/*
2
3OORegExpMatcher.m
4
5
6Copyright (C) 2010-2013 Jens Ayton
7
8Permission is hereby granted, free of charge, to any person obtaining a copy
9of this software and associated documentation files (the "Software"), to deal
10in the Software without restriction, including without limitation the rights
11to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12copies of the Software, and to permit persons to whom the Software is
13furnished to do so, subject to the following conditions:
14
15The above copyright notice and this permission notice shall be included in all
16copies or substantial portions of the Software.
17
18THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24SOFTWARE.
25
26*/
27
28#import "OORegExpMatcher.h"
29#import "OOJSFunction.h"
31
32
33// Pseudo-singleton: a single instance exists at a given time, but can be released.
35
36
37@implementation OORegExpMatcher
38
39+ (instancetype) regExpMatcher
40{
41 NSAssert(![NSThread respondsToSelector:@selector(isMainThread)] || [[NSThread currentThread] isMainThread], @"OORegExpMatcher may only be used on the main thread.");
42
43 if (sActiveInstance == nil)
44 {
45 sActiveInstance = [[[self alloc] init] autorelease];
46 }
47
48 return sActiveInstance;
49}
50
51
52- (id) init
53{
54 if ((self = [super init]))
55 {
56 const char *argumentNames[2] = { "string", "regexp" };
57 unsigned codeLine = __LINE__ + 1; // NB: should remain line before code.
58 NSString *code = @"return regexp.test(string);";
59
60 [OOJavaScriptEngine sharedEngine]; // Summon the beast from the Pit.
61
62 JSContext *context = OOJSAcquireContext();
63 _tester = [[OOJSFunction alloc] initWithName:@"matchesRegExp"
64 scope:NULL
65 code:code
66 argumentCount:2
67 argumentNames:argumentNames
68 fileName:[@__FILE__ lastPathComponent]
69 lineNumber:codeLine
70 context:context];
71
72 OOJSRelinquishContext(context);
73
74 if (_tester == nil) DESTROY(self);
75 }
76
77 return self;
78}
79
80
81- (void) dealloc
82{
83 if (sActiveInstance == self) sActiveInstance = nil;
84
88
89 [super dealloc];
90}
91
92
93- (BOOL) string:(NSString *)string matchesExpression:(NSString *)regExp
94{
95 return [self string:string matchesExpression:regExp flags:0];
96}
97
98
99- (BOOL) string:(NSString *)string matchesExpression:(NSString *)regExp flags:(NSUInteger)flags
100{
101 NSAssert(![NSThread respondsToSelector:@selector(isMainThread)] || [[NSThread currentThread] isMainThread], @"OORegExpMatcher may only be used on the main thread.");
102
103 size_t expLength = [regExp length];
104 if (EXPECT_NOT(expLength == 0)) return NO;
105
106 JSContext *context = OOJSAcquireContext();
107
108 // Create new RegExp object if necessary.
109 if (flags != _cachedFlags || ![regExp isEqualToString:_cachedRegExpString])
110 {
113
114 unichar *buffer;
115 buffer = malloc(expLength * sizeof *buffer);
116 if (EXPECT_NOT(buffer == NULL)) return NO;
117 [regExp getCharacters:buffer];
118
119 _cachedRegExpString = [regExp retain];
120 JSObject *regExpObj = JS_NewUCRegExpObjectNoStatics(context, buffer, expLength, (uintN)flags);
121 _cachedRegExpObject = [[OOJSValue alloc] initWithJSObject:regExpObj inContext:context];
122 _cachedFlags = flags;
123
124 free(buffer);
125 }
126
127 BOOL result = [_tester evaluatePredicateWithContext:context
128 scope:nil
129 arguments:[NSArray arrayWithObjects:string, _cachedRegExpObject, nil]];
130
131 OOJSRelinquishContext(context);
132
133 return result;
134}
135
136@end
137
138
139@implementation NSString (OORegExpMatcher)
140
141- (BOOL) oo_matchesRegularExpression:(NSString *)regExp
142{
143 return [[OORegExpMatcher regExpMatcher] string:self matchesExpression:regExp];
144}
145
146@end
#define DESTROY(x)
Definition OOCocoa.h:77
#define EXPECT_NOT(x)
OOINLINE JSContext * OOJSAcquireContext(void)
OOINLINE void OOJSRelinquishContext(JSContext *context)
return nil
static OORegExpMatcher * sActiveInstance
OOJavaScriptEngine * sharedEngine()
OOJSValue * _cachedRegExpObject
NSString * _cachedRegExpString
NSUInteger _cachedFlags
instancetype regExpMatcher()
OOJSFunction * _tester