Initial revision
[ITFoundation.git] / ITStringScanner.m
1 // I *could* use OWStringScanner, but I don't want to. Shut up, sabi :)
2
3 #import "ITStringScanner.h"
4
5 static NSString *AString(void)
6 {
7     return [NSString string];
8 }
9
10 @implementation ITStringScanner
11 -(id)init
12 {
13     if (self = [super init])
14     {
15         strCStr = NULL;
16         str = nil;
17         curPos = size = 0;
18     }
19     return self;
20 }
21
22 -(id)initWithString:(NSString*)str2
23 {
24     if (self = [super init])
25     {
26         strCStr = (char *)[str2 cString];
27         str = [str2 retain];
28         curPos = 0;
29         size = [str2 length];
30     }
31     return self;
32 }
33
34 -(NSString *)scanUpToCharacter:(char)c
35 {
36     size_t i=curPos,j=0;
37
38     if (strCStr[i] == c)
39     {
40         i++;
41         return AString();
42     }
43     else
44     {
45         NSRange r = {i,0};
46         NSString *tmpStr = nil;
47         const size_t tmp = size;
48         unsigned char foundIt = NO;
49
50         do
51         {
52             i++,j++;
53
54             if (strCStr[i] == c)
55             {
56                 foundIt = YES;
57                 r.length = j;
58             }
59
60         }
61         while ((!foundIt) && (i<tmp));
62
63         if (foundIt)
64         {
65             tmpStr = [str substringWithRange:r];
66             curPos = i;
67         }
68         return tmpStr;
69     }
70 }
71
72 -(NSString *)scanUpToString:(NSString*)str2
73 {
74     size_t i=curPos,j=0, len = [str2 length];
75     const char *str2cstr = [str2 cString];
76
77
78     if (len <= 1)
79     {
80         if (len)
81             return [self scanUpToCharacter:str2cstr[0]];
82         else
83             return AString();
84     }
85     else
86     {
87         NSRange r = {i,0};
88         NSString *tmpStr = nil;
89         const size_t tmp = size;
90         unsigned char foundIt = NO;
91
92         do
93         {
94             i++,j++;
95
96             if (0)
97             {//now we check for the rest of the string
98                 foundIt = YES;
99                 r.length = j;
100             }
101
102         }
103         while ((!foundIt) && (i<tmp));
104
105         if (foundIt)
106         {
107             tmpStr = [str substringWithRange:r];
108             curPos = i;
109         }
110         return tmpStr;
111     }
112
113 }
114 @end