sigs.k8s.io/external-dns@v0.14.1/source/gateway_hostname.go (about)

     1  // Copyright 2011 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // See:
     6  // 	- https://golang.org/LICENSE
     7  // 	- https://golang.org/src/crypto/x509/verify.go
     8  
     9  package source
    10  
    11  import (
    12  	"unicode/utf8"
    13  )
    14  
    15  // toLowerCaseASCII returns a lower-case version of in. See RFC 6125 6.4.1. We use
    16  // an explicitly ASCII function to avoid any sharp corners resulting from
    17  // performing Unicode operations on DNS labels.
    18  func toLowerCaseASCII(in string) string {
    19  	// If the string is already lower-case then there's nothing to do.
    20  	isAlreadyLowerCase := true
    21  	for _, c := range in {
    22  		if c == utf8.RuneError {
    23  			// If we get a UTF-8 error then there might be
    24  			// upper-case ASCII bytes in the invalid sequence.
    25  			isAlreadyLowerCase = false
    26  			break
    27  		}
    28  		if 'A' <= c && c <= 'Z' {
    29  			isAlreadyLowerCase = false
    30  			break
    31  		}
    32  	}
    33  
    34  	if isAlreadyLowerCase {
    35  		return in
    36  	}
    37  
    38  	out := []byte(in)
    39  	for i, c := range out {
    40  		if 'A' <= c && c <= 'Z' {
    41  			out[i] += 'a' - 'A'
    42  		}
    43  	}
    44  	return string(out)
    45  }