Skip to content

Commit a8a611e

Browse files
committed
[uni.go] added support for Unicode code point (hexadecimal) to glyph search, added Unicode code point range check function
1 parent a033534 commit a8a611e

File tree

1 file changed

+48
-19
lines changed

1 file changed

+48
-19
lines changed

uni.go

Lines changed: 48 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -88,43 +88,39 @@ func main() {
8888
if isGlyphSearch {
8989
stdinList := strings.Split(tmp.String(), ` `)
9090
for _, arg := range stdinList {
91-
i, err := strconv.ParseInt(arg, 16, 32)
91+
stdoutString, err := glyphSearch(arg)
9292
if err != nil {
93-
os.Stderr.WriteString("[Error] Unable to parse the Unicode code point value '" + arg + "'\n")
93+
errmsg := fmt.Sprintf("%v", err)
94+
os.Stderr.WriteString("[Error] Unable to parse the Unicode code point request '" + arg + "' to a glyph. " + errmsg + "\n")
9495
os.Exit(1)
9596
}
96-
// TODO: add check for printable glyph range integer value
97-
r := rune(i)
98-
stdoutString := "U+" + arg + " '" + string(r) + "'"
9997
fmt.Println(stdoutString)
10098
}
10199

102100
} else { // stdin stream search for Unicode code point from glyph search request
103101
stdinList := strings.Split(tmp.String(), "") // split the stdin string by glyph to a slice
104-
stdOutput := unicodeSearch(stdinList)
105-
for _, line := range stdOutput {
106-
fmt.Println(line)
102+
stdOutputList := unicodeSearch(stdinList)
103+
for _, stdoutString := range stdOutputList {
104+
fmt.Println(stdoutString)
107105
}
108106
}
109107

110108
} else {
111109
// argument search for glyph from Unicode code point search request
112110
if isGlyphSearch {
113111
for _, arg := range os.Args[2:] {
114-
i, err := strconv.ParseInt(arg, 16, 32)
112+
stdoutString, err := glyphSearch(arg)
115113
if err != nil {
116-
os.Stderr.WriteString("[Error] Unable to parse the Unicode code point value '" + arg + "'\n")
114+
errmsg := fmt.Sprintf("%v", err)
115+
os.Stderr.WriteString("[Error] Unable to parse the Unicode code point request '" + arg + "' to a glyph. " + errmsg + "\n")
117116
os.Exit(1)
118117
}
119-
// TODO: add check for printable glyph range integer value
120-
r := rune(i)
121-
stdoutString := "U+" + arg + " '" + string(r) + "'"
122118
fmt.Println(stdoutString)
123119
}
124-
} else { // argument search for Unicode code point from glyph search request
125-
stdOutput := unicodeSearch(os.Args[1:])
126-
for _, line := range stdOutput {
127-
fmt.Println(line)
120+
} else { // argument search for Unicode code point from glyph search request
121+
stdOutputList := unicodeSearch(os.Args[1:])
122+
for _, stdoutString := range stdOutputList {
123+
fmt.Println(stdoutString)
128124
}
129125
}
130126

@@ -148,12 +144,45 @@ func stdinValidates(stdin *os.File) bool {
148144
}
149145

150146
func handleStdInErrors() {
151-
os.Stderr.WriteString("[Error] Please include at least one argument or pipe a string to the executable through the stdin stream.\n")
147+
os.Stderr.WriteString("[Error] Please include at least one argument or pipe search requests to the executable through the stdin stream.\n")
152148
os.Stderr.WriteString(usage)
153149
os.Exit(1)
154150
}
155151

156-
// unicodeSearch decodes runs in command line requests to formatted Unicode code point values for stdout stream print
152+
func isIntInRange(value int32) bool {
153+
minPrintableInt, _ := strconv.ParseInt("0020", 16, 32)
154+
155+
if value > utf8.MaxRune {
156+
// value is higher than maximum acceptable Unicode code point
157+
return false
158+
}
159+
160+
if value < int32(minPrintableInt) {
161+
// value is lower than minimum Unicode code point for printable glyphs
162+
return false
163+
}
164+
// return true if integer value falls within acceptable range
165+
return true
166+
}
167+
168+
// glyphSearch identifies glyphs for Unicode code point (hexadecimal string) search requests
169+
func glyphSearch(arg string) (string, error) {
170+
i, err := strconv.ParseInt(arg, 16, 32)
171+
if err != nil {
172+
return "", err
173+
}
174+
// test to confirm that Unicode code point falls in range U+0020 = min printable glyph to utf8.MaxRune value
175+
ok := isIntInRange(int32(i))
176+
if !ok {
177+
errmsg := fmt.Errorf("Hexadecimal value '" + arg + "' was out of range.")
178+
return "", errmsg
179+
}
180+
r := rune(i)
181+
stdoutString := "U+" + arg + " '" + string(r) + "'"
182+
return stdoutString, nil
183+
}
184+
185+
// unicodeSearch identifies the Unicode code point for glyph search requests
157186
func unicodeSearch(argv []string) []string {
158187
var solist []string
159188
for i := 0; i < len(argv); i++ {

0 commit comments

Comments
 (0)