utils.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. //
  2. // Copyright 2024 CloudWeGo Authors
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. //
  16. package expr
  17. var op1ch = [...]bool{
  18. '+': true,
  19. '-': true,
  20. '*': true,
  21. '/': true,
  22. '%': true,
  23. '&': true,
  24. '|': true,
  25. '^': true,
  26. '~': true,
  27. '(': true,
  28. ')': true,
  29. }
  30. var op2ch = [...]bool{
  31. '*': true,
  32. '<': true,
  33. '>': true,
  34. }
  35. func neg2(v *Expr, err error) (*Expr, error) {
  36. if err != nil {
  37. return nil, err
  38. } else {
  39. return v.Neg(), nil
  40. }
  41. }
  42. func not2(v *Expr, err error) (*Expr, error) {
  43. if err != nil {
  44. return nil, err
  45. } else {
  46. return v.Not(), nil
  47. }
  48. }
  49. func isop1ch(ch rune) bool {
  50. return ch >= 0 && int(ch) < len(op1ch) && op1ch[ch]
  51. }
  52. func isop2ch(ch rune) bool {
  53. return ch >= 0 && int(ch) < len(op2ch) && op2ch[ch]
  54. }
  55. func isdigit(ch rune) bool {
  56. return ch >= '0' && ch <= '9'
  57. }
  58. func isident(ch rune) bool {
  59. return isdigit(ch) || isident0(ch)
  60. }
  61. func isident0(ch rune) bool {
  62. return (ch == '_') || (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')
  63. }
  64. func ishexdigit(ch rune) bool {
  65. return isdigit(ch) || (ch >= 'a' && ch <= 'f') || (ch >= 'A' && ch <= 'F')
  66. }