From 9adcdb14c4848213bd825e7b4985b6f0a390908c Mon Sep 17 00:00:00 2001 From: Ivan Markin Date: Wed, 26 Oct 2016 21:59:37 +0000 Subject: [PATCH] Parse lines with quoted substrings right --- cmd_protocolinfo.go | 4 +++- utils/utils.go | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/cmd_protocolinfo.go b/cmd_protocolinfo.go index d05da6d..44685b0 100644 --- a/cmd_protocolinfo.go +++ b/cmd_protocolinfo.go @@ -10,6 +10,8 @@ package bulb import ( "strconv" "strings" + + "github.com/yawning/bulb/utils" ) // ProtocolInfo is the result of the ProtocolInfo command. @@ -50,7 +52,7 @@ func (c *Conn) ProtocolInfo() (*ProtocolInfo, error) { pi.RawResponse = resp pi.AuthMethods = make(map[string]bool) for i := 1; i < len(resp.Data); i++ { - splitLine := strings.Split(resp.Data[i], " ") + splitLine := utils.SplitQuoted(resp.Data[i], '"', ' ') switch splitLine[0] { case "AUTH": // Parse an AuthLine detailing how to authenticate. diff --git a/utils/utils.go b/utils/utils.go index 0ec0fb7..d741a8b 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -15,6 +15,24 @@ import ( "strconv" ) +// SplitQuoted splits s by sep if it is found outside substring +// quoted by quote. +func SplitQuoted(s string, quote, sep rune) (splitted []string) { + quoteFlag := false +NewSubstring: + for i, c := range s { + if c == quote { + quoteFlag = !quoteFlag + } + if c == sep && !quoteFlag { + splitted = append(splitted, s[:i]) + s = s[i+1:] + goto NewSubstring + } + } + return append(splitted, s) +} + // ParseControlPortString parses a string representation of a control port // address into a network/address string pair suitable for use with "dial". //