Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 68bb0b411c
Fetching contributors…

Cannot retrieve contributors at this time

113 lines (91 sloc) 3.094 kb
// Copyright 2010 Todd Ditchendorf
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#import <ParseKit/PKWhitespaceState.h>
#import <ParseKit/PKReader.h>
#import <ParseKit/PKTokenizer.h>
#import <ParseKit/PKToken.h>
#import <ParseKit/PKTypes.h>
#define PKTRUE (id)kCFBooleanTrue
#define PKFALSE (id)kCFBooleanFalse
@interface PKToken ()
@property (nonatomic, readwrite) NSUInteger offset;
@end
@interface PKTokenizerState ()
- (void)resetWithReader:(PKReader *)r;
- (void)append:(PKUniChar)c;
- (NSString *)bufferedString;
@end
@interface PKWhitespaceState ()
@property (nonatomic, retain) NSMutableArray *whitespaceChars;
@end
@implementation PKWhitespaceState
- (id)init {
if (self = [super init]) {
const NSUInteger len = 255;
self.whitespaceChars = [NSMutableArray arrayWithCapacity:len];
NSUInteger i = 0;
for ( ; i <= len; i++) {
[whitespaceChars addObject:PKFALSE];
}
[self setWhitespaceChars:YES from:0 to:' '];
}
return self;
}
- (void)dealloc {
self.whitespaceChars = nil;
[super dealloc];
}
- (void)setWhitespaceChars:(BOOL)yn from:(PKUniChar)start to:(PKUniChar)end {
NSUInteger len = [whitespaceChars count];
if (start > len || end > len || start < 0 || end < 0) {
[NSException raise:@"PKWhitespaceStateNotSupportedException" format:@"PKWhitespaceState only supports setting word chars for chars in the latin1 set (under 256)"];
}
id obj = yn ? PKTRUE : PKFALSE;
NSUInteger i = start;
for ( ; i <= end; i++) {
[whitespaceChars replaceObjectAtIndex:i withObject:obj];
}
}
- (BOOL)isWhitespaceChar:(PKUniChar)cin {
if (cin < 0 || cin > [whitespaceChars count] - 1) {
return NO;
}
return PKTRUE == [whitespaceChars objectAtIndex:cin];
}
- (PKToken *)nextTokenFromReader:(PKReader *)r startingWith:(PKUniChar)cin tokenizer:(PKTokenizer *)t {
NSParameterAssert(r);
if (reportsWhitespaceTokens) {
[self resetWithReader:r];
}
PKUniChar c = cin;
while ([self isWhitespaceChar:c]) {
if (reportsWhitespaceTokens) {
[self append:c];
}
c = [r read];
}
if (PKEOF != c) {
[r unread];
}
if (reportsWhitespaceTokens) {
PKToken *tok = [PKToken tokenWithTokenType:PKTokenTypeWhitespace stringValue:[self bufferedString] floatValue:0.0];
tok.offset = offset;
return tok;
} else {
return [t nextToken];
}
}
@synthesize whitespaceChars;
@synthesize reportsWhitespaceTokens;
@end
Jump to Line
Something went wrong with that request. Please try again.