Beispiel #1
0
def _checkBindings ( left , right ):

    """
    check if all bindings have associated wildcards

    arguments:
        left  - pattern
        right - substitution

    returns:
        True if bindings are valid, False otherwise
    """

#   print 'BIND: left=' , left , ' right=' , right
    mxb = '0'       # maximum binding
    k = 0
    l = len(right)
    while k < l:    # iterate on pattern string
        c = right[k]
        k += 1
        if c == '\\':
            if k == l:
                break
            b = right[k]
            k += 1
            if '1' <= b and b <= '9':  # note character comparisons!
                if mxb < b: mxb = b

#       print 'mxb=' , mxb

    if mxb == '0': return True

    m = int(mxb)    # maximum binding

    n = 0           # to count up possible wildcard bindings
    k = 0           # to iterate on pattern string
    l = len(left)   # iteration limit
    while True:
        while k < l:
            c = left[k]
            k += 1
            if c in special:
                n += 1
            elif ellyWildcard.isWild(c):
                if c != ellyWildcard.cEND: n += 1
                break
        if k == l:
            break
        while k < l:
            c = left[k]
            k += 1
            if c in special:
                n += 1
                break
            elif not ellyWildcard.isWild(c):
                break

#       print 'BIND: m=' , m , 'n=' , n
    return m <= n   # must be as many wildcard sequences as maximum binding
Beispiel #2
0
def _checkBindings(left, right):
    """
    check if all bindings have associated wildcards

    arguments:
        left  - pattern
        right - substitution

    returns:
        True if bindings are valid, False otherwise
    """

    #   print ( 'BIND: left=' , left , ' right=' , right )
    mxb = '0'  # maximum binding
    k = 0
    l = len(right)
    while k < l:  # iterate on pattern string
        c = right[k]
        k += 1
        if c == '\\':
            if k == l:
                break
            b = right[k]
            k += 1
            if '1' <= b and b <= '9':  # note character comparisons!
                if mxb < b: mxb = b

#       print ( 'mxb=' , mxb )

    if mxb == '0': return True

    m = int(mxb)  # maximum binding

    n = 0  # to count up possible wildcard bindings
    k = 0  # to iterate on pattern string
    l = len(left)  # iteration limit
    while True:
        while k < l:
            c = left[k]
            k += 1
            if c in special:
                n += 1
            elif ellyWildcard.isWild(c):
                if c != ellyWildcard.cEND: n += 1
                break
        if k == l:
            break
        while k < l:
            c = left[k]
            k += 1
            if c in special:
                n += 1
                break
            elif not ellyWildcard.isWild(c):
                break


#       print ( 'BIND: m=' , m , 'n=' , n )
    return m <= n  # must be as many wildcard sequences as maximum binding
Beispiel #3
0
def _checkBindings ( left , right ):

    """
    check if all bindings have associated wildcards

    arguments:
        left  - pattern
        right - substitution

    returns:
        True if bindings are valid, False otherwise
    """

#       print 'BIND: left=' , left , ' right=' , right
    mxb = '0'       # maximum binding
    k = 0
    l = len(right)
    while k < l:    # iterate on pattern string
        c = right[k]
        k += 1
        if c == '\\':
            if k == l:
                break
            b = right[k]
            k += 1
            if 1 <= b and b <= '9':
                if mxb < b: mxb = b

#       print 'mxb=' , mxb

    if mxb == '0': return True

    m = int(mxb) - int('0')

    n = 0           # count of wildcard sequences
    k = 0
    l = len(left)   # iterate on substitution string
    while True:
        while k < l:
            c = left[k]
            k += 1
            if ellyWildcard.isWild(c):
                if c != ellyWildcard.cEND: n += 1
                break
        if k == l:
            break
        while k < l:
            c = left[k]
            k += 1
            if not ellyWildcard.isWild(c):
                break

#       print 'BIND: m=' , m , 'n=' , n
    return (m <= n) # must be as many wildcard sequences as maximum binding
Beispiel #4
0
def _checkExpansion ( left , right ):

    """
    check that substitution does not produce more non-space chars
    ignoring pattern wildcards and substitutions dependent on bindings

    arguments:
        left  - pattern
        right - substitution

    returns:
        True if substitution is not longer than original string, False otherwise
    """

#       print 'EXPN: left=' , left , 'right=' , right
    nh = 0          # hypen count in pattern
    n = 0           # non-space char count in pattern
    k = 0
    l = len(left)
    while k < l:    # iterate on pattern string
        c = left[k]
        k += 1
        if c == '-':
            n += 1
            nh += 1
        elif c != ' ' and c != '_':    # look at non-space chars
            if not ellyWildcard.isWild(c):
                n += 1

    mh = 0          # hyphen count in substitution
    m = 0           # non-space char count in substitution
    k = 0
    l = len(right)
    while k < l:    # iterate on substitution string
        c = right[k]
        k += 1
        if c == '-':
            m += 1
            mh += 1
        elif c != ' ' and c != '_':    # look at non-space chars
            if k == l:
                m += 1
            elif c == '\\':            # look for binding with \
                d = right[k]
                if '1' > d or d > '9': # if not binding, treat as non-space
                    m += 1
                else:
                    k += 1
            else:
                m += 1

#       print 'EXPN: m=' , m , 'n=' , n

    if m <= n: return True

    n -= nh         # another way to avoid warning
    m -= mh         #

    return (m <= n)
Beispiel #5
0
def _checkExpansion ( left , right ):

    """
    check that substitution does not produce more non-space chars
    ignoring pattern wildcards and substitutions dependent on bindings

    arguments:
        left  - pattern (converted)
        right - substitution (chars with escapes)

    returns:
        True if substitution is not longer than original text, False otherwise
    """

#   print 'expansion check: left=' , ellyWildcard.deconvert(left) , 'right=' , right
    inOpt = False
    nw = 0          # wildcard count in pattern
    n = 0           # literal char count
    for c in left:  # iterate on pattern string
#       print u'c= {:04x}'.format(ord(c))
        if not ellyWildcard.isWild(c):
            if not inOpt: n += 1
        elif c == ellyWildcard.cSOS:
            inOpt = True
        elif c == ellyWildcard.cEOS:
            inOpt = False
        elif not c in [ ellyWildcard.cEND , ellyWildcard.cALL ]:
            nw += 1

    afterEsc = False
    mw = 0          # wildcard binding count in substitution
    m = 0           # literal char count
    for c in right: # iterate on substitution string
        if afterEsc:
            if c >= '1' and c <= '9':     # if binding reference, ignore in count
                mw += 1
            else:
                m += 1
            afterEsc = False
        elif c == '\\':                   # look for binding reference
            afterEsc = True
        else:
            m += 1

    if afterEsc:                          # count lone \ at end of substitution
        m += 1

#   print 'expansion check: m=' , m , 'n=' , n , 'nw=' , nw , 'mw=' , mw

    return mw + m <= nw + n
Beispiel #6
0
def _checkExpansion(left, right):
    """
    check that substitution does not produce more non-space chars
    ignoring pattern wildcards and substitutions dependent on bindings

    arguments:
        left  - pattern (converted)
        right - substitution (chars with escapes)

    returns:
        True if substitution is not longer than original text, False otherwise
    """

    #   print ( 'expansion check: left=' , ellyWildcard.deconvert(left) , 'right=' , right )
    inOpt = False
    nw = 0  # wildcard count in pattern
    n = 0  # literal char count
    for c in left:  # iterate on pattern string
        #       print ( 'c= {:04x}'.format(ord(c)) )
        if not ellyWildcard.isWild(c):
            if not inOpt: n += 1
        elif c == ellyWildcard.cSOS:
            inOpt = True
        elif c == ellyWildcard.cEOS:
            inOpt = False
        elif not c in [ellyWildcard.cEND, ellyWildcard.cALL]:
            nw += 1

    afterEsc = False
    mw = 0  # wildcard binding count in substitution
    m = 0  # literal char count
    for c in right:  # iterate on substitution string
        if afterEsc:
            if c >= '1' and c <= '9':  # if binding reference, ignore in count
                mw += 1
            else:
                m += 1
            afterEsc = False
        elif c == '\\':  # look for binding reference
            afterEsc = True
        else:
            m += 1

    if afterEsc:  # count lone \ at end of substitution
        m += 1

#   print ( 'expansion check: m=' , m , 'n=' , n , 'nw=' , nw , 'mw=' , mw )

    return mw + m <= nw + n