LCOV - code coverage report
Current view: top level - Core - OORegExpMatcher.m (source / functions) Hit Total Coverage
Test: coverxygen.info Lines: 0 4 0.0 %
Date: 2025-05-28 07:50:54 Functions: 0 0 -

          Line data    Source code
       1           0 : /*
       2             : 
       3             : OORegExpMatcher.m
       4             : 
       5             : 
       6             : Copyright (C) 2010-2013 Jens Ayton
       7             : 
       8             : Permission is hereby granted, free of charge, to any person obtaining a copy
       9             : of this software and associated documentation files (the "Software"), to deal
      10             : in the Software without restriction, including without limitation the rights
      11             : to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
      12             : copies of the Software, and to permit persons to whom the Software is
      13             : furnished to do so, subject to the following conditions:
      14             : 
      15             : The above copyright notice and this permission notice shall be included in all
      16             : copies or substantial portions of the Software.
      17             : 
      18             : THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
      19             : IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
      20             : FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
      21             : AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
      22             : LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
      23             : OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
      24             : SOFTWARE.
      25             : 
      26             : */
      27             : 
      28             : #import "OORegExpMatcher.h"
      29             : #import "OOJSFunction.h"
      30             : #import "OOJavaScriptEngine.h"
      31             : 
      32             : 
      33             : // Pseudo-singleton: a single instance exists at a given time, but can be released.
      34           0 : static OORegExpMatcher *sActiveInstance;
      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           0 : - (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           0 : - (void) dealloc
      82             : {
      83             :         if (sActiveInstance == self)  sActiveInstance = nil;
      84             :         
      85             :         DESTROY(_tester);
      86             :         DESTROY(_cachedRegExpString);
      87             :         DESTROY(_cachedRegExpObject);
      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             :         {
     111             :                 DESTROY(_cachedRegExpString);
     112             :                 DESTROY(_cachedRegExpObject);
     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

Generated by: LCOV version 1.14