github.com/rck/u-root@v0.0.0-20180106144920-7eb602e381bb/pkg/pci/parse.go (about) 1 package pci 2 3 import ( 4 "bufio" 5 "bytes" 6 ) 7 8 func isHex(b byte) bool { 9 return ('a' <= b && b <= 'f') || ('A' <= b && b <= 'F') || ('0' <= b && b <= '9') 10 } 11 12 // scan searches for Vendor and Device lines from the input *bufio.Scanner based 13 // on pci.ids format. Found Vendors and Devices are added to the input ids map. 14 func scan(s *bufio.Scanner, ids map[string]Vendor) { 15 var currentVendor string 16 var line string 17 18 for s.Scan() { 19 line = s.Text() 20 21 switch { 22 case isHex(line[0]) && isHex(line[1]): 23 currentVendor = line[:4] 24 ids[currentVendor] = Vendor{Name: line[6:], Devices: make(map[string]Device)} 25 case currentVendor != "" && line[0] == '\t' && isHex(line[1]) && isHex(line[3]): 26 ids[currentVendor].Devices[line[1:5]] = Device(line[7:]) 27 } 28 } 29 } 30 31 func parse(input []byte) idMap { 32 ids := make(map[string]Vendor) 33 s := bufio.NewScanner(bytes.NewReader(input)) 34 scan(s, ids) 35 return ids 36 }