Java9ã®StringABuilder.appndã®å¦ç
Java9ã®å¤æ´ç¹ã調ã¹ã¦ããããã½ã¼ã¹ã³ã¼ããè¦ãããªã£ã¦ããã®ã§ãopenjdkã®ã½ã¼ã¹ã³ã¼ããè¦ã¦ã¿ãã
Stringã«é¢ãã¦ã¯ä»¥ä¸ã®ããã°ã§ç´¹ä»ããã¦ãã¾ãã
ä»å調ã¹ã¦è¦ãã®ã§ã¯ãStringBuilder.appendã®å¦çã
LATIN1ããUTF-16ã追å ããã¨ãã®å¦çã§ãã
ã½ã¼ã¹ã³ã¼ã
StringBuilder
public StringBuilder append(String str) { super.append(str); return this; }
AbstractStringBuilder
public AbstractStringBuilder append(String str) { if (str == null) { return appendNull(); } int len = str.length(); ensureCapacityInternal(count + len); putStringAt(count, str); count += len; return this; }
ensureCapacityInternalã§é åã確ä¿ãã¦ãputStringAtã§çµåãã¦ããããã§ãã
ensureCapacityInternalã«é¢ãã¦ã¯ãã©ã®ãããªãªã¼ãã¼ããã¼ãæ°ã«ãã¦ããã®ãï¼newCapacityã«ã¯ã©ã®ãããªãµã¤ãºãåå¾ãããã¨ãã¦ããã®ãããæ£ç´è¯ãããããªãã£ãã§ãã
private void ensureCapacityInternal(int minimumCapacity) { // overflow-conscious code int oldCapacity = value.length >> coder; if (minimumCapacity - oldCapacity > 0) { value = Arrays.copyOf(value, newCapacity(minimumCapacity) << coder); } }
private int newCapacity(int minCapacity) { // overflow-conscious code int oldCapacity = value.length >> coder; int newCapacity = (oldCapacity << 1) + 2; if (newCapacity - minCapacity < 0) { newCapacity = minCapacity; } int SAFE_BOUND = MAX_ARRAY_SIZE >> coder; return (newCapacity <= 0 || SAFE_BOUND - newCapacity < 0) ? hugeCapacity(minCapacity) : newCapacity; }
ããã¦æ¬é¡ã¯putStringAtã§ãã
ãã®ä¸ã§coderãæ¯è¼ãã¦éã£ã¦ãããæ¡å¼µãã¦ãã¾ãã
private final void putStringAt(int index, String str) { if (getCoder() != str.coder()) { inflate(); } str.getBytes(value, index, coder); }
ããã§ä¸æåãã¤ã³ãã¼ãã¦ããã®ããããã¾ãã
public static void inflate(byte[] src, int srcOff, byte[] dst, int dstOff, int len) { // We need a range check here because 'putChar' has no checks checkBoundsOffCount(dstOff, len, dst); for (int i = 0; i < len; i++) { putChar(dst, dstOff++, src[srcOff++] & 0xff); } }
static void putChar(byte[] val, int index, int c) { assert index >= 0 && index < length(val) : "Trusted caller missed bounds check"; index <<= 1; val[index++] = (byte)(c >> HI_BYTE_SHIFT); val[index] = (byte)(c >> LO_BYTE_SHIFT); }
ææ³
綺éºãªã½ã¼ã¹ã³ã¼ããèªãã¦ãå¦ã³ã¯ããããããããèªãã®ã¯çµæ§é£ããã