github.com/keybase/client/go@v0.0.0-20240309051027-028f7c731f8b/kbfs/data/util.go (about)

     1  // Copyright 2019 Keybase Inc. All rights reserved.
     2  // Use of this source code is governed by a BSD
     3  // license that can be found in the LICENSE file.
     4  
     5  package data
     6  
     7  // SplitFileExtension splits filename into a base name and the extension.
     8  func SplitFileExtension(path string) (string, string) {
     9  	for i := len(path) - 1; i > 0; i-- {
    10  		switch path[i] {
    11  		case '.':
    12  			// Handle some multipart extensions
    13  			if i >= 4 && path[i-4:i] == ".tar" {
    14  				i -= 4
    15  			}
    16  			// A leading dot is not an extension
    17  			if i == 0 || path[i-1] == '/' || path[i-1] == '\\' {
    18  				return path, ""
    19  			}
    20  			return path[:i], path[i:]
    21  		case '/', '\\', ' ':
    22  			return path, ""
    23  		}
    24  	}
    25  	return path, ""
    26  }