github.com/v2pro/plz@v0.0.0-20221028024117-e5f9aec5b631/parse/skip/range.go (about)

     1  package skip
     2  
     3  import (
     4  	"unicode"
     5  	"github.com/v2pro/plz/parse"
     6  )
     7  
     8  func UnicodeRange(src *parse.Source, table *unicode.RangeTable) int {
     9  	count := 0
    10  	for {
    11  		r, n := src.PeekRune()
    12  		if unicode.Is(table, r) {
    13  			src.ConsumeN(n)
    14  			count += n
    15  			continue
    16  		}
    17  		return count
    18  	}
    19  }
    20  
    21  func UnicodeRanges(src *parse.Source, includes []*unicode.RangeTable, excludes []*unicode.RangeTable) int {
    22  	count := 0
    23  	for {
    24  		r, n := src.PeekRune()
    25  		for _, exclude := range excludes {
    26  			if unicode.Is(exclude, r) {
    27  				return count
    28  			}
    29  		}
    30  		for _, include := range includes {
    31  			if unicode.Is(include, r) {
    32  				src.ConsumeN(n)
    33  				count += n
    34  				continue
    35  			}
    36  		}
    37  		return count
    38  	}
    39  }