Don't compile the regex if no valid files were given (eg. all files are directories); print error if file is a binary file (contains NULL character)
This commit is contained in:
@@ -1,6 +1,11 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import "slices"
|
import (
|
||||||
|
"bufio"
|
||||||
|
"os"
|
||||||
|
"slices"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
type character interface {
|
type character interface {
|
||||||
int | rune
|
int | rune
|
||||||
@@ -25,3 +30,15 @@ func genRange[T character](start, end T) []T {
|
|||||||
}
|
}
|
||||||
return toRet
|
return toRet
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Returns whether or not the given file contains a NULL character
|
||||||
|
func fileContainsNullChar(file *os.File) bool {
|
||||||
|
scanner := bufio.NewScanner(file)
|
||||||
|
for scanner.Scan() {
|
||||||
|
line := scanner.Text()
|
||||||
|
if strings.Contains(line, "\000") {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
@@ -101,6 +101,9 @@ func main() {
|
|||||||
} else {
|
} else {
|
||||||
if fileStat.Mode().IsDir() {
|
if fileStat.Mode().IsDir() {
|
||||||
fmt.Printf("%s: %s: Is a directory\n", os.Args[0], inputFilename)
|
fmt.Printf("%s: %s: Is a directory\n", os.Args[0], inputFilename)
|
||||||
|
} else {
|
||||||
|
if fileContainsNullChar(inputFile) {
|
||||||
|
fmt.Printf("%s: %s: Is a binary file\n", os.Args[0], inputFilename)
|
||||||
} else {
|
} else {
|
||||||
inputFiles = append(inputFiles, inputFile)
|
inputFiles = append(inputFiles, inputFile)
|
||||||
}
|
}
|
||||||
@@ -108,6 +111,10 @@ func main() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
if len(inputFiles) == 0 { // No valid files given
|
||||||
|
os.Exit(2)
|
||||||
|
}
|
||||||
|
|
||||||
var test_str string
|
var test_str string
|
||||||
var err error
|
var err error
|
||||||
|
Reference in New Issue
Block a user