github.com/ezbuy/gauge@v0.9.4-0.20171013092048-7ac5bd3931cd/parser/helper.go (about)

     1  // Copyright 2015 ThoughtWorks, Inc.
     2  
     3  // This file is part of Gauge.
     4  
     5  // Gauge is free software: you can redistribute it and/or modify
     6  // it under the terms of the GNU General Public License as published by
     7  // the Free Software Foundation, either version 3 of the License, or
     8  // (at your option) any later version.
     9  
    10  // Gauge is distributed in the hope that it will be useful,
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13  // GNU General Public License for more details.
    14  
    15  // You should have received a copy of the GNU General Public License
    16  // along with Gauge.  If not, see <http://www.gnu.org/licenses/>.
    17  
    18  package parser
    19  
    20  import "strconv"
    21  
    22  func isInState(currentState int, statesToCheck ...int) bool {
    23  	var mask int
    24  	for _, value := range statesToCheck {
    25  		mask |= value
    26  	}
    27  	return (mask & currentState) != 0
    28  }
    29  
    30  func isInAnyState(currentState int, statesToCheck ...int) bool {
    31  	for _, value := range statesToCheck {
    32  		if (currentState & value) != 0 {
    33  			return true
    34  		}
    35  	}
    36  	return false
    37  }
    38  
    39  func retainStates(currentState *int, statesToKeep ...int) {
    40  	var mask int
    41  	for _, value := range statesToKeep {
    42  		mask |= value
    43  	}
    44  	*currentState = mask & *currentState
    45  }
    46  
    47  func addStates(currentState *int, states ...int) {
    48  	var mask int
    49  	for _, value := range states {
    50  		mask |= value
    51  	}
    52  	*currentState = mask | *currentState
    53  }
    54  
    55  func isUnderline(text string, underlineChar rune) bool {
    56  	if len(text) == 0 || rune(text[0]) != underlineChar {
    57  		return false
    58  	}
    59  	for _, value := range text {
    60  		if rune(value) != underlineChar {
    61  			return false
    62  		}
    63  	}
    64  	return true
    65  }
    66  
    67  func areUnderlined(values []string) bool {
    68  	if len(values) == 0 {
    69  		return false
    70  	}
    71  	isValuesNonEmpty := false
    72  	for _, value := range values {
    73  		if len(value) == 0 {
    74  			continue
    75  		}
    76  		isValuesNonEmpty = true
    77  		if !isUnderline(value, rune('-')) {
    78  			return false
    79  		}
    80  	}
    81  	return isValuesNonEmpty
    82  }
    83  
    84  func arrayContains(array []string, toFind string) bool {
    85  	for _, value := range array {
    86  		if value == toFind {
    87  			return true
    88  		}
    89  	}
    90  	return false
    91  }
    92  
    93  func GetUnescapedString(string1 string) string {
    94  	unescaped := strconv.Quote(string1)
    95  	return unescaped[1 : len(unescaped)-1]
    96  }