Skip to content

Commit 14e5ea6

Browse files
committed
Precompile port-filter regexp
1 parent bcd8695 commit 14e5ea6

File tree

5 files changed

+29
-14
lines changed

5 files changed

+29
-14
lines changed

serial_darwin.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,15 @@
66

77
package serial
88

9-
import "golang.org/x/sys/unix"
9+
import (
10+
"regexp"
11+
12+
"golang.org/x/sys/unix"
13+
)
1014

1115
const devFolder = "/dev"
12-
const regexFilter = "^(cu|tty)\\..*"
16+
17+
var osPortFiler = regexp.MustCompile("^(cu|tty)\\..*")
1318

1419
const ioctlTcgetattr = unix.TIOCGETA
1520
const ioctlTcsetattr = unix.TIOCSETA

serial_freebsd.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,15 @@
66

77
package serial
88

9-
import "golang.org/x/sys/unix"
9+
import (
10+
"regexp"
11+
12+
"golang.org/x/sys/unix"
13+
)
1014

1115
const devFolder = "/dev"
12-
const regexFilter = "^(cu|tty)\\..*"
16+
17+
var osPortFiler = regexp.MustCompile("^(cu|tty)\\..*")
1318

1419
// termios manipulation functions
1520

serial_linux.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,15 @@
66

77
package serial
88

9-
import "golang.org/x/sys/unix"
9+
import (
10+
"regexp"
11+
12+
"golang.org/x/sys/unix"
13+
)
1014

1115
const devFolder = "/dev"
12-
const regexFilter = "(ttyS|ttyHS|ttyUSB|ttyACM|ttyAMA|rfcomm|ttyO|ttymxc)[0-9]{1,3}"
16+
17+
var osPortFilter = regexp.MustCompile("(ttyS|ttyHS|ttyUSB|ttyACM|ttyAMA|rfcomm|ttyO|ttymxc)[0-9]{1,3}")
1318

1419
// termios manipulation functions
1520

serial_openbsd.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,15 @@
66

77
package serial
88

9-
import "golang.org/x/sys/unix"
9+
import (
10+
"regexp"
11+
12+
"golang.org/x/sys/unix"
13+
)
1014

1115
const devFolder = "/dev"
12-
const regexFilter = "^(cu|tty)\\..*"
16+
17+
var osPortFiler = regexp.MustCompile("^(cu|tty)\\..*")
1318

1419
// termios manipulation functions
1520

serial_unix.go

+1-6
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ package serial
1111
import (
1212
"fmt"
1313
"os"
14-
"regexp"
1514
"strings"
1615
"sync"
1716
"sync/atomic"
@@ -298,18 +297,14 @@ func nativeGetPortsList() ([]string, error) {
298297
}
299298

300299
ports := make([]string, 0, len(files))
301-
regex, err := regexp.Compile(regexFilter)
302-
if err != nil {
303-
return nil, err
304-
}
305300
for _, f := range files {
306301
// Skip folders
307302
if f.IsDir() {
308303
continue
309304
}
310305

311306
// Keep only devices with the correct name
312-
if !regex.MatchString(f.Name()) {
307+
if !osPortFilter.MatchString(f.Name()) {
313308
continue
314309
}
315310

0 commit comments

Comments
 (0)