utilities.go 945 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package door
  2. import (
  3. "strconv"
  4. "strings"
  5. "golang.org/x/text/width"
  6. )
  7. // https://siongui.github.io/2016/03/23/go-utf8-string-width/
  8. func UnicodeWidth(r rune) int {
  9. p := width.LookupRune(r)
  10. if p.Kind() == width.EastAsianWide {
  11. return 2
  12. }
  13. return 1
  14. }
  15. func ArrayDelete[T any](stack *[]T, pos int) (T, bool) {
  16. var result T
  17. /*
  18. https://stackoverflow.com/questions/33834742/remove-and-adding-elements-to-array-in-go-lang
  19. https://github.com/golang/go/wiki/SliceTricks
  20. */
  21. if pos < 0 || pos > len(*stack) {
  22. return result, false
  23. }
  24. result = (*stack)[pos]
  25. copy((*stack)[pos:], (*stack)[pos+1:])
  26. // var temp T
  27. // (*stack)[len(*stack)-1] = temp
  28. *stack = (*stack)[:len(*stack)-1]
  29. return result, true
  30. }
  31. func SplitToInt(input string, sep string) []int {
  32. var result []int
  33. for _, number := range strings.Split(input, sep) {
  34. v, err := strconv.Atoi(number)
  35. if err == nil {
  36. result = append(result, v)
  37. }
  38. }
  39. return result
  40. }