Skip to content

Commit

Permalink
add {%dul uint64 %} support (#78)
Browse files Browse the repository at this point in the history
  • Loading branch information
phuslu authored Jul 30, 2020
1 parent d735c16 commit 96027a8
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ The `{%s x %}` is used for printing HTML-safe strings, while `{%= F() %}`
is used for embedding template function calls. Quicktemplate supports also
other output tags:

* `{%d int %}` and `{%dl int64 %}` for integers.
* `{%d int %}` and `{%dl int64 %}` `{%dul uint64 %}` for integers.
* `{%f float %}` for float64.
Floating point precision may be set via `{%f.precision float %}`.
For example, `{%f.2 1.2345 %}` outputs `1.23`.
Expand Down
4 changes: 2 additions & 2 deletions parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -489,8 +489,8 @@ func (p *parser) parseIf() error {
func (p *parser) tryParseCommonTags(tagBytes []byte) (bool, error) {
tagNameStr, prec := splitTagNamePrec(string(tagBytes))
switch tagNameStr {
case "s", "v", "d", "dl", "f", "q", "z", "j", "u",
"s=", "v=", "d=", "dl=", "f=", "q=", "z=", "j=", "u=",
case "s", "v", "d", "dl", "dul", "f", "q", "z", "j", "u",
"s=", "v=", "d=", "dl=", "dul=", "f=", "q=", "z=", "j=", "u=",
"sz", "qz", "jz", "uz",
"sz=", "qz=", "jz=", "uz=":
if err := p.parseOutputTag(tagNameStr, prec); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion testdata/qtc/test.qtpl
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ Now define private printArgs, which is called in Foo via {%= %} tag
{% for %}And this: {% break %} {% return %}{% endfor %}
{% endif %}
<li>
a[{%d i %}] = {S: {%q a.S %}, SS: {%qz []byte(a.S) %}, N: {%dl int64(a.N) %}}<br>
a[{%d i %}] = {S: {%q a.S %}, SS: {%qz []byte(a.S) %}, N: {%dl int64(a.N) %}, NN: {%dul uint64(a.N) %}}<br>
{%s a.S %}, {%z []byte(a.S) %}, {%sz []byte(a.S) %}
{%f 1.234 %}, {%f.1 1.234 %}, {% f.2= 1.234 %}
alert("foo {%j "bar\naaa" %} baz {%jz []byte("aaa") %}")<br/>
Expand Down
2 changes: 1 addition & 1 deletion testdata/qtc/test.qtpl.expected
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ Now define private printArgs, which is called in Foo via {%= %} tag
{% for %}And this: {% break %} {% return %}{% endfor %}
{% endif %}
<li>
a[{%d i %}] = {S: {%q a.S %}, SS: {%qz []byte(a.S) %}, N: {%dl int64(a.N) %}}<br>
a[{%d i %}] = {S: {%q a.S %}, SS: {%qz []byte(a.S) %}, N: {%dl int64(a.N) %}, NN: {%dul uint64(a.N) %}}<br>
{%s a.S %}, {%z []byte(a.S) %}, {%sz []byte(a.S) %}
{%f 1.234 %}, {%f.1 1.234 %}, {% f.2= 1.234 %}
alert("foo {%j "bar\naaa" %} baz {%jz []byte("aaa") %}")<br/>
Expand Down
11 changes: 11 additions & 0 deletions writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,17 @@ func (w *QWriter) DL(n int64) {
}
}

// DUL writes n to w
func (w *QWriter) DUL(n uint64) {
bb, ok := w.w.(*ByteBuffer)
if ok {
bb.B = strconv.AppendUint(bb.B, n, 10)
} else {
w.b = strconv.AppendUint(w.b[:0], n, 10)
w.Write(w.b)
}
}

// F writes f to w.
func (w *QWriter) F(f float64) {
n := int(f)
Expand Down
6 changes: 4 additions & 2 deletions writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func TestWriter(t *testing.T) {

wn.S("<a></a>")
wn.D(123)
we.DUL(18446744073709551615)
wn.Z([]byte("'"))
wn.Q("foo")
wn.J("ds")
Expand All @@ -34,6 +35,7 @@ func TestWriter(t *testing.T) {

we.S("<a></a>")
we.D(321)
we.DUL(18446744073709551615)
we.Z([]byte("'"))
we.Q("foo")
we.J("ds")
Expand All @@ -47,8 +49,8 @@ func TestWriter(t *testing.T) {

ReleaseWriter(qw)

expectedS := "<a></a>123'\"foo\"ds1.23%D0%B0%D0%B1%D0%B2{}aaa\"asadf\"asdabc" +
"&lt;a&gt;&lt;/a&gt;321&#39;&quot;foo&quot;ds1.23%D0%B0%D0%B1%D0%B2{}aaa&quot;asadf&quot;asdabc"
expectedS := "<a></a>12318446744073709551615'\"foo\"ds1.23%D0%B0%D0%B1%D0%B2{}aaa\"asadf\"asdabc" +
"&lt;a&gt;&lt;/a&gt;32118446744073709551615&#39;&quot;foo&quot;ds1.23%D0%B0%D0%B1%D0%B2{}aaa&quot;asadf&quot;asdabc"
if string(bb.B) != expectedS {
t.Fatalf("unexpected output:\n%q\nExpecting\n%q", bb.B, expectedS)
}
Expand Down

0 comments on commit 96027a8

Please sign in to comment.