seq.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. package parser
  2. import "math"
  3. // Shift and masks for sequence parameters and intermediates.
  4. const (
  5. MarkerShift = 8
  6. IntermedShift = 16
  7. CommandMask = 0xff
  8. HasMoreFlag = math.MinInt32
  9. ParamMask = ^HasMoreFlag
  10. MissingParam = ParamMask
  11. MissingCommand = MissingParam
  12. MaxParam = math.MaxUint16 // the maximum value a parameter can have
  13. )
  14. const (
  15. // MaxParamsSize is the maximum number of parameters a sequence can have.
  16. MaxParamsSize = 32
  17. // DefaultParamValue is the default value used for missing parameters.
  18. DefaultParamValue = 0
  19. )
  20. // Marker returns the marker byte of the sequence.
  21. // This is always gonna be one of the following '<' '=' '>' '?' and in the
  22. // range of 0x3C-0x3F.
  23. // Zero is returned if the sequence does not have a marker.
  24. func Marker(cmd int) int {
  25. return (cmd >> MarkerShift) & CommandMask
  26. }
  27. // Intermediate returns the intermediate byte of the sequence.
  28. // An intermediate byte is in the range of 0x20-0x2F. This includes these
  29. // characters from ' ', '!', '"', '#', '$', '%', '&', ”', '(', ')', '*', '+',
  30. // ',', '-', '.', '/'.
  31. // Zero is returned if the sequence does not have an intermediate byte.
  32. func Intermediate(cmd int) int {
  33. return (cmd >> IntermedShift) & CommandMask
  34. }
  35. // Command returns the command byte of the CSI sequence.
  36. func Command(cmd int) int {
  37. return cmd & CommandMask
  38. }
  39. // Param returns the parameter at the given index.
  40. // It returns -1 if the parameter does not exist.
  41. func Param(params []int, i int) int {
  42. if len(params) == 0 || i < 0 || i >= len(params) {
  43. return -1
  44. }
  45. p := params[i] & ParamMask
  46. if p == MissingParam {
  47. return -1
  48. }
  49. return p
  50. }
  51. // HasMore returns true if the parameter has more sub-parameters.
  52. func HasMore(params []int, i int) bool {
  53. if len(params) == 0 || i >= len(params) {
  54. return false
  55. }
  56. return params[i]&HasMoreFlag != 0
  57. }
  58. // Subparams returns the sub-parameters of the given parameter.
  59. // It returns nil if the parameter does not exist.
  60. func Subparams(params []int, i int) []int {
  61. if len(params) == 0 || i < 0 || i >= len(params) {
  62. return nil
  63. }
  64. // Count the number of parameters before the given parameter index.
  65. var count int
  66. var j int
  67. for j = 0; j < len(params); j++ {
  68. if count == i {
  69. break
  70. }
  71. if !HasMore(params, j) {
  72. count++
  73. }
  74. }
  75. if count > i || j >= len(params) {
  76. return nil
  77. }
  78. var subs []int
  79. for ; j < len(params); j++ {
  80. if !HasMore(params, j) {
  81. break
  82. }
  83. p := Param(params, j)
  84. if p == -1 {
  85. p = DefaultParamValue
  86. }
  87. subs = append(subs, p)
  88. }
  89. p := Param(params, j)
  90. if p == -1 {
  91. p = DefaultParamValue
  92. }
  93. return append(subs, p)
  94. }
  95. // Len returns the number of parameters in the sequence.
  96. // This will return the number of parameters in the sequence, excluding any
  97. // sub-parameters.
  98. func Len(params []int) int {
  99. var n int
  100. for i := 0; i < len(params); i++ {
  101. if !HasMore(params, i) {
  102. n++
  103. }
  104. }
  105. return n
  106. }
  107. // Range iterates over the parameters of the sequence and calls the given
  108. // function for each parameter.
  109. // The function should return false to stop the iteration.
  110. func Range(params []int, fn func(i int, param int, hasMore bool) bool) {
  111. for i := 0; i < len(params); i++ {
  112. if !fn(i, Param(params, i), HasMore(params, i)) {
  113. break
  114. }
  115. }
  116. }