回复:Adding appendHex method to StringBuilder
温绍锦(高铁)
shaojin.wensj at alibaba-inc.com
Mon Oct 2 12:46:55 UTC 2023
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? 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.
```java
package java.net;
class Inet6Address extends InetAddress {
static String numericToTextFormat(byte[] src) {
StringBuilder sb = new StringBuilder(39);
for (int i = 0; i < (INADDRSZ / INT16SZ); i++) {
sb.append(Integer.toHexString(((src[i<<1]<<8) & 0xff00)
| (src[(i<<1)+1] & 0xff)));
if (i < (INADDRSZ / INT16SZ) -1 ) {
sb.append(":");
}
}
return sb.toString();
}
}
class SocketPermission {
private boolean authorizedIPv6(String cname, byte[] addr) {
String authHost = "";
InetAddress auth;
try {
StringBuilder sb = new StringBuilder(39);
for (int i = 15; i >= 0; i--) {
sb.append(Integer.toHexString(((addr[i]) & 0x0f)));
sb.append('.');
sb.append(Integer.toHexString(((addr[i] >> 4) & 0x0f)));
sb.append('.');
}
...
}
}
```
```java
package sun.security.krb5.internal.crypto.dk;
class DkCrypto {
static String bytesToString(byte[] digest) {
// Get character representation of digest
StringBuilder digestString = new StringBuilder();
for (int i = 0; i < digest.length; i++) {
if ((digest[i] & 0x000000ff) < 0x10) {
digestString.append('0').append(Integer.toHexString(digest[i] & 0x000000ff));
} else {
digestString.append(
Integer.toHexString(digest[i] & 0x000000ff));
}
}
return digestString.toString();
}
}
```
- wenshao
------------------------------------------------------------------
发件人:Mark Reinhold <mark.reinhold at oracle.com>
发送时间:2023年10月2日(星期一) 20:19
收件人:Claes Redestad <claes.redestad at oracle.com>
抄 送:温绍锦(高铁) <shaojin.wensj at alibaba-inc.com>; core-libs-dev at openjdk.org <core-libs-dev at openjdk.org>
主 题:Re: Adding appendHex method to StringBuilder
2023/10/2 7:22:55 -0400, claes.redestad at oracle.com:
> I think this goes against the grain: StringBuilder is a simple builder
> for non-localized unformatted output. For formatted output there’s
> Formatter, String.format and, with JEP 430[1], FMT."..." to help output
> string data in more advanced, formatted and localized ways.
Agreed.
If we were to introduce a `fooBar(x)` method for every common
`foo(bar(x))` expression in the JDK code base then our APIs
would fast become inscrutable.
- Mark
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/core-libs-dev/attachments/20231002/28651516/attachment.htm>
More information about the core-libs-dev
mailing list