github.com/primecitizens/pcz/std@v0.2.1/text/ascii/print.go (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  // Copyright 2023 The Prime Citizens
     3  //
     4  // Copyright 2021 The Go Authors. All rights reserved.
     5  // Use of this source code is governed by a BSD-style
     6  // license that can be found in the LICENSE file.
     7  
     8  package ascii
     9  
    10  const (
    11  	Max = '\u007F'
    12  )
    13  
    14  // EqualFold reports whether s and t are equal, ASCII-case-insensitively.
    15  func EqualFold(s, t string) bool {
    16  	if len(s) != len(t) {
    17  		return false
    18  	}
    19  	for i := 0; i < len(s); i++ {
    20  		if Lower(s[i]) != Lower(t[i]) {
    21  			return false
    22  		}
    23  	}
    24  	return true
    25  }
    26  
    27  // Lower returns the ASCII lowercase version of b.
    28  func Lower(b byte) byte {
    29  	if 'A' <= b && b <= 'Z' {
    30  		return b + ('a' - 'A')
    31  	}
    32  	return b
    33  }
    34  
    35  // Upper returns the ASCII uppercase version of b.
    36  func Upper(b byte) byte {
    37  	if 'a' <= b && b <= 'z' {
    38  		return b - ('a' - 'A')
    39  	}
    40  	return b
    41  }
    42  
    43  // CharIsPrint returns whether b is ASCII and printable according to
    44  // https://tools.ietf.org/html/rfc20#section-4.2.
    45  func CharIsPrint(b byte) bool {
    46  	if b < ' ' || b > '~' {
    47  		return false
    48  	}
    49  
    50  	return true
    51  }
    52  
    53  func IsPrint(s string) bool {
    54  	for i := 0; i < len(s); i++ {
    55  		if s[i] < ' ' || s[i] > '~' {
    56  			return false
    57  		}
    58  	}
    59  	return true
    60  }
    61  
    62  // Valid returns true when s consists of ASCII characters only.
    63  func Valid(s string) bool {
    64  	for i := 0; i < len(s); i++ {
    65  		if s[i] > Max {
    66  			return false
    67  		}
    68  	}
    69  	return true
    70  }