From d178ce8a6b06a2392d31e7a8e58786ce6a204b22 Mon Sep 17 00:00:00 2001 From: Iulius Curt Date: Sun, 13 May 2012 17:07:33 +0300 Subject: [PATCH 1/2] String formatting (2nd iteration) handle %n from Java --- java2python/mod/transform.py | 6 +++++- test/Format0.java | 2 +- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/java2python/mod/transform.py b/java2python/mod/transform.py index 55b49cd..3565465 100644 --- a/java2python/mod/transform.py +++ b/java2python/mod/transform.py @@ -101,6 +101,10 @@ def formatSyntaxTransf(match): inside a format specifier string. """ groups = match.groupdict() + if groups['convers'] == 'n': + # Means platform-specific line separator + return '\\n' # Py converts \n to os.linesep + result = '{' # TODO: add flags, width and precision if(groups['idx']): @@ -124,7 +128,7 @@ def formatString(node, config): format = call_args[0].firstChildOfType(tokens.STRING_LITERAL) if format: format.token.text = \ - re.sub(r'%(?P\d+\$)?(?P[scdoxefg])', + re.sub(r'%(?P\d+\$)?(?P[scdoxefgn])', formatSyntaxTransf, format.token.text, flags=re.IGNORECASE) diff --git a/test/Format0.java b/test/Format0.java index 995aad2..68a22a4 100644 --- a/test/Format0.java +++ b/test/Format0.java @@ -2,7 +2,7 @@ public class Format0 { public static void main(String[] args) { int i = 22; String s = "text"; - String r = String.format("> (%1$d) %2$s", i, s); + String r = String.format("> (%1$d) %n %2$s", i, s); System.out.println(r); } From 2c82742327aff3d71de64412e07f041e2ee84acd Mon Sep 17 00:00:00 2001 From: Iulius Curt Date: Sun, 13 May 2012 17:48:08 +0300 Subject: [PATCH 2/2] Handle flags, width and precision implemented (string formatting) --- java2python/mod/transform.py | 28 +++++++++++++++++++++++++--- test/Format1.java | 19 +++++++++++++++++++ 2 files changed, 44 insertions(+), 3 deletions(-) create mode 100644 test/Format1.java diff --git a/java2python/mod/transform.py b/java2python/mod/transform.py index 3565465..d8e2652 100644 --- a/java2python/mod/transform.py +++ b/java2python/mod/transform.py @@ -106,11 +106,33 @@ def formatSyntaxTransf(match): return '\\n' # Py converts \n to os.linesep result = '{' - # TODO: add flags, width and precision + thous_sep = '' + if(groups['idx']): idx = int(groups['idx'][:-1]) result += str(idx - 1) # Py starts count from 0 - result += ':' + groups['convers'] + '}' + result += ':' + + if(groups['flags']): + if ',' in groups['flags']: + thous_sep = ',' + if '+' in groups['flags']: + result += '+' + elif ' ' in groups['flags']: + result += ' ' + if '#' in groups['flags']: + result += '#' + if '0' in groups['flags']: + result += '0' + + if(groups['width']): + result += groups['width'] + result += thous_sep + + if(groups['precision']): + result += groups['precision'] + + result += groups['convers'] + '}' return result @@ -128,7 +150,7 @@ def formatString(node, config): format = call_args[0].firstChildOfType(tokens.STRING_LITERAL) if format: format.token.text = \ - re.sub(r'%(?P\d+\$)?(?P[scdoxefgn])', + re.sub(r'%(?P\d+\$)?(?P[-+# 0,]+)?(?P[0-9]+)?(?P\.[0-9]+)?(?P[scdoxefgn])', formatSyntaxTransf, format.token.text, flags=re.IGNORECASE) diff --git a/test/Format1.java b/test/Format1.java new file mode 100644 index 0000000..29e94ee --- /dev/null +++ b/test/Format1.java @@ -0,0 +1,19 @@ +public class Format1 { + public static void main(String[] args) { + long n = 461012; + String f1 = String.format("%d%n", n); // --> "461012" + String f2 = String.format("%08d%n", n); // --> "00461012" + String f3 = String.format("%+8d%n", n); // --> " +461012" + String f4 = String.format("%,8d%n", n); // --> " 461,012" + String f5 = String.format("%+,8d%n", n); // --> "+461,012" + + System.out.println(f1 + f2 + f3 + f4 + f5); + + double pi = 3.14159265; + String pf1 = String.format("%f%n", pi); // --> "3.141593" + String pf2 = String.format("%.3f%n", pi); // --> "3.142" + String pf3 = String.format("%10.3f%n", pi); // --> " 3.142" + + System.out.println(pf1 + pf2 + pf3); + } +}