utils.go raw

   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  
  17  package expr
  18  
  19  var op1ch = [...]bool{
  20  	'+': true,
  21  	'-': true,
  22  	'*': true,
  23  	'/': true,
  24  	'%': true,
  25  	'&': true,
  26  	'|': true,
  27  	'^': true,
  28  	'~': true,
  29  	'(': true,
  30  	')': true,
  31  }
  32  
  33  var op2ch = [...]bool{
  34  	'*': true,
  35  	'<': true,
  36  	'>': true,
  37  }
  38  
  39  func neg2(v *Expr, err error) (*Expr, error) {
  40  	if err != nil {
  41  		return nil, err
  42  	} else {
  43  		return v.Neg(), nil
  44  	}
  45  }
  46  
  47  func not2(v *Expr, err error) (*Expr, error) {
  48  	if err != nil {
  49  		return nil, err
  50  	} else {
  51  		return v.Not(), nil
  52  	}
  53  }
  54  
  55  func isop1ch(ch rune) bool {
  56  	return ch >= 0 && int(ch) < len(op1ch) && op1ch[ch]
  57  }
  58  
  59  func isop2ch(ch rune) bool {
  60  	return ch >= 0 && int(ch) < len(op2ch) && op2ch[ch]
  61  }
  62  
  63  func isdigit(ch rune) bool {
  64  	return ch >= '0' && ch <= '9'
  65  }
  66  
  67  func isident(ch rune) bool {
  68  	return isdigit(ch) || isident0(ch)
  69  }
  70  
  71  func isident0(ch rune) bool {
  72  	return (ch == '_') || (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')
  73  }
  74  
  75  func ishexdigit(ch rune) bool {
  76  	return isdigit(ch) || (ch >= 'a' && ch <= 'f') || (ch >= 'A' && ch <= 'F')
  77  }
  78