Skip to content

Commit 836a819

Browse files
committed
[GR-11836] Implementation of str.zfill is missing.
1 parent 6b2ef5b commit 836a819

File tree

2 files changed

+83
-1
lines changed

2 files changed

+83
-1
lines changed

graalpython/com.oracle.graal.python.test/src/tests/test_string.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,8 @@ class subtype(self.__class__.type2test):
471471
else:
472472
obj = subtype(obj)
473473
realresult = getattr(obj, methodname)(*args)
474-
self.assertIsNot(obj, realresult)
474+
self.assertTrue(obj is not realresult)
475+
#self.assertIsNot(obj, realresult)
475476

476477
# check that obj.method(*args) raises exc
477478
def checkraises(self, exc, obj, methodname, *args):
@@ -684,6 +685,32 @@ def test_isprintable(self):
684685
self.assertTrue('\U0001F46F'.isprintable())
685686
# self.assertFalse('\U000E0020'.isprintable())
686687

688+
def test_zfill(self):
689+
self.checkequal('123', '123', 'zfill', 2)
690+
self.checkequal('123', '123', 'zfill', 3)
691+
self.checkequal('0123', '123', 'zfill', 4)
692+
self.checkequal('+123', '+123', 'zfill', 3)
693+
self.checkequal('+123', '+123', 'zfill', 4)
694+
self.checkequal('+0123', '+123', 'zfill', 5)
695+
self.checkequal('-123', '-123', 'zfill', 3)
696+
self.checkequal('-123', '-123', 'zfill', 4)
697+
self.checkequal('-0123', '-123', 'zfill', 5)
698+
self.checkequal('000', '', 'zfill', 3)
699+
self.checkequal('34', '34', 'zfill', 1)
700+
self.checkequal('0034', '34', 'zfill', 4)
701+
702+
self.checkraises(TypeError, '123', 'zfill')
703+
704+
def test_zfill_specialization(self):
705+
self.checkequal('123', '123', 'zfill', True)
706+
707+
class MyIndexable(object):
708+
def __init__(self, value):
709+
self.value = value
710+
def __index__(self):
711+
return self.value
712+
713+
self.checkequal('0123', '123', 'zfill', MyIndexable(4))
687714

688715
def test_same_id():
689716
empty_ids = set([id(str()) for i in range(100)])

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/str/StringBuiltins.java

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@
8686
import com.oracle.graal.python.nodes.function.builtins.PythonUnaryBuiltinNode;
8787
import com.oracle.graal.python.nodes.object.GetClassNode;
8888
import com.oracle.graal.python.nodes.truffle.PythonArithmeticTypes;
89+
import com.oracle.graal.python.nodes.util.CastToIntegerFromIndexNode;
8990
import com.oracle.graal.python.runtime.exception.PException;
9091
import com.oracle.graal.python.runtime.formatting.StringFormatter;
9192
import com.oracle.truffle.api.CompilerDirectives;
@@ -1539,6 +1540,60 @@ boolean doString(String self) {
15391540
}
15401541
}
15411542

1543+
@Builtin(name = "zfill", fixedNumOfPositionalArgs = 2)
1544+
@GenerateNodeFactory
1545+
@TypeSystemReference(PythonArithmeticTypes.class)
1546+
abstract static class ZFillNode extends PythonBinaryBuiltinNode {
1547+
1548+
public abstract String executeObject(String self, Object x);
1549+
1550+
@Specialization
1551+
public String doString(String self, long width) {
1552+
return zfill(self, (int) width);
1553+
}
1554+
1555+
@Specialization
1556+
public String doString(String self, PInt width) {
1557+
return zfill(self, width.intValue());
1558+
}
1559+
1560+
@Specialization
1561+
public String doString(String self, Object width,
1562+
@Cached("create()") CastToIntegerFromIndexNode widthCast,
1563+
@Cached("create()") ZFillNode recursiveNode) {
1564+
return recursiveNode.executeObject(self, widthCast.execute(width));
1565+
}
1566+
1567+
private String zfill(String self, int width) {
1568+
int len = self.length();
1569+
if (len >= width) {
1570+
return self;
1571+
}
1572+
char[] chars = new char[width];
1573+
int nzeros = width - len;
1574+
int i = 0;
1575+
int sStart = 0;
1576+
if (len > 0) {
1577+
char start = self.charAt(0);
1578+
if (start == '+' || start == '-') {
1579+
chars[0] = start;
1580+
i += 1;
1581+
nzeros++;
1582+
sStart = 1;
1583+
}
1584+
}
1585+
for (; i < nzeros; i++) {
1586+
chars[i] = '0';
1587+
}
1588+
self.getChars(sStart, len, chars, i);
1589+
return new String(chars);
1590+
}
1591+
1592+
public static ZFillNode create() {
1593+
return StringBuiltinsFactory.ZFillNodeFactory.create();
1594+
}
1595+
}
1596+
15421597
@Builtin(name = __GETITEM__, fixedNumOfPositionalArgs = 2)
15431598
@GenerateNodeFactory
15441599
@TypeSystemReference(PythonArithmeticTypes.class)

0 commit comments

Comments
 (0)