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

Code before:
func FSize(f *os.File) int64 {
	fi, _ := f.Stat()
	return fi.Size()
}

// IsWordChar returns whether or not the string is a 'word character'
// 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)

Review comment (bug):
IMHO this function is redundant and actually makes the code less readable (what is "word delimiter"? isn't it rather a "subword delimiter"? etc). What's wrong with using simple explicit `== '_'` checks instead?

Code after:

// FSize gets the size of a file
func FSize(f *os.File) int64 {
	fi, _ := f.Stat()
	return fi.Size()
}

// 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)
}