gitee.com/quant1x/gox@v1.21.2/api/string_helper.go (about)

     1  // Copyright (c) 2017, A. Stoewer <adrian.stoewer@rz.ifi.lmu.de>
     2  // All rights reserved.
     3  
     4  package api
     5  
     6  // isLower checks if a character is lower case. More precisely it evaluates if it is
     7  // in the range of ASCII character 'a' to 'z'.
     8  func isLower(ch rune) bool {
     9  	return ch >= 'a' && ch <= 'z'
    10  }
    11  
    12  // toLower converts a character in the range of ASCII characters 'A' to 'Z' to its lower
    13  // case counterpart. Other characters remain the same.
    14  func toLower(ch rune) rune {
    15  	if ch >= 'A' && ch <= 'Z' {
    16  		return ch + 32
    17  	}
    18  	return ch
    19  }
    20  
    21  // isLower checks if a character is upper case. More precisely it evaluates if it is
    22  // in the range of ASCII characters 'A' to 'Z'.
    23  func isUpper(ch rune) bool {
    24  	return ch >= 'A' && ch <= 'Z'
    25  }
    26  
    27  // toLower converts a character in the range of ASCII characters 'a' to 'z' to its lower
    28  // case counterpart. Other characters remain the same.
    29  func toUpper(ch rune) rune {
    30  	if ch >= 'a' && ch <= 'z' {
    31  		return ch - 32
    32  	}
    33  	return ch
    34  }
    35  
    36  // isSpace checks if a character is some kind of whitespace.
    37  func isSpace(ch rune) bool {
    38  	return ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r'
    39  }
    40  
    41  // isDelimiter checks if a character is some kind of whitespace or '_' or '-'.
    42  func isDelimiter(ch rune) bool {
    43  	return ch == '-' || ch == '_' || isSpace(ch)
    44  }
    45  
    46  // iterFunc is a callback that is called fro a specific position in a string. Its arguments are the
    47  // rune at the respective string position as well as the previous and the next rune. If curr is at the
    48  // first position of the string prev is zero. If curr is at the end of the string next is zero.
    49  type iterFunc func(prev, curr, next rune)
    50  
    51  // stringIter iterates over a string, invoking the callback for every single rune in the string.
    52  func stringIter(s string, callback iterFunc) {
    53  	var prev rune
    54  	var curr rune
    55  	for _, next := range s {
    56  		if curr == 0 {
    57  			prev = curr
    58  			curr = next
    59  			continue
    60  		}
    61  
    62  		callback(prev, curr, next)
    63  
    64  		prev = curr
    65  		curr = next
    66  	}
    67  
    68  	if len(s) > 0 {
    69  		callback(prev, curr, 0)
    70  	}
    71  }