示例#1
0
文件: regexp.py 项目: uternet/pycket
def regexp_replace_star(pattern, input, insert, prefix):
    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())
示例#2
0
文件: regexp.py 项目: pycket/pycket
def regexp_replace(pattern, input, insert, prefix):
    matches = match_positions(pattern, input)
    if matches is None:
        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, given : %s" % input.tostring())
    if isinstance(insert, values_string.W_String):
        ins = insert.as_unicode()
    elif isinstance(insert, values.W_Bytes):
        ins = insert.as_str().decode("utf-8")
    elif isinstance(insert, values.W_Procedure):
        # FIXME: hack
        return input
    else:
        raise SchemeException("regexp-replace: expected string or bytes as insert, given : %s" % insert.tostring())
    formatter = values_regex.parse_insert_string(ins)
    subs = values_regex.do_input_substitution(formatter, str, matches)
    start, end = matches[0]
    assert start >= 0 and end >= 0
    result = u"".join([str[0:start], subs, str[end:]])
    return values_string.W_String.fromunicode(result)
示例#3
0
文件: regexp.py 项目: cderici/pycket
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())
示例#4
0
文件: regexp.py 项目: pycket/pycket
def regexp_replace(pattern, input, insert, prefix):
    matches = match_positions(pattern, input)
    if matches is None:
        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, given : %s" %
            input.tostring())
    if isinstance(insert, values_string.W_String):
        ins = insert.as_unicode()
    elif isinstance(insert, values.W_Bytes):
        ins = insert.as_str().decode("utf-8")
    elif isinstance(insert, values.W_Procedure):
        # FIXME: hack
        return input
    else:
        raise SchemeException(
            "regexp-replace: expected string or bytes as insert, given : %s" %
            insert.tostring())
    formatter = values_regex.parse_insert_string(ins)
    subs = values_regex.do_input_substitution(formatter, str, matches)
    start, end = matches[0]
    assert start >= 0 and end >= 0
    result = u"".join([str[0:start], subs, str[end:]])
    return values_string.W_String.fromunicode(result)