<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body style="overflow-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;">
<div>It’s possible to build up and combine unprocessed RAW template snippets, then combine and process them together:</div>
<div><br>
</div>
<div>
<pre style="background-color: rgb(43, 43, 43); color: rgb(169, 183, 198); font-family: "JetBrains Mono", monospace;"><span style="color: rgb(204, 120, 50);">static int</span>[] <span style="color: rgb(152, 118, 170); font-style: italic;">val </span>= <span style="color: rgb(204, 120, 50);">new int</span>[] { <span style="color: rgb(104, 151, 187);">2048</span><span style="color: rgb(204, 120, 50);">, </span><span style="color: rgb(104, 151, 187);">31337</span><span style="color: rgb(204, 120, 50);">, </span><span style="color: rgb(104, 151, 187);">0xbeefcace</span><span style="color: rgb(204, 120, 50);">, </span><span style="color: rgb(104, 151, 187);">9000</span><span style="color: rgb(204, 120, 50);">, </span><span style="color: rgb(104, 151, 187);">4711</span><span style="color: rgb(204, 120, 50);">, </span><span style="color: rgb(104, 151, 187);">1337</span><span style="color: rgb(204, 120, 50);">, </span><span style="color: rgb(104, 151, 187);">2100</span><span style="color: rgb(204, 120, 50);">, </span><span style="color: rgb(104, 151, 187);">2600 </span>}<span style="color: rgb(204, 120, 50);">;<br></span>@Benchmark<br><span style="color: rgb(204, 120, 50);">public </span>String <span style="color: rgb(255, 198, 109);">toStringFMTCombineHex8</span>() {<br> <span style="color: rgb(204, 120, 50);">var </span>templates = <span style="color: rgb(204, 120, 50);">new </span>ArrayList<StringTemplate>()<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);"> for </span>(<span style="color: rgb(204, 120, 50);">int </span>i = <span style="color: rgb(104, 151, 187);">0</span><span style="color: rgb(204, 120, 50);">; </span>i < <span style="color: rgb(104, 151, 187);">8</span><span style="color: rgb(204, 120, 50);">; </span>i++) {<br> <span style="color: rgb(204, 120, 50);">int </span>h = <span style="color: rgb(152, 118, 170); font-style: italic;">val</span>[i]<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);"> </span>templates.add(<span style="color: rgb(152, 118, 170); font-style: italic;">RAW</span>.<span style="color: rgb(106, 135, 89);">"%x\{h}"</span>)<span style="color: rgb(204, 120, 50);">;<br></span><span style="color: rgb(204, 120, 50);"> </span>}<br> <span style="color: rgb(204, 120, 50);">return </span>StringTemplate.<span style="font-style: italic;">combine</span>(templates).process(<span style="color: rgb(152, 118, 170); font-style: italic;">FMT</span>)<span style="color: rgb(204, 120, 50);">;<br></span>}</pre>
</div>
<div>
<div>Performance is… not great, but I think we can and should work on that:</div>
<div><br>
</div>
<div>Benchmark Mode Cnt Score Error Units</div>
<div>StringBuilders.toStringFMTCombineHex8 avgt 15 2421,151 ± 646,501 ns/op</div>
</div>
<div><br>
</div>
<div>/Claes</div>
<div>
<div><br>
<blockquote type="cite">
<div>2 okt. 2023 kl. 14:46 skrev 温绍锦(高铁) <shaojin.wensj@alibaba-inc.com>:</div>
<br class="Apple-interchange-newline">
<div>
<div class="__aliyun_email_body_block">
<div style="line-height:1.7;font-family:Tahoma,Arial,STHeiti,SimSun;font-size:14.0px;color:#0070c0;">
<div style="clear:both;"><span>Using String Template FMT has better performance and better code readability. But String Template does not yet support for/while/if/switch. Is there any plan to enhance String Template to support for/while/if/switch? </span>The
following scenarios cannot be implemented using String.format or String Template. I think whether to build in support for foo(bar(x)) depends on whether it is a common enough method.</div>
<div style="clear:both;"><span><br>
</span></div>
<div style="clear:both;"><span></span><span>```java</span>
<div style="clear:both;">package java.net;</div>
<div style="clear:both;">class Inet6Address extends InetAddress {</div>
<div style="clear:both;"> static String numericToTextFormat(byte[] src) {</div>
<div style="clear:both;"> StringBuilder sb = new StringBuilder(39);</div>
<div style="clear:both;"> for (int i = 0; i < (INADDRSZ / INT16SZ); i++) {</div>
<div style="clear:both;"> sb.append(Integer.toHexString(((src[i<<1]<<8) & 0xff00)</div>
<div style="clear:both;"> | (src[(i<<1)+1] & 0xff)));</div>
<div style="clear:both;"> if (i < (INADDRSZ / INT16SZ) -1 ) {</div>
<div style="clear:both;"> sb.append(":");</div>
<div style="clear:both;"> }</div>
<div style="clear:both;"> }</div>
<div style="clear:both;"> return sb.toString();</div>
<div style="clear:both;"> }</div>
<div style="clear:both;">}</div>
<div style="clear:both;">class SocketPermission {</div>
<div style="clear:both;"> private boolean authorizedIPv6(String cname, byte[] addr) {</div>
<div style="clear:both;"> String authHost = "";</div>
<div style="clear:both;"> InetAddress auth;</div>
<div style="clear:both;"> try {</div>
<div style="clear:both;"> StringBuilder sb = new StringBuilder(39);</div>
<div style="clear:both;"> for (int i = 15; i >= 0; i--) {</div>
<div style="clear:both;"> sb.append(Integer.toHexString(((addr[i]) & 0x0f)));</div>
<div style="clear:both;"> sb.append('.');</div>
<div style="clear:both;"> sb.append(Integer.toHexString(((addr[i] >> 4) & 0x0f)));</div>
<div style="clear:both;"> sb.append('.');</div>
<div style="clear:both;"> }</div>
<div style="clear:both;"> ...</div>
<div style="clear:both;"> }</div>
<div style="clear:both;">}</div>
<div style="clear:both;">```</div>
<div style="clear:both;">```java</div>
<div style="clear:both;">package sun.security.krb5.internal.crypto.dk;</div>
<div style="clear:both;">class DkCrypto {</div>
<div style="clear:both;"> static String bytesToString(byte[] digest) {</div>
<div style="clear:both;"> // Get character representation of digest</div>
<div style="clear:both;"> StringBuilder digestString = new StringBuilder();</div>
<div style="clear:both;"> for (int i = 0; i < digest.length; i++) {</div>
<div style="clear:both;"> if ((digest[i] & 0x000000ff) < 0x10) {</div>
<div style="clear:both;"> digestString.append('0').append(Integer.toHexString(digest[i] & 0x000000ff));</div>
<div style="clear:both;"> } else {</div>
<div style="clear:both;"> digestString.append(</div>
<div style="clear:both;"> Integer.toHexString(digest[i] & 0x000000ff));</div>
<div style="clear:both;"> }</div>
<div style="clear:both;"> }</div>
<div style="clear:both;"> return digestString.toString();</div>
<div style="clear:both;"> }</div>
<div style="clear:both;">}</div>
<span>```</span><span></span></div>
<div style="clear:both;"><br>
</div>
<div style="clear:both;">- wenshao</div>
<div style="clear:both;"><br>
</div>
<blockquote _quote="1" style="margin-right: 0px; margin-top: 0px; margin-bottom: 0px; font-family: Tahoma, Arial, STHeiti, SimSun; font-size: 14px;">
<div style="clear:both;">------------------------------------------------------------------</div>
<div style="clear:both;">发件人:Mark Reinhold <mark.reinhold@oracle.com></div>
<div style="clear:both;">发送时间:2023年10月2日(星期一) 20:19</div>
<div style="clear:both;">收件人:Claes Redestad <claes.redestad@oracle.com></div>
<div style="clear:both;">抄 送:温绍锦(高铁) <shaojin.wensj@alibaba-inc.com>; core-libs-dev@openjdk.org <core-libs-dev@openjdk.org></div>
<div style="clear:both;">主 题:Re: Adding appendHex method to StringBuilder</div>
<div style="clear:both;"><br>
</div>
2023/10/2 7:22:55 -0400, claes.redestad@oracle.com:<br>
> I think this goes against the grain: StringBuilder is a simple builder<br>
> for non-localized unformatted output. For formatted output there’s<br>
> Formatter, String.format and, with JEP 430[1], FMT."..." to help output<br>
> string data in more advanced, formatted and localized ways.<br>
<br>
Agreed.<br>
<br>
If we were to introduce a `fooBar(x)` method for every common<br>
`foo(bar(x))` expression in the JDK code base then our APIs<br>
would fast become inscrutable.<br>
<br>
- Mark</blockquote>
<div style="line-height:20.0px;clear:both;"><br>
</div>
</div>
</div>
</div>
</blockquote>
</div>
<br>
</div>
</body>
</html>