Repository: zyedidia/micro
File: internal/util/util.go (Go)

Code before:
// Word characters are defined as numbers, letters, or '_'
func IsWordChar(r rune) bool {
	return IsAlphanumeric(r) || IsWordDelimiter(r)
}

// IsUpperWordChar returns whether or not the string is a 'upper word character'
// Upper word characters are defined as numbers, upper-case letters, or delimiters ([A-Z0-9_])
func IsUpperWordChar(r rune) bool {
	return IsUpperAlphanumeric(r) || IsWordDelimiter(r)
}

// IsLowerWordChar returns whether or not the string is a 'lower word character'
// Lower word characters are defined as numbers, upper-case letters, or delimiters ([a-z0-9_])
func IsLowerWordChar(r rune) bool {
	return IsLowerAlphanumeric(r) || IsWordDelimiter(r)
}

// IsWordDelimiter returns whether or not the string is a 'delimiter character'
// Delimiter characters are defined as '_'
func IsWordDelimiter(r rune) bool {
	return r == '_'
}

// IsNoneWordChar returns whether or not the string is not a 'word character'
// None word characters are defined as all characters not being alphanumerics, whitespaces or delimiters ([!A-Za-z0-9\s_])
func IsNoneWordChar(r rune) bool {
	return !IsAlphanumeric(r) && !IsWhitespace(r) && !IsWordDelimiter(r)
}

// IsAlphanumeric returns whether or not the string is an 'alphanumeric character'
// Alphanumeric characters are defined as numbers or letters ([A-Za-z0-9])
func IsAlphanumeric(r rune) bool {
	return unicode.IsLetter(r) || unicode.IsNumber(r)
}

// IsUpperAlphanumeric returns whether or not the string is an 'upper alphanumeric character'
// Upper alphanumeric characters are defined as numbers or upper-case letters ([A-Z0-9])
func IsUpperAlphanumeric(r rune) bool {
	return IsUpperLetter(r) || unicode.IsNumber(r)
}

// IsLowerAlphanumeric returns whether or not the string is an 'lower alphanumeric character'
// Lower alphanumeric characters are defined as numbers or lower-case letters ([a-z0-9])
func IsLowerAlphanumeric(r rune) bool {
	return IsLowerLetter(r) || unicode.IsNumber(r)
}

// IsUpperLetter returns whether or not the string is a 'upper letter character'
// Upper letter characters are defined as upper-case letters ([A-Z])
func IsUpperLetter(r rune) bool {
	return unicode.IsLetter(r) && unicode.IsUpper(r)

Review comment (bug):
This also seems rather unneeded. What's wrong with explicit `!IsWordChar() && !IsWhitespace()` checks?

Moreover, there is already `IsNonAlphaNumeric()`, i.e. a function that uses almost the same naming style but different semantics: it returns true for whitespace. So it you add these functions but leave the existing `IsNonAlphaNumeric()` as is, IMO that creates a mess.

And there is a reason why `IsNonAlphaNumeric()` is a separate function: it is used as a callback, in multiple places. Whereas in your case there is no strong need for a separate function.

So I'd propose to:
1. Use explicit `!IsWordChar() && !IsWhitespace()` checks instead of adding `IsNoneWordChar()`
2. Rename the existing `IsNonAlphaNumeric()` to `IsNonWordChar()`.

Code after:

// IsWordChar returns whether or not a rune is a 'word character'
// Word characters are defined as numbers, letters or sub-word delimiters
func IsWordChar(r rune) bool {
	return IsAlphanumeric(r) || IsSubwordDelimiter(r)
}

// IsNonWordChar returns whether or not a rune is not a 'word character'
// Non word characters are defined as all characters not being numbers, letters or sub-word delimiters
// See IsWordChar()
func IsNonWordChar(r rune) bool {
	return !IsWordChar(r)
}

// IsUpperWordChar returns whether or not a rune is an 'upper word character'
// Upper word characters are defined as numbers, upper-case letters or sub-word delimiters
func IsUpperWordChar(r rune) bool {
	return IsUpperAlphanumeric(r) || IsSubwordDelimiter(r)
}

// IsLowerWordChar returns whether or not a rune is a 'lower word character'
// Lower word characters are defined as numbers, lower-case letters or sub-word delimiters
func IsLowerWordChar(r rune) bool {
	return IsLowerAlphanumeric(r) || IsSubwordDelimiter(r)
}

// IsSubwordDelimiter returns whether or not a rune is a 'sub-word delimiter character'
// i.e. is considered a part of the word and is used as a delimiter between sub-words of the word.
// For now the only sub-word delimiter character is '_'.
func IsSubwordDelimiter(r rune) bool {
	return r == '_'
}

// IsAlphanumeric returns whether or not a rune is an 'alphanumeric character'
// Alphanumeric characters are defined as numbers or letters
func IsAlphanumeric(r rune) bool {
	return unicode.IsLetter(r) || unicode.IsNumber(r)
}

// IsUpperAlphanumeric returns whether or not a rune is an 'upper alphanumeric character'
// Upper alphanumeric characters are defined as numbers or upper-case letters
func IsUpperAlphanumeric(r rune) bool {
	return IsUpperLetter(r) || unicode.IsNumber(r)
}

// IsLowerAlphanumeric returns whether or not a rune is a 'lower alphanumeric character'
// Lower alphanumeric characters are defined as numbers or lower-case letters
func IsLowerAlphanumeric(r rune) bool {
	return IsLowerLetter(r) || unicode.IsNumber(r)
}