RFR: JDK-8285932 Implementation of JEP-430 String Templates (Preview) [v13]

Rémi Forax forax at openjdk.org
Sat Nov 5 23:13:37 UTC 2022


On Thu, 3 Nov 2022 17:23:53 GMT, Jim Laskey <jlaskey at openjdk.org> wrote:

>> Enhance the Java programming language with string templates, which are similar to string literals but contain embedded expressions. A string template is interpreted at run time by replacing each expression with the result of evaluating that expression, possibly after further validation and transformation. This is a [preview language feature and API](http://openjdk.java.net/jeps/12).
>
> Jim Laskey has updated the pull request incrementally with one additional commit since the last revision:
> 
>   Internalize FormatConcatItem

src/java.base/share/classes/java/lang/template/TemplateRuntime.java line 158:

> 156:                 values[j++] = value;
> 157:             }
> 158:         }

The sides effects `fragments[i++] += fragmentIter.next();` and `i--` are ugly and can easily be overlook.
Also there is no need to use an iterator here, we know that the fragments have a List.get() in O(1).
The idea is to concat the last fragment of a template with the first one of the next template, so i propose

        ...
        String[] fragments = new String[size + 1];
        String last = "";
        int i = 0, j = 0;
        for (StringTemplate st : sts) {
            List<String> templateFragments = st.fragments();
            fragments[i++] = last.concat(templateFragments.get(0));  // concat last fragment with first new fragment
            int k = 0;
            for(; k < templateFragments.size() - 1; k++) {
              fragments[i++] = templateFragments.get(k);
            }
            last = templateFragments.get(k);
            for (Object value : st.values()) {
                values[j++] = value;
            }
        }
        fragments[i] = last;

-------------

PR: https://git.openjdk.org/jdk/pull/10889


More information about the compiler-dev mailing list