utilities.go 883 B

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