From a996f1668cb94ca5ec6e025c101bdb8d2c292de6 Mon Sep 17 00:00:00 2001 From: mappu Date: Sat, 27 Aug 2016 15:22:39 +1200 Subject: [PATCH] better myinfo parsing for zero-length speed strings --- UserInfo.go | 50 +++++++++++++++++++++++++++++++++++++------------- 1 file changed, 37 insertions(+), 13 deletions(-) diff --git a/UserInfo.go b/UserInfo.go index 7c3caff..a86187d 100644 --- a/UserInfo.go +++ b/UserInfo.go @@ -56,12 +56,19 @@ var rx_myinfo_notag *regexp.Regexp func init() { // $ALL $ $$$$ - rx_myinfo = regexp.MustCompile("(?ms)^\\$ALL ([^ ]+) ([^<]*)<([^,]*)V:([^,]+),M:(.),H:([0-9]+)/([0-9]+)/([0-9]+),S:([0-9]+)>\\$.\\$(.+?)(.)\\$([^$]*)\\$([0-9]*)\\$$") - rx_myinfo_notag = regexp.MustCompile("(?ms)^\\$ALL ([^ ]+) ([^$]*)\\$.\\$(.*)(.)\\$([^$]*)\\$([0-9]*)\\$$") // Fallback for no tag + + HEAD := `(?ms)^\$ALL ([^ ]+) ` + FOOT := `\$.\$([^$]+)\$([^$]*)\$([0-9]*)\$$` + + rx_myinfo = regexp.MustCompile(HEAD + `([^<]*)<(.+?) V:([^,]+),M:(.),H:([0-9]+)/([0-9]+)/([0-9]+),S:([0-9]+)>` + FOOT) + rx_myinfo_notag = regexp.MustCompile(HEAD + `([^$]*)` + FOOT) // Fallback for no tag /* - sample := "$ALL Betty description$ $0.01\x01$xyz@xyz.com$53054999578$" - sample = "$ALL ivysaur80 $P$10A$$0$" + sample := "" + // sample = "$ALL Betty description$ $0.01\x01$xyz@xyz.com$53054999578$" + // sample = "$ALL ivysaur80 $P$10A$$0$" + // sample = "$ALL SHINY_IVYSAUR $ $0.005Q$$0$" + // sample = "$ALL mappu desccccc$ $p$$0$" u := UserInfo{} err := u.fromMyINFO(sample) @@ -71,7 +78,7 @@ func init() { fmt.Printf("%+v\n", u) } - os.Exit(1) + panic("") */ } @@ -93,6 +100,8 @@ func maybeParse(str string, dest *uint64, default_val uint64) { } func (this *UserInfo) fromMyINFO(protomsg string) error { + fmt.Printf("PARSE: %s\n", protomsg) + // Normal format (with tag in exact V/M/H/S order) matches := rx_myinfo.FindStringSubmatch(protomsg) if matches != nil { @@ -105,10 +114,16 @@ func (this *UserInfo) fromMyINFO(protomsg string) error { maybeParse(matches[7], &this.HubsRegistered, 0) maybeParse(matches[8], &this.HubsOperator, 0) maybeParse(matches[9], &this.Slots, 0) - this.Speed = matches[10] - this.Flag = UserFlag(matches[11][0]) - this.Email = NMDCUnescape(matches[12]) - maybeParse(matches[13], &this.ShareSize, 0) + if len(matches[10]) > 1 { + this.Speed = matches[10][:len(matches[10])-2] + } else { + this.Speed = "" + } + this.Flag = UserFlag(matches[10][len(matches[10])-1]) + this.Email = NMDCUnescape(matches[11]) + maybeParse(matches[12], &this.ShareSize, 0) + + fmt.Printf("PARSE: Success (full VMHS)\n") return nil } @@ -125,14 +140,23 @@ func (this *UserInfo) fromMyINFO(protomsg string) error { this.HubsRegistered = 0 this.HubsOperator = 0 this.Slots = 0 - this.Speed = matches[3] - this.Flag = UserFlag(matches[4][0]) - this.Email = NMDCUnescape(matches[5]) - maybeParse(matches[6], &this.ShareSize, 0) + + if len(matches[3]) > 1 { + this.Speed = matches[3][:len(matches[3])-2] + } else { + this.Speed = "" + } + this.Flag = UserFlag(matches[3][len(matches[3])-1]) + this.Email = NMDCUnescape(matches[4]) + maybeParse(matches[5], &this.ShareSize, 0) + + fmt.Printf("PARSE: Partial/no tag\n") return nil } + fmt.Printf("PARSE: malformed\n") + // Couldn't get anything out of it... return errors.New("Malformed MyINFO") }