def regexp_replace_star(pattern, input, insert, start, end, prefix): # FIXME : start end matches = match_all_positions("regexp-replace*", pattern, input) if not matches: return input if isinstance(input, values_string.W_String): str = input.as_unicode() elif isinstance(input, values.W_Bytes): str = input.as_str().decode("utf-8") else: raise SchemeException("regexp-replace*: expected string or bytes input") if isinstance(insert, values_string.W_String): ins = insert.as_unicode() elif isinstance(insert, values.W_Bytes): ins = insert.as_str().decode("utf-8") else: raise SchemeException("regexp-replace*: expected string or bytes insert string") builder = rstring.UnicodeBuilder() lhs = 0 formatter = values_regex.parse_insert_string(ins) for match in matches: start, end = match[0] subs = values_regex.do_input_substitution(formatter, str, match) assert start >= 0 and end >= 0 and lhs >= 0 builder.append_slice(str, lhs, start) builder.append(subs) lhs = end builder.append_slice(str, lhs, len(str)) return values_string.W_String.fromunicode(builder.build())
def parse_insert_string(str): source = regexp.Source(str) buffer = rstring.UnicodeBuilder() result = [] while not source.at_end(): if source.match(u"\\"): escaped = parse_escape_sequence(source, buffer) if escaped is not None: if buffer.getlength(): result.append(StringLiteral(buffer.build())) buffer = rstring.UnicodeBuilder() result.append(escaped) else: ch = source.get() buffer.append(ch) if buffer.getlength(): result.append(StringLiteral(buffer.build())) return result
def _build_string(self, start, end, level): space = self.space if self.is_unicode: out = rstring.UnicodeBuilder() else: out = rstring.StringBuilder() if not level: raise oefmt(space.w_ValueError, "Recursion depth exceeded") level -= 1 s = self.template return self._do_build_string(start, end, level, out, s)
def PyUnicode_TransformDecimalToASCII(space, s, size): """Create a Unicode object by replacing all decimal digits in Py_UNICODE buffer of the given size by ASCII digits 0--9 according to their decimal value. Return NULL if an exception occurs.""" result = rstring.UnicodeBuilder(size) for i in range(size): ch = s[i] if ord(ch) > 127: decimal = Py_UNICODE_TODECIMAL(space, ch) decimal = rffi.cast(lltype.Signed, decimal) if decimal >= 0: ch = unichr(ord('0') + decimal) result.append(ch) return space.wrap(result.build())
def _builder(self): if self.is_unicode: return rstring.UnicodeBuilder() else: return rstring.StringBuilder()