duke@435: /* twisti@1573: * Copyright 1997-2009 Sun Microsystems, Inc. All Rights Reserved. duke@435: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@435: * duke@435: * This code is free software; you can redistribute it and/or modify it duke@435: * under the terms of the GNU General Public License version 2 only, as duke@435: * published by the Free Software Foundation. duke@435: * duke@435: * This code is distributed in the hope that it will be useful, but WITHOUT duke@435: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@435: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@435: * version 2 for more details (a copy is included in the LICENSE file that duke@435: * accompanied this code). duke@435: * duke@435: * You should have received a copy of the GNU General Public License version duke@435: * 2 along with this work; if not, write to the Free Software Foundation, duke@435: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@435: * duke@435: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, duke@435: * CA 95054 USA or visit www.sun.com if you need additional information or duke@435: * have any questions. duke@435: * duke@435: */ duke@435: duke@435: # include "incls/_precompiled.incl" duke@435: # include "incls/_symbolOop.cpp.incl" duke@435: twisti@1573: twisti@1573: // ------------------------------------------------------------------ twisti@1573: // symbolOopDesc::equals twisti@1573: // twisti@1573: // Compares the symbol with a string of the given length. duke@435: bool symbolOopDesc::equals(const char* str, int len) const { duke@435: int l = utf8_length(); duke@435: if (l != len) return false; duke@435: while (l-- > 0) { duke@435: if (str[l] != (char) byte_at(l)) duke@435: return false; duke@435: } duke@435: assert(l == -1, "we should be at the beginning"); duke@435: return true; duke@435: } duke@435: twisti@1573: twisti@1573: // ------------------------------------------------------------------ twisti@1573: // symbolOopDesc::starts_with twisti@1573: // twisti@1573: // Tests if the symbol starts with the specified prefix of the given twisti@1573: // length. twisti@1573: bool symbolOopDesc::starts_with(const char* prefix, int len) const { twisti@1573: if (len > utf8_length()) return false; twisti@1573: while (len-- > 0) { twisti@1573: if (prefix[len] != (char) byte_at(len)) twisti@1573: return false; twisti@1573: } twisti@1573: assert(len == -1, "we should be at the beginning"); twisti@1573: return true; twisti@1573: } twisti@1573: twisti@1573: twisti@1573: // ------------------------------------------------------------------ twisti@1573: // symbolOopDesc::index_of twisti@1573: // twisti@1573: // Finds if the given string is a substring of this symbol's utf8 bytes. twisti@1573: // Return -1 on failure. Otherwise return the first index where str occurs. twisti@1573: int symbolOopDesc::index_of_at(int i, const char* str, int len) const { twisti@1573: assert(i >= 0 && i <= utf8_length(), "oob"); twisti@1573: if (len <= 0) return 0; twisti@1573: char first_char = str[0]; twisti@1573: address bytes = (address) ((symbolOopDesc*)this)->base(); twisti@1573: address limit = bytes + utf8_length() - len; // inclusive limit twisti@1573: address scan = bytes + i; twisti@1573: if (scan > limit) twisti@1573: return -1; twisti@1573: for (;;) { twisti@1573: scan = (address) memchr(scan, first_char, (limit + 1 - scan)); twisti@1573: if (scan == NULL) twisti@1573: return -1; // not found twisti@1573: assert(scan >= bytes+i && scan <= limit, "scan oob"); twisti@1573: if (memcmp(scan, str, len) == 0) twisti@1573: return (int)(scan - bytes); twisti@1573: } twisti@1573: } twisti@1573: twisti@1573: duke@435: char* symbolOopDesc::as_C_string(char* buf, int size) const { duke@435: if (size > 0) { duke@435: int len = MIN2(size - 1, utf8_length()); duke@435: for (int i = 0; i < len; i++) { duke@435: buf[i] = byte_at(i); duke@435: } duke@435: buf[len] = '\0'; duke@435: } duke@435: return buf; duke@435: } duke@435: duke@435: char* symbolOopDesc::as_C_string() const { duke@435: int len = utf8_length(); duke@435: char* str = NEW_RESOURCE_ARRAY(char, len + 1); duke@435: return as_C_string(str, len + 1); duke@435: } duke@435: duke@435: char* symbolOopDesc::as_C_string_flexible_buffer(Thread* t, duke@435: char* buf, int size) const { duke@435: char* str; duke@435: int len = utf8_length(); duke@435: int buf_len = len + 1; duke@435: if (size < buf_len) { duke@435: str = NEW_RESOURCE_ARRAY(char, buf_len); duke@435: } else { duke@435: str = buf; duke@435: } duke@435: return as_C_string(str, buf_len); duke@435: } duke@435: duke@435: void symbolOopDesc::print_symbol_on(outputStream* st) { duke@435: st = st ? st : tty; never@657: int length = UTF8::unicode_length((const char*)bytes(), utf8_length()); never@657: const char *ptr = (const char *)bytes(); never@657: jchar value; never@657: for (int index = 0; index < length; index++) { never@657: ptr = UTF8::next(ptr, &value); never@657: if (value >= 32 && value < 127 || value == '\'' || value == '\\') { never@657: st->put(value); never@657: } else { never@657: st->print("\\u%04x", value); never@657: } never@657: } duke@435: } duke@435: duke@435: jchar* symbolOopDesc::as_unicode(int& length) const { duke@435: symbolOopDesc* this_ptr = (symbolOopDesc*)this; duke@435: length = UTF8::unicode_length((char*)this_ptr->bytes(), utf8_length()); duke@435: jchar* result = NEW_RESOURCE_ARRAY(jchar, length); duke@435: if (length > 0) { duke@435: UTF8::convert_to_unicode((char*)this_ptr->bytes(), result, length); duke@435: } duke@435: return result; duke@435: } duke@435: duke@435: const char* symbolOopDesc::as_klass_external_name(char* buf, int size) const { duke@435: if (size > 0) { duke@435: char* str = as_C_string(buf, size); duke@435: int length = (int)strlen(str); duke@435: // Turn all '/'s into '.'s (also for array klasses) duke@435: for (int index = 0; index < length; index++) { duke@435: if (str[index] == '/') { duke@435: str[index] = '.'; duke@435: } duke@435: } duke@435: return str; duke@435: } else { duke@435: return buf; duke@435: } duke@435: } duke@435: duke@435: const char* symbolOopDesc::as_klass_external_name() const { duke@435: char* str = as_C_string(); duke@435: int length = (int)strlen(str); duke@435: // Turn all '/'s into '.'s (also for array klasses) duke@435: for (int index = 0; index < length; index++) { duke@435: if (str[index] == '/') { duke@435: str[index] = '.'; duke@435: } duke@435: } duke@435: return str; duke@435: }