go-hep.org/x/hep@v0.38.1/hbook/yoda.go (about)

     1  // Copyright ©2018 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 hbook // import "go-hep.org/x/hep/hbook"
     6  
     7  import (
     8  	"bytes"
     9  	"fmt"
    10  	"strings"
    11  )
    12  
    13  // readYODAHeader parses the input buffer and extracts the YODA header line
    14  // from that buffer.
    15  // readYODAHeader returns the associated YODA path and an error if any.
    16  func readYODAHeader(r *rbuffer, hdr string) (string, int, error) {
    17  	pos := bytes.Index(r.Bytes(), []byte("\n"))
    18  	if pos < 0 {
    19  		return "", 0, fmt.Errorf("hbook: could not find %s line", hdr)
    20  	}
    21  	var (
    22  		path = string(r.next(pos + 1))
    23  		vers int
    24  	)
    25  	switch {
    26  	case strings.HasPrefix(path, hdr+"_V2 "):
    27  		hdr += "_V2"
    28  		vers = 2
    29  	case strings.HasPrefix(path, hdr+" "):
    30  		vers = 1
    31  	default:
    32  		return "", 0, fmt.Errorf("hbook: could not find %s mark", hdr)
    33  	}
    34  
    35  	return path[len(hdr)+1 : len(path)-1], vers, nil
    36  }