You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by di...@apache.org on 2021/07/23 11:23:12 UTC

[sling-org-apache-sling-scripting-sightly] branch issue/SLING-10654 updated: remove micro optimisation

This is an automated email from the ASF dual-hosted git repository.

diru pushed a commit to branch issue/SLING-10654
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-scripting-sightly.git


The following commit(s) were added to refs/heads/issue/SLING-10654 by this push:
     new d63c67b  remove micro optimisation
d63c67b is described below

commit d63c67b7693df30f58e973b1d3f0711bd20fc1d1
Author: Dirk Rudolph <dr...@adobe.com>
AuthorDate: Fri Jul 23 13:19:43 2021 +0200

    remove micro optimisation
---
 .../engine/extension/FormatFilterExtension.java    | 66 +++++++++-------------
 1 file changed, 26 insertions(+), 40 deletions(-)

diff --git a/src/main/java/org/apache/sling/scripting/sightly/impl/engine/extension/FormatFilterExtension.java b/src/main/java/org/apache/sling/scripting/sightly/impl/engine/extension/FormatFilterExtension.java
index 6ec400e..cb40de9 100644
--- a/src/main/java/org/apache/sling/scripting/sightly/impl/engine/extension/FormatFilterExtension.java
+++ b/src/main/java/org/apache/sling/scripting/sightly/impl/engine/extension/FormatFilterExtension.java
@@ -75,25 +75,17 @@ public class FormatFilterExtension implements RuntimeExtension {
 
         String formattingType = runtimeObjectModel.toString(options.get(TYPE_OPTION));
         Object formatObject = options.get(FORMAT_OPTION);
-        boolean hasPlaceHolders = PLACEHOLDER_REGEX.matcher(source).find();
-        // Check for complex placeholders only if no simple placeholders were found. If simple placeholders were found getFormattedString()
-        // is called anyways and the source can be matched complex placeholders later.
-        Boolean hasComplexPlaceholders = null;
-        if (!hasPlaceHolders && hasIcuSupport) {
-            hasComplexPlaceholders = COMPLEX_PLACEHOLDER_REGEX.matcher(source).find();
-            hasPlaceHolders = hasComplexPlaceholders;
-        }
+        boolean hasPlaceHolders = PLACEHOLDER_REGEX.matcher(source).find() || COMPLEX_PLACEHOLDER_REGEX.matcher(source).find();
         if (STRING_FORMAT_TYPE.equals(formattingType)) {
-            return getFormattedString(runtimeObjectModel, source, options, formatObject, hasComplexPlaceholders);
+            return getFormattedString(runtimeObjectModel, source, options, formatObject);
         } else if (DATE_FORMAT_TYPE.equals(formattingType) || (!hasPlaceHolders && runtimeObjectModel.isDate(formatObject))) {
             return getDateFormattedString(runtimeObjectModel, source, options, formatObject);
         } else if (NUMBER_FORMAT_TYPE.equals(formattingType) || (!hasPlaceHolders && runtimeObjectModel.isNumber(formatObject))) {
             return getNumberFormattedString(runtimeObjectModel, source, options, formatObject);
         }
         if (hasPlaceHolders) {
-            return getFormattedString(runtimeObjectModel, source, options, formatObject, hasComplexPlaceholders);
+            return getFormattedString(runtimeObjectModel, source, options, formatObject);
         }
-
         try {
             // try to parse as DateTimeFormatter
             DateTimeFormatter.ofPattern(source);
@@ -108,23 +100,18 @@ public class FormatFilterExtension implements RuntimeExtension {
         } catch (IllegalArgumentException e) {
             // ignore
         }
-        return getFormattedString(runtimeObjectModel, source, options, formatObject, hasComplexPlaceholders);
+        return getFormattedString(runtimeObjectModel, source, options, formatObject);
     }
 
     private Object getFormattedString(RuntimeObjectModel runtimeObjectModel, String source, Map<String, Object> options,
-                                      Object formatObject, Boolean hasComplexPlaceholders) {
+                                      Object formatObject) {
         Object[] params = decodeParams(runtimeObjectModel, formatObject);
-        if (hasIcuSupport) {
-            if (hasComplexPlaceholders == null) {
-                hasComplexPlaceholders = COMPLEX_PLACEHOLDER_REGEX.matcher(source).find();
-            }
-            if (hasComplexPlaceholders) {
-                Locale locale = getLocale(runtimeObjectModel, options);
-                return formatStringIcu(source, locale, params);
-            }
+        if (hasIcuSupport && COMPLEX_PLACEHOLDER_REGEX.matcher(source).find()) {
+            Locale locale = getLocale(runtimeObjectModel, options);
+            return formatStringIcu(runtimeObjectModel, source, locale, params);
+        } else {
+            return formatString(runtimeObjectModel, source, params);
         }
-
-        return formatString(runtimeObjectModel, source, params);
     }
 
     private String getNumberFormattedString(RuntimeObjectModel runtimeObjectModel, String source, Map<String, Object> options,
@@ -180,29 +167,28 @@ public class FormatFilterExtension implements RuntimeExtension {
         Matcher matcher = PLACEHOLDER_REGEX.matcher(source);
         StringBuilder builder = new StringBuilder();
         int lastPos = 0;
-        boolean matched = true;
-        while (matched) {
-            matched = matcher.find();
-            if (matched) {
-                String group = matcher.group();
-                int paramIndex = Integer.parseInt(group.substring(1, group.length() - 1));
-                String replacement = toString(runtimeObjectModel, params, paramIndex);
-                int matchStart = matcher.start();
-                int matchEnd = matcher.end();
-                builder.append(source, lastPos, matchStart).append(replacement);
-                lastPos = matchEnd;
-            }
+        while (matcher.find()) {
+            String group = matcher.group();
+            int paramIndex = Integer.parseInt(group.substring(1, group.length() - 1));
+            String replacement = toString(runtimeObjectModel, params, paramIndex);
+            int matchStart = matcher.start();
+            int matchEnd = matcher.end();
+            builder.append(source, lastPos, matchStart).append(replacement);
+            lastPos = matchEnd;
         }
         builder.append(source, lastPos, source.length());
         return builder.toString();
     }
 
-    private String formatStringIcu(String source, Locale locale, Object[] params) {
-        MessageFormat messageFormat = new MessageFormat(source);
-        if (locale != null) {
-            messageFormat.setLocale(locale);
+    private String formatStringIcu(RuntimeObjectModel runtimeObjectModel, String source, Locale locale, Object[] params) {
+        try {
+            MessageFormat messageFormat = locale != null ? new MessageFormat(source, locale) : new MessageFormat(source);
+            return messageFormat.format(params);
+        } catch (NoClassDefFoundError ex) {
+            LOG.trace("ICU4J not found, falling back to simple pattern replacement.", ex);
+            hasIcuSupport = false;
+            return formatString(runtimeObjectModel, source, params);
         }
-        return messageFormat.format(params);
     }
 
     private String toString(RuntimeObjectModel runtimeObjectModel, Object[] params, int index) {