util.go 922 B

123456789101112131415161718192021222324252627282930313233
  1. // Copyright (C) 2014 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at https://mozilla.org/MPL/2.0/.
  6. package versioner
  7. import (
  8. "path/filepath"
  9. "regexp"
  10. )
  11. // Inserts ~tag just before the extension of the filename.
  12. func taggedFilename(name, tag string) string {
  13. dir, file := filepath.Dir(name), filepath.Base(name)
  14. ext := filepath.Ext(file)
  15. withoutExt := file[:len(file)-len(ext)]
  16. return filepath.Join(dir, withoutExt+"~"+tag+ext)
  17. }
  18. var tagExp = regexp.MustCompile(`.*~([^~.]+)(?:\.[^.]+)?$`)
  19. // Returns the tag from a filename, whether at the end or middle.
  20. func filenameTag(path string) string {
  21. match := tagExp.FindStringSubmatch(path)
  22. // match is []string{"whole match", "submatch"} when successful
  23. if len(match) != 2 {
  24. return ""
  25. }
  26. return match[1]
  27. }