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.

26 lines
776 B

  1. package node
  2. import (
  3. "strings"
  4. )
  5. // splitAndTrimEmpty slices s into all subslices separated by sep and returns a
  6. // slice of the string s with all leading and trailing Unicode code points
  7. // contained in cutset removed. If sep is empty, SplitAndTrim splits after each
  8. // UTF-8 sequence. First part is equivalent to strings.SplitN with a count of
  9. // -1. also filter out empty strings, only return non-empty strings.
  10. func splitAndTrimEmpty(s, sep, cutset string) []string {
  11. if s == "" {
  12. return []string{}
  13. }
  14. spl := strings.Split(s, sep)
  15. nonEmptyStrings := make([]string, 0, len(spl))
  16. for i := 0; i < len(spl); i++ {
  17. element := strings.Trim(spl[i], cutset)
  18. if element != "" {
  19. nonEmptyStrings = append(nonEmptyStrings, element)
  20. }
  21. }
  22. return nonEmptyStrings
  23. }