utilities.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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 ArrayPop[T any](stack *[]T, count int) bool {
  32. /*
  33. https://stackoverflow.com/questions/33834742/remove-and-adding-elements-to-array-in-go-lang
  34. https://github.com/golang/go/wiki/SliceTricks
  35. */
  36. if count < 0 || count > len(*stack) {
  37. return false
  38. }
  39. copy((*stack)[0:], (*stack)[count:])
  40. // var temp T
  41. // (*stack)[len(*stack)-1] = temp
  42. *stack = (*stack)[:len(*stack)-count]
  43. return true
  44. }
  45. func SplitToInt(input string, sep string) []int {
  46. var result []int
  47. for _, number := range strings.Split(input, sep) {
  48. v, err := strconv.Atoi(number)
  49. if err == nil {
  50. result = append(result, v)
  51. }
  52. }
  53. return result
  54. }