Skip to content

Commit

Permalink
Merge pull request #24 from tomato3713/bugfix-radiospec1parse
Browse files Browse the repository at this point in the history
Bugfix radiospec1parse
  • Loading branch information
tomato3713 authored Oct 11, 2021
2 parents 11cbae5 + d8c5413 commit 6e8a8be
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 22 deletions.
56 changes: 34 additions & 22 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,16 @@ func decodeBody(resp *http.Response, out interface{}, f *os.File) error {
return decorder.Decode(out)
}

func removeAllSpace(str string) string {
var ret []rune
for _, c := range str {
if !unicode.IsSpace(c) {
ret = append(ret, c)
}
}
return string(ret)
}

func parseRadioSpec1(str string) ([]RadioSpec, error) {
var rs []RadioSpec
strRune := []rune(str)
Expand All @@ -61,30 +71,32 @@ func parseRadioSpec1(str string) ([]RadioSpec, error) {
if unicode.IsSpace(strRune[i]) {
continue
}
if string(strRune[i]) == string("\\") {
if i+1 < len(strRune) && string(strRune[i+1]) == "t" {
if len(v.RadioFormat) == 0 {
rf := string(strRune[j:i])
v.RadioFormat = strings.Fields(rf)
} else {
v.Freq = strings.ReplaceAll(string(strRune[j:i]), " ", "")
}
i += 2
j = i
} else if i+1 < len(strRune) && string(strRune[i+1]) == "n" {
powerstr := strings.ReplaceAll(string(strRune[j:i]), " ", "")
if len(powerstr) > 0 {
power, err := rmSIPrefix(powerstr)
if err != nil {
return nil, err
}
v.Power = power
if i+2 < len(strRune) && string(strRune[i:i+2]) == string("\\t") {
if len(v.RadioFormat) == 0 {
rf := string(strRune[j:i])
v.RadioFormat = strings.Fields(rf)
} else {
v.Freq = removeAllSpace(string(strRune[j:i]))
}
i += 2
j = i
} else if i+2 < len(strRune) && string(strRune[i:i+2]) == string("\\n") || i+1 == len(strRune) {
var powerstr string
if i+1 == len(strRune) {
powerstr = removeAllSpace(string(strRune[j : i+1]))
} else {
powerstr = removeAllSpace(string(strRune[j:i]))
}
if len(powerstr) > 0 {
power, err := rmSIPrefix(powerstr)
if err != nil {
return nil, err
}
i += 2
j = i - 1
rs = append(rs, v)
break
v.Power = power
}
rs = append(rs, v)
j = i + 1
break
}
}
}
Expand Down
86 changes: 86 additions & 0 deletions utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package soumuradio

import (
"reflect"
"testing"
)

func TestParseRadioSpec1(t *testing.T) {
tests := []struct {
name string
str string
want []RadioSpec
}{
{
name: "case1",
str: "3MA\\t1910 kHz\\t50 W\\n3HA\\t3537.5 kHz\\t50 W\\n3HD\\t3798 kHz\\t50 W\\n3HA\\t7100 kHz\\t50 W\\n2HC\\t10125 kHz\\t50 W\\n2HA\\t14175 kHz\\t50 W\\n3HA\\t18118 kHz\\t50 W\\n3HA\\t21225 kHz\\t50 W\\n3HA\\t24940 kHz\\t50 W\\n3VA\\t28.85 MHz\\t50 W\\n3VA\\t52 MHz\\t50 W\\n3VA\\t145 MHz\\t50 W\\n3VA\\t435 MHz\\t50 W\\nA1A\\t4630 kHz\\t50 W",
want: []RadioSpec{
{RadioFormat: []string{"3MA"}, Freq: "1910kHz", Power: 50},
{RadioFormat: []string{"3HA"}, Freq: "3537.5kHz", Power: 50},
{RadioFormat: []string{"3HD"}, Freq: "3798kHz", Power: 50},
{RadioFormat: []string{"3HA"}, Freq: "7100kHz", Power: 50},
{RadioFormat: []string{"2HC"}, Freq: "10125kHz", Power: 50},
{RadioFormat: []string{"2HA"}, Freq: "14175kHz", Power: 50},
{RadioFormat: []string{"3HA"}, Freq: "18118kHz", Power: 50},
{RadioFormat: []string{"3HA"}, Freq: "21225kHz", Power: 50},
{RadioFormat: []string{"3HA"}, Freq: "24940kHz", Power: 50},
{RadioFormat: []string{"3VA"}, Freq: "28.85MHz", Power: 50},
{RadioFormat: []string{"3VA"}, Freq: "52MHz", Power: 50},
{RadioFormat: []string{"3VA"}, Freq: "145MHz", Power: 50},
{RadioFormat: []string{"3VA"}, Freq: "435MHz", Power: 50},
{RadioFormat: []string{"A1A"}, Freq: "4630kHz", Power: 50},
},
},
{
name: "case2",
str: "3MA\\t1910 kHz\\t50 W\\n3HA\\t3537.5 kHz\\t50 W\\n3HD\\t3798 kHz\\t50 W\\n3HA\\t7100 kHz\\t50 W\\n2HC\\t10125 kHz\\t50 W\\n2HA\\t14175 kHz\\t50 W\\n3HA\\t18118 kHz\\t50 W\\n3HA\\t21225 kHz\\t50 W\\n3HA\\t24940 kHz\\t50 W\\n3VA\\t28.85 MHz\\t50 W\\n3VA\\t52 MHz\\t50 W\\n3VA\\t145 MHz\\t50 W\\n3VA\\t435 MHz\\t50 W\\n3SA\\t1280 MHz\\t10 W\\n3SA\\t2425 MHz\\t1 W\\n3SA\\t5750 MHz\\t2 W\\n3SA\\t10.125 GHz\\t2 W\\n3SA\\t10.475 GHz\\t2 W",
want: []RadioSpec{
{RadioFormat: []string{"3MA"}, Freq: "1910kHz", Power: 50},
{RadioFormat: []string{"3HA"}, Freq: "3537.5kHz", Power: 50},
{RadioFormat: []string{"3HD"}, Freq: "3798kHz", Power: 50},
{RadioFormat: []string{"3HA"}, Freq: "7100kHz", Power: 50},
{RadioFormat: []string{"2HC"}, Freq: "10125kHz", Power: 50},
{RadioFormat: []string{"2HA"}, Freq: "14175kHz", Power: 50},
{RadioFormat: []string{"3HA"}, Freq: "18118kHz", Power: 50},
{RadioFormat: []string{"3HA"}, Freq: "21225kHz", Power: 50},
{RadioFormat: []string{"3HA"}, Freq: "24940kHz", Power: 50},
{RadioFormat: []string{"3VA"}, Freq: "28.85MHz", Power: 50},
{RadioFormat: []string{"3VA"}, Freq: "52MHz", Power: 50},
{RadioFormat: []string{"3VA"}, Freq: "145MHz", Power: 50},
{RadioFormat: []string{"3VA"}, Freq: "435MHz", Power: 50},
{RadioFormat: []string{"3SA"}, Freq: "1280MHz", Power: 10},
{RadioFormat: []string{"3SA"}, Freq: "2425MHz", Power: 1},
{RadioFormat: []string{"3SA"}, Freq: "5750MHz", Power: 2},
{RadioFormat: []string{"3SA"}, Freq: "10.125GHz", Power: 2},
{RadioFormat: []string{"3SA"}, Freq: "10.475GHz", Power: 2},
},
},
{
name: "case3",
str: "500HA1A\\t437.075 MHz\\t100 mW\\n16K0F2D\\t437.075 MHz\\t800 mW\\n16K0F3E\\t \\t \\n16K0F3F\\t \\t \\n26K0F1D\\t437.075 MHz\\t800 mW\\n30K0G1D\\t435.9 MHz\\t400 mW\\n30K0F1D\\t \\t \\n500HA1A\\t435.88 MHz から  435.91 MHz まで\\t500 mW\\n3K00J3E\\t435.88 MHz から  435.91 MHz まで\\t500 mW",
want: []RadioSpec{
{RadioFormat: []string{"500HA1A"}, Freq: "437.075MHz", Power: 0.1},
{RadioFormat: []string{"16K0F2D"}, Freq: "437.075MHz", Power: 0.8},
{RadioFormat: []string{"16K0F3E"}, Freq: "", Power: 0},
{RadioFormat: []string{"16K0F3F"}, Freq: "", Power: 0},
{RadioFormat: []string{"26K0F1D"}, Freq: "437.075MHz", Power: 0.8},
{RadioFormat: []string{"30K0G1D"}, Freq: "435.9MHz", Power: 0.4},
{RadioFormat: []string{"30K0F1D"}, Freq: "", Power: 0},
{RadioFormat: []string{"500HA1A"}, Freq: "435.88MHzから435.91MHzまで", Power: 0.5},
{RadioFormat: []string{"3K00J3E"}, Freq: "435.88MHzから435.91MHzまで", Power: 0.5},
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got, err := parseRadioSpec1(tt.str); err == nil {
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("parseRadioSpec = \nget %v,\n want %v", got, tt.want)
}
} else {
t.Fatal(err)
}
})
}
}

0 comments on commit 6e8a8be

Please sign in to comment.