Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/woip/app/ReadStream.m
blob: a02cfb2bbce76c43d661843d74ba2b717332d50f (plain)
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
#import "ReadStream.h"

@implementation ReadStream

-(ReadStream *) initWithString: (NSString *) s {
  [self init];
  str = s;
  pos = 0;
  return self;
}

-(unichar) safeCharAtIndex: (int) i {
  return [self isAtEnd] ? 0 : [str characterAtIndex: i];
}

-(unichar) peekBack {
  return [self safeCharAtIndex: pos - 1];
}

-(unichar) peek {
  return [self safeCharAtIndex: pos];
}

-(unichar) peekTwo {
  return [self safeCharAtIndex: pos + 1];
}

-(unichar) read {
  unichar c = [self safeCharAtIndex: pos];
  pos++;
  return c;
}

-(BOOL) isAtEnd {
  return pos == [str length];
}

-(unichar) expect: (unichar) c {
  unichar r;
  if((r = [self read]) != c) NSLog(@"WARNING: expected %c at position %d, found %c", c, pos - 1, r);
  return r;
}

-(NSString *) readUpto: (unichar) c {
  int found = pos;
  
  while(found != [str length] && [self safeCharAtIndex: found++] != c);
  //if(found == [str length]) return NULL; // not found
  
  found--;
  NSString *s = [str substringWithRange: NSMakeRange(pos, found - pos)];
  pos = found;
  
  return s;
}

/*-(NSString *) readUptoString: (NSString *) s {
  NSRange range = [str rangeOfString: s options: 0 range: NSMakeRange(pos, [str length] - pos)];
  NSString *text;

  if(range.location != NSNotFound) {
    text = [str substringToIndex: range.location];
    pos = range.location;
  } else {
    text = [str substringFromIndex: pos];
  }

  return text;
}*/

@end