]> Cypherpunks repositories - gostls13.git/commitdiff
json: calculate Offset for Indent correctly
authorJeff Hodges <jeff@somethingsimilar.com>
Mon, 22 Aug 2011 05:19:27 +0000 (15:19 +1000)
committerAndrew Gerrand <adg@golang.org>
Mon, 22 Aug 2011 05:19:27 +0000 (15:19 +1000)
Fixes #2171

This is the real change.

R=adg
CC=golang-dev, r, rsc
https://golang.org/cl/4943041

src/pkg/json/indent.go
src/pkg/json/scanner_test.go

index 000da42f6fb0d53a2b35fc803f1c84df6ca34485..2a7530373081c94332db7b320fab4a2bf82e5197 100644 (file)
@@ -59,6 +59,7 @@ func Indent(dst *bytes.Buffer, src []byte, prefix, indent string) os.Error {
        needIndent := false
        depth := 0
        for _, c := range src {
+               scan.bytes++
                v := scan.step(&scan, int(c))
                if v == scanSkipSpace {
                        continue
index 023e7c81ee457736adda232704dd23dd0b2c5222..67d4a28c756b73839cdec14fa88b440e87135cdb 100644 (file)
@@ -7,7 +7,9 @@ package json
 import (
        "bytes"
        "math"
+       "os"
        "rand"
+       "reflect"
        "testing"
 )
 
@@ -136,6 +138,29 @@ func TestIndentBig(t *testing.T) {
        }
 }
 
+type indentErrorTest struct {
+       in  string
+       err os.Error
+}
+
+var indentErrorTests = []indentErrorTest{
+       {`{"X": "foo", "Y"}`, &SyntaxError{"invalid character '}' after object key", 17}},
+       {`{"X": "foo" "Y": "bar"}`, &SyntaxError{"invalid character '\"' after object key:value pair", 13}},
+}
+
+func TestIdentErrors(t *testing.T) {
+       for i, tt := range indentErrorTests {
+               slice := make([]uint8, 0)
+               buf := bytes.NewBuffer(slice)
+               if err := Indent(buf, []uint8(tt.in), "", ""); err != nil {
+                       if !reflect.DeepEqual(err, tt.err) {
+                               t.Errorf("#%d: Indent: %#v", i, err)
+                               continue
+                       }
+               }
+       }
+}
+
 func TestNextValueBig(t *testing.T) {
        initBig()
        var scan scanner