Skip to content

Commit 0096108

Browse files
committed
Faster utf8validate.
1 parent 88fe146 commit 0096108

1 file changed

Lines changed: 9 additions & 7 deletions

File tree

ws4py/utf8validator.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -103,13 +103,15 @@ def validate(self, ba):
103103
When valid? == True, currentIndex will be len(ba) and totalIndex the
104104
total amount of consumed bytes.
105105
"""
106-
l = len(ba)
107-
for i in range(0, l):
106+
state = self.state
107+
DFA = Utf8Validator.UTF8VALIDATOR_DFA
108+
for i, b in enumerate(ba):
108109
## optimized version of decode(), since we are not interested in actual code points
109-
self.state = Utf8Validator.UTF8VALIDATOR_DFA[256 + (self.state << 4) + Utf8Validator.UTF8VALIDATOR_DFA[ba[i]]]
110-
if self.state == Utf8Validator.UTF8_REJECT:
110+
state = DFA[256 + (state << 4) + DFA[b]]
111+
if state == Utf8Validator.UTF8_REJECT:
111112
self.i += i
113+
self.state = state
112114
return False, False, i, self.i
113-
self.i += l
114-
return True, self.state == Utf8Validator.UTF8_ACCEPT, l, self.i
115-
115+
self.i += i
116+
self.state = state
117+
return True, state == Utf8Validator.UTF8_ACCEPT, i, self.i

0 commit comments

Comments
 (0)