Skip to content

Commit

Permalink
Remove unnecessary loop.
Browse files Browse the repository at this point in the history
Loops were returning directly.

Fixes #181.
  • Loading branch information
alecthomas committed Dec 31, 2018
1 parent c4bec47 commit e27f19c
Showing 1 changed file with 41 additions and 42 deletions.
83 changes: 41 additions & 42 deletions lexers/h/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,56 +77,55 @@ func (d *httpBodyContentTyper) Tokenise(options *TokeniseOptions, text string) (
}

return func() Token {
for token := it(); token != EOF; token = it() {
switch {
case token.Type == Name && strings.ToLower(token.Value) == "content-type":
{
isContentType = true
token := it()

if token == EOF {
if subIterator != nil {
return subIterator()
}
return EOF
}

switch {
case token.Type == Name && strings.ToLower(token.Value) == "content-type":
{
isContentType = true
}
case token.Type == Literal && isContentType:
{
isContentType = false
contentType = strings.TrimSpace(token.Value)
pos := strings.Index(contentType, ";")
if pos > 0 {
contentType = strings.TrimSpace(contentType[:pos])
}
case token.Type == Literal && isContentType:
{
isContentType = false
contentType = strings.TrimSpace(token.Value)
pos := strings.Index(contentType, ";")
if pos > 0 {
contentType = strings.TrimSpace(contentType[:pos])
}
}
case token.Type == Generic && contentType != "":
{
lexer := internal.MatchMimeType(contentType)

// application/calendar+xml can be treated as application/xml
// if there's not a better match.
if lexer == nil && strings.Contains(contentType, "+") {
slashPos := strings.Index(contentType, "/")
plusPos := strings.LastIndex(contentType, "+")
contentType = contentType[:slashPos+1] + contentType[plusPos+1:]
lexer = internal.MatchMimeType(contentType)
}
case token.Type == Generic && contentType != "":
{
lexer := internal.MatchMimeType(contentType)

// application/calendar+xml can be treated as application/xml
// if there's not a better match.
if lexer == nil && strings.Contains(contentType, "+") {
slashPos := strings.Index(contentType, "/")
plusPos := strings.LastIndex(contentType, "+")
contentType = contentType[:slashPos+1] + contentType[plusPos+1:]
lexer = internal.MatchMimeType(contentType)
}

if lexer == nil {
token.Type = Text
} else {
subIterator, err = lexer.Tokenise(nil, token.Value)
if err != nil {
panic(err)
}
return EOF
if lexer == nil {
token.Type = Text
} else {
subIterator, err = lexer.Tokenise(nil, token.Value)
if err != nil {
panic(err)
}
return EOF
}

}

return token
}

if subIterator != nil {
for token := subIterator(); token != EOF; token = subIterator() {
return token
}
}
return EOF
return token

}, nil
}

0 comments on commit e27f19c

Please sign in to comment.