Skip to content
Newer
Older
100644 155 lines (119 sloc) 4.75 KB
05eedb5 @itod initial add
authored
1 // Copyright 2010 Todd Ditchendorf
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #import <ParseKit/PKSymbolRootNode.h>
16 #import <ParseKit/PKReader.h>
17
18 @interface PKSymbolNode ()
19 @property (nonatomic, retain) NSMutableDictionary *children;
20 @end
21
22 @interface PKSymbolRootNode ()
23 - (void)addWithFirst:(PKUniChar)c rest:(NSString *)s parent:(PKSymbolNode *)p;
24 - (void)removeWithFirst:(PKUniChar)c rest:(NSString *)s parent:(PKSymbolNode *)p;
25 - (NSString *)nextWithFirst:(PKUniChar)c rest:(PKReader *)r parent:(PKSymbolNode *)p;
26 @end
27
28 @implementation PKSymbolRootNode
29
30 - (id)init {
3731156 @itod adding URLState/EmailState
authored
31 if (self = [super initWithParent:nil character:PKEOF]) {
05eedb5 @itod initial add
authored
32
33 }
34 return self;
35 }
36
37
38 - (void)add:(NSString *)s {
39 NSParameterAssert(s);
40 if ([s length] < 2) return;
41
42 [self addWithFirst:[s characterAtIndex:0] rest:[s substringFromIndex:1] parent:self];
43 }
44
45
46 - (void)remove:(NSString *)s {
47 NSParameterAssert(s);
48 if ([s length] < 2) return;
49
50 [self removeWithFirst:[s characterAtIndex:0] rest:[s substringFromIndex:1] parent:self];
51 }
52
53
54 - (void)addWithFirst:(PKUniChar)c rest:(NSString *)s parent:(PKSymbolNode *)p {
55 NSParameterAssert(p);
56 NSNumber *key = [NSNumber numberWithInteger:c];
3731156 @itod adding URLState/EmailState
authored
57 PKSymbolNode *child = [p->children objectForKey:key];
05eedb5 @itod initial add
authored
58 if (!child) {
59 child = [[PKSymbolNode alloc] initWithParent:p character:c];
3731156 @itod adding URLState/EmailState
authored
60 [p->children setObject:child forKey:key];
05eedb5 @itod initial add
authored
61 [child release];
62 }
63
64 NSString *rest = nil;
65
3731156 @itod adding URLState/EmailState
authored
66 NSUInteger len = [s length];
67 if (0 == len) {
05eedb5 @itod initial add
authored
68 return;
3731156 @itod adding URLState/EmailState
authored
69 } else if (len > 1) {
05eedb5 @itod initial add
authored
70 rest = [s substringFromIndex:1];
71 }
72
73 [self addWithFirst:[s characterAtIndex:0] rest:rest parent:child];
74 }
75
76
77 - (void)removeWithFirst:(PKUniChar)c rest:(NSString *)s parent:(PKSymbolNode *)p {
78 NSParameterAssert(p);
79 NSNumber *key = [NSNumber numberWithInteger:c];
3731156 @itod adding URLState/EmailState
authored
80 PKSymbolNode *child = [p->children objectForKey:key];
05eedb5 @itod initial add
authored
81 if (child) {
82 NSString *rest = nil;
83
3731156 @itod adding URLState/EmailState
authored
84 NSUInteger len = [s length];
85 if (0 == len) {
05eedb5 @itod initial add
authored
86 return;
3731156 @itod adding URLState/EmailState
authored
87 } else if (len > 1) {
05eedb5 @itod initial add
authored
88 rest = [s substringFromIndex:1];
89 [self removeWithFirst:[s characterAtIndex:0] rest:rest parent:child];
90 }
91
3731156 @itod adding URLState/EmailState
authored
92 [p->children removeObjectForKey:key];
05eedb5 @itod initial add
authored
93 }
94 }
95
96
97 - (NSString *)nextSymbol:(PKReader *)r startingWith:(PKUniChar)cin {
98 NSParameterAssert(r);
99 return [self nextWithFirst:cin rest:r parent:self];
100 }
101
102
103 - (NSString *)nextWithFirst:(PKUniChar)c rest:(PKReader *)r parent:(PKSymbolNode *)p {
104 NSParameterAssert(p);
eb778c5 @itod merging changes from Google Code SVN repo
authored
105 NSString *result = [NSString stringWithFormat:@"%C", (unichar)c];
05eedb5 @itod initial add
authored
106
107 // this also works.
108 // NSString *result = [[[NSString alloc] initWithCharacters:(const unichar *)&c length:1] autorelease];
109
110 // none of these work.
111 //NSString *result = [[[NSString alloc] initWithBytes:&c length:1 encoding:NSUTF8StringEncoding] autorelease];
112
113 // NSLog(@"c: %d", c);
114 // NSLog(@"string for c: %@", result);
115 // NSString *chars = [[[NSString alloc] initWithCharacters:(const unichar *)&c length:1] autorelease];
116 // NSString *utfs = [[[NSString alloc] initWithUTF8String:(const char *)&c] autorelease];
117 // NSString *utf8 = [[[NSString alloc] initWithBytes:&c length:1 encoding:NSUTF8StringEncoding] autorelease];
118 // NSString *utf16 = [[[NSString alloc] initWithBytes:&c length:1 encoding:NSUTF16StringEncoding] autorelease];
119 // NSString *ascii = [[[NSString alloc] initWithBytes:&c length:1 encoding:NSASCIIStringEncoding] autorelease];
120 // NSString *iso = [[[NSString alloc] initWithBytes:&c length:1 encoding:NSISOLatin1StringEncoding] autorelease];
121 //
122 // NSLog(@"chars: '%@'", chars);
123 // NSLog(@"utfs: '%@'", utfs);
124 // NSLog(@"utf8: '%@'", utf8);
125 // NSLog(@"utf16: '%@'", utf16);
126 // NSLog(@"ascii: '%@'", ascii);
127 // NSLog(@"iso: '%@'", iso);
128
129 NSNumber *key = [NSNumber numberWithInteger:c];
3731156 @itod adding URLState/EmailState
authored
130 PKSymbolNode *child = [p->children objectForKey:key];
05eedb5 @itod initial add
authored
131
132 if (!child) {
133 if (p == self) {
134 return result;
135 } else {
136 [r unread];
137 return @"";
138 }
139 }
140
141 c = [r read];
142 if (PKEOF == c) {
143 return result;
144 }
145
146 return [result stringByAppendingString:[self nextWithFirst:c rest:r parent:child]];
147 }
148
149
150 - (NSString *)description {
151 return @"<PKSymbolRootNode>";
152 }
153
154 @end
Something went wrong with that request. Please try again.