Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Longest common prefix #23

Open
izissise opened this issue Oct 26, 2023 · 0 comments
Open

Longest common prefix #23

izissise opened this issue Oct 26, 2023 · 0 comments

Comments

@izissise
Copy link
Contributor

I needed that at some point and ended up not using it, but it could be useful in bashkit
it can be made to work with all strings by removing all lines with a '/' in them

longest_common_prefix() {
    __="$1"
    shift 1
    if (( $# == 0 )); then return; fi
    local i=0
    while (( $# > 0 )); do
        for ((i=0; i<${#__}; i++)); do
            if [[ "${__:i:1}" != "${1:i:1}" ]]; then
                __=${__:0:i}
            fi
        done
        if [[ "${1:i:1}" == "/" ]]; then
            __="${__}/" # common path in arg list
        fi
        shift 1
    done
    __=${__%/*} # cut to the last /
}

https://stackoverflow.com/a/77175913/2838914

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant