File: callbacks.m

package info (click to toggle)
gnustep-base 1.31.1-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 26,580 kB
  • sloc: objc: 239,446; ansic: 36,519; cpp: 122; sh: 112; makefile: 100; xml: 32
file content (87 lines) | stat: -rw-r--r-- 2,378 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#import <Foundation/Foundation.h>
#import "ObjectTesting.h"

  static void callback(void *context, NSTextCheckingResult *match,
    NSMatchingFlags flags, BOOL *stop)
  {
    if (match)
      {
        (*(NSInteger*)context)++;
      }
    else
      {
	NSLog(@"FLAGS: %lu", (unsigned long)flags);
      }
  }

int main(void)
{
  START_SET("NSRegularExpression + callbacks")

#if !(__APPLE__ || GS_USE_ICU)
    SKIP("NSRegularExpression not built, please install libicu")
#else
  NSString 		*sourceText;
  NSRegularExpression 	*simpleRegex;
  NSRange 		sourceRange;
  NSArray 		*simpleMatches;
  NSUInteger		matchCount = 0;

  // load source file containing some text repeated 1000 times
  sourceText = [NSString stringWithContentsOfFile: @"bigSource.txt"];
  simpleRegex = [NSRegularExpression regularExpressionWithPattern: @"ABC"
							  options: 0
							    error: NULL];

  sourceRange = NSMakeRange(0, [sourceText length] - 1);
  // matchesInString:... uses enumerateMatchesInString:... without any callbacks
  simpleMatches = [simpleRegex matchesInString: sourceText
				       options: 0
					 range: sourceRange];

  // NSLog(@"Simple matches: %ld", [simpleMatches count]);
  PASS([simpleMatches count] == 1000, "1000 matches");

# ifndef __has_feature
# define __has_feature(x) 0
# endif
# if __has_feature(blocks)

  // call enumerateMatchesInString:... directly, with block
  __block NSInteger blockCount = 0;
  [simpleRegex enumerateMatchesInString: sourceText
				options: NSMatchingReportProgress
				  range: NSMakeRange(0, [sourceText length] - 1)
                             usingBlock:
    ^(NSTextCheckingResult * result, NSMatchingFlags flags, BOOL *stop)
      {
        if (result)
	  {
	    blockCount++;
	  }
	else
	  {
	    NSLog(@"FLAGS: %lu", (unsigned long)flags);
	  }
      }];

//  NSLog(@"Number of matches: %ld", blockCount);
  PASS(blockCount == 1000, "enumerate with block has same count");
# endif

#endif

#if     defined(GNUSTEP_BASE_LIBRARY)

  [simpleRegex enumerateMatchesInString: sourceText
				options: NSMatchingReportProgress
				  range: NSMakeRange(0, [sourceText length] - 1)
			       callback: callback
				context: (void*)&matchCount];
//  NSLog(@"Number of matches: %ld", matchCount);
  PASS(matchCount == 1000, "enumerate with callback has same count");
#endif

  END_SET("NSRegularExpression + callbacks")
  return 0;
}