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