Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
grumpy
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
grumpy
Commits
cad9e30f
Commit
cad9e30f
authored
Jan 13, 2017
by
YOU
Committed by
Dylan Trotter
Jan 13, 2017
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add str interpolation for %x (#107)
parent
2bc2a2e3
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
49 additions
and
6 deletions
+49
-6
runtime/str.go
runtime/str.go
+14
-5
runtime/str_test.go
runtime/str_test.go
+5
-1
testing/str_test.py
testing/str_test.py
+30
-0
No files found.
runtime/str.go
View file @
cad9e30f
...
@@ -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
(
"%"
)
...
...
runtime/str_test.go
View file @
cad9e30f
...
@@ -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"
)},
...
...
testing/str_test.py
0 → 100644
View file @
cad9e30f
# 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"
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment