stringutil.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. // Copyright (C) 2016 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 stringutil
  7. import (
  8. "strings"
  9. "time"
  10. )
  11. // UniqueTrimmedStrings returns a list of all unique strings in ss,
  12. // in the order in which they first appear in ss, after trimming away
  13. // leading and trailing spaces.
  14. func UniqueTrimmedStrings(ss []string) []string {
  15. m := make(map[string]struct{}, len(ss))
  16. us := make([]string, 0, len(ss))
  17. for _, v := range ss {
  18. v = strings.Trim(v, " ")
  19. if _, ok := m[v]; ok {
  20. continue
  21. }
  22. m[v] = struct{}{}
  23. us = append(us, v)
  24. }
  25. return us
  26. }
  27. func NiceDurationString(d time.Duration) string {
  28. switch {
  29. case d > 24*time.Hour:
  30. d = d.Round(time.Hour)
  31. case d > time.Hour:
  32. d = d.Round(time.Minute)
  33. case d > time.Minute:
  34. d = d.Round(time.Second)
  35. case d > time.Second:
  36. d = d.Round(time.Millisecond)
  37. case d > time.Millisecond:
  38. d = d.Round(time.Microsecond)
  39. }
  40. return d.String()
  41. }