diff --git a/util.go b/util.go index c5bc21a..df7df82 100644 --- a/util.go +++ b/util.go @@ -1,6 +1,7 @@ package main import ( + "bytes" "os" "path/filepath" @@ -53,3 +54,21 @@ func RemoveFmtUnderscore(in string) (out string) { } return } + +func RemoveJsoncComments(data []byte) []byte { + var buf bytes.Buffer + var inLineComment bool = false + for i, b := range data { + if b == '/' && i+1 < len(data) && data[i+1] == '/' { + inLineComment = true + } + if b == '\n' { + inLineComment = false + } + if inLineComment { + continue + } + buf.WriteByte(b) + } + return buf.Bytes() +} diff --git a/util_test.go b/util_test.go index 5e3dd57..8e0ee0b 100644 --- a/util_test.go +++ b/util_test.go @@ -1,6 +1,7 @@ package main import ( + "reflect" "testing" ) @@ -27,3 +28,41 @@ func TestRemoveFmtUnderscore(t *testing.T) { }) } } + +func TestRemoveJsoncComments(t *testing.T) { + type args struct { + data []byte + } + tests := []struct { + name string + args args + want []byte + }{ + {"1", args{ + []byte( + ` +a//bcde// +//c +// +a +`, + ), + }, + []byte( + ` +a + + +a +`, + ), + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := RemoveJsoncComments(tt.args.data); !reflect.DeepEqual(got, tt.want) { + t.Errorf("RemoveJsoncComments() = %v, want %v", got, tt.want) + } + }) + } +}