gitee.com/mirrors_u-root/u-root@v7.0.0+incompatible/pkg/complete/prefix.go (about)

     1  // Copyright 2012-2020 the u-root Authors. All rights reserved
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package complete
     6  
     7  import "strings"
     8  
     9  // Prefix finds the longest common prefix string
    10  // from a []string.
    11  func Prefix(s []string) string {
    12  	if len(s) == 0 {
    13  		return ""
    14  	}
    15  	var a = s[0]
    16  	for _, h := range s {
    17  		if len(h) < len(a) {
    18  			a = h
    19  		}
    20  	}
    21  	var done bool
    22  	for !done && len(a) > 0 {
    23  		done = true
    24  		for _, h := range s {
    25  			if !strings.HasPrefix(h, a) {
    26  				a = a[:len(a)-1]
    27  				done = false
    28  				break
    29  			}
    30  
    31  		}
    32  	}
    33  	return a
    34  }