< prev index next >

src/java.base/share/classes/java/lang/StringConcatHelper.java

Print this page
rev 54622 : 8222852: Reduce String concat combinator tree shapes by folding constants into prependers
Reviewed-by: shade, plevart
Contributed-by: claes.redestad@oracle.com, peter.levart@gmail.com


 127      * @return            new length and coder
 128      */
 129     static long mix(long lengthCoder, String value) {
 130         lengthCoder += value.length();
 131         if (value.coder() == String.UTF16) {
 132             lengthCoder |= UTF16;
 133         }
 134         return checkOverflow(lengthCoder);
 135     }
 136 
 137     /**
 138      * Prepends the stringly representation of boolean value into buffer,
 139      * given the coder and final index. Index is measured in chars, not in bytes!
 140      *
 141      * @param indexCoder final char index in the buffer, along with coder packed
 142      *                   into higher bits.
 143      * @param buf        buffer to append to
 144      * @param value      boolean value to encode
 145      * @return           updated index (coder value retained)
 146      */
 147     static long prepend(long indexCoder, byte[] buf, boolean value) {
 148         int index = (int)indexCoder;
 149         if (indexCoder < UTF16) {
 150             if (value) {
 151                 buf[--index] = 'e';
 152                 buf[--index] = 'u';
 153                 buf[--index] = 'r';
 154                 buf[--index] = 't';
 155             } else {
 156                 buf[--index] = 'e';
 157                 buf[--index] = 's';
 158                 buf[--index] = 'l';
 159                 buf[--index] = 'a';
 160                 buf[--index] = 'f';
 161             }
 162             return index;
 163         } else {
 164             if (value) {
 165                 StringUTF16.putChar(buf, --index, 'e');
 166                 StringUTF16.putChar(buf, --index, 'u');
 167                 StringUTF16.putChar(buf, --index, 'r');
 168                 StringUTF16.putChar(buf, --index, 't');
 169             } else {
 170                 StringUTF16.putChar(buf, --index, 'e');
 171                 StringUTF16.putChar(buf, --index, 's');
 172                 StringUTF16.putChar(buf, --index, 'l');
 173                 StringUTF16.putChar(buf, --index, 'a');
 174                 StringUTF16.putChar(buf, --index, 'f');
 175             }
 176             return index | UTF16;
 177         }
 178     }
 179 
 180     /**
 181      * Prepends the stringly representation of byte value into buffer,
 182      * given the coder and final index. Index is measured in chars, not in bytes!
 183      *
 184      * @param indexCoder final char index in the buffer, along with coder packed
 185      *                   into higher bits.
 186      * @param buf        buffer to append to
 187      * @param value      byte value to encode


 188      * @return           updated index (coder value retained)
 189      */
 190     static long prepend(long indexCoder, byte[] buf, byte value) {
 191         return prepend(indexCoder, buf, (int)value);






















 192     }
 193 
 194     /**
 195      * Prepends the stringly representation of char value into buffer,
 196      * given the coder and final index. Index is measured in chars, not in bytes!
 197      *
 198      * @param indexCoder final char index in the buffer, along with coder packed
 199      *                   into higher bits.
 200      * @param buf        buffer to append to
 201      * @param value      char value to encode
 202      * @return           updated index (coder value retained)
 203      */
 204     static long prepend(long indexCoder, byte[] buf, char value) {
 205         if (indexCoder < UTF16) {
 206             buf[(int)(--indexCoder)] = (byte) (value & 0xFF);
 207         } else {
 208             StringUTF16.putChar(buf, (int)(--indexCoder), value);
 209         }
 210         return indexCoder;
 211     }
 212 
 213     /**
 214      * Prepends the stringly representation of short value into buffer,
 215      * given the coder and final index. Index is measured in chars, not in bytes!
 216      *
 217      * @param indexCoder final char index in the buffer, along with coder packed
 218      *                   into higher bits.
 219      * @param buf        buffer to append to
 220      * @param value      short value to encode


 221      * @return           updated index (coder value retained)
 222      */
 223     static long prepend(long indexCoder, byte[] buf, short value) {
 224         return prepend(indexCoder, buf, (int)value);






















 225     }
 226 
 227     /**
 228      * Prepends the stringly representation of integer value into buffer,
 229      * given the coder and final index. Index is measured in chars, not in bytes!
 230      *
 231      * @param indexCoder final char index in the buffer, along with coder packed
 232      *                   into higher bits.
 233      * @param buf        buffer to append to
 234      * @param value      integer value to encode
 235      * @return           updated index (coder value retained)
 236      */
 237     static long prepend(long indexCoder, byte[] buf, int value) {
 238         if (indexCoder < UTF16) {
 239             return Integer.getChars(value, (int)indexCoder, buf);
 240         } else {
 241             return StringUTF16.getChars(value, (int)indexCoder, buf) | UTF16;
 242         }
 243     }
 244 
 245     /**



















 246      * Prepends the stringly representation of long value into buffer,
 247      * given the coder and final index. Index is measured in chars, not in bytes!
 248      *
 249      * @param indexCoder final char index in the buffer, along with coder packed
 250      *                   into higher bits.
 251      * @param buf        buffer to append to
 252      * @param value      long value to encode
 253      * @return           updated index (coder value retained)
 254      */
 255     static long prepend(long indexCoder, byte[] buf, long value) {
 256         if (indexCoder < UTF16) {
 257             return Long.getChars(value, (int)indexCoder, buf);
 258         } else {
 259             return StringUTF16.getChars(value, (int)indexCoder, buf) | UTF16;
 260         }
 261     }
 262 
 263     /**



















 264      * Prepends the stringly representation of String value into buffer,
 265      * given the coder and final index. Index is measured in chars, not in bytes!
 266      *
 267      * @param indexCoder final char index in the buffer, along with coder packed
 268      *                   into higher bits.
 269      * @param buf        buffer to append to
 270      * @param value      String value to encode
 271      * @return           updated index (coder value retained)
 272      */
 273     static long prepend(long indexCoder, byte[] buf, String value) {
 274         indexCoder -= value.length();
 275         if (indexCoder < UTF16) {
 276             value.getBytes(buf, (int)indexCoder, String.LATIN1);
 277         } else {
 278             value.getBytes(buf, (int)indexCoder, String.UTF16);
 279         }



















 280         return indexCoder;
 281     }
 282 
 283     /**
 284      * Instantiates the String with given buffer and coder
 285      * @param buf           buffer to use
 286      * @param indexCoder    remaining index (should be zero) and coder
 287      * @return String       resulting string
 288      */
 289     static String newString(byte[] buf, long indexCoder) {
 290         // Use the private, non-copying constructor (unsafe!)
 291         if (indexCoder == LATIN1) {
 292             return new String(buf, String.LATIN1);
 293         } else if (indexCoder == UTF16) {
 294             return new String(buf, String.UTF16);
 295         } else {
 296             throw new InternalError("Storage is not completely initialized, " + (int)indexCoder + " bytes left");
 297         }
 298     }
 299 




 127      * @return            new length and coder
 128      */
 129     static long mix(long lengthCoder, String value) {
 130         lengthCoder += value.length();
 131         if (value.coder() == String.UTF16) {
 132             lengthCoder |= UTF16;
 133         }
 134         return checkOverflow(lengthCoder);
 135     }
 136 
 137     /**
 138      * Prepends the stringly representation of boolean value into buffer,
 139      * given the coder and final index. Index is measured in chars, not in bytes!
 140      *
 141      * @param indexCoder final char index in the buffer, along with coder packed
 142      *                   into higher bits.
 143      * @param buf        buffer to append to
 144      * @param value      boolean value to encode
 145      * @return           updated index (coder value retained)
 146      */
 147     private static long prepend(long indexCoder, byte[] buf, boolean value) {
 148         int index = (int)indexCoder;
 149         if (indexCoder < UTF16) {
 150             if (value) {
 151                 buf[--index] = 'e';
 152                 buf[--index] = 'u';
 153                 buf[--index] = 'r';
 154                 buf[--index] = 't';
 155             } else {
 156                 buf[--index] = 'e';
 157                 buf[--index] = 's';
 158                 buf[--index] = 'l';
 159                 buf[--index] = 'a';
 160                 buf[--index] = 'f';
 161             }
 162             return index;
 163         } else {
 164             if (value) {
 165                 StringUTF16.putChar(buf, --index, 'e');
 166                 StringUTF16.putChar(buf, --index, 'u');
 167                 StringUTF16.putChar(buf, --index, 'r');
 168                 StringUTF16.putChar(buf, --index, 't');
 169             } else {
 170                 StringUTF16.putChar(buf, --index, 'e');
 171                 StringUTF16.putChar(buf, --index, 's');
 172                 StringUTF16.putChar(buf, --index, 'l');
 173                 StringUTF16.putChar(buf, --index, 'a');
 174                 StringUTF16.putChar(buf, --index, 'f');
 175             }
 176             return index | UTF16;
 177         }
 178     }
 179 
 180     /**
 181      * Prepends constant and the stringly representation of value into buffer,
 182      * given the coder and final index. Index is measured in chars, not in bytes!
 183      *
 184      * @param indexCoder final char index in the buffer, along with coder packed
 185      *                   into higher bits.
 186      * @param buf        buffer to append to
 187      * @param prefix     a constant to prepend before value
 188      * @param value      boolean value to encode
 189      * @param suffix     a constant to prepend after value
 190      * @return           updated index (coder value retained)
 191      */
 192     static long prepend(long indexCoder, byte[] buf, String prefix, boolean value, String suffix) {
 193         if (suffix != null) indexCoder = prepend(indexCoder, buf, suffix);
 194         indexCoder = prepend(indexCoder, buf, value);
 195         if (prefix != null) indexCoder = prepend(indexCoder, buf, prefix);
 196         return indexCoder;
 197     }
 198 
 199     /**
 200      * Prepends constant and the stringly representation of value into buffer,
 201      * given the coder and final index. Index is measured in chars, not in bytes!
 202      *
 203      * @param indexCoder final char index in the buffer, along with coder packed
 204      *                   into higher bits.
 205      * @param buf        buffer to append to
 206      * @param prefix     a constant to prepend before value
 207      * @param value      boolean value to encode
 208      * @param suffix     a constant to prepend after value
 209      * @return           updated index (coder value retained)
 210      */
 211     static long prepend(long indexCoder, byte[] buf, String prefix, byte value, String suffix) {
 212         if (suffix != null) indexCoder = prepend(indexCoder, buf, suffix);
 213         indexCoder = prepend(indexCoder, buf, (int)value);
 214         if (prefix != null) indexCoder = prepend(indexCoder, buf, prefix);
 215         return indexCoder;
 216     }
 217 
 218     /**
 219      * Prepends the stringly representation of char value into buffer,
 220      * given the coder and final index. Index is measured in chars, not in bytes!
 221      *
 222      * @param indexCoder final char index in the buffer, along with coder packed
 223      *                   into higher bits.
 224      * @param buf        buffer to append to
 225      * @param value      char value to encode
 226      * @return           updated index (coder value retained)
 227      */
 228     private static long prepend(long indexCoder, byte[] buf, char value) {
 229         if (indexCoder < UTF16) {
 230             buf[(int)(--indexCoder)] = (byte) (value & 0xFF);
 231         } else {
 232             StringUTF16.putChar(buf, (int)(--indexCoder), value);
 233         }
 234         return indexCoder;
 235     }
 236 
 237     /**
 238      * Prepends constant and the stringly representation of value into buffer,
 239      * given the coder and final index. Index is measured in chars, not in bytes!
 240      *
 241      * @param indexCoder final char index in the buffer, along with coder packed
 242      *                   into higher bits.
 243      * @param buf        buffer to append to
 244      * @param prefix     a constant to prepend before value
 245      * @param value      boolean value to encode
 246      * @param suffix     a constant to prepend after value
 247      * @return           updated index (coder value retained)
 248      */
 249     static long prepend(long indexCoder, byte[] buf, String prefix, char value, String suffix) {
 250         if (suffix != null) indexCoder = prepend(indexCoder, buf, suffix);
 251         indexCoder = prepend(indexCoder, buf, value);
 252         if (prefix != null) indexCoder = prepend(indexCoder, buf, prefix);
 253         return indexCoder;
 254     }
 255 
 256     /**
 257      * Prepends constant and the stringly representation of value into buffer,
 258      * given the coder and final index. Index is measured in chars, not in bytes!
 259      *
 260      * @param indexCoder final char index in the buffer, along with coder packed
 261      *                   into higher bits.
 262      * @param buf        buffer to append to
 263      * @param prefix     a constant to prepend before value
 264      * @param value      boolean value to encode
 265      * @param suffix     a constant to prepend after value
 266      * @return           updated index (coder value retained)
 267      */
 268     static long prepend(long indexCoder, byte[] buf, String prefix, short value, String suffix) {
 269         if (suffix != null) indexCoder = prepend(indexCoder, buf, suffix);
 270         indexCoder = prepend(indexCoder, buf, (int)value);
 271         if (prefix != null) indexCoder = prepend(indexCoder, buf, prefix);
 272         return indexCoder;
 273     }
 274 
 275     /**
 276      * Prepends the stringly representation of integer value into buffer,
 277      * given the coder and final index. Index is measured in chars, not in bytes!
 278      *
 279      * @param indexCoder final char index in the buffer, along with coder packed
 280      *                   into higher bits.
 281      * @param buf        buffer to append to
 282      * @param value      integer value to encode
 283      * @return           updated index (coder value retained)
 284      */
 285     private static long prepend(long indexCoder, byte[] buf, int value) {
 286         if (indexCoder < UTF16) {
 287             return Integer.getChars(value, (int)indexCoder, buf);
 288         } else {
 289             return StringUTF16.getChars(value, (int)indexCoder, buf) | UTF16;
 290         }
 291     }
 292 
 293     /**
 294      * Prepends constant and the stringly representation of value into buffer,
 295      * given the coder and final index. Index is measured in chars, not in bytes!
 296      *
 297      * @param indexCoder final char index in the buffer, along with coder packed
 298      *                   into higher bits.
 299      * @param buf        buffer to append to
 300      * @param prefix     a constant to prepend before value
 301      * @param value      boolean value to encode
 302      * @param suffix     a constant to prepend after value
 303      * @return           updated index (coder value retained)
 304      */
 305     static long prepend(long indexCoder, byte[] buf, String prefix, int value, String suffix) {
 306         if (suffix != null) indexCoder = prepend(indexCoder, buf, suffix);
 307         indexCoder = prepend(indexCoder, buf, value);
 308         if (prefix != null) indexCoder = prepend(indexCoder, buf, prefix);
 309         return indexCoder;
 310     }
 311 
 312     /**
 313      * Prepends the stringly representation of long value into buffer,
 314      * given the coder and final index. Index is measured in chars, not in bytes!
 315      *
 316      * @param indexCoder final char index in the buffer, along with coder packed
 317      *                   into higher bits.
 318      * @param buf        buffer to append to
 319      * @param value      long value to encode
 320      * @return           updated index (coder value retained)
 321      */
 322     private static long prepend(long indexCoder, byte[] buf, long value) {
 323         if (indexCoder < UTF16) {
 324             return Long.getChars(value, (int)indexCoder, buf);
 325         } else {
 326             return StringUTF16.getChars(value, (int)indexCoder, buf) | UTF16;
 327         }
 328     }
 329 
 330     /**
 331      * Prepends constant and the stringly representation of value into buffer,
 332      * given the coder and final index. Index is measured in chars, not in bytes!
 333      *
 334      * @param indexCoder final char index in the buffer, along with coder packed
 335      *                   into higher bits.
 336      * @param buf        buffer to append to
 337      * @param prefix     a constant to prepend before value
 338      * @param value      boolean value to encode
 339      * @param suffix     a constant to prepend after value
 340      * @return           updated index (coder value retained)
 341      */
 342     static long prepend(long indexCoder, byte[] buf, String prefix, long value, String suffix) {
 343         if (suffix != null) indexCoder = prepend(indexCoder, buf, suffix);
 344         indexCoder = prepend(indexCoder, buf, value);
 345         if (prefix != null) indexCoder = prepend(indexCoder, buf, prefix);
 346         return indexCoder;
 347     }
 348 
 349     /**
 350      * Prepends the stringly representation of String value into buffer,
 351      * given the coder and final index. Index is measured in chars, not in bytes!
 352      *
 353      * @param indexCoder final char index in the buffer, along with coder packed
 354      *                   into higher bits.
 355      * @param buf        buffer to append to
 356      * @param value      String value to encode
 357      * @return           updated index (coder value retained)
 358      */
 359     private static long prepend(long indexCoder, byte[] buf, String value) {
 360         indexCoder -= value.length();
 361         if (indexCoder < UTF16) {
 362             value.getBytes(buf, (int)indexCoder, String.LATIN1);
 363         } else {
 364             value.getBytes(buf, (int)indexCoder, String.UTF16);
 365         }
 366         return indexCoder;
 367     }
 368 
 369     /**
 370      * Prepends constant and the stringly representation of value into buffer,
 371      * given the coder and final index. Index is measured in chars, not in bytes!
 372      *
 373      * @param indexCoder final char index in the buffer, along with coder packed
 374      *                   into higher bits.
 375      * @param buf        buffer to append to
 376      * @param prefix     a constant to prepend before value
 377      * @param value      boolean value to encode
 378      * @param suffix     a constant to prepend after value
 379      * @return           updated index (coder value retained)
 380      */
 381     static long prepend(long indexCoder, byte[] buf, String prefix, String value, String suffix) {
 382         if (suffix != null) indexCoder = prepend(indexCoder, buf, suffix);
 383         indexCoder = prepend(indexCoder, buf, value);
 384         if (prefix != null) indexCoder = prepend(indexCoder, buf, prefix);
 385         return indexCoder;
 386     }
 387 
 388     /**
 389      * Instantiates the String with given buffer and coder
 390      * @param buf           buffer to use
 391      * @param indexCoder    remaining index (should be zero) and coder
 392      * @return String       resulting string
 393      */
 394     static String newString(byte[] buf, long indexCoder) {
 395         // Use the private, non-copying constructor (unsafe!)
 396         if (indexCoder == LATIN1) {
 397             return new String(buf, String.LATIN1);
 398         } else if (indexCoder == UTF16) {
 399             return new String(buf, String.UTF16);
 400         } else {
 401             throw new InternalError("Storage is not completely initialized, " + (int)indexCoder + " bytes left");
 402         }
 403     }
 404 


< prev index next >