Oolite
Loading...
Searching...
No Matches
OORegExpMatcher Class Reference

#include <OORegExpMatcher.h>

Inheritance diagram for OORegExpMatcher:
Collaboration diagram for OORegExpMatcher:

Instance Methods

(BOOL) - string:matchesExpression:
(BOOL) - string:matchesExpression:flags:
(id) - init [implementation]
(void) - dealloc [implementation]

Class Methods

(instancetype) + regExpMatcher

Private Attributes

OOJSFunction * _tester
NSString * _cachedRegExpString
OOJSValue * _cachedRegExpObject
NSUInteger _cachedFlags

Detailed Description

Definition at line 54 of file OORegExpMatcher.h.

Method Documentation

◆ dealloc

- (void) dealloc
implementation

Definition at line 80 of file OORegExpMatcher.m.

81{
82 if (sActiveInstance == self)
84
88
89 [super dealloc];
90}
#define DESTROY(x)
return nil
static OORegExpMatcher * sActiveInstance
OOJSValue * _cachedRegExpObject
NSString * _cachedRegExpString
OOJSFunction * _tester

References _cachedRegExpObject, _cachedRegExpString, _tester, dealloc, DESTROY, nil, and sActiveInstance.

Referenced by dealloc.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ init

- (id) init
implementation

Definition at line 48 of file OORegExpMatcher.m.

49{
50 if ((self = [super init])) {
51 const char* argumentNames[2] = { "string", "regexp" };
52 unsigned codeLine = __LINE__ + 1; // NB: should remain line before code.
53 NSString* code = @"return regexp.test(string);";
54
55 [OOJavaScriptEngine sharedEngine]; // Summon the beast from the Pit.
56
57 JSContext* context = OOJSAcquireContext();
58 _tester = [[OOJSFunction alloc] initWithName:@"matchesRegExp"
59#if OOJS_QUICKJS
60 scope:JS_UNDEFINED
61#else
62 scope:NULL
63#endif
64 code:code
65 argumentCount:2
66 argumentNames:argumentNames
67 fileName:[@__FILE__ lastPathComponent]
68 lineNumber:codeLine
69 context:context];
70
71 OOJSRelinquishContext(context);
72
73 if (_tester == nil)
74 DESTROY(self);
75 }
76
77 return self;
78}
OOINLINE JSContext * OOJSAcquireContext(void)
OOINLINE void OOJSRelinquishContext(JSContext *context)
OOJavaScriptEngine * sharedEngine()

References _tester, DESTROY, init, nil, OOJSAcquireContext(), OOJSRelinquishContext(), and OOJavaScriptEngine::sharedEngine.

Referenced by init.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ regExpMatcher

+ (instancetype) regExpMatcher

Definition at line 37 of file OORegExpMatcher.m.

38{
39 NSAssert(![NSThread respondsToSelector:@selector(isMainThread)] || [[NSThread currentThread] isMainThread], @"OORegExpMatcher may only be used on the main thread.");
40
41 if (sActiveInstance == nil) {
42 sActiveInstance = [[[self alloc] init] autorelease];
43 }
44
45 return sActiveInstance;
46}

References nil, regExpMatcher, and sActiveInstance.

Referenced by NSString(OORegExpMatcher)::oo_matchesRegularExpression:, and regExpMatcher.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ string:matchesExpression:

- (BOOL) string: (NSString*) string
matchesExpression: (NSString*) regExp 

Definition at line 92 of file OORegExpMatcher.m.

92 :(NSString*)string matchesExpression:(NSString*)regExp
93{
94 return [self string:string matchesExpression:regExp flags:0];
95}

References string:matchesExpression:flags:.

Here is the call graph for this function:

◆ string:matchesExpression:flags:

- (BOOL) string: (NSString*) string
matchesExpression: (NSString*) regExp
flags: (NSUInteger) flags 

Definition at line 97 of file OORegExpMatcher.m.

97 :(NSString*)string matchesExpression:(NSString*)regExp flags:(NSUInteger)flags
98{
99 NSAssert(![NSThread respondsToSelector:@selector(isMainThread)] || [[NSThread currentThread] isMainThread], @"OORegExpMatcher may only be used on the main thread.");
100
101 size_t expLength = [regExp length];
102 if (EXPECT_NOT(expLength == 0))
103 return NO;
104
105 JSContext* context = OOJSAcquireContext();
106
107 // Create new RegExp object if necessary.
108 if (flags != _cachedFlags || ![regExp isEqualToString:_cachedRegExpString]) {
111
112#if OOJS_QUICKJS
113 // QuickJS has no API to create a RegExp directly, so call the RegExp constructor.
114 _cachedRegExpString = [regExp retain];
115 NSString* flagString = [NSString stringWithFormat:@"%@%@",
116 (flags & kOORegExpCaseInsensitive) ? @"i" : @"",
117 (flags & kOORegExpMultiLine) ? @"m" : @""];
118 JSValue global = JS_GetGlobalObject(context);
119 JSValue constructor = JS_GetPropertyStr(context, global, "RegExp");
120 JSValue args[2] = { OOJSValueFromNativeObject(context, regExp), OOJSValueFromNativeObject(context, flagString) };
121 JSValue regExpObj = JS_CallConstructor(context, constructor, 2, args);
122 if (JS_IsException(regExpObj)) {
124 regExpObj = JS_NULL;
125 }
126 _cachedRegExpObject = [[OOJSValue alloc] initWithJSValue:regExpObj inContext:context];
127 _cachedFlags = flags;
128
129 JS_FreeValue(context, regExpObj);
130 JS_FreeValue(context, args[0]);
131 JS_FreeValue(context, args[1]);
132 JS_FreeValue(context, constructor);
133 JS_FreeValue(context, global);
134#else
135 unichar* buffer;
136 buffer = malloc(expLength * sizeof *buffer);
137 if (EXPECT_NOT(buffer == NULL))
138 return NO;
139 [regExp getCharacters:buffer];
140
141 _cachedRegExpString = [regExp retain];
142 JSObject* regExpObj = JS_NewUCRegExpObjectNoStatics(context, buffer, expLength, (uintN)flags);
143 _cachedRegExpObject = [[OOJSValue alloc] initWithJSObject:regExpObj inContext:context];
144 _cachedFlags = flags;
145
146 free(buffer);
147#endif
148 }
149
150 BOOL result = [_tester evaluatePredicateWithContext:context
151 scope:nil
152 arguments:[NSArray arrayWithObjects:string, _cachedRegExpObject, nil]];
153
154 OOJSRelinquishContext(context);
155
156 return result;
157}
#define EXPECT_NOT(x)
OOINLINE jsval OOJSValueFromNativeObject(JSContext *context, id object)
OOINLINE void OOJSReportPendingException(JSContext *context)
NSUInteger _cachedFlags

References _cachedFlags, _cachedRegExpObject, _cachedRegExpString, DESTROY, OOJSFunction::evaluatePredicateWithContext:scope:arguments:, EXPECT_NOT, OOJSAcquireContext(), OOJSRelinquishContext(), OOJSReportPendingException(), and OOJSValueFromNativeObject().

Referenced by string:matchesExpression:.

Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ _cachedFlags

- (NSUInteger) _cachedFlags
private

Definition at line 59 of file OORegExpMatcher.h.

Referenced by string:matchesExpression:flags:.

◆ _cachedRegExpObject

- (OOJSValue*) _cachedRegExpObject
private

Definition at line 58 of file OORegExpMatcher.h.

Referenced by dealloc, and string:matchesExpression:flags:.

◆ _cachedRegExpString

- (NSString*) _cachedRegExpString
private

Definition at line 57 of file OORegExpMatcher.h.

Referenced by dealloc, and string:matchesExpression:flags:.

◆ _tester

- (OOJSFunction*) _tester
private

Definition at line 56 of file OORegExpMatcher.h.

Referenced by dealloc, and init.


The documentation for this class was generated from the following files: