github.com/aaabigfish/gopkg@v1.1.0/stringx/is.go (about)

     1  // Copyright 2021 ByteDance Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package stringx
    16  
    17  import (
    18  	"unicode"
    19  )
    20  
    21  // IsAlpha checks if the string contains only unicode letters.
    22  func IsAlpha(s string) bool {
    23  	if s == "" {
    24  		return false
    25  	}
    26  	for _, v := range s {
    27  		if !unicode.IsLetter(v) {
    28  			return false
    29  		}
    30  	}
    31  	return true
    32  }
    33  
    34  // IsAlphanumeric checks if the string contains only Unicode letters or digits.
    35  func IsAlphanumeric(s string) bool {
    36  	if s == "" {
    37  		return false
    38  	}
    39  	for _, v := range s {
    40  		if !isAlphanumeric(v) {
    41  			return false
    42  		}
    43  	}
    44  	return true
    45  }
    46  
    47  // IsNumeric Checks if the string contains only digits. A decimal point is not a digit and returns false.
    48  func IsNumeric(s string) bool {
    49  	if s == "" {
    50  		return false
    51  	}
    52  	for _, v := range s {
    53  		if !unicode.IsDigit(v) {
    54  			return false
    55  		}
    56  	}
    57  	return true
    58  }
    59  
    60  func isAlphanumeric(v rune) bool {
    61  	return unicode.IsDigit(v) || unicode.IsLetter(v)
    62  }