-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstring.py
40 lines (37 loc) · 1.06 KB
/
string.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
def string(string: str, index: int) -> (str, int):
"""
It will get a string and the begin index, and return the string value and
the end index.
"""
state = {
'quota': None
}
if string[index] == "'":
state['quota'] = "'"
elif string[index] == '"':
state['quota'] = '"'
else:
# TODO: fix there to return a Error value.
return "Error", index + 1
result = ''
while True:
# looking for the next character
index += 1
# if need escape
if string[index] == '\\':
index += 1
if string[index] == '\\':
result += '\\'
elif string[index] == '"':
result += '"'
elif string[index] == "'":
result += "'"
else:
result += string[index]
# else if it touch the end place
elif string[index] == state['quota']:
break
# else it touch the normal character
else:
result += string[index]
return result, index + 1