mirror of
https://github.com/miniflux/v2.git
synced 2025-08-16 18:01:37 +00:00
refactor(parser): centralize seek logic and provide a hint for the compiler to eliminate a useless bound check
- Move the seeking inside of DetectFeedFormat instead of having it everywhere in ParseFeed - Provide a hint for the compiler to eliminate a useless bound check in DetectJSONFormat, otherwise it'll check that buffer[i] is valid on every iteration of the loop. This shouldn't make a big difference, but oh well.
This commit is contained in:
parent
3bb965913d
commit
a43d150a27
2 changed files with 7 additions and 5 deletions
|
@ -22,6 +22,9 @@ const (
|
||||||
|
|
||||||
// DetectFeedFormat tries to guess the feed format from input data.
|
// DetectFeedFormat tries to guess the feed format from input data.
|
||||||
func DetectFeedFormat(r io.ReadSeeker) (string, string) {
|
func DetectFeedFormat(r io.ReadSeeker) (string, string) {
|
||||||
|
r.Seek(0, io.SeekStart)
|
||||||
|
defer r.Seek(0, io.SeekStart)
|
||||||
|
|
||||||
if isJSON, err := detectJSONFormat(r); err == nil && isJSON {
|
if isJSON, err := detectJSONFormat(r); err == nil && isJSON {
|
||||||
return FormatJSON, ""
|
return FormatJSON, ""
|
||||||
}
|
}
|
||||||
|
@ -70,6 +73,10 @@ func detectJSONFormat(r io.ReadSeeker) (bool, error) {
|
||||||
return false, err
|
return false, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if len(buffer) < n {
|
||||||
|
panic("unreachable") // bounds check hint to compiler
|
||||||
|
}
|
||||||
|
|
||||||
// Check each byte in the buffer
|
// Check each byte in the buffer
|
||||||
for i := range n {
|
for i := range n {
|
||||||
ch := buffer[i]
|
ch := buffer[i]
|
||||||
|
|
|
@ -18,20 +18,15 @@ var ErrFeedFormatNotDetected = errors.New("parser: unable to detect feed format"
|
||||||
|
|
||||||
// ParseFeed analyzes the input data and returns a normalized feed object.
|
// ParseFeed analyzes the input data and returns a normalized feed object.
|
||||||
func ParseFeed(baseURL string, r io.ReadSeeker) (*model.Feed, error) {
|
func ParseFeed(baseURL string, r io.ReadSeeker) (*model.Feed, error) {
|
||||||
r.Seek(0, io.SeekStart)
|
|
||||||
format, version := DetectFeedFormat(r)
|
format, version := DetectFeedFormat(r)
|
||||||
switch format {
|
switch format {
|
||||||
case FormatAtom:
|
case FormatAtom:
|
||||||
r.Seek(0, io.SeekStart)
|
|
||||||
return atom.Parse(baseURL, r, version)
|
return atom.Parse(baseURL, r, version)
|
||||||
case FormatRSS:
|
case FormatRSS:
|
||||||
r.Seek(0, io.SeekStart)
|
|
||||||
return rss.Parse(baseURL, r)
|
return rss.Parse(baseURL, r)
|
||||||
case FormatJSON:
|
case FormatJSON:
|
||||||
r.Seek(0, io.SeekStart)
|
|
||||||
return json.Parse(baseURL, r)
|
return json.Parse(baseURL, r)
|
||||||
case FormatRDF:
|
case FormatRDF:
|
||||||
r.Seek(0, io.SeekStart)
|
|
||||||
return rdf.Parse(baseURL, r)
|
return rdf.Parse(baseURL, r)
|
||||||
default:
|
default:
|
||||||
return nil, ErrFeedFormatNotDetected
|
return nil, ErrFeedFormatNotDetected
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue