io/files/internal/Glob.protos

Repository
guillermomolina/protos
Commit
2723a55555e7beecee0816bf99d16e3c8c1ae635
Tree
c36054e4e5abd7fc29a039b4e096cac2e8706492
Blob
2ce479fc90e477c0e6fe8e47d951f813ba6f42e1
Source
View exact source on GitHub
// THE LICENSED WORK IS PROVIDED UNDER THE TERMS OF THE ADAPTIVE PUBLIC LICENSE
// ("LICENSE") AS FIRST COMPLETED BY: Guillermo Adrián Molina. ANY USE, PUBLIC
// DISPLAY, PUBLIC PERFORMANCE, REPRODUCTION OR DISTRIBUTION OF, OR PREPARATION OF
// DERIVATIVE WORKS BASED ON, THE LICENSED WORK CONSTITUTES RECIPIENT'S ACCEPTANCE
// OF THIS LICENSE AND ITS TERMS, WHETHER OR NOT SUCH RECIPIENT READS THE TERMS OF
// THE LICENSE. "LICENSED WORK" AND "RECIPIENT" ARE DEFINED IN THE LICENSE. A COPY
// OF THE LICENSE IS LOCATED IN THE TEXT FILE ENTITLED "LICENSE.TXT" ACCOMPANYING
// THE CONTENTS OF THIS FILE. IF A COPY OF THE LICENSE DOES NOT ACCOMPANY THIS
// FILE, A COPY OF THE LICENSE MAY ALSO BE OBTAINED AT THE FOLLOWING WEB SITE:
// https://github.com/guillermomolina/protos
//
// Software distributed under the License is distributed on an "AS IS" basis,
// WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
// the specific language governing rights and limitations under the License.

//! Private glob pattern admission, component matching and pattern-state
//! transitions of `std:io/files`. Pure source: no Filesystem or other
//! authority is reachable from here; every input is explicit.
//!
//! A pattern is portable relative text of slash-separated, nonempty
//! components, never `.` or `..`, free of backslash, colon and NUL. Inside one
//! component `*` matches zero or more Unicode scalars and `?` exactly one; a
//! component that is exactly `**` matches zero or more complete Path
//! components. Every other scalar is literal: no brackets, escapes, case
//! folding or regular expressions.

invalid: () => {
    failure: InvalidIOArgument {}
    failure.signal()
}

// Answers a fresh Array of `size` copies of `value` in O(size).
filled: null
filled = (size, value) => {
    result: Array()
    if(size > 0) {
        half: filled(size.div(2), value)
        result = Array(...half, ...half)
        if((size % 2) == 1) {
            result = Array(...result, value)
        }
    }
    result
}

// Copies the `count` values of a reversed linked list into an Array in
// insertion order.
listArray: (last, count) => {
    result: filled(count, null)
    node: last
    index: count - 1
    while(index >= 0) {
        result[index] = node.value
        node = node.next
        index = index - 1
    }
    result
}

// One admitted component: `any` for `**`, otherwise its exact scalars.
component: (scalars, count) => {
    values: listArray(scalars, count)
    isAny: false
    if(count == 2) {
        if(values[0] == "*") {
            if(values[1] == "*") {
                isAny = true
            }
        }
    }
    isDot: false
    if(count == 1) {
        if(values[0] == ".") {
            isDot = true
        }
    }
    if(count == 2) {
        if(values[0] == ".") {
            if(values[1] == ".") {
                isDot = true
            }
        }
    }
    if(isDot) {
        invalid()
    }
    result: {
        any: isAny
        scalars: values
    }
    result.freeze()
}

/// Admits `pattern` and answers its frozen compiled form, or signals a fresh
/// InvalidIOArgument.
this.compile: (pattern) => {
    String.recognizes(pattern).ifFalse(() => {
        invalid()
    })
    size: pattern.size()
    if(size == 0) {
        invalid()
    }
    components: null
    componentCount: 0
    scalars: null
    scalarCount: 0
    index: 0
    while(index <= size) {
        boundary: index == size
        if(!boundary) {
            scalar: pattern[index]
            if(scalar == "/") {
                boundary = true
            } else {
                if((scalar == "\\") || (scalar == ":") || (scalar == "\u{0}")) {
                    invalid()
                }
                scalars = { value: scalar; next: scalars }
                scalarCount = scalarCount + 1
            }
        }
        if(boundary) {
            // Leading, trailing and repeated separators leave an empty
            // component; a leading separator is the absolute form.
            if(scalarCount == 0) {
                invalid()
            }
            components = { value: component(scalars, scalarCount); next: components }
            componentCount = componentCount + 1
            scalars = null
            scalarCount = 0
        }
        index = index + 1
    }
    result: {
        components: listArray(components, componentCount)
    }
    result.freeze()
}

/// Answers whether the compiled single component `part` matches the exact
/// component String `name`. Iterative wildcard matching with one remembered
/// star position: O(|part| * |name|) in the worst case, never exponential.
matchComponent: (part, name) => {
    pattern: part.scalars
    patternSize: pattern.size()
    nameSize: name.size()
    p: 0
    n: 0
    star: -1
    mark: 0
    matched: true
    scanning: true
    while(scanning) {
        if(n < nameSize) {
            advanced: false
            if(p < patternSize) {
                scalar: pattern[p]
                if(scalar == "*") {
                    star = p
                    mark = n
                    p = p + 1
                    advanced = true
                } else {
                    if((scalar == "?") || (scalar == name[n])) {
                        p = p + 1
                        n = n + 1
                        advanced = true
                    }
                }
            }
            if(!advanced) {
                if(star >= 0) {
                    p = star + 1
                    mark = mark + 1
                    n = mark
                } else {
                    matched = false
                    scanning = false
                }
            }
        } else {
            scanning = false
        }
    }
    if(matched) {
        trailing: true
        while(trailing) {
            trailing = false
            if(p < patternSize) {
                if(pattern[p] == "*") {
                    p = p + 1
                    trailing = true
                }
            }
        }
        matched = p == patternSize
    }
    matched
}

// Adds every position reachable from the marked ones without consuming a
// component: a `**` component may match zero components. Positions only
// increase, so one ascending pass computes the finite closure.
close: (compiled, marks) => {
    parts: compiled.components
    size: parts.size()
    index: 0
    while(index < size) {
        if(marks[index]) {
            if(parts[index].any) {
                marks[index + 1] = true
            }
        }
        index = index + 1
    }
    marks
}

/// Answers the closed reachable-position set before any component.
this.start: (compiled) => {
    marks: filled(compiled.components.size() + 1, false)
    marks[0] = true
    close(compiled, marks)
}

/// Answers the closed, deduplicated reachable-position set after consuming the
/// exact component `name` from `marks`.
this.step: (compiled, marks, name) => {
    parts: compiled.components
    size: parts.size()
    next: filled(size + 1, false)
    index: 0
    while(index < size) {
        if(marks[index]) {
            part: parts[index]
            if(part.any) {
                // `**` consumes this component and stays at the same position.
                next[index] = true
            } else {
                if(matchComponent(part, name)) {
                    next[index + 1] = true
                }
            }
        }
        index = index + 1
    }
    close(compiled, next)
}

/// Answers whether the consumed components match the whole pattern.
this.matches: (compiled, marks) => {
    marks[compiled.components.size()]
}

/// Answers whether some descendant could still match: some reachable
/// position has not consumed the whole pattern.
this.viable: (compiled, marks) => {
    size: compiled.components.size()
    found: false
    index: 0
    while(index < size) {
        if(marks[index]) {
            found = true
        }
        index = index + 1
    }
    found
}