-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathhelpers.go
More file actions
41 lines (37 loc) · 1.03 KB
/
helpers.go
File metadata and controls
41 lines (37 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// Copyright (c) 2017-2026 The visa developers. All rights reserved.
// Project site: https://github.com/gotmc/visa
// Use of this source code is governed by a MIT-style license that
// can be found in the LICENSE.txt file for the project.
package visa
import (
"fmt"
"regexp"
"strings"
)
var addressRe = regexp.MustCompile(
`^(?P<interfaceType>[A-Za-z]+)(?P<boardIndex>\d*)::(?P<allElse>.*)$`,
)
func determineInterfaceType(address string) (InterfaceType, error) {
res := addressRe.FindStringSubmatch(address)
if res == nil {
return UNKNOWN, fmt.Errorf("%w: %q", ErrInvalidAddress, address)
}
subexpNames := addressRe.SubexpNames()
matchMap := map[string]string{}
for i, n := range res {
matchMap[subexpNames[i]] = string(n)
}
interfaceType := strings.ToUpper(matchMap["interfaceType"])
switch interfaceType {
case "USB":
return USBTMC, nil
case "TCPIP":
return TCPIP, nil
case "ASRL":
return ASRL, nil
default:
return UNKNOWN, fmt.Errorf(
"%w: %q in address %q", ErrUnknownInterfaceType, interfaceType, address,
)
}
}