@@ -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