1 /*
   2  * Copyright 1997-2005 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  20  * CA 95054 USA or visit www.sun.com if you need additional information or
  21  * have any questions.
  22  *
  23  */
  24 
  25 # include "incls/_precompiled.incl"
  26 # include "incls/_symbolOop.cpp.incl"
  27 
  28 bool symbolOopDesc::equals(const char* str, int len) const {
  29   int l = utf8_length();
  30   if (l != len) return false;
  31   while (l-- > 0) {
  32     if (str[l] != (char) byte_at(l))
  33       return false;
  34   }
  35   assert(l == -1, "we should be at the beginning");
  36   return true;
  37 }
  38 
  39 char* symbolOopDesc::as_C_string(char* buf, int size) const {
  40   if (size > 0) {
  41     int len = MIN2(size - 1, utf8_length());
  42     for (int i = 0; i < len; i++) {
  43       buf[i] = byte_at(i);
  44     }
  45     buf[len] = '\0';
  46   }
  47   return buf;
  48 }
  49 
  50 char* symbolOopDesc::as_C_string() const {
  51   int len = utf8_length();
  52   char* str = NEW_RESOURCE_ARRAY(char, len + 1);
  53   return as_C_string(str, len + 1);
  54 }
  55 
  56 char* symbolOopDesc::as_C_string_flexible_buffer(Thread* t,
  57                                                  char* buf, int size) const {
  58   char* str;
  59   int len = utf8_length();
  60   int buf_len = len + 1;
  61   if (size < buf_len) {
  62     str = NEW_RESOURCE_ARRAY(char, buf_len);
  63   } else {
  64     str = buf;
  65   }
  66   return as_C_string(str, buf_len);
  67 }
  68 
  69 void symbolOopDesc::print_symbol_on(outputStream* st) {
  70   st = st ? st : tty;
  71   int length = UTF8::unicode_length((const char*)bytes(), utf8_length());
  72   const char *ptr = (const char *)bytes();
  73   jchar value;
  74   for (int index = 0; index < length; index++) {
  75     ptr = UTF8::next(ptr, &value);
  76     if (value >= 32 && value < 127 || value = '\'' || value = '\\') {
  77       st->put(value);
  78     } else {
  79       st->print("\\u%04x", value);
  80     }
  81   }
  82 }
  83 
  84 jchar* symbolOopDesc::as_unicode(int& length) const {
  85   symbolOopDesc* this_ptr = (symbolOopDesc*)this;
  86   length = UTF8::unicode_length((char*)this_ptr->bytes(), utf8_length());
  87   jchar* result = NEW_RESOURCE_ARRAY(jchar, length);
  88   if (length > 0) {
  89     UTF8::convert_to_unicode((char*)this_ptr->bytes(), result, length);
  90   }
  91   return result;
  92 }
  93 
  94 const char* symbolOopDesc::as_klass_external_name(char* buf, int size) const {
  95   if (size > 0) {
  96     char* str    = as_C_string(buf, size);
  97     int   length = (int)strlen(str);
  98     // Turn all '/'s into '.'s (also for array klasses)
  99     for (int index = 0; index < length; index++) {
 100       if (str[index] == '/') {
 101         str[index] = '.';
 102       }
 103     }
 104     return str;
 105   } else {
 106     return buf;
 107   }
 108 }
 109 
 110 const char* symbolOopDesc::as_klass_external_name() const {
 111   char* str    = as_C_string();
 112   int   length = (int)strlen(str);
 113   // Turn all '/'s into '.'s (also for array klasses)
 114   for (int index = 0; index < length; index++) {
 115     if (str[index] == '/') {
 116       str[index] = '.';
 117     }
 118   }
 119   return str;
 120 }