strings.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. /*
  2. Copyright 2014 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package strings
  14. import (
  15. "path"
  16. "strings"
  17. )
  18. // Splits a fully qualified name and returns its namespace and name.
  19. // Assumes that the input 'str' has been validated.
  20. func SplitQualifiedName(str string) (string, string) {
  21. parts := strings.Split(str, "/")
  22. if len(parts) < 2 {
  23. return "", str
  24. }
  25. return parts[0], parts[1]
  26. }
  27. // Joins 'namespace' and 'name' and returns a fully qualified name
  28. // Assumes that the input is valid.
  29. func JoinQualifiedName(namespace, name string) string {
  30. return path.Join(namespace, name)
  31. }
  32. // Returns the first N slice of a string.
  33. func ShortenString(str string, n int) string {
  34. if len(str) <= n {
  35. return str
  36. } else {
  37. return str[:n]
  38. }
  39. }