src/share/vm/oops/symbolOop.cpp

Thu, 27 May 2010 19:08:38 -0700

author
trims
date
Thu, 27 May 2010 19:08:38 -0700
changeset 1907
c18cbe5936b8
parent 1573
dd57230ba8fe
child 2314
f95d63e2154a
permissions
-rw-r--r--

6941466: Oracle rebranding changes for Hotspot repositories
Summary: Change all the Sun copyrights to Oracle copyright
Reviewed-by: ohair

duke@435 1 /*
trims@1907 2 * Copyright (c) 1997, 2009, Oracle and/or its affiliates. All rights reserved.
duke@435 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@435 4 *
duke@435 5 * This code is free software; you can redistribute it and/or modify it
duke@435 6 * under the terms of the GNU General Public License version 2 only, as
duke@435 7 * published by the Free Software Foundation.
duke@435 8 *
duke@435 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@435 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@435 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@435 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@435 13 * accompanied this code).
duke@435 14 *
duke@435 15 * You should have received a copy of the GNU General Public License version
duke@435 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@435 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@435 18 *
trims@1907 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
trims@1907 20 * or visit www.oracle.com if you need additional information or have any
trims@1907 21 * questions.
duke@435 22 *
duke@435 23 */
duke@435 24
duke@435 25 # include "incls/_precompiled.incl"
duke@435 26 # include "incls/_symbolOop.cpp.incl"
duke@435 27
twisti@1573 28
twisti@1573 29 // ------------------------------------------------------------------
twisti@1573 30 // symbolOopDesc::equals
twisti@1573 31 //
twisti@1573 32 // Compares the symbol with a string of the given length.
duke@435 33 bool symbolOopDesc::equals(const char* str, int len) const {
duke@435 34 int l = utf8_length();
duke@435 35 if (l != len) return false;
duke@435 36 while (l-- > 0) {
duke@435 37 if (str[l] != (char) byte_at(l))
duke@435 38 return false;
duke@435 39 }
duke@435 40 assert(l == -1, "we should be at the beginning");
duke@435 41 return true;
duke@435 42 }
duke@435 43
twisti@1573 44
twisti@1573 45 // ------------------------------------------------------------------
twisti@1573 46 // symbolOopDesc::starts_with
twisti@1573 47 //
twisti@1573 48 // Tests if the symbol starts with the specified prefix of the given
twisti@1573 49 // length.
twisti@1573 50 bool symbolOopDesc::starts_with(const char* prefix, int len) const {
twisti@1573 51 if (len > utf8_length()) return false;
twisti@1573 52 while (len-- > 0) {
twisti@1573 53 if (prefix[len] != (char) byte_at(len))
twisti@1573 54 return false;
twisti@1573 55 }
twisti@1573 56 assert(len == -1, "we should be at the beginning");
twisti@1573 57 return true;
twisti@1573 58 }
twisti@1573 59
twisti@1573 60
twisti@1573 61 // ------------------------------------------------------------------
twisti@1573 62 // symbolOopDesc::index_of
twisti@1573 63 //
twisti@1573 64 // Finds if the given string is a substring of this symbol's utf8 bytes.
twisti@1573 65 // Return -1 on failure. Otherwise return the first index where str occurs.
twisti@1573 66 int symbolOopDesc::index_of_at(int i, const char* str, int len) const {
twisti@1573 67 assert(i >= 0 && i <= utf8_length(), "oob");
twisti@1573 68 if (len <= 0) return 0;
twisti@1573 69 char first_char = str[0];
twisti@1573 70 address bytes = (address) ((symbolOopDesc*)this)->base();
twisti@1573 71 address limit = bytes + utf8_length() - len; // inclusive limit
twisti@1573 72 address scan = bytes + i;
twisti@1573 73 if (scan > limit)
twisti@1573 74 return -1;
twisti@1573 75 for (;;) {
twisti@1573 76 scan = (address) memchr(scan, first_char, (limit + 1 - scan));
twisti@1573 77 if (scan == NULL)
twisti@1573 78 return -1; // not found
twisti@1573 79 assert(scan >= bytes+i && scan <= limit, "scan oob");
twisti@1573 80 if (memcmp(scan, str, len) == 0)
twisti@1573 81 return (int)(scan - bytes);
twisti@1573 82 }
twisti@1573 83 }
twisti@1573 84
twisti@1573 85
duke@435 86 char* symbolOopDesc::as_C_string(char* buf, int size) const {
duke@435 87 if (size > 0) {
duke@435 88 int len = MIN2(size - 1, utf8_length());
duke@435 89 for (int i = 0; i < len; i++) {
duke@435 90 buf[i] = byte_at(i);
duke@435 91 }
duke@435 92 buf[len] = '\0';
duke@435 93 }
duke@435 94 return buf;
duke@435 95 }
duke@435 96
duke@435 97 char* symbolOopDesc::as_C_string() const {
duke@435 98 int len = utf8_length();
duke@435 99 char* str = NEW_RESOURCE_ARRAY(char, len + 1);
duke@435 100 return as_C_string(str, len + 1);
duke@435 101 }
duke@435 102
duke@435 103 char* symbolOopDesc::as_C_string_flexible_buffer(Thread* t,
duke@435 104 char* buf, int size) const {
duke@435 105 char* str;
duke@435 106 int len = utf8_length();
duke@435 107 int buf_len = len + 1;
duke@435 108 if (size < buf_len) {
duke@435 109 str = NEW_RESOURCE_ARRAY(char, buf_len);
duke@435 110 } else {
duke@435 111 str = buf;
duke@435 112 }
duke@435 113 return as_C_string(str, buf_len);
duke@435 114 }
duke@435 115
duke@435 116 void symbolOopDesc::print_symbol_on(outputStream* st) {
duke@435 117 st = st ? st : tty;
never@657 118 int length = UTF8::unicode_length((const char*)bytes(), utf8_length());
never@657 119 const char *ptr = (const char *)bytes();
never@657 120 jchar value;
never@657 121 for (int index = 0; index < length; index++) {
never@657 122 ptr = UTF8::next(ptr, &value);
never@657 123 if (value >= 32 && value < 127 || value == '\'' || value == '\\') {
never@657 124 st->put(value);
never@657 125 } else {
never@657 126 st->print("\\u%04x", value);
never@657 127 }
never@657 128 }
duke@435 129 }
duke@435 130
duke@435 131 jchar* symbolOopDesc::as_unicode(int& length) const {
duke@435 132 symbolOopDesc* this_ptr = (symbolOopDesc*)this;
duke@435 133 length = UTF8::unicode_length((char*)this_ptr->bytes(), utf8_length());
duke@435 134 jchar* result = NEW_RESOURCE_ARRAY(jchar, length);
duke@435 135 if (length > 0) {
duke@435 136 UTF8::convert_to_unicode((char*)this_ptr->bytes(), result, length);
duke@435 137 }
duke@435 138 return result;
duke@435 139 }
duke@435 140
duke@435 141 const char* symbolOopDesc::as_klass_external_name(char* buf, int size) const {
duke@435 142 if (size > 0) {
duke@435 143 char* str = as_C_string(buf, size);
duke@435 144 int length = (int)strlen(str);
duke@435 145 // Turn all '/'s into '.'s (also for array klasses)
duke@435 146 for (int index = 0; index < length; index++) {
duke@435 147 if (str[index] == '/') {
duke@435 148 str[index] = '.';
duke@435 149 }
duke@435 150 }
duke@435 151 return str;
duke@435 152 } else {
duke@435 153 return buf;
duke@435 154 }
duke@435 155 }
duke@435 156
duke@435 157 const char* symbolOopDesc::as_klass_external_name() const {
duke@435 158 char* str = as_C_string();
duke@435 159 int length = (int)strlen(str);
duke@435 160 // Turn all '/'s into '.'s (also for array klasses)
duke@435 161 for (int index = 0; index < length; index++) {
duke@435 162 if (str[index] == '/') {
duke@435 163 str[index] = '.';
duke@435 164 }
duke@435 165 }
duke@435 166 return str;
duke@435 167 }

mercurial