mirror of
https://github.com/zyedidia/micro.git
synced 2026-03-25 18:07:07 +09:00
syntax parser: reintroduce header regex in .hdr files
Replacing header patterns with signature patterns was a mistake, since both have their own uses. So restore support for header regex, while keeping support for signature regex as well.
This commit is contained in:
@@ -39,6 +39,7 @@ type Def struct {
|
|||||||
type Header struct {
|
type Header struct {
|
||||||
FileType string
|
FileType string
|
||||||
FileNameRegex *regexp.Regexp
|
FileNameRegex *regexp.Regexp
|
||||||
|
HeaderRegex *regexp.Regexp
|
||||||
SignatureRegex *regexp.Regexp
|
SignatureRegex *regexp.Regexp
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -46,6 +47,7 @@ type HeaderYaml struct {
|
|||||||
FileType string `yaml:"filetype"`
|
FileType string `yaml:"filetype"`
|
||||||
Detect struct {
|
Detect struct {
|
||||||
FNameRegexStr string `yaml:"filename"`
|
FNameRegexStr string `yaml:"filename"`
|
||||||
|
HeaderRegexStr string `yaml:"header"`
|
||||||
SignatureRegexStr string `yaml:"signature"`
|
SignatureRegexStr string `yaml:"signature"`
|
||||||
} `yaml:"detect"`
|
} `yaml:"detect"`
|
||||||
}
|
}
|
||||||
@@ -96,18 +98,22 @@ func init() {
|
|||||||
// A yaml file might take ~400us to parse while a header file only takes ~20us
|
// A yaml file might take ~400us to parse while a header file only takes ~20us
|
||||||
func MakeHeader(data []byte) (*Header, error) {
|
func MakeHeader(data []byte) (*Header, error) {
|
||||||
lines := bytes.Split(data, []byte{'\n'})
|
lines := bytes.Split(data, []byte{'\n'})
|
||||||
if len(lines) < 3 {
|
if len(lines) < 4 {
|
||||||
return nil, errors.New("Header file has incorrect format")
|
return nil, errors.New("Header file has incorrect format")
|
||||||
}
|
}
|
||||||
header := new(Header)
|
header := new(Header)
|
||||||
var err error
|
var err error
|
||||||
header.FileType = string(lines[0])
|
header.FileType = string(lines[0])
|
||||||
fnameRegexStr := string(lines[1])
|
fnameRegexStr := string(lines[1])
|
||||||
signatureRegexStr := string(lines[2])
|
headerRegexStr := string(lines[2])
|
||||||
|
signatureRegexStr := string(lines[3])
|
||||||
|
|
||||||
if fnameRegexStr != "" {
|
if fnameRegexStr != "" {
|
||||||
header.FileNameRegex, err = regexp.Compile(fnameRegexStr)
|
header.FileNameRegex, err = regexp.Compile(fnameRegexStr)
|
||||||
}
|
}
|
||||||
|
if err == nil && headerRegexStr != "" {
|
||||||
|
header.HeaderRegex, err = regexp.Compile(headerRegexStr)
|
||||||
|
}
|
||||||
if err == nil && signatureRegexStr != "" {
|
if err == nil && signatureRegexStr != "" {
|
||||||
header.SignatureRegex, err = regexp.Compile(signatureRegexStr)
|
header.SignatureRegex, err = regexp.Compile(signatureRegexStr)
|
||||||
}
|
}
|
||||||
@@ -134,6 +140,9 @@ func MakeHeaderYaml(data []byte) (*Header, error) {
|
|||||||
if hdrYaml.Detect.FNameRegexStr != "" {
|
if hdrYaml.Detect.FNameRegexStr != "" {
|
||||||
header.FileNameRegex, err = regexp.Compile(hdrYaml.Detect.FNameRegexStr)
|
header.FileNameRegex, err = regexp.Compile(hdrYaml.Detect.FNameRegexStr)
|
||||||
}
|
}
|
||||||
|
if err == nil && hdrYaml.Detect.HeaderRegexStr != "" {
|
||||||
|
header.HeaderRegex, err = regexp.Compile(hdrYaml.Detect.HeaderRegexStr)
|
||||||
|
}
|
||||||
if err == nil && hdrYaml.Detect.SignatureRegexStr != "" {
|
if err == nil && hdrYaml.Detect.SignatureRegexStr != "" {
|
||||||
header.SignatureRegex, err = regexp.Compile(hdrYaml.Detect.SignatureRegexStr)
|
header.SignatureRegex, err = regexp.Compile(hdrYaml.Detect.SignatureRegexStr)
|
||||||
}
|
}
|
||||||
@@ -154,6 +163,14 @@ func (header *Header) MatchFileName(filename string) bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (header *Header) MatchFileHeader(firstLine []byte) bool {
|
||||||
|
if header.HeaderRegex != nil {
|
||||||
|
return header.HeaderRegex.Match(firstLine)
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
// HasFileSignature checks the presence of a stored signature
|
// HasFileSignature checks the presence of a stored signature
|
||||||
func (header *Header) HasFileSignature() bool {
|
func (header *Header) HasFileSignature() bool {
|
||||||
return header.SignatureRegex != nil
|
return header.SignatureRegex != nil
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ type HeaderYaml struct {
|
|||||||
FileType string `yaml:"filetype"`
|
FileType string `yaml:"filetype"`
|
||||||
Detect struct {
|
Detect struct {
|
||||||
FNameRgx string `yaml:"filename"`
|
FNameRgx string `yaml:"filename"`
|
||||||
|
HeaderRgx string `yaml:"header"`
|
||||||
SignatureRgx string `yaml:"signature"`
|
SignatureRgx string `yaml:"signature"`
|
||||||
} `yaml:"detect"`
|
} `yaml:"detect"`
|
||||||
}
|
}
|
||||||
@@ -25,6 +26,7 @@ type HeaderYaml struct {
|
|||||||
type Header struct {
|
type Header struct {
|
||||||
FileType string
|
FileType string
|
||||||
FNameRgx string
|
FNameRgx string
|
||||||
|
HeaderRgx string
|
||||||
SignatureRgx string
|
SignatureRgx string
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -59,6 +61,7 @@ func encode(name string, c HeaderYaml) {
|
|||||||
f, _ := os.Create(name + ".hdr")
|
f, _ := os.Create(name + ".hdr")
|
||||||
f.WriteString(c.FileType + "\n")
|
f.WriteString(c.FileType + "\n")
|
||||||
f.WriteString(c.Detect.FNameRgx + "\n")
|
f.WriteString(c.Detect.FNameRgx + "\n")
|
||||||
|
f.WriteString(c.Detect.HeaderRgx + "\n")
|
||||||
f.WriteString(c.Detect.SignatureRgx + "\n")
|
f.WriteString(c.Detect.SignatureRgx + "\n")
|
||||||
f.Close()
|
f.Close()
|
||||||
}
|
}
|
||||||
@@ -70,7 +73,8 @@ func decode(name string) Header {
|
|||||||
var hdr Header
|
var hdr Header
|
||||||
hdr.FileType = string(strs[0])
|
hdr.FileType = string(strs[0])
|
||||||
hdr.FNameRgx = string(strs[1])
|
hdr.FNameRgx = string(strs[1])
|
||||||
hdr.SignatureRgx = string(strs[2])
|
hdr.HeaderRgx = string(strs[2])
|
||||||
|
hdr.SignatureRgx = string(strs[3])
|
||||||
fmt.Printf("took %v\n", time.Since(start))
|
fmt.Printf("took %v\n", time.Since(start))
|
||||||
|
|
||||||
return hdr
|
return hdr
|
||||||
|
|||||||
Reference in New Issue
Block a user