本站源代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

50 line
973B

  1. package couchbase
  2. import (
  3. "fmt"
  4. "net/url"
  5. "strings"
  6. )
  7. // CleanupHost returns the hostname with the given suffix removed.
  8. func CleanupHost(h, commonSuffix string) string {
  9. if strings.HasSuffix(h, commonSuffix) {
  10. return h[:len(h)-len(commonSuffix)]
  11. }
  12. return h
  13. }
  14. // FindCommonSuffix returns the longest common suffix from the given
  15. // strings.
  16. func FindCommonSuffix(input []string) string {
  17. rv := ""
  18. if len(input) < 2 {
  19. return ""
  20. }
  21. from := input
  22. for i := len(input[0]); i > 0; i-- {
  23. common := true
  24. suffix := input[0][i:]
  25. for _, s := range from {
  26. if !strings.HasSuffix(s, suffix) {
  27. common = false
  28. break
  29. }
  30. }
  31. if common {
  32. rv = suffix
  33. }
  34. }
  35. return rv
  36. }
  37. // ParseURL is a wrapper around url.Parse with some sanity-checking
  38. func ParseURL(urlStr string) (result *url.URL, err error) {
  39. result, err = url.Parse(urlStr)
  40. if result != nil && result.Scheme == "" {
  41. result = nil
  42. err = fmt.Errorf("invalid URL <%s>", urlStr)
  43. }
  44. return
  45. }
上海开阖软件有限公司 沪ICP备12045867号-1