def fn(): res = True res = res and replace('abc', 'ab', '--', 0) == 'abc' res = res and replace('abc', 'xy', '--') == 'abc' res = res and replace('abc', 'ab', '--', 0) == 'abc' res = res and replace('abc', 'xy', '--') == 'abc' return res
def string_unquote(string): s = string single_quotes = True if s.startswith('"'): assert s.endswith('"') single_quotes = False else: assert s.startswith("'") assert s.endswith("'") s = s[:-1] s = s[1:] if not single_quotes: variables_ = [] variables = CURLYVARIABLE.findall(s) # remove curly braces around variables for variable in variables: s = replace(s, '{' + variable + '}', variable) # is this an array access? indexes = ARRAYINDEX.findall(variable) identifier = variable for index in indexes: identifier = replace(identifier, '[' + index + ']', '') variables_.append((variable, identifier, indexes)) else: variables_ = [] return s, variables_
def fn(): res = True res = res and replace("abc", "ab", "--", 0) == "abc" res = res and replace("abc", "xy", "--") == "abc" res = res and replace("abc", "ab", "--", 0) == "abc" res = res and replace("abc", "xy", "--") == "abc" return res
def check_replace(value, sub, *args, **kwargs): result = kwargs['res'] assert replace(value, sub, *args) == result assert replace(list(value), sub, *args) == list(result) count = value.count(sub) if len(args) >= 2: count = min(count, args[1]) assert replace_count(value, sub, *args) == (result, count) assert replace_count(value, sub, *args, isutf8=True) == (result, count)
def string_var_export(string): assert string is not None parts = string.split('\x00') escaped_parts = [] for part in parts: escaped_part = "'%s'" % replace(replace(part, '\\', '\\\\'), "'", "\\'") escaped_parts.append(escaped_part) return ' . "\\0" . '.join(escaped_parts)
def parse(lines): lines = replace(lines, "'\n'", "'###'") lines = lines.splitlines() instructionlist = [] for line in lines: if line.strip(): # if line is not empty line = replace(line, "'###'", "'\n'") if line != "'": instruction = line_to_instruction(line) instructionlist.append(instruction) return instructionlist
def descr_replace(self, space, w_old, w_new, count=-1): from rpython.rlib.rstring import replace old_is_unicode = space.isinstance_w(w_old, space.w_unicode) new_is_unicode = space.isinstance_w(w_new, space.w_unicode) if old_is_unicode or new_is_unicode: self_as_uni = unicode_from_encoded_object(space, self, None, None) return self_as_uni.descr_replace(space, w_old, w_new, count) # almost copy of StringMethods.descr_replace :-( input = self._value sub = self._op_val(space, w_old) by = self._op_val(space, w_new) # the following two lines are for being bug-to-bug compatible # with CPython: see issue #2448 if count >= 0 and len(input) == 0: return self._empty() try: res = replace(input, sub, by, count) except OverflowError: raise oefmt(space.w_OverflowError, "replace string is too long") # difference: reuse self if no replacement was done if type(self) is W_BytesObject and res is input: return self return self._new(res)
def test_hypothesis_search(needle, pieces, by, maxcount): needle = needle.encode("utf-8") pieces = [piece.encode("utf-8") for piece in pieces] by = by.encode("utf-8") input = needle.join(pieces) assume(len(input) > 0) res = replace(input, needle, by, maxcount) assert res == input.replace(needle, by, maxcount)
def descr_replace(self, space, w_old, w_new, count=-1): input = self._val(space) sub = self._op_val(space, w_old) by = self._op_val(space, w_new) try: res = replace(input, sub, by, count) except OverflowError: raise oefmt(space.w_OverflowError, "replace string is too long") return self._new(res)
def unicode_replace__Unicode_Unicode_Unicode_ANY(space, w_self, w_old, w_new, w_maxsplit): maxsplit = space.int_w(w_maxsplit) try: return W_UnicodeObject( replace(w_self._value, w_old._value, w_new._value, maxsplit)) except OverflowError: raise OperationError( space.w_OverflowError, space.wrap("replace string is too long"))
def str_replace__String_String_String_ANY(space, w_self, w_sub, w_by, w_maxsplit=-1): sub = w_sub._value by = w_by._value maxsplit = space.int_w(w_maxsplit) try: res = replace(w_self._value, sub, by, maxsplit) except OverflowError: raise OperationError(space.w_OverflowError, space.wrap("replace string is too long") ) return space.wrap(res)
def str_replace__String_ANY_ANY_ANY(space, w_self, w_sub, w_by, w_maxsplit): sub = space.buffer_w(w_sub).as_str() by = space.buffer_w(w_by).as_str() maxsplit = space.int_w(w_maxsplit) try: res = replace(w_self._value, sub, by, maxsplit) except OverflowError: raise OperationError(space.w_OverflowError, space.wrap("replace string is too long") ) return space.wrap(res)
def _parse(line): for token in lexer.lexer.lex(line): if token.gettokentype() == "COMMENT": line = rstring.replace(line, token.getstr(), "", 1) if token.gettokentype() in lexer.keywords or lexer.operations: pass else: print "Invalid token", token, token.gettokentype() os._exit(1) #if token is in keywords: continue; else: print "Invalid token found"; os_exit(1); print("TOKEN:('"+token.gettokentype()+"','"+token.getstr()+"')") result = Parser.parser.parse(lexer.lexer.lex(line));
def descr_replace(self, space, w_old, w_new, count=-1): input = self._val(space) sub = self._op_val(space, w_old) by = self._op_val(space, w_new) # the following two lines are for being bug-to-bug compatible # with CPython: see issue #2448 if count >= 0 and len(input) == 0: return self._empty() try: res = replace(input, sub, by, count) except OverflowError: raise oefmt(space.w_OverflowError, "replace string is too long") return self._new(res)
def test_unicode_replace_overflow(): if sys.maxint > 2**31 - 1: py.test.skip("Wrong platform") s = u"a" * (2**16) with py.test.raises(OverflowError): replace(s, u"", s) with py.test.raises(OverflowError): replace(s, u"a", s) with py.test.raises(OverflowError): replace(s, u"a", s, len(s) - 10)
def test_unicode_replace_overflow(): if sys.maxint > 2**31-1: py.test.skip("Wrong platform") s = u"a" * (2**16) with py.test.raises(OverflowError): replace(s, u"", s) with py.test.raises(OverflowError): replace(s, u"a", s) with py.test.raises(OverflowError): replace(s, u"a", s, len(s) - 10)
def unicode_replace__Unicode_ANY_ANY_ANY(space, w_self, w_old, w_new, w_maxsplit): if not space.isinstance_w(w_old, space.w_unicode): old = unicode(space.bufferstr_w(w_old)) else: old = space.unicode_w(w_old) if not space.isinstance_w(w_new, space.w_unicode): new = unicode(space.bufferstr_w(w_new)) else: new = space.unicode_w(w_new) maxsplit = space.int_w(w_maxsplit) try: return W_UnicodeObject(replace(w_self._value, old, new, maxsplit)) except OverflowError: raise OperationError( space.w_OverflowError, space.wrap("replace string is too long"))
def _format(self, out): rule = self.rule if rule.line_range is not None: if rule.line_range[0] + 1 == rule.line_range[1]: lines = "line %s " % (rule.line_range[0] + 1, ) else: lines = "lines %s-%s " % (rule.line_range[0] + 1, rule.line_range[1]) else: lines = "" out.append(" File \"%s\" %sin %s:%s" % ( rule.file_name, lines, rule.module.name, rule.signature.string())) source = rule.source if source is not None: # poor man's indent out.append(" " + rstring.replace(source, "\n", "\n ")) if self.next is not None: self.next._format(out)
def _format(self, out): rule = self.rule if rule.line_range is not None: if rule.line_range[0] + 1 == rule.line_range[1]: lines = "line %s " % (rule.line_range[0] + 1, ) else: lines = "lines %s-%s " % (rule.line_range[0] + 1, rule.line_range[1]) else: lines = "" out.append( " File \"%s\" %sin %s:%s" % (rule.file_name, lines, rule.module.name, rule.signature.string())) source = rule.source if source is not None: # poor man's indent out.append(" " + rstring.replace(source, "\n", "\n ")) if self.next is not None: self.next._format(out)
def eval(self, frame): stringval = self.value for variable in self.variables: search, identifier, indexes = variable ref = frame.get_reference(identifier) value = ref.get_value(identifier) for key in indexes: if key[0] == '$': ref = frame.get_reference(key) key = ref.get_value(key) elif str(int(key)) == key: key = W_IntObject(int(key)) else: key = W_StringObject(key) value = value.get(key) replace_with = value.str() stringval = replace(stringval, search, replace_with) frame.push(W_StringObject(stringval))
def descr_replace(self, space, w_old, w_new, count=-1): old_is_unicode = space.isinstance_w(w_old, space.w_unicode) new_is_unicode = space.isinstance_w(w_new, space.w_unicode) if old_is_unicode or new_is_unicode: self_as_uni = unicode_from_encoded_object(space, self, None, None) if not old_is_unicode: w_old = unicode_from_encoded_object(space, w_old, None, None) if not new_is_unicode: w_new = unicode_from_encoded_object(space, w_new, None, None) input = self_as_uni._val(space) sub = self_as_uni._op_val(space, w_old) by = self_as_uni._op_val(space, w_new) try: res = replace(input, sub, by, count) except OverflowError: raise oefmt(space.w_OverflowError, "replace string is too long") return self_as_uni._new(res) return self._StringMethods_descr_replace(space, w_old, w_new, count)
def String_replace(a, b, c): return String(rstring.replace(a.string, b.string, c.string))
def recv(self, atom, args): if atom is ADD_1: other = args[0] if isinstance(other, BytesObject): return BytesObject(self._bs + other._bs) if isinstance(other, IntObject): return BytesObject(self._bs + str(chr(other._i))) if atom is ASLIST_0: from typhon.objects.collections.lists import ConstList return ConstList(self.asList()) if atom is ASSET_0: from typhon.objects.collections.sets import ConstSet return ConstSet(self.asSet()) if atom is CONTAINS_1: needle = args[0] if isinstance(needle, IntObject): return wrapBool(chr(needle._i) in self._bs) if isinstance(needle, BytesObject): return wrapBool(needle._bs in self._bs) if atom is GET_1: index = unwrapInt(args[0]) if not 0 <= index < len(self._bs): raise userError(u"string.get/1: Index out of bounds: %d" % index) return IntObject(ord(self._bs[index])) if atom is INDEXOF_1: needle = unwrapBytes(args[0]) return IntObject(self._bs.find(needle)) if atom is INDEXOF_2: needle = unwrapBytes(args[0]) offset = unwrapInt(args[1]) if offset < 0: raise userError(u"indexOf/2: Negative offset %d not supported" % offset) return IntObject(self._bs.find(needle, offset)) if atom is JOIN_1: from typhon.objects.collections.lists import unwrapList return BytesObject(self.join(unwrapList(args[0]))) if atom is LASTINDEXOF_1: needle = unwrapBytes(args[0]) return IntObject(self._bs.rfind(needle)) if atom is MULTIPLY_1: amount = args[0] if isinstance(amount, IntObject): return BytesObject(self._bs * amount._i) if atom is OP__CMP_1: return polyCmp(self._bs, unwrapBytes(args[0])) if atom is REPLACE_2: return BytesObject(replace(self._bs, unwrapBytes(args[0]), unwrapBytes(args[1]))) if atom is SIZE_0: return IntObject(len(self._bs)) if atom is SLICE_1: start = unwrapInt(args[0]) if start < 0: raise userError(u"Slice start cannot be negative") return BytesObject(self._bs[start:]) if atom is SLICE_2: start = unwrapInt(args[0]) stop = unwrapInt(args[1]) if start < 0: raise userError(u"Slice start cannot be negative") if stop < 0: raise userError(u"Slice stop cannot be negative") return BytesObject(self._bs[start:stop]) if atom is SPLIT_1: from typhon.objects.collections.lists import ConstList return ConstList(self.split(unwrapBytes(args[0]))) if atom is SPLIT_2: from typhon.objects.collections.lists import ConstList return ConstList(self.split(unwrapBytes(args[0]), unwrapInt(args[1]))) if atom is TOLOWERCASE_0: return BytesObject(self.toLowerCase()) if atom is TOUPPERCASE_0: return BytesObject(self.toUpperCase()) if atom is TRIM_0: return BytesObject(self.trim()) if atom is WITH_1: return BytesObject(self._bs + chr(unwrapInt(args[0]))) if atom is _MAKEITERATOR_0: return bytesIterator(self._bs) raise Refused(self, atom, args)
def string_to_int(s): return model.w_integer( rarithmetic.string_to_int(rstring.replace(s, "_", ""), base=0))
def zero(program): return replace( program, '[-]', '000') # use three '0' to keep pc, so bracket_map won't broke
def visit_Str(self, node): from rpython.rlib import rstring s, l = self.space.utf8_len_w(node.s) s = rstring.replace(s, "{", "{{") s = rstring.replace(s, "}", "}}") self.append_utf8(s)
def replace(self, src, dest): return replace(self._bs, src, dest)
def test_unicode_replace(): assert replace(u"one!two!three!", u"!", u"@", 1) == u"one@two!three!" assert replace(u"one!two!three!", u"!", u"") == u"onetwothree" assert replace(u"one!two!three!", u"!", u"@", 2) == u"one@two@three!" assert replace(u"one!two!three!", u"!", u"@", 3) == u"one@two@three@" assert replace(u"one!two!three!", u"!", u"@", 4) == u"one@two@three@" assert replace(u"one!two!three!", u"!", u"@", 0) == u"one!two!three!" assert replace(u"one!two!three!", u"!", u"@") == u"one@two@three@" assert replace(u"one!two!three!", u"x", u"@") == u"one!two!three!" assert replace(u"one!two!three!", u"x", u"@", 2) == u"one!two!three!" assert replace(u"abc", u"", u"-") == u"-a-b-c-" assert replace(u"abc", u"", u"-", 3) == u"-a-b-c" assert replace(u"abc", u"", u"-", 0) == u"abc" assert replace(u"", u"", u"") == u"" assert replace(u"", u"", u"a") == u"a" assert replace(u"abc", u"ab", u"--", 0) == u"abc" assert replace(u"abc", u"xy", u"--") == u"abc" assert replace(u"123", u"123", u"") == u"" assert replace(u"123123", u"123", u"") == u"" assert replace(u"123x123", u"123", u"") == u"x"
def line_to_instruction(line): print line if "':'" in line: # escape before split line = replace(line, "':'", "'#'") labelsplit = line.split(":") for i in range(len(labelsplit)): labelsplit[i] = replace(labelsplit[i], "'#'", "':'") else: labelsplit = line.split(":") label = int(labelsplit[0]) line = labelsplit[1] charlist = [] character = "\0" capturetype = "\0" goto = idx = size = behindvalue = -1 if "->" in line: # assuming format of "stuff -> int" gotosplit = line.split("->") goto = int(gotosplit[1]) line = gotosplit[0] if "[" in line and "]" in line: #assuming format similar to "labelname [(stuff)(stuff)]" cut1 = line.find("[") cut2 = line.find("]") + 1 assert cut1 >= 0 assert cut2 >= 0 bracketline = line[cut1:cut2] line = line[:cut1] + line[cut2:] bracketsplit = bracketline.split(")(") charlist = [] for element in bracketsplit: element = ( element.strip("()[]") ) if "-" in element: # describes a range of values sublist = [] rangevalues = element.split("-") range1 = int(rangevalues[0], 16) range2 = int(rangevalues[1], 16) charlist.append(CharRange(chr(range1), chr(range2))) else: # describes single value charlist.append(SingleChar(chr(int(element, 16)))) if "(" in line: # assuming format simillar to #"labelname (something = int) (somethingelse = int)" if "'('" in line: # escape parent character as parameter before split line = replace(line, "'('", "'#'") parensplit = line.split("(") for i in range(len(parensplit)): parensplit[i] = replace(parensplit[i], "'#'", "'('") else: parensplit = line.split("(") line = parensplit[0] for element in parensplit: number = "" for x in element: if x.isdigit(): number += x if "idx" in element: idx = int(number) elif "size" in element: size = int(number) elif ")" in element: raise Exception("Unexpected bytecode parameter: " + element) if "\'" in line: # assuming format of "bytecodename 'character'" character = line.split("'")[1] character = chr(int(character, 16)) while line[-1] == " ": line = line[:-1] while line[0] == " ": line = line[1:] if " " in line: # assuming format of "bytecodename extrainfo" behind_capture_split = line.split(" ") name = behind_capture_split[0] if behind_capture_split[1].isdigit(): behindvalue = int(behind_capture_split[1]) else: capturetype = behind_capture_split[1] else: name = line return Instruction(name, label, goto, charlist[:], idx, size, character, behindvalue, capturetype)
def tokenize(s): "Convert a string into a list of tokens." return rsplit(replace(replace(s, '(', ' ( '), ')', ' ) '))
def encode(value): for escape in reversed(escapes.keys()): # #value = escapes[escape].join(value.split(escape)) value = replace(value, escape, escapes[escape]) return '"' + value + '"'
def check_replace(value, sub, *args, **kwargs): result = kwargs['res'] assert replace(value, sub, *args) == result assert replace(list(value), sub, *args) == list(result)
def diff(self, other): assert isinstance(other, SodaString) return SodaString(replace(self.value, other.value, u""))
def test_unicode_replace(): assert replace(u'one!two!three!', u'!', u'@', 1) == u'one@two!three!' assert replace(u'one!two!three!', u'!', u'') == u'onetwothree' assert replace(u'one!two!three!', u'!', u'@', 2) == u'one@two@three!' assert replace(u'one!two!three!', u'!', u'@', 3) == u'one@two@three@' assert replace(u'one!two!three!', u'!', u'@', 4) == u'one@two@three@' assert replace(u'one!two!three!', u'!', u'@', 0) == u'one!two!three!' assert replace(u'one!two!three!', u'!', u'@') == u'one@two@three@' assert replace(u'one!two!three!', u'x', u'@') == u'one!two!three!' assert replace(u'one!two!three!', u'x', u'@', 2) == u'one!two!three!' assert replace(u'abc', u'', u'-') == u'-a-b-c-' assert replace(u'abc', u'', u'-', 3) == u'-a-b-c' assert replace(u'abc', u'', u'-', 0) == u'abc' assert replace(u'', u'', u'') == u'' assert replace(u'', u'', u'a') == u'a' assert replace(u'abc', u'ab', u'--', 0) == u'abc' assert replace(u'abc', u'xy', u'--') == u'abc' assert replace(u'123', u'123', u'') == u'' assert replace(u'123123', u'123', u'') == u'' assert replace(u'123x123', u'123', u'') == u'x'
def var_export(self, space, indent, recursion, suffix): s = self.unwrap() assert s is not None s = replace(replace(s, '\\', '\\\\'), "'", "\\'") return '%s\'%s\'%s' % (indent, s, suffix)
def test_string_replace(): assert replace('one!two!three!', '!', '@', 1) == 'one@two!three!' assert replace('one!two!three!', '!', '') == 'onetwothree' assert replace('one!two!three!', '!', '@', 2) == 'one@two@three!' assert replace('one!two!three!', '!', '@', 3) == 'one@two@three@' assert replace('one!two!three!', '!', '@', 4) == 'one@two@three@' assert replace('one!two!three!', '!', '@', 0) == 'one!two!three!' assert replace('one!two!three!', '!', '@') == 'one@two@three@' assert replace('one!two!three!', 'x', '@') == 'one!two!three!' assert replace('one!two!three!', 'x', '@', 2) == 'one!two!three!' assert replace('abc', '', '-') == '-a-b-c-' assert replace('abc', '', '-', 3) == '-a-b-c' assert replace('abc', '', '-', 0) == 'abc' assert replace('', '', '') == '' assert replace('', '', 'a') == 'a' assert replace('abc', 'ab', '--', 0) == 'abc' assert replace('abc', 'xy', '--') == 'abc' assert replace('123', '123', '') == '' assert replace('123123', '123', '') == '' assert replace('123x123', '123', '') == 'x'
def test_replace_no_occurrence(): s = "xyz" assert replace(s, "a", "b") is s s = "xyz" assert replace(s, "abc", "b") is s