Commit cad9e30f authored by YOU's avatar YOU Committed by Dylan Trotter

Add str interpolation for %x (#107)

parent 2bc2a2e3
...@@ -616,7 +616,8 @@ func strInterpolate(f *Frame, format string, values *Tuple) (*Object, *BaseExcep ...@@ -616,7 +616,8 @@ func strInterpolate(f *Frame, format string, values *Tuple) (*Object, *BaseExcep
} else { } else {
return nil, f.RaiseType(TypeErrorType, fmt.Sprintf("float argument required, not %s", o.typ.Name())) return nil, f.RaiseType(TypeErrorType, fmt.Sprintf("float argument required, not %s", o.typ.Name()))
} }
case "d": case "d", "x", "X":
var val string
o := values.elems[valueIndex] o := values.elems[valueIndex]
if o.typ.slots.Int == nil { if o.typ.slots.Int == nil {
return nil, f.RaiseType(TypeErrorType, "%d format: a number is required, not "+o.typ.Name()) return nil, f.RaiseType(TypeErrorType, "%d format: a number is required, not "+o.typ.Name())
...@@ -625,11 +626,19 @@ func strInterpolate(f *Frame, format string, values *Tuple) (*Object, *BaseExcep ...@@ -625,11 +626,19 @@ func strInterpolate(f *Frame, format string, values *Tuple) (*Object, *BaseExcep
if raised != nil { if raised != nil {
return nil, raised return nil, raised
} }
s, raised := ToStr(f, i) if matches[7] == "d" {
if raised != nil { s, raised := ToStr(f, i)
return nil, raised if raised != nil {
return nil, raised
}
val = s.Value()
} else {
val = strconv.FormatInt(int64(toIntUnsafe(i).Value()), 16)
if matches[7] == "X" {
val = strings.ToUpper(val)
}
} }
buf.WriteString(s.Value()) buf.WriteString(val)
valueIndex++ valueIndex++
case "%": case "%":
buf.WriteString("%") buf.WriteString("%")
......
...@@ -74,7 +74,11 @@ func TestStrBinaryOps(t *testing.T) { ...@@ -74,7 +74,11 @@ func TestStrBinaryOps(t *testing.T) {
{args: wrapArgs(Mod, "%s", NewDict()), wantExc: mustCreateException(NotImplementedErrorType, "mappings not yet supported")}, {args: wrapArgs(Mod, "%s", NewDict()), wantExc: mustCreateException(NotImplementedErrorType, "mappings not yet supported")},
{args: wrapArgs(Mod, "% d", 23), wantExc: mustCreateException(NotImplementedErrorType, "conversion flags not yet supported")}, {args: wrapArgs(Mod, "% d", 23), wantExc: mustCreateException(NotImplementedErrorType, "conversion flags not yet supported")},
{args: wrapArgs(Mod, "%.3f", 102.1), wantExc: mustCreateException(NotImplementedErrorType, "field width not yet supported")}, {args: wrapArgs(Mod, "%.3f", 102.1), wantExc: mustCreateException(NotImplementedErrorType, "field width not yet supported")},
{args: wrapArgs(Mod, "%x", 24), wantExc: mustCreateException(NotImplementedErrorType, "conversion type not yet supported: x")}, {args: wrapArgs(Mod, "%x", 0x1f), want: NewStr("1f").ToObject()},
{args: wrapArgs(Mod, "%X", 0xffff), want: NewStr("FFFF").ToObject()},
{args: wrapArgs(Mod, "%x", 1.2), want: NewStr("1").ToObject()},
{args: wrapArgs(Mod, "abc %x", NewLong(big.NewInt(123))), want: NewStr("abc 7b").ToObject()},
{args: wrapArgs(Mod, "%x", None), wantExc: mustCreateException(TypeErrorType, "%d format: a number is required, not NoneType")},
{args: wrapArgs(Mod, "%f", None), wantExc: mustCreateException(TypeErrorType, "float argument required, not NoneType")}, {args: wrapArgs(Mod, "%f", None), wantExc: mustCreateException(TypeErrorType, "float argument required, not NoneType")},
{args: wrapArgs(Mod, "%s", newTestTuple(123, None)), wantExc: mustCreateException(TypeErrorType, "not all arguments converted during string formatting")}, {args: wrapArgs(Mod, "%s", newTestTuple(123, None)), wantExc: mustCreateException(TypeErrorType, "not all arguments converted during string formatting")},
{args: wrapArgs(Mod, "%d", newTestTuple("123")), wantExc: mustCreateException(TypeErrorType, "%d format: a number is required, not str")}, {args: wrapArgs(Mod, "%d", newTestTuple("123")), wantExc: mustCreateException(TypeErrorType, "%d format: a number is required, not str")},
......
# Copyright 2016 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# pylint: disable=redefined-outer-name
# Test Add
assert "foo" + "bar" == "foobar"
assert "foo" + u"bar" == u"foobar"
assert "baz" + "" == "baz"
# Test Mod
assert "%s" % 42 == "42"
assert "%f" % 3.14 == "3.140000"
assert "abc %d" % 123L == "abc 123"
assert "%d" % 3.14 == "3"
assert "%%" % tuple() == "%"
assert "%r" % "abc" == "'abc'"
assert "%x" % 0x1f == "1f"
assert "%X" % 0xffff == "FFFF"
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment