-
Notifications
You must be signed in to change notification settings - Fork 0
/
StringProcessor.py
265 lines (263 loc) · 9.6 KB
/
StringProcessor.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#coding=utf-8
import re
class StringProcessor(object):
'''
add convenience to analyse string
'''
def __init__(self, s):
self.__strBuffer = s
self.__pointer = 0
def skipSpace(self):
'''
ignore spaces
'''
strLen = len(self.__strBuffer)
while self.__pointer < strLen and self.__strBuffer[self.__pointer].isspace():
self.__pointer += 1
return self
def readChar(self):
'''
return current char and move pointer
return empty string if there is no char in buffer
'''
if self.__pointer == len(self.__strBuffer):
return ''
self.__pointer += 1
return self.__strBuffer[self.__pointer - 1]
def readWord(self):
'''
return current word and move pointer
a word is alphanumeric, or a float number
return current symbol if current pointer points to a unalphanumeric symbol
return empty string if there is no char in buffer
'''
strLen = len(self.__strBuffer)
if self.__pointer >= strLen:
return ''
tempPointer = self.__pointer
if self.__strBuffer[self.__pointer] == '0' and self.__pointer < strLen and self.__strBuffer[self.__pointer + 1].lower() == 'x':
self.__pointer += 1
if self.__strBuffer[self.__pointer] == 'x' or self.__strBuffer[self.__pointer] == 'X':
while self.__pointer < strLen and (self.__strBuffer[self.__pointer].isalnum()):
self.__pointer += 1
elif self.__strBuffer[self.__pointer] == '.' or self.__strBuffer[self.__pointer] == '-' or \
self.__strBuffer[self.__pointer] == '+' or self.__strBuffer[self.__pointer].isnumeric():
tempString = self.__strBuffer[self.__pointer]
self.__pointer += 1
while self.__pointer < strLen:
tempString += self.__strBuffer[self.__pointer]
if self.__strBuffer[self.__pointer].lower() == 'e':
self.__pointer += 1
if self.__strBuffer[self.__pointer] == '-' or self.__strBuffer[self.__pointer] == '+':
tempString += self.__strBuffer[self.__pointer]
self.__pointer += 1
continue
if not StringProcessor.isFloat(tempString):
break
self.__pointer += 1
elif not (self.__strBuffer[self.__pointer].isalnum() or self.__strBuffer[self.__pointer] == '_'):
self.__pointer += 1
else:
while self.__pointer < strLen and (self.__strBuffer[self.__pointer].isalnum() or self.__strBuffer[self.__pointer] == '_'):
self.__pointer += 1
print(self.__strBuffer[tempPointer:self.__pointer])
return self.__strBuffer[tempPointer:self.__pointer]
def readStringWithWrapper(self, wrapperString):
'''
return sub string and move pointer
the string is begin with the wrapper string,
and end with it, if not, return empty string
also return empty string when there is no char in buffer
'''
wrapperLen = len(wrapperString)
subStr = wrapperString
headStr = self.readString(wrapperLen)
if headStr != wrapperString:
self.unRead(len(subStr))
return ''
while True:
tempChar = self.readChar()
if tempChar == '':
self.unRead(len(subStr))
return ''
elif tempChar == '\\':
subStr += tempChar
tempChar = self.readChar()
if tempChar == '':
self.unRead(len(subStr))
return ''
elif tempChar == wrapperString[0]:
self.unRead(1)
tempStr = self.readString(wrapperLen)
if tempStr == wrapperString:
subStr += tempStr
return subStr
self.unRead(len(tempStr) - 1)
subStr += tempChar
def readString(self, charNum):
'''
return sub string from string buffer,
if char num is equal or less than 0,
it will return empty string
if there is no enough chars in buffer,
it will return all chars left in buffer
'''
if charNum <= 0:
return ''
strLen = len(self.__strBuffer)
if self.__pointer >= strLen:
return ''
tempPointer = self.__pointer
self.__pointer += charNum
if self.__pointer >= strLen:
self.__pointer = strLen
return self.__strBuffer[tempPointer:self.__pointer]
def unRead(self, charNum = 1):
'''
move pointer back charNum chars
'''
if charNum <= 0:
return self
self.__pointer -= charNum
if self.__pointer < 0:
self.__pointer = 0
return self
def readStringWithSameLayerBracket(self, headBracket):
'''
get a sub string that is wrapped by headBracket and tailBracket
it will ensure that the two brackets are in the same layer
'''
if headBracket == '{':
tailBracket = '}'
elif headBracket == '[':
tailBracket = ']'
elif headBracket == '(':
tailBracket = ')'
elif headBracket == '<':
tailBracket = '>'
else:
tailBracket = ''
if tailBracket == '':
raise RuntimeError('illegal bracket : ' + headBracket)
currentLayer = 0
headPointer = self.__pointer
tempChar = self.readChar()
if tempChar == '':
return ''
if tempChar != headBracket:
self.unRead(1)
return ''
while True:
tempChar = self.readChar()
if tempChar == '"' or tempChar == "'":
self.unRead(1)
if self.readStringWithWrapper(tempChar) == '':
raise RuntimeError('illegal syntax')
elif tempChar == headBracket:
currentLayer += 1
elif tempChar == tailBracket:
if currentLayer > 0:
currentLayer -= 1
else:
return self.__strBuffer[headPointer:self.__pointer]
elif tempChar == '':
raise RuntimeError('illegal syntax')
else:
pass
def skipString(self, s):
'''
jump string and return self
'''
strLen = len(s)
if self.readString(strLen) != s:
self.unRead(strLen)
return self
def getStringInBuffer(self):
'''
return string left in buffer
'''
if self.__pointer >= len(self.__strBuffer):
return ''
else:
return self.__strBuffer[self.__pointer:len(self.__strBuffer) - 1]
def readTo(self, token):
'''
read to a token and return string has read
'''
readString = ''
while True:
tempChar = self.readChar()
if tempChar == '':
self.unRead(len(readString))
return ''
if tempChar == token[0]:
tempToken = self.unRead().readString(len(token))
if tempToken == token:
readString += token
return readString
self.unRead(len(tempToken) - 1)
readString += tempChar
@staticmethod
def isWrappedBy(originalString, wrapperString, ignoreSideSpaces = True):
'''
determine if originalString is wrapped by wrapperString
ignoreSideSpaces is used to setting whether it should ignore the front and end spaces
'''
if ignoreSideSpaces:
currentString = StringProcessor.removeFrontEndSpaces(originalString)
else:
currentString = originalString
wrapperLen = len(wrapperString)
headStr = currentString[0:wrapperLen]
if headStr != wrapperString:
return False
currentLen = len(currentString)
tailStr = currentString[currentLen - wrapperLen:currentLen]
if tailStr != wrapperString:
return False
return True
@staticmethod
def getNoneSpaceSite(originalString):
'''
get front and end site that is not spaces
output: (headSite, tailSite)
if no non-space char in string, headSite will larger than tailSite
'''
headSite = 0
tailSite = len(originalString) - 1
while headSite <= tailSite and originalString[headSite].isspace():
headSite += 1
if headSite <= tailSite:
while originalString[tailSite].isspace():
tailSite -= 1
return (headSite, tailSite)
@staticmethod
def removeFrontEndSpaces(originalString):
'''
remove the spaces of originalString and return
'''
(head, tail) = StringProcessor.getNoneSpaceSite(originalString)
if head > tail:
return ''
else:
return originalString[head:tail + 1]
@staticmethod
def cutHeadAndTail(s):
'''
delete first char and the last char
'''
return s[1:len(s) - 1]
@staticmethod
def isFloat(s):
'''
return if the input string is a number, include '.'
'''
prog = re.compile(r'[-+]?(\d*\.\d*|\d+)([eE][-+]?(\d)+)?')
match = prog.match(s)
if match and match.group(0) == s:
return True
return False
if __name__ == '__main__':
processor = StringProcessor('-0.3E-123a')
print(processor.readWord())
print(processor.readWord())