You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

inst.go 1.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. // Copyright (c) 2017 Couchbase, Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package regexp
  15. import "fmt"
  16. // instOp represents a instruction operation
  17. type instOp int
  18. // the enumeration of operations
  19. const (
  20. OpMatch instOp = iota
  21. OpJmp
  22. OpSplit
  23. OpRange
  24. )
  25. // instSize is the approximate size of the an inst struct in bytes
  26. const instSize = 40
  27. type inst struct {
  28. op instOp
  29. to uint
  30. splitA uint
  31. splitB uint
  32. rangeStart byte
  33. rangeEnd byte
  34. }
  35. func (i *inst) String() string {
  36. switch i.op {
  37. case OpJmp:
  38. return fmt.Sprintf("JMP: %d", i.to)
  39. case OpSplit:
  40. return fmt.Sprintf("SPLIT: %d - %d", i.splitA, i.splitB)
  41. case OpRange:
  42. return fmt.Sprintf("RANGE: %x - %x", i.rangeStart, i.rangeEnd)
  43. }
  44. return "MATCH"
  45. }
  46. type prog []*inst
  47. func (p prog) String() string {
  48. rv := "\n"
  49. for i, pi := range p {
  50. rv += fmt.Sprintf("%d %v\n", i, pi)
  51. }
  52. return rv
  53. }