-
Notifications
You must be signed in to change notification settings - Fork 1
/
StringParser.py
80 lines (67 loc) · 2.33 KB
/
StringParser.py
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
72
73
74
75
76
77
78
79
80
class StringParser:
def __init__(self, str:str):
self.base = str
self.pos = 0
base = ""
pos = 0
# bool ? x : y convert to python:
# x if bool else y
# str.substring(start, length) convert to python:
# str[start:start+length]
def Skip(self, length):
if type(length) == int:
self.pos += length
elif type(length) == str:
self.pos += len(length)
def ReadFloat(self, terminator = None):
end = self.base.find(terminator, self.pos) if terminator != None else (int(len(self.base)) - 1)
value = float(self.base[self.pos:end])
self.pos += (end - self.pos + 1)
return value
def ReadInt(self, terminator = None):
end = self.base.find(terminator, self.pos) if terminator != None else (int(len(self.base)) - 1)
value = int(self.base[self.pos:end])
self.pos += (end - self.pos + 1)
return value
def ReadBool(self, terminator = None):
end = self.base.find(terminator, self.pos) if terminator != None else (int(len(self.base)) - 1)
value = bool(self.base[self.pos:end].lower() == "true")
self.pos += (end - self.pos + 1)
return value
def ReadString(self, terminator = None):
end = self.base.find(terminator, self.pos) if terminator != None else (int(len(self.base)) - 1)
value = self.base[self.pos:end]
self.pos += (end - self.pos + 1)
return value
def Current(self):
return self.base[self.pos]
def Peek(self, length = 1):
return self.base[self.pos:self.pos + length]
def TryReadFloat(self, terminator = None):
try:
return self.ReadFloat(terminator)
except:
return 0
def TryReadInt(self, terminator = None):
try:
return self.ReadInt(terminator)
except:
return 0
def CanReadFloat(self, terminator = None):
originPos = pos
try:
self.ReadFloat(terminator)
pos = originPos
return True
except:
pos = originPos
return False
def CanReadInt(self, terminator = None):
originPos = pos
try:
self.ReadInt(terminator)
pos = originPos
return True
except:
pos = originPos
return False