ns map[string]string
err error
line int
+ linestart int64
offset int64
unmarshalDepth int
}
}
if b == '\n' {
d.line++
+ d.linestart = d.offset + 1
}
d.offset++
return b, true
return d.offset
}
+// InputPos retuns the line of the current decoder position and the 1 based
+// input position of the line. The position gives the location of the end of the
+// most recently returned token.
+func (d *Decoder) InputPos() (line, column int) {
+ return d.line, int(d.offset-d.linestart) + 1
+}
+
// Return saved offset.
// If we did ungetc (nextByte >= 0), have to back up one.
func (d *Decoder) savedOffset() int {
}
}
+func TestInputLinePos(t *testing.T) {
+ testInput := `<root>
+<?pi
+ ?> <elt
+att
+=
+"val">
+<![CDATA[
+]]><!--
+
+--></elt>
+</root>`
+ linePos := [][]int{
+ {1, 7},
+ {2, 1},
+ {3, 4},
+ {3, 6},
+ {6, 7},
+ {7, 1},
+ {8, 4},
+ {10, 4},
+ {10, 10},
+ {11, 1},
+ {11, 8},
+ }
+ dec := NewDecoder(strings.NewReader(testInput))
+ for _, want := range linePos {
+ if _, err := dec.Token(); err != nil {
+ t.Errorf("Unexpected error: %v", err)
+ continue
+ }
+
+ gotLine, gotCol := dec.InputPos()
+ if gotLine != want[0] || gotCol != want[1] {
+ t.Errorf("dec.InputPos() = %d,%d, want %d,%d", gotLine, gotCol, want[0], want[1])
+ }
+ }
+}
+
type allScalars struct {
True1 bool
True2 bool