github.com/hugh712/snapd@v0.0.0-20200910133618-1a99902bd583/spdx/scanner.go (about) 1 // -*- Mode: Go; indent-tabs-mode: t -*- 2 3 /* 4 * Copyright (C) 2016 Canonical Ltd 5 * 6 * This program is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License version 3 as 8 * published by the Free Software Foundation. 9 * 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * 15 * You should have received a copy of the GNU General Public License 16 * along with this program. If not, see <http://www.gnu.org/licenses/>. 17 * 18 */ 19 20 package spdx 21 22 import ( 23 "bufio" 24 "io" 25 ) 26 27 type Scanner struct { 28 *bufio.Scanner 29 } 30 31 func spdxSplit(data []byte, atEOF bool) (advance int, token []byte, err error) { 32 // skip WS 33 start := 0 34 for ; start < len(data); start++ { 35 if data[start] != ' ' && data[start] != '\n' { 36 break 37 } 38 } 39 if start == len(data) { 40 return start, nil, nil 41 } 42 43 switch data[start] { 44 // found ( or ) 45 case '(', ')': 46 return start + 1, data[start : start+1], nil 47 } 48 49 for i := start; i < len(data); i++ { 50 switch data[i] { 51 // token finished 52 case ' ', '\n': 53 return i + 1, data[start:i], nil 54 // found ( or ) - we need to rescan it 55 case '(', ')': 56 return i, data[start:i], nil 57 } 58 } 59 if atEOF && len(data) > start { 60 return len(data), data[start:], nil 61 } 62 return start, nil, nil 63 } 64 65 func NewScanner(r io.Reader) *Scanner { 66 scanner := bufio.NewScanner(r) 67 scanner.Split(spdxSplit) 68 return &Scanner{scanner} 69 }