GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

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
Filter options
  • java2python/mod
    • transform.py
  • test
    • Format0.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎java2python/mod/transform.py‎

Copy file name to clipboardExpand all lines: java2python/mod/transform.py
+5-1Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,10 @@ def formatSyntaxTransf(match):
101101
inside a format specifier string.
102102
"""
103103
groups = match.groupdict()
104+
if groups['convers'] == 'n':
105+
# Means platform-specific line separator
106+
return '\\n' # Py converts \n to os.linesep
107+
104108
result = '{'
105109
# TODO: add flags, width and precision
106110
if(groups['idx']):
@@ -124,7 +128,7 @@ def formatString(node, config):
124128
format = call_args[0].firstChildOfType(tokens.STRING_LITERAL)
125129
if format:
126130
format.token.text = \
127-
re.sub(r'%(?P<idx>\d+\$)?(?P<convers>[scdoxefg])',
131+
re.sub(r'%(?P<idx>\d+\$)?(?P<convers>[scdoxefgn])',
128132
formatSyntaxTransf,
129133
format.token.text,
130134
flags=re.IGNORECASE)
Collapse file

‎test/Format0.java‎

Copy file name to clipboardExpand all lines: test/Format0.java
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ public class Format0 {
22
public static void main(String[] args) {
33
int i = 22;
44
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);
66

77
System.out.println(r);
88
}

0 commit comments

Comments
0 (0)
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.