src/java.base/share/classes/java/lang/StringConcatHelper.java
author rriggs
Wed, 28 Nov 2018 15:53:49 -0500
changeset 52724 0bdbf854472f
parent 52326 77018c2b97df
child 54550 5fa7fbddfe9d
permissions -rw-r--r--
4947890: Minimize JNI upcalls in system-properties initialization Reviewed-by: erikj, mchung, bchristi, ihse, coleenp, stuefe
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
35388
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
     1
/*
44642
331e669007f7 8158168: Missing bounds checks for some String intrinsics
dlong
parents: 40810
diff changeset
     2
 * Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.
35388
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
     4
 *
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
48373
f9152f462cbc 8193758: Update copyright headers of files in src tree that are missing Classpath exception
alanb
parents: 47216
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
f9152f462cbc 8193758: Update copyright headers of files in src tree that are missing Classpath exception
alanb
parents: 47216
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
f9152f462cbc 8193758: Update copyright headers of files in src tree that are missing Classpath exception
alanb
parents: 47216
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
35388
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    10
 *
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    15
 * accompanied this code).
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    16
 *
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    20
 *
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    23
 * questions.
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    24
 */
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    25
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    26
package java.lang;
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    27
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    28
/**
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    29
 * Helper for string concatenation. These methods are mostly looked up with private lookups
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    30
 * from {@link java.lang.invoke.StringConcatFactory}, and used in {@link java.lang.invoke.MethodHandle}
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    31
 * combinators there.
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    32
 */
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    33
final class StringConcatHelper {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    34
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    35
    private StringConcatHelper() {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    36
        // no instantiation
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    37
    }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    38
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    39
    /**
52326
77018c2b97df 8213035: Pack MethodHandleInlineStrategy coder and length into a long
redestad
parents: 52177
diff changeset
    40
     * Check for overflow, throw exception on overflow.
77018c2b97df 8213035: Pack MethodHandleInlineStrategy coder and length into a long
redestad
parents: 52177
diff changeset
    41
     * @param lengthCoder String length and coder
77018c2b97df 8213035: Pack MethodHandleInlineStrategy coder and length into a long
redestad
parents: 52177
diff changeset
    42
     * @return lengthCoder
35388
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    43
     */
52326
77018c2b97df 8213035: Pack MethodHandleInlineStrategy coder and length into a long
redestad
parents: 52177
diff changeset
    44
    private static long checkOverflow(long lengthCoder) {
77018c2b97df 8213035: Pack MethodHandleInlineStrategy coder and length into a long
redestad
parents: 52177
diff changeset
    45
        if ((int)lengthCoder >= 0) {
77018c2b97df 8213035: Pack MethodHandleInlineStrategy coder and length into a long
redestad
parents: 52177
diff changeset
    46
            return lengthCoder;
35388
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    47
        }
52326
77018c2b97df 8213035: Pack MethodHandleInlineStrategy coder and length into a long
redestad
parents: 52177
diff changeset
    48
        throw new OutOfMemoryError("Overflow: String length out of range");
35388
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    49
    }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    50
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    51
    /**
52326
77018c2b97df 8213035: Pack MethodHandleInlineStrategy coder and length into a long
redestad
parents: 52177
diff changeset
    52
     * Mix value length and coder into current length and coder.
35388
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    53
     * @param current current length
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    54
     * @param value   value to mix in
52326
77018c2b97df 8213035: Pack MethodHandleInlineStrategy coder and length into a long
redestad
parents: 52177
diff changeset
    55
     * @return new length and coder
35388
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    56
     */
52326
77018c2b97df 8213035: Pack MethodHandleInlineStrategy coder and length into a long
redestad
parents: 52177
diff changeset
    57
    static long mix(long current, boolean value) {
35388
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    58
        return checkOverflow(current + (value ? 4 : 5));
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    59
    }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    60
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    61
    /**
52326
77018c2b97df 8213035: Pack MethodHandleInlineStrategy coder and length into a long
redestad
parents: 52177
diff changeset
    62
     * Mix value length and coder into current length and coder.
35388
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    63
     * @param current current length
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    64
     * @param value   value to mix in
52326
77018c2b97df 8213035: Pack MethodHandleInlineStrategy coder and length into a long
redestad
parents: 52177
diff changeset
    65
     * @return new length and coder
35388
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    66
     */
52326
77018c2b97df 8213035: Pack MethodHandleInlineStrategy coder and length into a long
redestad
parents: 52177
diff changeset
    67
    static long mix(long current, byte value) {
77018c2b97df 8213035: Pack MethodHandleInlineStrategy coder and length into a long
redestad
parents: 52177
diff changeset
    68
        return mix(current, (int)value);
35388
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    69
    }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    70
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    71
    /**
52326
77018c2b97df 8213035: Pack MethodHandleInlineStrategy coder and length into a long
redestad
parents: 52177
diff changeset
    72
     * Mix value length and coder into current length and coder.
35388
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    73
     * @param current current length
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    74
     * @param value   value to mix in
52326
77018c2b97df 8213035: Pack MethodHandleInlineStrategy coder and length into a long
redestad
parents: 52177
diff changeset
    75
     * @return new length and coder
35388
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    76
     */
52326
77018c2b97df 8213035: Pack MethodHandleInlineStrategy coder and length into a long
redestad
parents: 52177
diff changeset
    77
    static long mix(long current, char value) {
77018c2b97df 8213035: Pack MethodHandleInlineStrategy coder and length into a long
redestad
parents: 52177
diff changeset
    78
        return checkOverflow(current + 1) | (StringLatin1.canEncode(value) ? 0 : UTF16);
35388
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    79
    }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    80
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    81
    /**
52326
77018c2b97df 8213035: Pack MethodHandleInlineStrategy coder and length into a long
redestad
parents: 52177
diff changeset
    82
     * Mix value length and coder into current length and coder.
35388
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    83
     * @param current current length
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    84
     * @param value   value to mix in
52326
77018c2b97df 8213035: Pack MethodHandleInlineStrategy coder and length into a long
redestad
parents: 52177
diff changeset
    85
     * @return new length and coder
35388
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    86
     */
52326
77018c2b97df 8213035: Pack MethodHandleInlineStrategy coder and length into a long
redestad
parents: 52177
diff changeset
    87
    static long mix(long current, short value) {
77018c2b97df 8213035: Pack MethodHandleInlineStrategy coder and length into a long
redestad
parents: 52177
diff changeset
    88
        return mix(current, (int)value);
35388
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    89
    }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    90
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    91
    /**
52326
77018c2b97df 8213035: Pack MethodHandleInlineStrategy coder and length into a long
redestad
parents: 52177
diff changeset
    92
     * Mix value length and coder into current length and coder.
35388
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    93
     * @param current current length
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    94
     * @param value   value to mix in
52326
77018c2b97df 8213035: Pack MethodHandleInlineStrategy coder and length into a long
redestad
parents: 52177
diff changeset
    95
     * @return new length and coder
35388
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    96
     */
52326
77018c2b97df 8213035: Pack MethodHandleInlineStrategy coder and length into a long
redestad
parents: 52177
diff changeset
    97
    static long mix(long current, int value) {
35388
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    98
        return checkOverflow(current + Integer.stringSize(value));
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
    99
    }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   100
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   101
    /**
52326
77018c2b97df 8213035: Pack MethodHandleInlineStrategy coder and length into a long
redestad
parents: 52177
diff changeset
   102
     * Mix value length and coder into current length and coder.
35388
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   103
     * @param current current length
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   104
     * @param value   value to mix in
52326
77018c2b97df 8213035: Pack MethodHandleInlineStrategy coder and length into a long
redestad
parents: 52177
diff changeset
   105
     * @return new length and coder
35388
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   106
     */
52326
77018c2b97df 8213035: Pack MethodHandleInlineStrategy coder and length into a long
redestad
parents: 52177
diff changeset
   107
    static long mix(long current, long value) {
35388
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   108
        return checkOverflow(current + Long.stringSize(value));
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   109
    }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   110
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   111
    /**
52326
77018c2b97df 8213035: Pack MethodHandleInlineStrategy coder and length into a long
redestad
parents: 52177
diff changeset
   112
     * Mix value length and coder into current length and coder.
35388
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   113
     * @param current current length
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   114
     * @param value   value to mix in
52326
77018c2b97df 8213035: Pack MethodHandleInlineStrategy coder and length into a long
redestad
parents: 52177
diff changeset
   115
     * @return new length and coder
35388
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   116
     */
52326
77018c2b97df 8213035: Pack MethodHandleInlineStrategy coder and length into a long
redestad
parents: 52177
diff changeset
   117
    static long mix(long current, String value) {
77018c2b97df 8213035: Pack MethodHandleInlineStrategy coder and length into a long
redestad
parents: 52177
diff changeset
   118
        current += value.length();
77018c2b97df 8213035: Pack MethodHandleInlineStrategy coder and length into a long
redestad
parents: 52177
diff changeset
   119
        if (value.coder() == String.UTF16) {
77018c2b97df 8213035: Pack MethodHandleInlineStrategy coder and length into a long
redestad
parents: 52177
diff changeset
   120
            current |= UTF16;
77018c2b97df 8213035: Pack MethodHandleInlineStrategy coder and length into a long
redestad
parents: 52177
diff changeset
   121
        }
77018c2b97df 8213035: Pack MethodHandleInlineStrategy coder and length into a long
redestad
parents: 52177
diff changeset
   122
        return checkOverflow(current);
35388
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   123
    }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   124
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   125
    /**
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   126
     * Prepends the stringly representation of boolean value into buffer,
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   127
     * given the coder and final index. Index is measured in chars, not in bytes!
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   128
     *
52326
77018c2b97df 8213035: Pack MethodHandleInlineStrategy coder and length into a long
redestad
parents: 52177
diff changeset
   129
     * @param indexCoder final char index in the buffer, along with coder packed
77018c2b97df 8213035: Pack MethodHandleInlineStrategy coder and length into a long
redestad
parents: 52177
diff changeset
   130
     *                   into higher bits.
77018c2b97df 8213035: Pack MethodHandleInlineStrategy coder and length into a long
redestad
parents: 52177
diff changeset
   131
     * @param buf        buffer to append to
77018c2b97df 8213035: Pack MethodHandleInlineStrategy coder and length into a long
redestad
parents: 52177
diff changeset
   132
     * @param value      boolean value to encode
77018c2b97df 8213035: Pack MethodHandleInlineStrategy coder and length into a long
redestad
parents: 52177
diff changeset
   133
     * @return           updated index (coder value retained)
35388
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   134
     */
52326
77018c2b97df 8213035: Pack MethodHandleInlineStrategy coder and length into a long
redestad
parents: 52177
diff changeset
   135
    static long prepend(long indexCoder, byte[] buf, boolean value) {
77018c2b97df 8213035: Pack MethodHandleInlineStrategy coder and length into a long
redestad
parents: 52177
diff changeset
   136
        int index = (int)indexCoder;
77018c2b97df 8213035: Pack MethodHandleInlineStrategy coder and length into a long
redestad
parents: 52177
diff changeset
   137
        if (indexCoder < UTF16) {
35388
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   138
            if (value) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   139
                buf[--index] = 'e';
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   140
                buf[--index] = 'u';
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   141
                buf[--index] = 'r';
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   142
                buf[--index] = 't';
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   143
            } else {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   144
                buf[--index] = 'e';
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   145
                buf[--index] = 's';
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   146
                buf[--index] = 'l';
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   147
                buf[--index] = 'a';
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   148
                buf[--index] = 'f';
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   149
            }
52326
77018c2b97df 8213035: Pack MethodHandleInlineStrategy coder and length into a long
redestad
parents: 52177
diff changeset
   150
            return index;
35388
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   151
        } else {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   152
            if (value) {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   153
                StringUTF16.putChar(buf, --index, 'e');
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   154
                StringUTF16.putChar(buf, --index, 'u');
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   155
                StringUTF16.putChar(buf, --index, 'r');
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   156
                StringUTF16.putChar(buf, --index, 't');
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   157
            } else {
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   158
                StringUTF16.putChar(buf, --index, 'e');
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   159
                StringUTF16.putChar(buf, --index, 's');
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   160
                StringUTF16.putChar(buf, --index, 'l');
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   161
                StringUTF16.putChar(buf, --index, 'a');
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   162
                StringUTF16.putChar(buf, --index, 'f');
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   163
            }
52326
77018c2b97df 8213035: Pack MethodHandleInlineStrategy coder and length into a long
redestad
parents: 52177
diff changeset
   164
            return index | UTF16;
35388
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   165
        }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   166
    }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   167
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   168
    /**
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   169
     * Prepends the stringly representation of byte value into buffer,
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   170
     * given the coder and final index. Index is measured in chars, not in bytes!
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   171
     *
52326
77018c2b97df 8213035: Pack MethodHandleInlineStrategy coder and length into a long
redestad
parents: 52177
diff changeset
   172
     * @param indexCoder final char index in the buffer, along with coder packed
77018c2b97df 8213035: Pack MethodHandleInlineStrategy coder and length into a long
redestad
parents: 52177
diff changeset
   173
     *                   into higher bits.
77018c2b97df 8213035: Pack MethodHandleInlineStrategy coder and length into a long
redestad
parents: 52177
diff changeset
   174
     * @param buf        buffer to append to
77018c2b97df 8213035: Pack MethodHandleInlineStrategy coder and length into a long
redestad
parents: 52177
diff changeset
   175
     * @param value      byte value to encode
77018c2b97df 8213035: Pack MethodHandleInlineStrategy coder and length into a long
redestad
parents: 52177
diff changeset
   176
     * @return           updated index (coder value retained)
35388
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   177
     */
52326
77018c2b97df 8213035: Pack MethodHandleInlineStrategy coder and length into a long
redestad
parents: 52177
diff changeset
   178
    static long prepend(long indexCoder, byte[] buf, byte value) {
77018c2b97df 8213035: Pack MethodHandleInlineStrategy coder and length into a long
redestad
parents: 52177
diff changeset
   179
        return prepend(indexCoder, buf, (int)value);
35388
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   180
    }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   181
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   182
    /**
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   183
     * Prepends the stringly representation of char value into buffer,
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   184
     * given the coder and final index. Index is measured in chars, not in bytes!
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   185
     *
52326
77018c2b97df 8213035: Pack MethodHandleInlineStrategy coder and length into a long
redestad
parents: 52177
diff changeset
   186
     * @param indexCoder final char index in the buffer, along with coder packed
77018c2b97df 8213035: Pack MethodHandleInlineStrategy coder and length into a long
redestad
parents: 52177
diff changeset
   187
     *                   into higher bits.
77018c2b97df 8213035: Pack MethodHandleInlineStrategy coder and length into a long
redestad
parents: 52177
diff changeset
   188
     * @param buf        buffer to append to
77018c2b97df 8213035: Pack MethodHandleInlineStrategy coder and length into a long
redestad
parents: 52177
diff changeset
   189
     * @param value      char value to encode
77018c2b97df 8213035: Pack MethodHandleInlineStrategy coder and length into a long
redestad
parents: 52177
diff changeset
   190
     * @return           updated index (coder value retained)
35388
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   191
     */
52326
77018c2b97df 8213035: Pack MethodHandleInlineStrategy coder and length into a long
redestad
parents: 52177
diff changeset
   192
    static long prepend(long indexCoder, byte[] buf, char value) {
77018c2b97df 8213035: Pack MethodHandleInlineStrategy coder and length into a long
redestad
parents: 52177
diff changeset
   193
        if (indexCoder < UTF16) {
77018c2b97df 8213035: Pack MethodHandleInlineStrategy coder and length into a long
redestad
parents: 52177
diff changeset
   194
            buf[(int)(--indexCoder)] = (byte) (value & 0xFF);
35388
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   195
        } else {
52326
77018c2b97df 8213035: Pack MethodHandleInlineStrategy coder and length into a long
redestad
parents: 52177
diff changeset
   196
            StringUTF16.putChar(buf, (int)(--indexCoder), value);
35388
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   197
        }
52326
77018c2b97df 8213035: Pack MethodHandleInlineStrategy coder and length into a long
redestad
parents: 52177
diff changeset
   198
        return indexCoder;
35388
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   199
    }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   200
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   201
    /**
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   202
     * Prepends the stringly representation of short value into buffer,
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   203
     * given the coder and final index. Index is measured in chars, not in bytes!
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   204
     *
52326
77018c2b97df 8213035: Pack MethodHandleInlineStrategy coder and length into a long
redestad
parents: 52177
diff changeset
   205
     * @param indexCoder final char index in the buffer, along with coder packed
77018c2b97df 8213035: Pack MethodHandleInlineStrategy coder and length into a long
redestad
parents: 52177
diff changeset
   206
     *                   into higher bits.
77018c2b97df 8213035: Pack MethodHandleInlineStrategy coder and length into a long
redestad
parents: 52177
diff changeset
   207
     * @param buf        buffer to append to
77018c2b97df 8213035: Pack MethodHandleInlineStrategy coder and length into a long
redestad
parents: 52177
diff changeset
   208
     * @param value      short value to encode
77018c2b97df 8213035: Pack MethodHandleInlineStrategy coder and length into a long
redestad
parents: 52177
diff changeset
   209
     * @return           updated index (coder value retained)
35388
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   210
     */
52326
77018c2b97df 8213035: Pack MethodHandleInlineStrategy coder and length into a long
redestad
parents: 52177
diff changeset
   211
    static long prepend(long indexCoder, byte[] buf, short value) {
77018c2b97df 8213035: Pack MethodHandleInlineStrategy coder and length into a long
redestad
parents: 52177
diff changeset
   212
        return prepend(indexCoder, buf, (int)value);
35388
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   213
    }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   214
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   215
    /**
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   216
     * Prepends the stringly representation of integer value into buffer,
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   217
     * given the coder and final index. Index is measured in chars, not in bytes!
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   218
     *
52326
77018c2b97df 8213035: Pack MethodHandleInlineStrategy coder and length into a long
redestad
parents: 52177
diff changeset
   219
     * @param indexCoder final char index in the buffer, along with coder packed
77018c2b97df 8213035: Pack MethodHandleInlineStrategy coder and length into a long
redestad
parents: 52177
diff changeset
   220
     *                   into higher bits.
77018c2b97df 8213035: Pack MethodHandleInlineStrategy coder and length into a long
redestad
parents: 52177
diff changeset
   221
     * @param buf        buffer to append to
77018c2b97df 8213035: Pack MethodHandleInlineStrategy coder and length into a long
redestad
parents: 52177
diff changeset
   222
     * @param value      integer value to encode
77018c2b97df 8213035: Pack MethodHandleInlineStrategy coder and length into a long
redestad
parents: 52177
diff changeset
   223
     * @return           updated index (coder value retained)
35388
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   224
     */
52326
77018c2b97df 8213035: Pack MethodHandleInlineStrategy coder and length into a long
redestad
parents: 52177
diff changeset
   225
    static long prepend(long indexCoder, byte[] buf, int value) {
77018c2b97df 8213035: Pack MethodHandleInlineStrategy coder and length into a long
redestad
parents: 52177
diff changeset
   226
        if (indexCoder < UTF16) {
77018c2b97df 8213035: Pack MethodHandleInlineStrategy coder and length into a long
redestad
parents: 52177
diff changeset
   227
            return Integer.getChars(value, (int)indexCoder, buf);
35388
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   228
        } else {
52326
77018c2b97df 8213035: Pack MethodHandleInlineStrategy coder and length into a long
redestad
parents: 52177
diff changeset
   229
            return StringUTF16.getChars(value, (int)indexCoder, buf) | UTF16;
35388
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   230
        }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   231
    }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   232
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   233
    /**
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   234
     * Prepends the stringly representation of long value into buffer,
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   235
     * given the coder and final index. Index is measured in chars, not in bytes!
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   236
     *
52326
77018c2b97df 8213035: Pack MethodHandleInlineStrategy coder and length into a long
redestad
parents: 52177
diff changeset
   237
     * @param indexCoder final char index in the buffer, along with coder packed
77018c2b97df 8213035: Pack MethodHandleInlineStrategy coder and length into a long
redestad
parents: 52177
diff changeset
   238
     *                   into higher bits.
77018c2b97df 8213035: Pack MethodHandleInlineStrategy coder and length into a long
redestad
parents: 52177
diff changeset
   239
     * @param buf        buffer to append to
77018c2b97df 8213035: Pack MethodHandleInlineStrategy coder and length into a long
redestad
parents: 52177
diff changeset
   240
     * @param value      long value to encode
77018c2b97df 8213035: Pack MethodHandleInlineStrategy coder and length into a long
redestad
parents: 52177
diff changeset
   241
     * @return           updated index (coder value retained)
35388
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   242
     */
52326
77018c2b97df 8213035: Pack MethodHandleInlineStrategy coder and length into a long
redestad
parents: 52177
diff changeset
   243
    static long prepend(long indexCoder, byte[] buf, long value) {
77018c2b97df 8213035: Pack MethodHandleInlineStrategy coder and length into a long
redestad
parents: 52177
diff changeset
   244
        if (indexCoder < UTF16) {
77018c2b97df 8213035: Pack MethodHandleInlineStrategy coder and length into a long
redestad
parents: 52177
diff changeset
   245
            return Long.getChars(value, (int)indexCoder, buf);
35388
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   246
        } else {
52326
77018c2b97df 8213035: Pack MethodHandleInlineStrategy coder and length into a long
redestad
parents: 52177
diff changeset
   247
            return StringUTF16.getChars(value, (int)indexCoder, buf) | UTF16;
35388
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   248
        }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   249
    }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   250
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   251
    /**
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   252
     * Prepends the stringly representation of String value into buffer,
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   253
     * given the coder and final index. Index is measured in chars, not in bytes!
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   254
     *
52326
77018c2b97df 8213035: Pack MethodHandleInlineStrategy coder and length into a long
redestad
parents: 52177
diff changeset
   255
     * @param indexCoder final char index in the buffer, along with coder packed
77018c2b97df 8213035: Pack MethodHandleInlineStrategy coder and length into a long
redestad
parents: 52177
diff changeset
   256
     *                   into higher bits.
77018c2b97df 8213035: Pack MethodHandleInlineStrategy coder and length into a long
redestad
parents: 52177
diff changeset
   257
     * @param buf        buffer to append to
77018c2b97df 8213035: Pack MethodHandleInlineStrategy coder and length into a long
redestad
parents: 52177
diff changeset
   258
     * @param value      String value to encode
77018c2b97df 8213035: Pack MethodHandleInlineStrategy coder and length into a long
redestad
parents: 52177
diff changeset
   259
     * @return           updated index (coder value retained)
35388
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   260
     */
52326
77018c2b97df 8213035: Pack MethodHandleInlineStrategy coder and length into a long
redestad
parents: 52177
diff changeset
   261
    static long prepend(long indexCoder, byte[] buf, String value) {
77018c2b97df 8213035: Pack MethodHandleInlineStrategy coder and length into a long
redestad
parents: 52177
diff changeset
   262
        indexCoder -= value.length();
77018c2b97df 8213035: Pack MethodHandleInlineStrategy coder and length into a long
redestad
parents: 52177
diff changeset
   263
        if (indexCoder < UTF16) {
77018c2b97df 8213035: Pack MethodHandleInlineStrategy coder and length into a long
redestad
parents: 52177
diff changeset
   264
            value.getBytes(buf, (int)indexCoder, String.LATIN1);
77018c2b97df 8213035: Pack MethodHandleInlineStrategy coder and length into a long
redestad
parents: 52177
diff changeset
   265
        } else {
77018c2b97df 8213035: Pack MethodHandleInlineStrategy coder and length into a long
redestad
parents: 52177
diff changeset
   266
            value.getBytes(buf, (int)indexCoder, String.UTF16);
77018c2b97df 8213035: Pack MethodHandleInlineStrategy coder and length into a long
redestad
parents: 52177
diff changeset
   267
        }
77018c2b97df 8213035: Pack MethodHandleInlineStrategy coder and length into a long
redestad
parents: 52177
diff changeset
   268
        return indexCoder;
35388
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   269
    }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   270
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   271
    /**
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   272
     * Instantiates the String with given buffer and coder
52326
77018c2b97df 8213035: Pack MethodHandleInlineStrategy coder and length into a long
redestad
parents: 52177
diff changeset
   273
     * @param buf           buffer to use
77018c2b97df 8213035: Pack MethodHandleInlineStrategy coder and length into a long
redestad
parents: 52177
diff changeset
   274
     * @param indexCoder    remaining index (should be zero) and coder
77018c2b97df 8213035: Pack MethodHandleInlineStrategy coder and length into a long
redestad
parents: 52177
diff changeset
   275
     * @return String       resulting string
35388
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   276
     */
52326
77018c2b97df 8213035: Pack MethodHandleInlineStrategy coder and length into a long
redestad
parents: 52177
diff changeset
   277
    static String newString(byte[] buf, long indexCoder) {
35388
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   278
        // Use the private, non-copying constructor (unsafe!)
52326
77018c2b97df 8213035: Pack MethodHandleInlineStrategy coder and length into a long
redestad
parents: 52177
diff changeset
   279
        if (indexCoder == LATIN1) {
77018c2b97df 8213035: Pack MethodHandleInlineStrategy coder and length into a long
redestad
parents: 52177
diff changeset
   280
            return new String(buf, String.LATIN1);
77018c2b97df 8213035: Pack MethodHandleInlineStrategy coder and length into a long
redestad
parents: 52177
diff changeset
   281
        } else if (indexCoder == UTF16) {
77018c2b97df 8213035: Pack MethodHandleInlineStrategy coder and length into a long
redestad
parents: 52177
diff changeset
   282
            return new String(buf, String.UTF16);
77018c2b97df 8213035: Pack MethodHandleInlineStrategy coder and length into a long
redestad
parents: 52177
diff changeset
   283
        } else {
77018c2b97df 8213035: Pack MethodHandleInlineStrategy coder and length into a long
redestad
parents: 52177
diff changeset
   284
            throw new InternalError("Storage is not completely initialized, " + (int)indexCoder + " bytes left");
40810
b88d5910ea1e 8165492: Reduce number of lambda forms generated by MethodHandleInlineCopyStrategy
redestad
parents: 35700
diff changeset
   285
        }
35388
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   286
    }
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   287
52326
77018c2b97df 8213035: Pack MethodHandleInlineStrategy coder and length into a long
redestad
parents: 52177
diff changeset
   288
    private static final long LATIN1 = (long)String.LATIN1 << 32;
77018c2b97df 8213035: Pack MethodHandleInlineStrategy coder and length into a long
redestad
parents: 52177
diff changeset
   289
77018c2b97df 8213035: Pack MethodHandleInlineStrategy coder and length into a long
redestad
parents: 52177
diff changeset
   290
    private static final long UTF16 = (long)String.UTF16 << 32;
77018c2b97df 8213035: Pack MethodHandleInlineStrategy coder and length into a long
redestad
parents: 52177
diff changeset
   291
35700
b933119b8f84 8148869: StringConcatFactory MH_INLINE_SIZED_EXACT strategy does not work with -XX:-CompactStrings
shade
parents: 35388
diff changeset
   292
    /**
b933119b8f84 8148869: StringConcatFactory MH_INLINE_SIZED_EXACT strategy does not work with -XX:-CompactStrings
shade
parents: 35388
diff changeset
   293
     * Provides the initial coder for the String.
52326
77018c2b97df 8213035: Pack MethodHandleInlineStrategy coder and length into a long
redestad
parents: 52177
diff changeset
   294
     * @return initial coder, adjusted into the upper half
35700
b933119b8f84 8148869: StringConcatFactory MH_INLINE_SIZED_EXACT strategy does not work with -XX:-CompactStrings
shade
parents: 35388
diff changeset
   295
     */
52326
77018c2b97df 8213035: Pack MethodHandleInlineStrategy coder and length into a long
redestad
parents: 52177
diff changeset
   296
    static long initialCoder() {
77018c2b97df 8213035: Pack MethodHandleInlineStrategy coder and length into a long
redestad
parents: 52177
diff changeset
   297
        return String.COMPACT_STRINGS ? LATIN1 : UTF16;
35700
b933119b8f84 8148869: StringConcatFactory MH_INLINE_SIZED_EXACT strategy does not work with -XX:-CompactStrings
shade
parents: 35388
diff changeset
   298
    }
b933119b8f84 8148869: StringConcatFactory MH_INLINE_SIZED_EXACT strategy does not work with -XX:-CompactStrings
shade
parents: 35388
diff changeset
   299
35388
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents:
diff changeset
   300
}