Skip to content

Commit

Permalink
Optimize type handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Sean-Der committed Mar 6, 2024
1 parent 37a57e6 commit 0eee6f5
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 110 deletions.
36 changes: 9 additions & 27 deletions base_lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,45 +178,27 @@ func (l *baseLexer) readLine() (string, error) {
}
}

func (l *baseLexer) readString(until byte) (string, error) {
start := l.pos
func (l *baseLexer) readType() (byte, error) {
for {
ch, err := l.readByte()
firstByte, err := l.readByte()
if err != nil {
return "", err
}
if ch == until {
return string(l.value[start:l.pos]), nil
return 0, err
}
}
}

func (l *baseLexer) readType() (string, error) {
for {
b, err := l.readByte()
if err != nil {
return "", err
}

if isNewline(b) {
if isNewline(firstByte) {
continue
}

err = l.unreadByte()
if err != nil {
return "", err
}

key, err := l.readString('=')
secondByte, err := l.readByte()
if err != nil {
return key, err
return 0, err
}

if len(key) == 2 {
return key, nil
if secondByte != '=' {
return firstByte, l.syntaxError()
}

return key, l.syntaxError()
return firstByte, nil
}
}

Expand Down
162 changes: 81 additions & 81 deletions unmarshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,260 +114,260 @@ func (s *SessionDescription) Unmarshal(value []byte) error {
}

func s1(l *lexer) (stateFn, error) {
return l.handleType(func(key string) stateFn {
if key == "v=" {
return l.handleType(func(key byte) stateFn {
if key == 'v' {
return unmarshalProtocolVersion
}
return nil
})
}

func s2(l *lexer) (stateFn, error) {
return l.handleType(func(key string) stateFn {
if key == "o=" {
return l.handleType(func(key byte) stateFn {
if key == 'o' {
return unmarshalOrigin
}
return nil
})
}

func s3(l *lexer) (stateFn, error) {
return l.handleType(func(key string) stateFn {
if key == "s=" {
return l.handleType(func(key byte) stateFn {
if key == 's' {
return unmarshalSessionName
}
return nil
})
}

func s4(l *lexer) (stateFn, error) {
return l.handleType(func(key string) stateFn {
return l.handleType(func(key byte) stateFn {
switch key {
case "i=":
case 'i':
return unmarshalSessionInformation
case "u=":
case 'u':
return unmarshalURI
case "e=":
case 'e':
return unmarshalEmail
case "p=":
case 'p':
return unmarshalPhone
case "c=":
case 'c':
return unmarshalSessionConnectionInformation
case "b=":
case 'b':
return unmarshalSessionBandwidth
case "t=":
case 't':
return unmarshalTiming
}
return nil
})
}

func s5(l *lexer) (stateFn, error) {
return l.handleType(func(key string) stateFn {
return l.handleType(func(key byte) stateFn {
switch key {
case "b=":
case 'b':
return unmarshalSessionBandwidth
case "t=":
case 't':
return unmarshalTiming
}
return nil
})
}

func s6(l *lexer) (stateFn, error) {
return l.handleType(func(key string) stateFn {
return l.handleType(func(key byte) stateFn {
switch key {
case "p=":
case 'p':
return unmarshalPhone
case "c=":
case 'c':
return unmarshalSessionConnectionInformation
case "b=":
case 'b':
return unmarshalSessionBandwidth
case "t=":
case 't':
return unmarshalTiming
}
return nil
})
}

func s7(l *lexer) (stateFn, error) {
return l.handleType(func(key string) stateFn {
return l.handleType(func(key byte) stateFn {
switch key {
case "u=":
case 'u':
return unmarshalURI
case "e=":
case 'e':
return unmarshalEmail
case "p=":
case 'p':
return unmarshalPhone
case "c=":
case 'c':
return unmarshalSessionConnectionInformation
case "b=":
case 'b':
return unmarshalSessionBandwidth
case "t=":
case 't':
return unmarshalTiming
}
return nil
})
}

func s8(l *lexer) (stateFn, error) {
return l.handleType(func(key string) stateFn {
return l.handleType(func(key byte) stateFn {
switch key {
case "c=":
case 'c':
return unmarshalSessionConnectionInformation
case "b=":
case 'b':
return unmarshalSessionBandwidth
case "t=":
case 't':
return unmarshalTiming
}
return nil
})
}

func s9(l *lexer) (stateFn, error) {
return l.handleType(func(key string) stateFn {
return l.handleType(func(key byte) stateFn {
switch key {
case "z=":
case 'z':
return unmarshalTimeZones
case "k=":
case 'k':
return unmarshalSessionEncryptionKey
case "a=":
case 'a':
return unmarshalSessionAttribute
case "r=":
case 'r':
return unmarshalRepeatTimes
case "t=":
case 't':
return unmarshalTiming
case "m=":
case 'm':
return unmarshalMediaDescription
}
return nil
})
}

func s10(l *lexer) (stateFn, error) {
return l.handleType(func(key string) stateFn {
return l.handleType(func(key byte) stateFn {
switch key {
case "e=":
case 'e':
return unmarshalEmail
case "p=":
case 'p':
return unmarshalPhone
case "c=":
case 'c':
return unmarshalSessionConnectionInformation
case "b=":
case 'b':
return unmarshalSessionBandwidth
case "t=":
case 't':
return unmarshalTiming
}
return nil
})
}

func s11(l *lexer) (stateFn, error) {
return l.handleType(func(key string) stateFn {
return l.handleType(func(key byte) stateFn {
switch key {
case "a=":
case 'a':
return unmarshalSessionAttribute
case "m=":
case 'm':
return unmarshalMediaDescription
}
return nil
})
}

func s12(l *lexer) (stateFn, error) {
return l.handleType(func(key string) stateFn {
return l.handleType(func(key byte) stateFn {
switch key {
case "a=":
case 'a':
return unmarshalMediaAttribute
case "k=":
case 'k':
return unmarshalMediaEncryptionKey
case "b=":
case 'b':
return unmarshalMediaBandwidth
case "c=":
case 'c':
return unmarshalMediaConnectionInformation
case "i=":
case 'i':
return unmarshalMediaTitle
case "m=":
case 'm':
return unmarshalMediaDescription
}
return nil
})
}

func s13(l *lexer) (stateFn, error) {
return l.handleType(func(key string) stateFn {
return l.handleType(func(key byte) stateFn {
switch key {
case "a=":
case 'a':
return unmarshalSessionAttribute
case "k=":
case 'k':
return unmarshalSessionEncryptionKey
case "m=":
case 'm':
return unmarshalMediaDescription
}
return nil
})
}

func s14(l *lexer) (stateFn, error) {
return l.handleType(func(key string) stateFn {
return l.handleType(func(key byte) stateFn {
switch key {
case "a=":
case 'a':
return unmarshalMediaAttribute
case "k=":
case 'k':
// Non-spec ordering
return unmarshalMediaEncryptionKey
case "b=":
case 'b':
// Non-spec ordering
return unmarshalMediaBandwidth
case "c=":
case 'c':
// Non-spec ordering
return unmarshalMediaConnectionInformation
case "i=":
case 'i':
// Non-spec ordering
return unmarshalMediaTitle
case "m=":
case 'm':
return unmarshalMediaDescription
}
return nil
})
}

func s15(l *lexer) (stateFn, error) {
return l.handleType(func(key string) stateFn {
return l.handleType(func(key byte) stateFn {
switch key {
case "a=":
case 'a':
return unmarshalMediaAttribute
case "k=":
case 'k':
return unmarshalMediaEncryptionKey
case "b=":
case 'b':
return unmarshalMediaBandwidth
case "c=":
case 'c':
return unmarshalMediaConnectionInformation
case "i=":
case 'i':
// Non-spec ordering
return unmarshalMediaTitle
case "m=":
case 'm':
return unmarshalMediaDescription
}
return nil
})
}

func s16(l *lexer) (stateFn, error) {
return l.handleType(func(key string) stateFn {
return l.handleType(func(key byte) stateFn {
switch key {
case "a=":
case 'a':
return unmarshalMediaAttribute
case "k=":
case 'k':
return unmarshalMediaEncryptionKey
case "c=":
case 'c':
return unmarshalMediaConnectionInformation
case "b=":
case 'b':
return unmarshalMediaBandwidth
case "i=":
case 'i':
// Non-spec ordering
return unmarshalMediaTitle
case "m=":
case 'm':
return unmarshalMediaDescription
}
return nil
Expand Down
Loading

0 comments on commit 0eee6f5

Please sign in to comment.