Adding ITDebug.h to the framework header.
[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 @implementation ITStringScanner
6
7 - (id)init
8 {
9     if ( ( self = [super init] ) )
10     {
11         _cString = NULL;
12         _string = nil;
13         _currentPosition = 0;
14         _size = 0;
15     }
16     return self;
17 }
18
19 - (id)initWithString:(NSString*)string
20 {
21     if ( ( self = [super init] ) )
22     {
23         _cString = (char *)[string cString];
24         _string = [string retain];
25         _currentPosition = 0;
26         _size = [string length];
27     }
28     return self;
29 }
30
31 - (NSString *)scanUpToCharacter:(char)character
32 {
33     size_t i=_currentPosition,j=0;
34
35     if (_cString[i] == character)
36     {
37         i++;
38         return @"";
39     }
40     else
41     {
42         NSRange r = {i,0};
43         NSString *tmpStr = nil;
44         const size_t tmp = _size;
45         unsigned char foundIt = NO;
46
47         do
48         {
49             i++,j++;
50
51             if (_cString[i] == character)
52             {
53                 foundIt = YES;
54                 r.length = j;
55             }
56
57         }
58         while ((!foundIt) && (i<tmp));
59
60         if (foundIt)
61         {
62             tmpStr = [_string substringWithRange:r];
63             _currentPosition = i;
64         }
65         return tmpStr;
66     }
67 }
68
69 - (NSString *)scanUpToString:(NSString*)string
70 {
71     size_t i=_currentPosition,j=0, len = [string length];
72     const char *str2cstr = [string cString];
73
74
75     if (len <= 1)
76     {
77         if (len)
78             return [self scanUpToCharacter:str2cstr[0]];
79         else
80             return @"";
81     }
82     else
83     {
84         NSRange r = {i,0};
85         NSString *tmpStr = nil;
86         const size_t tmp = _size;
87         unsigned char foundIt = NO;
88
89         do
90         {
91             i++,j++;
92
93             if (0)
94             {//now we check for the rest of the string
95                 foundIt = YES;
96                 r.length = j;
97             }
98
99         }
100         while ((!foundIt) && (i<tmp));
101
102         if (foundIt)
103         {
104             tmpStr = [_string substringWithRange:r];
105             _currentPosition = i;
106         }
107         return tmpStr;
108     }
109
110 }
111 @end