Beispiel #1
0
def xx(pattern, target, s_from, s_to, mem, not_match):
    global nerror
    global nsucc
    global nfail
    global region
    
    reg = onig.OnigRegex()
    einfo = onig.OnigErrorInfo()
    syn = onig.OnigSyntaxType()
    msg = create_string_buffer(onig.ONIG_MAX_ERROR_MESSAGE_LEN)
    
    pattern2 = pattern
    if not isinstance(pattern, bytes):
        pattern2 = pattern.encode(encoding)
    patternp = strptr(pattern2)
    
    target2 = target
    if not isinstance(target, bytes):
        s_from = cc_to_cb(target, encoding, s_from)
        s_to = cc_to_cb(target, encoding, s_to)
        target2 = target.encode(encoding)
    targetp = strptr(target2)
    
    onig.onig_copy_syntax(byref(syn), onig.ONIG_SYNTAX_DEFAULT)
    syn.options = syn.options & ~onig.ONIG_OPTION_ASCII_RANGE
    
    r = onig.onig_new(byref(reg), patternp.getptr(), patternp.getptr(-1),
            onig.ONIG_OPTION_DEFAULT, onig_encoding, byref(syn), byref(einfo));
    if r != 0:
        onig.onig_error_code_to_str(msg, r, byref(einfo))
        nerror += 1
        print_result("ERROR", "%s (/%s/ '%s')" % (msg.value, pattern, target),
                file=sys.stderr)
        return
    
    r = onig.onig_search(reg, targetp.getptr(), targetp.getptr(-1),
                    targetp.getptr(), targetp.getptr(-1),
                    region, onig.ONIG_OPTION_NONE);
    if r < onig.ONIG_MISMATCH:
        onig.onig_error_code_to_str(msg, r)
        nerror += 1
        print_result("ERROR", "%s (/%s/ '%s')" % (msg.value, pattern, target),
                file=sys.stderr)
        return
    
    if r == onig.ONIG_MISMATCH:
        if not_match:
            nsucc += 1
            print_result("OK(N)", "/%s/ '%s'" % (pattern, target))
        else:
            nfail += 1
            print_result("FAIL", "/%s/ '%s'" % (pattern, target))
    else:
        if not_match:
            nfail += 1
            print_result("FAIL(N)", "/%s/ '%s'" % (pattern, target))
        else:
            start = region[0].beg[mem]
            end = region[0].end[mem]
            if (start == s_from) and (end == s_to):
                nsucc += 1
                print_result("OK", "/%s/ '%s'" % (pattern, target))
            else:
                nfail += 1
                print_result("FAIL", "/%s/ '%s' %d-%d : %d-%d" % (pattern, target,
                        s_from, s_to, start, end))
    onig.onig_free(reg)
Beispiel #2
0
from ctypes import *
import onig
import sys
import io
import locale

nerror = 0
nsucc = 0
nfail = 0

# default encoding
onig_encoding = onig.ONIG_ENCODING_EUC_JP
encoding = onig_encoding[0].name.decode()

# special syntactic settings
_syntax_default = onig.OnigSyntaxType()
onig.onig_copy_syntax(byref(_syntax_default), onig.ONIG_SYNTAX_DEFAULT)
_syntax_default.options &= ~onig.ONIG_OPTION_ASCII_RANGE
syntax_default = byref(_syntax_default)


class strptr:
    """a helper class to get a pointer to a string"""
    def __init__(self, s):
        if not isinstance(s, bytes):
            raise TypeError
        self._str = s
        try:
            self._ptr = cast(self._str, c_void_p)  # CPython 2.x/3.x
        except TypeError:
            self._ptr = c_void_p(self._str)  # PyPy 1.x