2023-10-06 11:30:21 -04:00
|
|
|
package char
|
2021-04-01 16:17:13 -04:00
|
|
|
|
2023-10-04 12:50:21 -04:00
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
"unicode"
|
|
|
|
)
|
2021-04-07 16:26:05 -04:00
|
|
|
|
2021-04-01 16:17:13 -04:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// !Terminal Input Escape Code Sequences
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
const (
|
2023-10-06 11:30:21 -04:00
|
|
|
Backspace = '\x7f'
|
|
|
|
Backslash = '\\'
|
|
|
|
Esc = '\x1b'
|
|
|
|
Enter = '\r'
|
|
|
|
SingleQuote = '\''
|
|
|
|
DoubleQuote = '"'
|
2021-04-01 16:17:13 -04:00
|
|
|
)
|
|
|
|
|
2021-04-13 14:54:44 -04:00
|
|
|
// IsAscii Is this an ASCII character?
|
2021-04-02 19:24:20 -04:00
|
|
|
func IsAscii(char rune) bool {
|
2021-04-07 16:26:05 -04:00
|
|
|
return char <= unicode.MaxASCII
|
2021-04-01 16:17:13 -04:00
|
|
|
}
|
|
|
|
|
2021-04-13 14:54:44 -04:00
|
|
|
// IsCtrl Is this an ASCII ctrl character?
|
2021-04-01 16:17:13 -04:00
|
|
|
func IsCtrl(char rune) bool {
|
2021-04-02 19:24:20 -04:00
|
|
|
if !IsAscii(char) {
|
2021-04-01 16:17:13 -04:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return char == 0x7f || char < 0x20
|
|
|
|
}
|
|
|
|
|
2021-04-13 14:54:44 -04:00
|
|
|
// Ctrl Return the input code of a Ctrl-key chord.
|
2021-04-01 16:17:13 -04:00
|
|
|
func Ctrl(char rune) rune {
|
2021-04-02 19:24:20 -04:00
|
|
|
if !IsAscii(char) {
|
2021-04-01 16:17:13 -04:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
ch := char & 0x1f
|
|
|
|
|
|
|
|
return ch
|
|
|
|
}
|
2021-04-07 16:26:05 -04:00
|
|
|
|
2021-04-13 14:54:44 -04:00
|
|
|
// IsSeparator Is the character a general token separator type?
|
2021-04-07 16:26:05 -04:00
|
|
|
func IsSeparator(char rune) bool {
|
2023-10-04 12:50:21 -04:00
|
|
|
return unicode.IsSpace(char) || strings.ContainsRune(",.()+-/*=~%<>[];", char)
|
2021-04-16 11:46:16 -04:00
|
|
|
}
|
2023-10-06 11:30:21 -04:00
|
|
|
|
|
|
|
// IsDigit is a simple wrapper around built-in unicode.IsDigit
|
|
|
|
func IsDigit(char rune) bool {
|
|
|
|
return unicode.IsDigit(char)
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsNumeric checks whether the character is a digit, or a numeric separator
|
|
|
|
func IsNumeric(char rune) bool {
|
|
|
|
return IsDigit(char) || char == '.'
|
|
|
|
}
|