1 #ifdef USE_PRAGMA_IDENT_SRC
   2 #pragma ident "@(#)symbolOop.cpp        1.28 07/05/05 17:06:08 JVM"
   3 #endif
   4 /*
   5  * Copyright 1997-2005 Sun Microsystems, Inc.  All Rights Reserved.
   6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   7  *
   8  * This code is free software; you can redistribute it and/or modify it
   9  * under the terms of the GNU General Public License version 2 only, as
  10  * published by the Free Software Foundation.
  11  *
  12  * This code is distributed in the hope that it will be useful, but WITHOUT
  13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  15  * version 2 for more details (a copy is included in the LICENSE file that
  16  * accompanied this code).
  17  *
  18  * You should have received a copy of the GNU General Public License version
  19  * 2 along with this work; if not, write to the Free Software Foundation,
  20  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  21  *
  22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  23  * CA 95054 USA or visit www.sun.com if you need additional information or
  24  * have any questions.
  25  *  
  26  */
  27 
  28 # include "incls/_precompiled.incl"
  29 # include "incls/_symbolOop.cpp.incl"
  30 
  31 bool symbolOopDesc::equals(const char* str, int len) const {
  32   int l = utf8_length();
  33   if (l != len) return false;
  34   while (l-- > 0) {
  35     if (str[l] != (char) byte_at(l)) 
  36       return false;
  37   }
  38   assert(l == -1, "we should be at the beginning");
  39   return true;
  40 }
  41 
  42 char* symbolOopDesc::as_C_string(char* buf, int size) const {
  43   if (size > 0) {
  44     int len = MIN2(size - 1, utf8_length());
  45     for (int i = 0; i < len; i++) {
  46       buf[i] = byte_at(i);
  47     }
  48     buf[len] = '\0';
  49   }
  50   return buf;
  51 }
  52 
  53 char* symbolOopDesc::as_C_string() const {
  54   int len = utf8_length();
  55   char* str = NEW_RESOURCE_ARRAY(char, len + 1);
  56   return as_C_string(str, len + 1);
  57 }
  58 
  59 char* symbolOopDesc::as_C_string_flexible_buffer(Thread* t,
  60                                                  char* buf, int size) const {
  61   char* str;
  62   int len = utf8_length();
  63   int buf_len = len + 1;
  64   if (size < buf_len) {
  65     str = NEW_RESOURCE_ARRAY(char, buf_len);
  66   } else {
  67     str = buf;
  68   }
  69   return as_C_string(str, buf_len);
  70 }
  71 
  72 void symbolOopDesc::print_symbol_on(outputStream* st) {
  73   st = st ? st : tty;
  74   for (int index = 0; index < utf8_length(); index++)
  75     st->put((char)byte_at(index));
  76 }
  77 
  78 jchar* symbolOopDesc::as_unicode(int& length) const {
  79   symbolOopDesc* this_ptr = (symbolOopDesc*)this;
  80   length = UTF8::unicode_length((char*)this_ptr->bytes(), utf8_length());
  81   jchar* result = NEW_RESOURCE_ARRAY(jchar, length);
  82   if (length > 0) {
  83     UTF8::convert_to_unicode((char*)this_ptr->bytes(), result, length);
  84   }
  85   return result;
  86 }
  87 
  88 const char* symbolOopDesc::as_klass_external_name(char* buf, int size) const {
  89   if (size > 0) {
  90     char* str    = as_C_string(buf, size);
  91     int   length = (int)strlen(str);
  92     // Turn all '/'s into '.'s (also for array klasses)
  93     for (int index = 0; index < length; index++) {
  94       if (str[index] == '/') {
  95         str[index] = '.';
  96       }
  97     }
  98     return str;
  99   } else {
 100     return buf;
 101   }
 102 }
 103 
 104 const char* symbolOopDesc::as_klass_external_name() const {
 105   char* str    = as_C_string();
 106   int   length = (int)strlen(str);
 107   // Turn all '/'s into '.'s (also for array klasses)
 108   for (int index = 0; index < length; index++) {
 109     if (str[index] == '/') {
 110       str[index] = '.';
 111     }
 112   }
 113   return str;
 114 }