Skip to content

Longest common prefix #23

Description

@izissise

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions