class W_StringState(W_State): if THREE_VERSIONS_OF_CORE: rsre.insert_sre_methods(locals(), 'str') def unwrap_object(self): self.string = self.space.str_w(self.w_string) return len(self.string) def get_char_ord(self, p): return ord(self.string[p])
class W_UnicodeState(W_State): if THREE_VERSIONS_OF_CORE: rsre.insert_sre_methods(locals(), 'unicode') def unwrap_object(self): self.unicode = self.space.unicode_w(self.w_string) return len(self.unicode) def get_char_ord(self, p): return ord(self.unicode[p])
class W_GenericState(W_State): if THREE_VERSIONS_OF_CORE: rsre.insert_sre_methods(locals(), 'generic') def unwrap_object(self): self.buffer = self.space.buffer_w(self.w_string) return self.buffer.getlength() def get_char_ord(self, p): return ord(self.buffer.getitem(p))
class W_GenericState(W_State): if THREE_VERSIONS_OF_CORE: rsre.insert_sre_methods(locals(), 'generic') def unwrap_object(self): # cannot unwrap in the general case space = self.space # some type-checking if (space.lookup(self.w_string, '__getitem__') is None or space.lookup(self.w_string, 'keys') is not None): msg = "string or sequence of characters expected" raise OperationError(space.w_TypeError, space.wrap(msg)) return space.int_w(space.len(self.w_string)) def get_char_ord(self, p): space = self.space w_char = space.getitem(self.w_string, space.wrap(p)) return space.int_w(space.ord(w_char))
class W_State(Wrappable): if not THREE_VERSIONS_OF_CORE: rsre.insert_sre_methods(locals(), 'all') def __init__(self, space, w_string, start, end, flags): self.space = space self.w_string = w_string length = self.unwrap_object() if start < 0: start = 0 if end > length: end = length self.start = start self.pos = start # records the original start position self.end = end self.flags = flags self.reset() def lower(self, char_ord): return getlower(char_ord, self.flags) # methods overridden by subclasses def unwrap_object(self): raise NotImplementedError if 'reset' not in locals(): def reset(self): raise NotImplementedError if 'search' not in locals(): def search(self, pattern_codes): raise NotImplementedError if 'match' not in locals(): def match(self, pattern_codes): raise NotImplementedError # Accessors for the typedef def w_reset(self): self.reset() def w_create_regs(self, group_count): """Creates a tuple of index pairs representing matched groups, a format that's convenient for SRE_Match.""" space = self.space lst = [] for value1, value2 in self.create_regs(group_count): lst.append(space.newtuple([space.wrap(value1), space.wrap(value2)])) return space.newtuple(lst) w_create_regs.unwrap_spec = ['self', int] def fget_start(space, self): return space.wrap(self.start) def fset_start(space, self, w_value): self.start = space.int_w(w_value) def fget_string_position(space, self): return space.wrap(self.string_position) def fset_string_position(space, self, w_value): self.start = space.int_w(w_value) def get_char_ord(self, p): raise NotImplementedError