go-hep.org/x/hep@v0.38.1/hepmc/utils.go (about) 1 // Copyright ©2017 The go-hep Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package hepmc 6 7 import ( 8 "strconv" 9 "strings" 10 ) 11 12 type tokens struct { 13 toks []string 14 pos int 15 } 16 17 func newtokens(toks []string) tokens { 18 return tokens{ 19 toks: toks, 20 pos: 0, 21 } 22 } 23 24 func (t *tokens) next() string { 25 if t.pos >= len(t.toks) { 26 return "" 27 } 28 str := t.toks[t.pos] 29 t.pos++ 30 return str 31 } 32 33 func (t *tokens) at(i int) string { 34 return t.toks[i] 35 } 36 37 func (t *tokens) float64() (float64, error) { 38 s := t.next() 39 return strconv.ParseFloat(s, 64) 40 } 41 42 func (t *tokens) float32() (float32, error) { 43 s := t.next() 44 v, err := strconv.ParseFloat(s, 64) 45 return float32(v), err 46 } 47 48 func (t *tokens) int() (int, error) { 49 s := t.next() 50 return strconv.Atoi(s) 51 } 52 53 func (t *tokens) int64() (int64, error) { 54 s := t.next() 55 return strconv.ParseInt(s, 10, 0) 56 } 57 58 func (t *tokens) String() string { 59 return strings.Join(t.toks, " ") 60 }