From 25a9426221426a8c434214749d3dd9e409c7df5e Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Tue, 29 Jun 2021 16:40:56 +0200 Subject: [PATCH] Move regex compile outside loop --- serial_unix.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/serial_unix.go b/serial_unix.go index f9e887c..e4dad89 100644 --- a/serial_unix.go +++ b/serial_unix.go @@ -220,6 +220,10 @@ 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() { @@ -227,11 +231,7 @@ func nativeGetPortsList() ([]string, error) { } // Keep only devices with the correct name - match, err := regexp.MatchString(regexFilter, f.Name()) - if err != nil { - return nil, err - } - if !match { + if !regex.MatchString(f.Name()) { continue }