hotspot/src/share/vm/utilities/utf8.hpp
author thartmann
Tue, 03 Nov 2015 09:41:03 +0100
changeset 33628 09241459a8b8
parent 16602 5df51d3bc550
child 36508 5f9eee6b383b
permissions -rw-r--r--
8141132: JEP 254: Compact Strings Summary: Adopt a more space-efficient internal representation for strings. Reviewed-by: alanb, bdelsart, coleenp, iklam, jiangli, jrose, kevinw, naoto, pliden, roland, smarks, twisti Contributed-by: Brent Christian <brent.christian@oracle.com>, Vivek Deshpande <vivek.r.deshpande@intel.com>, Tobias Hartmann <tobias.hartmann@oracle.com>, Charlie Hunt <charlie.hunt@oracle.com>, Vladimir Kozlov <vladimir.kozlov@oracle.com>, Roger Riggs <roger.riggs@oracle.com>, Xueming Shen <xueming.shen@oracle.com>, Aleksey Shipilev <aleksey.shipilev@oracle.com>, Sandhya Viswanathan <sandhya.viswanathan@intel.com>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     1
/*
33628
09241459a8b8 8141132: JEP 254: Compact Strings
thartmann
parents: 16602
diff changeset
     2
 * Copyright (c) 1997, 2015, Oracle and/or its affiliates. All rights reserved.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     4
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
489c9b5090e2 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
489c9b5090e2 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     8
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
489c9b5090e2 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
489c9b5090e2 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
489c9b5090e2 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
489c9b5090e2 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
489c9b5090e2 Initial load
duke
parents:
diff changeset
    14
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
489c9b5090e2 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    18
 *
5547
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 1
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 1
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 1
diff changeset
    21
 * questions.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    22
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    23
 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    24
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    25
#ifndef SHARE_VM_UTILITIES_UTF8_HPP
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    26
#define SHARE_VM_UTILITIES_UTF8_HPP
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    27
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    28
#include "memory/allocation.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    29
#include "utilities/top.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    30
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    31
// Low-level interface for UTF8 strings
489c9b5090e2 Initial load
duke
parents:
diff changeset
    32
489c9b5090e2 Initial load
duke
parents:
diff changeset
    33
class UTF8 : AllStatic {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    34
 public:
14477
95e66ea71f71 6830717: replay of compilations would help with debugging
minqi
parents: 8921
diff changeset
    35
  // returns the unicode length of a 0-terminated utf8 string
33628
09241459a8b8 8141132: JEP 254: Compact Strings
thartmann
parents: 16602
diff changeset
    36
  static int unicode_length(const char* utf8_str) {
09241459a8b8 8141132: JEP 254: Compact Strings
thartmann
parents: 16602
diff changeset
    37
    bool is_latin1, has_multibyte;
09241459a8b8 8141132: JEP 254: Compact Strings
thartmann
parents: 16602
diff changeset
    38
    return unicode_length(utf8_str, is_latin1, has_multibyte);
09241459a8b8 8141132: JEP 254: Compact Strings
thartmann
parents: 16602
diff changeset
    39
  }
09241459a8b8 8141132: JEP 254: Compact Strings
thartmann
parents: 16602
diff changeset
    40
  static int unicode_length(const char* utf8_str, bool& is_latin1, bool& has_multibyte);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    41
14477
95e66ea71f71 6830717: replay of compilations would help with debugging
minqi
parents: 8921
diff changeset
    42
  // returns the unicode length of a non-0-terminated utf8 string
33628
09241459a8b8 8141132: JEP 254: Compact Strings
thartmann
parents: 16602
diff changeset
    43
  static int unicode_length(const char* utf8_str, int len) {
09241459a8b8 8141132: JEP 254: Compact Strings
thartmann
parents: 16602
diff changeset
    44
    bool is_latin1, has_multibyte;
09241459a8b8 8141132: JEP 254: Compact Strings
thartmann
parents: 16602
diff changeset
    45
    return unicode_length(utf8_str, len, is_latin1, has_multibyte);
09241459a8b8 8141132: JEP 254: Compact Strings
thartmann
parents: 16602
diff changeset
    46
  }
09241459a8b8 8141132: JEP 254: Compact Strings
thartmann
parents: 16602
diff changeset
    47
  static int unicode_length(const char* utf8_str, int len, bool& is_latin1, bool& has_multibyte);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    48
14477
95e66ea71f71 6830717: replay of compilations would help with debugging
minqi
parents: 8921
diff changeset
    49
  // converts a utf8 string to a unicode string
33628
09241459a8b8 8141132: JEP 254: Compact Strings
thartmann
parents: 16602
diff changeset
    50
  template<typename T> static void convert_to_unicode(const char* utf8_str, T* unicode_str, int unicode_length);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    51
14477
95e66ea71f71 6830717: replay of compilations would help with debugging
minqi
parents: 8921
diff changeset
    52
  // returns the quoted ascii length of a utf8 string
95e66ea71f71 6830717: replay of compilations would help with debugging
minqi
parents: 8921
diff changeset
    53
  static int quoted_ascii_length(const char* utf8_str, int utf8_length);
95e66ea71f71 6830717: replay of compilations would help with debugging
minqi
parents: 8921
diff changeset
    54
95e66ea71f71 6830717: replay of compilations would help with debugging
minqi
parents: 8921
diff changeset
    55
  // converts a utf8 string to quoted ascii
16602
5df51d3bc550 8011048: Possible reading from unmapped memory in UTF8::as_quoted_ascii()
iklam
parents: 14477
diff changeset
    56
  static void as_quoted_ascii(const char* utf8_str, int utf8_length, char* buf, int buflen);
14477
95e66ea71f71 6830717: replay of compilations would help with debugging
minqi
parents: 8921
diff changeset
    57
95e66ea71f71 6830717: replay of compilations would help with debugging
minqi
parents: 8921
diff changeset
    58
  // converts a quoted ascii string to utf8 string.  returns the original
95e66ea71f71 6830717: replay of compilations would help with debugging
minqi
parents: 8921
diff changeset
    59
  // string unchanged if nothing needs to be done.
95e66ea71f71 6830717: replay of compilations would help with debugging
minqi
parents: 8921
diff changeset
    60
  static const char* from_quoted_ascii(const char* quoted_ascii_string);
95e66ea71f71 6830717: replay of compilations would help with debugging
minqi
parents: 8921
diff changeset
    61
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    62
  // decodes the current utf8 character, stores the result in value,
14477
95e66ea71f71 6830717: replay of compilations would help with debugging
minqi
parents: 8921
diff changeset
    63
  // and returns the end of the current utf8 chararacter.
33628
09241459a8b8 8141132: JEP 254: Compact Strings
thartmann
parents: 16602
diff changeset
    64
  template<typename T> static char* next(const char* str, T* value);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    65
489c9b5090e2 Initial load
duke
parents:
diff changeset
    66
  // decodes the current utf8 character, gets the supplementary character instead of
489c9b5090e2 Initial load
duke
parents:
diff changeset
    67
  // the surrogate pair when seeing a supplementary character in string,
14477
95e66ea71f71 6830717: replay of compilations would help with debugging
minqi
parents: 8921
diff changeset
    68
  // stores the result in value, and returns the end of the current utf8 chararacter.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    69
  static char* next_character(const char* str, jint* value);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    70
489c9b5090e2 Initial load
duke
parents:
diff changeset
    71
  // Utility methods
8076
96d498ec7ae1 6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents: 7397
diff changeset
    72
  static const jbyte* strrchr(const jbyte* base, int length, jbyte c);
96d498ec7ae1 6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents: 7397
diff changeset
    73
  static bool   equal(const jbyte* base1, int length1, const jbyte* base2,int length2);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    74
  static bool   is_supplementary_character(const unsigned char* str);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    75
  static jint   get_supplementary_character(const unsigned char* str);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    76
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
    77
489c9b5090e2 Initial load
duke
parents:
diff changeset
    78
489c9b5090e2 Initial load
duke
parents:
diff changeset
    79
// Low-level interface for UNICODE strings
489c9b5090e2 Initial load
duke
parents:
diff changeset
    80
489c9b5090e2 Initial load
duke
parents:
diff changeset
    81
// A unicode string represents a string in the UTF-16 format in which supplementary
489c9b5090e2 Initial load
duke
parents:
diff changeset
    82
// characters are represented by surrogate pairs. Index values refer to char code
489c9b5090e2 Initial load
duke
parents:
diff changeset
    83
// units, so a supplementary character uses two positions in a unicode string.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    84
489c9b5090e2 Initial load
duke
parents:
diff changeset
    85
class UNICODE : AllStatic {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    86
 public:
33628
09241459a8b8 8141132: JEP 254: Compact Strings
thartmann
parents: 16602
diff changeset
    87
  // checks if the given unicode character can be encoded as latin1
09241459a8b8 8141132: JEP 254: Compact Strings
thartmann
parents: 16602
diff changeset
    88
  static bool is_latin1(jchar c);
09241459a8b8 8141132: JEP 254: Compact Strings
thartmann
parents: 16602
diff changeset
    89
09241459a8b8 8141132: JEP 254: Compact Strings
thartmann
parents: 16602
diff changeset
    90
  // checks if the given string can be encoded as latin1
09241459a8b8 8141132: JEP 254: Compact Strings
thartmann
parents: 16602
diff changeset
    91
  static bool is_latin1(jchar* base, int length);
09241459a8b8 8141132: JEP 254: Compact Strings
thartmann
parents: 16602
diff changeset
    92
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    93
  // returns the utf8 size of a unicode character
489c9b5090e2 Initial load
duke
parents:
diff changeset
    94
  static int utf8_size(jchar c);
33628
09241459a8b8 8141132: JEP 254: Compact Strings
thartmann
parents: 16602
diff changeset
    95
  static int utf8_size(jbyte c);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    96
489c9b5090e2 Initial load
duke
parents:
diff changeset
    97
  // returns the utf8 length of a unicode string
489c9b5090e2 Initial load
duke
parents:
diff changeset
    98
  static int utf8_length(jchar* base, int length);
33628
09241459a8b8 8141132: JEP 254: Compact Strings
thartmann
parents: 16602
diff changeset
    99
  static int utf8_length(jbyte* base, int length);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   100
489c9b5090e2 Initial load
duke
parents:
diff changeset
   101
  // converts a unicode string to utf8 string
489c9b5090e2 Initial load
duke
parents:
diff changeset
   102
  static void convert_to_utf8(const jchar* base, int length, char* utf8_buffer);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   103
489c9b5090e2 Initial load
duke
parents:
diff changeset
   104
  // converts a unicode string to a utf8 string; result is allocated
489c9b5090e2 Initial load
duke
parents:
diff changeset
   105
  // in resource area unless a buffer is provided.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   106
  static char* as_utf8(jchar* base, int length);
33628
09241459a8b8 8141132: JEP 254: Compact Strings
thartmann
parents: 16602
diff changeset
   107
  static char* as_utf8(jbyte* base, int length);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   108
  static char* as_utf8(jchar* base, int length, char* buf, int buflen);
33628
09241459a8b8 8141132: JEP 254: Compact Strings
thartmann
parents: 16602
diff changeset
   109
  static char* as_utf8(jbyte* base, int length, char* buf, int buflen);
14477
95e66ea71f71 6830717: replay of compilations would help with debugging
minqi
parents: 8921
diff changeset
   110
95e66ea71f71 6830717: replay of compilations would help with debugging
minqi
parents: 8921
diff changeset
   111
  // returns the quoted ascii length of a unicode string
33628
09241459a8b8 8141132: JEP 254: Compact Strings
thartmann
parents: 16602
diff changeset
   112
  template<typename T> static int quoted_ascii_length(T* base, int length);
14477
95e66ea71f71 6830717: replay of compilations would help with debugging
minqi
parents: 8921
diff changeset
   113
33628
09241459a8b8 8141132: JEP 254: Compact Strings
thartmann
parents: 16602
diff changeset
   114
  // converts a unicode string to quoted ascii
09241459a8b8 8141132: JEP 254: Compact Strings
thartmann
parents: 16602
diff changeset
   115
  template<typename T> static void as_quoted_ascii(const T* base, int length, char* buf, int buflen);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   116
};
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
   117
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
   118
#endif // SHARE_VM_UTILITIES_UTF8_HPP