github.com/richardwilkes/toolbox@v1.121.0/txt/vowel.go (about)

     1  // Copyright (c) 2016-2024 by Richard A. Wilkes. All rights reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the Mozilla Public
     4  // License, version 2.0. If a copy of the MPL was not distributed with
     5  // this file, You can obtain one at http://mozilla.org/MPL/2.0/.
     6  //
     7  // This Source Code Form is "Incompatible With Secondary Licenses", as
     8  // defined by the Mozilla Public License, version 2.0.
     9  
    10  package txt
    11  
    12  import "unicode"
    13  
    14  // VowelChecker defines a function that returns true if the specified rune is to be considered a vowel.
    15  type VowelChecker func(rune) bool
    16  
    17  // IsVowel is a concrete implementation of VowelChecker.
    18  func IsVowel(ch rune) bool {
    19  	if unicode.IsUpper(ch) {
    20  		ch = unicode.ToLower(ch)
    21  	}
    22  	switch ch {
    23  	case 'a', 'à', 'á', 'â', 'ä', 'æ', 'ã', 'å', 'ā',
    24  		'e', 'è', 'é', 'ê', 'ë', 'ē', 'ė', 'ę',
    25  		'i', 'î', 'ï', 'í', 'ī', 'į', 'ì',
    26  		'o', 'ô', 'ö', 'ò', 'ó', 'œ', 'ø', 'ō', 'õ',
    27  		'u', 'û', 'ü', 'ù', 'ú', 'ū':
    28  		return true
    29  	default:
    30  		return false
    31  	}
    32  }
    33  
    34  // IsVowely is a concrete implementation of VowelChecker that includes 'y'.
    35  func IsVowely(ch rune) bool {
    36  	if unicode.IsUpper(ch) {
    37  		ch = unicode.ToLower(ch)
    38  	}
    39  	switch ch {
    40  	case 'y', 'ÿ':
    41  		return true
    42  	default:
    43  		return IsVowel(ch)
    44  	}
    45  }