From 14e5ea68ce7e08e3fc27ff237e66d41ed3e502ae Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Tue, 20 Feb 2024 15:24:08 +0100 Subject: [PATCH] Precompile port-filter regexp --- serial_darwin.go | 9 +++++++-- serial_freebsd.go | 9 +++++++-- serial_linux.go | 9 +++++++-- serial_openbsd.go | 9 +++++++-- serial_unix.go | 7 +------ 5 files changed, 29 insertions(+), 14 deletions(-) diff --git a/serial_darwin.go b/serial_darwin.go index 2817041..3baa489 100644 --- a/serial_darwin.go +++ b/serial_darwin.go @@ -6,10 +6,15 @@ package serial -import "golang.org/x/sys/unix" +import ( + "regexp" + + "golang.org/x/sys/unix" +) const devFolder = "/dev" -const regexFilter = "^(cu|tty)\\..*" + +var osPortFiler = regexp.MustCompile("^(cu|tty)\\..*") const ioctlTcgetattr = unix.TIOCGETA const ioctlTcsetattr = unix.TIOCSETA diff --git a/serial_freebsd.go b/serial_freebsd.go index aceb08a..be0fa01 100644 --- a/serial_freebsd.go +++ b/serial_freebsd.go @@ -6,10 +6,15 @@ package serial -import "golang.org/x/sys/unix" +import ( + "regexp" + + "golang.org/x/sys/unix" +) const devFolder = "/dev" -const regexFilter = "^(cu|tty)\\..*" + +var osPortFiler = regexp.MustCompile("^(cu|tty)\\..*") // termios manipulation functions diff --git a/serial_linux.go b/serial_linux.go index fe114d9..93482cd 100644 --- a/serial_linux.go +++ b/serial_linux.go @@ -6,10 +6,15 @@ package serial -import "golang.org/x/sys/unix" +import ( + "regexp" + + "golang.org/x/sys/unix" +) const devFolder = "/dev" -const regexFilter = "(ttyS|ttyHS|ttyUSB|ttyACM|ttyAMA|rfcomm|ttyO|ttymxc)[0-9]{1,3}" + +var osPortFilter = regexp.MustCompile("(ttyS|ttyHS|ttyUSB|ttyACM|ttyAMA|rfcomm|ttyO|ttymxc)[0-9]{1,3}") // termios manipulation functions diff --git a/serial_openbsd.go b/serial_openbsd.go index 644dc92..e56a760 100644 --- a/serial_openbsd.go +++ b/serial_openbsd.go @@ -6,10 +6,15 @@ package serial -import "golang.org/x/sys/unix" +import ( + "regexp" + + "golang.org/x/sys/unix" +) const devFolder = "/dev" -const regexFilter = "^(cu|tty)\\..*" + +var osPortFiler = regexp.MustCompile("^(cu|tty)\\..*") // termios manipulation functions diff --git a/serial_unix.go b/serial_unix.go index 9328656..dffdcb4 100644 --- a/serial_unix.go +++ b/serial_unix.go @@ -11,7 +11,6 @@ package serial import ( "fmt" "os" - "regexp" "strings" "sync" "sync/atomic" @@ -298,10 +297,6 @@ func nativeGetPortsList() ([]string, error) { } ports := make([]string, 0, len(files)) - regex, err := regexp.Compile(regexFilter) - if err != nil { - return nil, err - } for _, f := range files { // Skip folders if f.IsDir() { @@ -309,7 +304,7 @@ func nativeGetPortsList() ([]string, error) { } // Keep only devices with the correct name - if !regex.MatchString(f.Name()) { + if !osPortFilter.MatchString(f.Name()) { continue }