Skip to content

Commit ab2f452

Browse files
committed
[GR-11977] center, rjust and ljust methods are not implemented for str.
PullRequest: graalpython/213
2 parents f24bf29 + 9d07625 commit ab2f452

File tree

2 files changed

+142
-0
lines changed

2 files changed

+142
-0
lines changed

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -742,6 +742,38 @@ def test_title_uni(self):
742742
self.assertEqual('fifiNNfiISH'.title(), 'Fifinnfiish')
743743
self.assertEqual('A\u03a3A'.title(), 'A\u03c3a')
744744

745+
def test_ljust(self):
746+
self.checkequal('abc ', 'abc', 'ljust', 10)
747+
self.checkequal('abc ', 'abc', 'ljust', 6)
748+
self.checkequal('abc', 'abc', 'ljust', 3)
749+
self.checkequal('abc', 'abc', 'ljust', 2)
750+
self.checkequal('abc*******', 'abc', 'ljust', 10, '*')
751+
self.checkraises(TypeError, 'abc', 'ljust')
752+
753+
def test_rjust(self):
754+
self.checkequal(' abc', 'abc', 'rjust', 10)
755+
self.checkequal(' abc', 'abc', 'rjust', 6)
756+
self.checkequal('abc', 'abc', 'rjust', 3)
757+
self.checkequal('abc', 'abc', 'rjust', 2)
758+
self.checkequal('*******abc', 'abc', 'rjust', 10, '*')
759+
self.checkraises(TypeError, 'abc', 'rjust')
760+
761+
def test_center(self):
762+
self.checkequal(' abc ', 'abc', 'center', 10)
763+
self.checkequal(' abc ', 'abc', 'center', 6)
764+
self.checkequal('abc', 'abc', 'center', 3)
765+
self.checkequal('abc', 'abc', 'center', 2)
766+
self.checkequal('***abc****', 'abc', 'center', 10, '*')
767+
self.checkraises(TypeError, 'abc', 'center')
768+
769+
def test_center_uni(self):
770+
self.assertEqual('x'.center(2, '\U0010FFFF'),
771+
'x\U0010FFFF')
772+
self.assertEqual('x'.center(3, '\U0010FFFF'),
773+
'\U0010FFFFx\U0010FFFF')
774+
self.assertEqual('x'.center(4, '\U0010FFFF'),
775+
'\U0010FFFFx\U0010FFFF\U0010FFFF')
776+
745777
def test_same_id():
746778
empty_ids = set([id(str()) for i in range(100)])
747779
assert len(empty_ids) == 1

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

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1664,6 +1664,116 @@ public String doTitle(String self) {
16641664
}
16651665
}
16661666

1667+
@Builtin(name = "center", minNumOfPositionalArgs = 2, maxNumOfPositionalArgs = 3)
1668+
@GenerateNodeFactory
1669+
@TypeSystemReference(PythonArithmeticTypes.class)
1670+
abstract static class CenterNode extends PythonBuiltinNode {
1671+
1672+
private @Child CastToIndexNode toIndexNode;
1673+
1674+
private CastToIndexNode getCastToIndexNode() {
1675+
if (toIndexNode == null) {
1676+
CompilerDirectives.transferToInterpreterAndInvalidate();
1677+
toIndexNode = insert(CastToIndexNode.createOverflow());
1678+
}
1679+
return toIndexNode;
1680+
}
1681+
1682+
@Specialization
1683+
public String createDefault(String self, long width, @SuppressWarnings("unused") PNone fill) {
1684+
return make(self, getCastToIndexNode().execute(width), " ");
1685+
}
1686+
1687+
@Specialization(guards = "fill.codePointCount(0, fill.length()) == 1")
1688+
public String create(String self, long width, String fill) {
1689+
return make(self, getCastToIndexNode().execute(width), fill);
1690+
}
1691+
1692+
@Specialization(guards = "fill.codePointCount(0, fill.length()) != 1")
1693+
@SuppressWarnings("unused")
1694+
public String createError(String self, long width, String fill) {
1695+
throw raise(TypeError, "The fill character must be exactly one character long");
1696+
}
1697+
1698+
@Specialization
1699+
public String createDefault(String self, PInt width, @SuppressWarnings("unused") PNone fill) {
1700+
return make(self, getCastToIndexNode().execute(width), " ");
1701+
}
1702+
1703+
@Specialization(guards = "fill.codePointCount(0, fill.length()) == 1")
1704+
public String create(String self, PInt width, String fill) {
1705+
return make(self, getCastToIndexNode().execute(width), fill);
1706+
}
1707+
1708+
@Specialization(guards = "fill.codePointCount(0, fill.length()) != 1")
1709+
@SuppressWarnings("unused")
1710+
public String createError(String self, PInt width, String fill) {
1711+
throw raise(TypeError, "The fill character must be exactly one character long");
1712+
}
1713+
1714+
protected String make(String self, int width, String fill) {
1715+
int fillChar = parseCodePoint(fill);
1716+
int len = width - self.length();
1717+
if (len <= 0) {
1718+
return self;
1719+
}
1720+
int half = len / 2;
1721+
if (len % 2 > 0 && width % 2 > 0) {
1722+
half += 1;
1723+
}
1724+
1725+
return padding(half, fillChar) + self + padding(len - half, fillChar);
1726+
}
1727+
1728+
protected static String padding(int len, int codePoint) {
1729+
int[] result = new int[len];
1730+
for (int i = 0; i < len; i++) {
1731+
result[i] = codePoint;
1732+
}
1733+
return new String(result, 0, len);
1734+
}
1735+
1736+
@TruffleBoundary
1737+
protected static int parseCodePoint(String fillchar) {
1738+
if (fillchar == null) {
1739+
return ' ';
1740+
}
1741+
return fillchar.codePointAt(0);
1742+
}
1743+
}
1744+
1745+
@Builtin(name = "ljust", minNumOfPositionalArgs = 2, maxNumOfPositionalArgs = 3)
1746+
@GenerateNodeFactory
1747+
abstract static class LJustNode extends CenterNode {
1748+
1749+
@Override
1750+
protected String make(String self, int width, String fill) {
1751+
int fillChar = parseCodePoint(fill);
1752+
int len = width - self.length();
1753+
if (len <= 0) {
1754+
return self;
1755+
}
1756+
return self + padding(len, fillChar);
1757+
}
1758+
1759+
}
1760+
1761+
@Builtin(name = "rjust", minNumOfPositionalArgs = 2, maxNumOfPositionalArgs = 3)
1762+
@GenerateNodeFactory
1763+
abstract static class RJustNode extends CenterNode {
1764+
1765+
@Override
1766+
protected String make(String self, int width, String fill) {
1767+
int fillChar = parseCodePoint(fill);
1768+
int len = width - self.length();
1769+
if (len <= 0) {
1770+
return self;
1771+
}
1772+
return padding(len, fillChar) + self;
1773+
}
1774+
1775+
}
1776+
16671777
@Builtin(name = __GETITEM__, fixedNumOfPositionalArgs = 2)
16681778
@GenerateNodeFactory
16691779
@TypeSystemReference(PythonArithmeticTypes.class)

0 commit comments

Comments
 (0)