GeezerVison is showing github.com — String formatting (2nd iteration) handle %n from Java · akumar5/java2python@d178ce8
File tree
Expand file treeCollapse file tree
Open diff view settings
Expand file treeCollapse file tree
Open diff view settings
Collapse file
Copy file name to clipboardExpand all lines: java2python/mod/transform.py +5-1Lines changed: 5 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -101,6 +101,10 @@ def formatSyntaxTransf(match): |
101 | 101 | inside a format specifier string. |
102 | 102 | """ |
103 | 103 | groups = match.groupdict() |
| 104 | + if groups['convers'] == 'n': |
| 105 | + # Means platform-specific line separator |
| 106 | + return '\\n' # Py converts \n to os.linesep |
| 107 | + |
104 | 108 | result = '{' |
105 | 109 | # TODO: add flags, width and precision |
106 | 110 | if(groups['idx']): |
@@ -124,7 +128,7 @@ def formatString(node, config): |
124 | 128 | format = call_args[0].firstChildOfType(tokens.STRING_LITERAL) |
125 | 129 | if format: |
126 | 130 | format.token.text = \ |
127 | | - re.sub(r'%(?P<idx>\d+\$)?(?P<convers>[scdoxefg])', |
| 131 | + re.sub(r'%(?P<idx>\d+\$)?(?P<convers>[scdoxefgn])', |
128 | 132 | formatSyntaxTransf, |
129 | 133 | format.token.text, |
130 | 134 | flags=re.IGNORECASE) |
|
Collapse file
Copy file name to clipboardExpand all lines: test/Format0.java +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -2,7 +2,7 @@ public class Format0 { |
2 | 2 | public static void main(String[] args) { |
3 | 3 | int i = 22; |
4 | 4 | String s = "text"; |
5 | | - String r = String.format("> (%1$d) %2$s", i, s); |
| 5 | + String r = String.format("> (%1$d) %n %2$s", i, s); |
6 | 6 |
|
7 | 7 | System.out.println(r); |
8 | 8 | } |
|