Precompile port-filter regexp

This commit is contained in:
Cristian Maglie
2024-02-20 15:24:08 +01:00
parent bcd8695df4
commit 14e5ea68ce
5 changed files with 29 additions and 14 deletions

View File

@@ -6,10 +6,15 @@
package serial package serial
import "golang.org/x/sys/unix" import (
"regexp"
"golang.org/x/sys/unix"
)
const devFolder = "/dev" const devFolder = "/dev"
const regexFilter = "^(cu|tty)\\..*"
var osPortFiler = regexp.MustCompile("^(cu|tty)\\..*")
const ioctlTcgetattr = unix.TIOCGETA const ioctlTcgetattr = unix.TIOCGETA
const ioctlTcsetattr = unix.TIOCSETA const ioctlTcsetattr = unix.TIOCSETA

View File

@@ -6,10 +6,15 @@
package serial package serial
import "golang.org/x/sys/unix" import (
"regexp"
"golang.org/x/sys/unix"
)
const devFolder = "/dev" const devFolder = "/dev"
const regexFilter = "^(cu|tty)\\..*"
var osPortFiler = regexp.MustCompile("^(cu|tty)\\..*")
// termios manipulation functions // termios manipulation functions

View File

@@ -6,10 +6,15 @@
package serial package serial
import "golang.org/x/sys/unix" import (
"regexp"
"golang.org/x/sys/unix"
)
const devFolder = "/dev" 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 // termios manipulation functions

View File

@@ -6,10 +6,15 @@
package serial package serial
import "golang.org/x/sys/unix" import (
"regexp"
"golang.org/x/sys/unix"
)
const devFolder = "/dev" const devFolder = "/dev"
const regexFilter = "^(cu|tty)\\..*"
var osPortFiler = regexp.MustCompile("^(cu|tty)\\..*")
// termios manipulation functions // termios manipulation functions

View File

@@ -11,7 +11,6 @@ package serial
import ( import (
"fmt" "fmt"
"os" "os"
"regexp"
"strings" "strings"
"sync" "sync"
"sync/atomic" "sync/atomic"
@@ -298,10 +297,6 @@ func nativeGetPortsList() ([]string, error) {
} }
ports := make([]string, 0, len(files)) ports := make([]string, 0, len(files))
regex, err := regexp.Compile(regexFilter)
if err != nil {
return nil, err
}
for _, f := range files { for _, f := range files {
// Skip folders // Skip folders
if f.IsDir() { if f.IsDir() {
@@ -309,7 +304,7 @@ func nativeGetPortsList() ([]string, error) {
} }
// Keep only devices with the correct name // Keep only devices with the correct name
if !regex.MatchString(f.Name()) { if !osPortFilter.MatchString(f.Name()) {
continue continue
} }