github.com/kcburge/terraform@v0.11.12-beta1/svchost/label_iter.go (about)

     1  package svchost
     2  
     3  import (
     4  	"strings"
     5  )
     6  
     7  // A labelIter allows iterating over domain name labels.
     8  //
     9  // This type is copied from golang.org/x/net/idna, where it is used
    10  // to segment hostnames into their separate labels for analysis. We use
    11  // it for the same purpose here, in ForComparison.
    12  type labelIter struct {
    13  	orig     string
    14  	slice    []string
    15  	curStart int
    16  	curEnd   int
    17  	i        int
    18  }
    19  
    20  func (l *labelIter) reset() {
    21  	l.curStart = 0
    22  	l.curEnd = 0
    23  	l.i = 0
    24  }
    25  
    26  func (l *labelIter) done() bool {
    27  	return l.curStart >= len(l.orig)
    28  }
    29  
    30  func (l *labelIter) result() string {
    31  	if l.slice != nil {
    32  		return strings.Join(l.slice, ".")
    33  	}
    34  	return l.orig
    35  }
    36  
    37  func (l *labelIter) label() string {
    38  	if l.slice != nil {
    39  		return l.slice[l.i]
    40  	}
    41  	p := strings.IndexByte(l.orig[l.curStart:], '.')
    42  	l.curEnd = l.curStart + p
    43  	if p == -1 {
    44  		l.curEnd = len(l.orig)
    45  	}
    46  	return l.orig[l.curStart:l.curEnd]
    47  }
    48  
    49  // next sets the value to the next label. It skips the last label if it is empty.
    50  func (l *labelIter) next() {
    51  	l.i++
    52  	if l.slice != nil {
    53  		if l.i >= len(l.slice) || l.i == len(l.slice)-1 && l.slice[l.i] == "" {
    54  			l.curStart = len(l.orig)
    55  		}
    56  	} else {
    57  		l.curStart = l.curEnd + 1
    58  		if l.curStart == len(l.orig)-1 && l.orig[l.curStart] == '.' {
    59  			l.curStart = len(l.orig)
    60  		}
    61  	}
    62  }
    63  
    64  func (l *labelIter) set(s string) {
    65  	if l.slice == nil {
    66  		l.slice = strings.Split(l.orig, ".")
    67  	}
    68  	l.slice[l.i] = s
    69  }