Oolite 1.91.0.7646-241128-10e222e
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 47 of file OORegExpMatcher.h.

Method Documentation

◆ dealloc

- (void) dealloc
implementation

Definition at line 34 of file OORegExpMatcher.m.

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

References nil, and sActiveInstance.

◆ init

- (id) init
implementation

Definition at line 34 of file OORegExpMatcher.m.

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}
OOINLINE JSContext * OOJSAcquireContext(void)
OOINLINE void OOJSRelinquishContext(JSContext *context)
OOJavaScriptEngine * sharedEngine()

◆ regExpMatcher

+ (instancetype) regExpMatcher

Definition at line 34 of file OORegExpMatcher.m.

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}

◆ string:matchesExpression:

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

Definition at line 34 of file OORegExpMatcher.m.

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

◆ string:matchesExpression:flags:

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

Definition at line 34 of file OORegExpMatcher.m.

99 :(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}
#define EXPECT_NOT(x)
NSUInteger _cachedFlags

Member Data Documentation

◆ _cachedFlags

- (NSUInteger) _cachedFlags
private

Definition at line 53 of file OORegExpMatcher.h.

◆ _cachedRegExpObject

- (OOJSValue*) _cachedRegExpObject
private

Definition at line 52 of file OORegExpMatcher.h.

◆ _cachedRegExpString

- (NSString*) _cachedRegExpString
private

Definition at line 51 of file OORegExpMatcher.h.

◆ _tester

- (OOJSFunction*) _tester
private

Definition at line 50 of file OORegExpMatcher.h.


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