< prev index next >

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

Print this page




 229                 putChar(buf, j, Character.lowSurrogate(cp));
 230             }
 231         }
 232         return buf;
 233     }
 234 
 235     public static byte[] toBytes(char c) {
 236         byte[] result = new byte[2];
 237         putChar(result, 0, c);
 238         return result;
 239     }
 240 
 241     static byte[] toBytesSupplementary(int cp) {
 242         byte[] result = new byte[4];
 243         putChar(result, 0, Character.highSurrogate(cp));
 244         putChar(result, 1, Character.lowSurrogate(cp));
 245         return result;
 246     }
 247 
 248     @HotSpotIntrinsicCandidate
 249     public static void getChars(byte[] value, int srcBegin, int srcEnd, char dst[], int dstBegin) {
 250         // We need a range check here because 'getChar' has no checks
 251         if (srcBegin < srcEnd) {
 252             checkBoundsOffCount(srcBegin, srcEnd - srcBegin, value);
 253         }
 254         for (int i = srcBegin; i < srcEnd; i++) {
 255             dst[dstBegin++] = getChar(value, i);
 256         }
 257     }
 258 
 259     /* @see java.lang.String.getBytes(int, int, byte[], int) */
 260     public static void getBytes(byte[] value, int srcBegin, int srcEnd, byte dst[], int dstBegin) {
 261         srcBegin <<= 1;
 262         srcEnd <<= 1;
 263         for (int i = srcBegin + (1 >> LO_BYTE_SHIFT); i < srcEnd; i += 2) {
 264             dst[dstBegin++] = value[i];
 265         }
 266     }
 267 
 268     @HotSpotIntrinsicCandidate
 269     public static boolean equals(byte[] value, byte[] other) {
 270         if (value.length == other.length) {
 271             int len = value.length >> 1;
 272             for (int i = 0; i < len; i++) {
 273                 if (getChar(value, i) != getChar(other, i)) {
 274                     return false;
 275                 }
 276             }
 277             return true;
 278         }
 279         return false;
 280     }


 557             char lo = Character.lowSurrogate(ch);
 558             int i = Math.min(fromIndex, (value.length >> 1) - 2);
 559             for (; i >= 0; i--) {
 560                 if (getChar(value, i) == hi && getChar(value, i + 1) == lo) {
 561                     return i;
 562                 }
 563             }
 564         }
 565         return -1;
 566     }
 567 
 568     public static String replace(byte[] value, char oldChar, char newChar) {
 569         int len = value.length >> 1;
 570         int i = -1;
 571         while (++i < len) {
 572             if (getChar(value, i) == oldChar) {
 573                 break;
 574             }
 575         }
 576         if (i < len) {
 577             byte buf[] = new byte[value.length];
 578             for (int j = 0; j < i; j++) {
 579                 putChar(buf, j, getChar(value, j)); // TBD:arraycopy?
 580             }
 581             while (i < len) {
 582                 char c = getChar(value, i);
 583                 putChar(buf, i, c == oldChar ? newChar : c);
 584                 i++;
 585            }
 586            // Check if we should try to compress to latin1
 587            if (String.COMPACT_STRINGS &&
 588                !StringLatin1.canEncode(oldChar) &&
 589                StringLatin1.canEncode(newChar)) {
 590                byte[] val = compress(buf, 0, len);
 591                if (val != null) {
 592                    return new String(val, LATIN1);
 593                }
 594            }
 595            return new String(buf, UTF16);
 596         }
 597         return null;




 229                 putChar(buf, j, Character.lowSurrogate(cp));
 230             }
 231         }
 232         return buf;
 233     }
 234 
 235     public static byte[] toBytes(char c) {
 236         byte[] result = new byte[2];
 237         putChar(result, 0, c);
 238         return result;
 239     }
 240 
 241     static byte[] toBytesSupplementary(int cp) {
 242         byte[] result = new byte[4];
 243         putChar(result, 0, Character.highSurrogate(cp));
 244         putChar(result, 1, Character.lowSurrogate(cp));
 245         return result;
 246     }
 247 
 248     @HotSpotIntrinsicCandidate
 249     public static void getChars(byte[] value, int srcBegin, int srcEnd, char[] dst, int dstBegin) {
 250         // We need a range check here because 'getChar' has no checks
 251         if (srcBegin < srcEnd) {
 252             checkBoundsOffCount(srcBegin, srcEnd - srcBegin, value);
 253         }
 254         for (int i = srcBegin; i < srcEnd; i++) {
 255             dst[dstBegin++] = getChar(value, i);
 256         }
 257     }
 258 
 259     /* @see java.lang.String.getBytes(int, int, byte[], int) */
 260     public static void getBytes(byte[] value, int srcBegin, int srcEnd, byte[] dst, int dstBegin) {
 261         srcBegin <<= 1;
 262         srcEnd <<= 1;
 263         for (int i = srcBegin + (1 >> LO_BYTE_SHIFT); i < srcEnd; i += 2) {
 264             dst[dstBegin++] = value[i];
 265         }
 266     }
 267 
 268     @HotSpotIntrinsicCandidate
 269     public static boolean equals(byte[] value, byte[] other) {
 270         if (value.length == other.length) {
 271             int len = value.length >> 1;
 272             for (int i = 0; i < len; i++) {
 273                 if (getChar(value, i) != getChar(other, i)) {
 274                     return false;
 275                 }
 276             }
 277             return true;
 278         }
 279         return false;
 280     }


 557             char lo = Character.lowSurrogate(ch);
 558             int i = Math.min(fromIndex, (value.length >> 1) - 2);
 559             for (; i >= 0; i--) {
 560                 if (getChar(value, i) == hi && getChar(value, i + 1) == lo) {
 561                     return i;
 562                 }
 563             }
 564         }
 565         return -1;
 566     }
 567 
 568     public static String replace(byte[] value, char oldChar, char newChar) {
 569         int len = value.length >> 1;
 570         int i = -1;
 571         while (++i < len) {
 572             if (getChar(value, i) == oldChar) {
 573                 break;
 574             }
 575         }
 576         if (i < len) {
 577             byte[] buf = new byte[value.length];
 578             for (int j = 0; j < i; j++) {
 579                 putChar(buf, j, getChar(value, j)); // TBD:arraycopy?
 580             }
 581             while (i < len) {
 582                 char c = getChar(value, i);
 583                 putChar(buf, i, c == oldChar ? newChar : c);
 584                 i++;
 585            }
 586            // Check if we should try to compress to latin1
 587            if (String.COMPACT_STRINGS &&
 588                !StringLatin1.canEncode(oldChar) &&
 589                StringLatin1.canEncode(newChar)) {
 590                byte[] val = compress(buf, 0, len);
 591                if (val != null) {
 592                    return new String(val, LATIN1);
 593                }
 594            }
 595            return new String(buf, UTF16);
 596         }
 597         return null;


< prev index next >