...

Source file src/github.com/emicklei/proto/range.go

Documentation: github.com/emicklei/proto

     1  // Copyright (c) 2017 Ernest Micklei
     2  //
     3  // MIT License
     4  //
     5  // Permission is hereby granted, free of charge, to any person obtaining
     6  // a copy of this software and associated documentation files (the
     7  // "Software"), to deal in the Software without restriction, including
     8  // without limitation the rights to use, copy, modify, merge, publish,
     9  // distribute, sublicense, and/or sell copies of the Software, and to
    10  // permit persons to whom the Software is furnished to do so, subject to
    11  // the following conditions:
    12  //
    13  // The above copyright notice and this permission notice shall be
    14  // included in all copies or substantial portions of the Software.
    15  //
    16  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
    17  // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
    18  // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
    19  // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
    20  // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
    21  // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
    22  // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
    23  
    24  package proto
    25  
    26  import (
    27  	"fmt"
    28  	"strconv"
    29  )
    30  
    31  // Range is to specify number intervals (with special end value "max")
    32  type Range struct {
    33  	From, To int
    34  	Max      bool
    35  }
    36  
    37  // SourceRepresentation return a single number if from = to. Returns <from> to <to> otherwise unless Max then return <from> to max.
    38  func (r Range) SourceRepresentation() string {
    39  	if r.Max {
    40  		return fmt.Sprintf("%d to max", r.From)
    41  	}
    42  	if r.From == r.To {
    43  		return strconv.Itoa(r.From)
    44  	}
    45  	return fmt.Sprintf("%d to %d", r.From, r.To)
    46  }
    47  
    48  // parseRanges is used to parse ranges for extensions and reserved
    49  func parseRanges(p *Parser, n Visitee) (list []Range, err error) {
    50  	seenTo := false
    51  	negate := false // for numbers
    52  	for {
    53  		pos, tok, lit := p.next()
    54  		if isString(lit) {
    55  			return list, p.unexpected(lit, "integer, <to> <max>", n)
    56  		}
    57  		switch lit {
    58  		case "-":
    59  			negate = true
    60  		case ",":
    61  		case "to":
    62  			seenTo = true
    63  		case ";":
    64  			p.nextPut(pos, tok, lit) // allow for inline comment parsing
    65  			goto done
    66  		case "max":
    67  			if !seenTo {
    68  				return list, p.unexpected(lit, "to", n)
    69  			}
    70  			from := list[len(list)-1]
    71  			list = append(list[0:len(list)-1], Range{From: from.From, Max: true})
    72  		default:
    73  			// must be number
    74  			i, err := strconv.Atoi(lit)
    75  			if err != nil {
    76  				return list, p.unexpected(lit, "range integer", n)
    77  			}
    78  			if negate {
    79  				i = -i
    80  				negate = false
    81  			}
    82  			if seenTo {
    83  				// replace last two ranges with one
    84  				if len(list) < 1 {
    85  					p.unexpected(lit, "integer", n)
    86  				}
    87  				from := list[len(list)-1]
    88  				list = append(list[0:len(list)-1], Range{From: from.From, To: i})
    89  				seenTo = false
    90  			} else {
    91  				list = append(list, Range{From: i, To: i})
    92  			}
    93  		}
    94  	}
    95  done:
    96  	return
    97  }
    98  

View as plain text