File: TestRigHelpers.m

package info (click to toggle)
oolite 1.77.1-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 41,264 kB
  • ctags: 5,362
  • sloc: objc: 132,090; ansic: 10,457; python: 2,225; sh: 1,325; makefile: 332; perl: 259; xml: 125; php: 5
file content (179 lines) | stat: -rw-r--r-- 3,065 bytes parent folder | download
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
//
//  TestRigHelpers.m
//  ScriptConverter
//
//  Created by Jens Ayton on 2009-03-13.
//  Copyright 2009 Jens Ayton. All rights reserved.
//

#import "TestRigHelpers.h"


@implementation NSString (TestRigHelpers)

- (NSString *)escapedForJavaScriptLiteral
{
	NSMutableString			*result = nil;
	unsigned				i, length;
	unichar					c;
	NSAutoreleasePool		*pool = nil;
	
	length = [self length];
	result = [NSMutableString stringWithCapacity:[self length]];
	
	// Not hugely efficient.
	pool = [[NSAutoreleasePool alloc] init];
	for (i = 0; i != length; ++i)
	{
		c = [self characterAtIndex:i];
		switch (c)
		{
			case '\\':
				[result appendString:@"\\\\"];
				break;
				
			case '\b':
				[result appendString:@"\\b"];
				break;
				
			case '\f':
				[result appendString:@"\\f"];
				break;
				
			case '\n':
				[result appendString:@"\\n"];
				break;
				
			case '\r':
				[result appendString:@"\\r"];
				break;
				
			case '\t':
				[result appendString:@"\\t"];
				break;
				
			case '\v':
				[result appendString:@"\\v"];
				break;
				
			case '\'':
				[result appendString:@"\\\'"];
				break;
				
			case '\"':
				[result appendString:@"\\\""];
				break;
				
			default:
				[result appendString:[NSString stringWithCharacters:&c length:1]];
		}
	}
	[pool release];
	return result;
}


- (BOOL) isJavaScriptIdentifier
{
	static NSCharacterSet		*notIdentifierCharSet = nil;
	unichar						first;
	
	if (notIdentifierCharSet == nil)
	{
		notIdentifierCharSet = [[NSCharacterSet characterSetWithCharactersInString:@"_0123456789QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm$"] invertedSet];
		[notIdentifierCharSet retain];
	}
	
	if ([self length] == 0)  return NO;
	
	// Identifiers may not start with a digit.
	first = [self characterAtIndex:0];
	if ('0' <= first && first <= '9')  return NO;
	
	// Look for any non-identifier char.
	if ([self rangeOfCharacterFromSet:notIdentifierCharSet].location != NSNotFound)  return NO;
	
	return YES;
}


static NSSet *KeywordList(void)
{
	NSString *kKeywords[] =
	{
		@"break",
		@"continue",
		@"do",
		@"for",
		@"import",
		@"new",
		@"this",
		@"void",
		@"case",
		@"default",
		@"else",
		@"function",
		@"in",
		@"return",
		@"typeof",
		@"while",
		@"comment",
		@"delete",
		@"export",
		@"if",
		@"label",
		@"switch",
		@"var",
		@"with",
		@"abstract",
		@"implements",
		@"protected",
		@"boolean",
		@"instanceof",
		@"public",
		@"byte",
		@"int",
		@"short",
		@"char",
		@"interface",
		@"static",
		@"double",
		@"long",
		@"synchronized",
		@"false",
		@"native",
		@"throws",
		@"final",
		@"null",
		@"transient",
		@"float",
		@"package",
		@"true",
		@"goto",
		@"private",
		@"catch",
		@"enum",
		@"throw",
		@"class",
		@"extends",
		@"try",
		@"const",
		@"finally",
		@"debugger",
		@"super"
	};
	
	return [NSSet setWithObjects:kKeywords count:sizeof kKeywords / sizeof *kKeywords];
}


- (BOOL) isJavaScriptKeyword
{
	static NSSet				*keywords = nil;
	
	if (keywords == nil)  keywords = [KeywordList() retain];
	
	return [keywords containsObject:self];
}

@end