span.go 549 B

12345678910111213141516171819202122232425262728293031
  1. package source
  2. import "fmt"
  3. type Span struct {
  4. Start int
  5. End int
  6. }
  7. func (span Span) Merged(another Span) Span {
  8. if span == (Span{}) {
  9. return another
  10. }
  11. if another == (Span{}) {
  12. return span
  13. }
  14. var merged = Span { Start: span.Start, End: another.End }
  15. if !(merged.Start <= merged.End) {
  16. panic(fmt.Sprintf("invalid span merge: %+v and %+v", span, another))
  17. }
  18. return merged
  19. }
  20. func (span Span) Contains(pos int) bool {
  21. return (span.Start <= pos && pos < span.End)
  22. }
  23. func (span Span) Size() int {
  24. return (span.End - span.Start)
  25. }