Skip to content

Commit ba85122

Browse files
authored
Python to Java string formatting
1 parent 3cffb49 commit ba85122

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

BasicExpression.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6773,6 +6773,14 @@ else if (data.equals("$fin") && parameters.size() > 0)
67736773
{ return "new Date()"; }
67746774
}
67756775

6776+
if (data.equals("formattedString") &&
6777+
"StringLib".equals(objectRef + ""))
6778+
{ Expression par = (Expression) parameters.get(0);
6779+
String pqf = par.queryForm(env,local);
6780+
String cmd = "(new Object() { public String call(Object _x) { return " + Expression.formattedString(pqf) + "; } }).call(this)";
6781+
return cmd;
6782+
}
6783+
67766784

67776785
if (umlkind == VALUE || umlkind == CONSTANT)
67786786
{ if (data.equals("Set{}") || data.equals("Sequence{}"))
@@ -7335,6 +7343,14 @@ public String queryFormJava6(java.util.Map env, boolean local)
73357343
{ return "new Date()"; }
73367344
}
73377345

7346+
if (data.equals("formattedString") &&
7347+
"StringLib".equals(objectRef + ""))
7348+
{ Expression par = (Expression) parameters.get(0);
7349+
String pqf = par.queryFormJava6(env,local);
7350+
String cmd = "(new Object() { public String call(Object _x) { return " + Expression.formattedString(pqf) + "; } }).call(this)";
7351+
return cmd;
7352+
}
7353+
73387354
if (type != null && type.isEnumeration() &&
73397355
type.hasValue(data))
73407356
{ return type.getName() + "." + data; }

Expression.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -265,21 +265,25 @@ public static String formattedString(String f)
265265
boolean inElement = false;
266266
boolean inFormat = false;
267267
String var = "";
268+
String fmt = "";
268269
for (int i = 0; i < f.length(); i++)
269270
{ char c = f.charAt(i);
270271
if (inElement)
271272
{ if (':' == c)
272-
{ res = res + "\" + " + var + " + \"";
273-
var = "";
273+
{ fmt = "";
274274
inFormat = true;
275275
}
276276
else if ('}' == c)
277-
{ inElement = false;
277+
{ if ("".equals(fmt))
278+
{ fmt = "s"; }
279+
res = res + "\" + String.format(\"%" + fmt + "\", " + var + ") + \"";
280+
inElement = false;
278281
inFormat = false;
279282
var = "";
283+
fmt = "";
280284
}
281285
else if (inFormat)
282-
{ } // skip the format
286+
{ fmt = fmt + c; } // skip the format
283287
else
284288
{ var = var + c; } // var name character
285289
}

0 commit comments

Comments
 (0)