62 63 void* Symbol::operator new(size_t sz, int len, Arena* arena, TRAPS) throw() { 64 int alloc_size = size(len)*wordSize; 65 address res = (address)arena->Amalloc_4(alloc_size); 66 return res; 67 } 68 69 void Symbol::operator delete(void *p) { 70 assert(((Symbol*)p)->refcount() == 0, "should not call this"); 71 FreeHeap(p); 72 } 73 74 // ------------------------------------------------------------------ 75 // Symbol::starts_with 76 // 77 // Tests if the symbol starts with the specified prefix of the given 78 // length. 79 bool Symbol::starts_with(const char* prefix, int len) const { 80 if (len > utf8_length()) return false; 81 while (len-- > 0) { 82 if (prefix[len] != (char) byte_at(len)) 83 return false; 84 } 85 assert(len == -1, "we should be at the beginning"); 86 return true; 87 } 88 89 90 // ------------------------------------------------------------------ 91 // Symbol::index_of 92 // 93 // Finds if the given string is a substring of this symbol's utf8 bytes. 94 // Return -1 on failure. Otherwise return the first index where str occurs. 95 int Symbol::index_of_at(int i, const char* str, int len) const { 96 assert(i >= 0 && i <= utf8_length(), "oob"); 97 if (len <= 0) return 0; 98 char first_char = str[0]; 99 address bytes = (address) ((Symbol*)this)->base(); 100 address limit = bytes + utf8_length() - len; // inclusive limit 101 address scan = bytes + i; 102 if (scan > limit) 103 return -1; 104 for (; scan <= limit; scan++) { 105 scan = (address) memchr(scan, first_char, (limit + 1 - scan)); 106 if (scan == NULL) 107 return -1; // not found 108 assert(scan >= bytes+i && scan <= limit, "scan oob"); 109 if (memcmp(scan, str, len) == 0) 110 return (int)(scan - bytes); 111 } 112 return -1; 113 } 114 115 116 char* Symbol::as_C_string(char* buf, int size) const { 117 if (size > 0) { 118 int len = MIN2(size - 1, utf8_length()); 119 for (int i = 0; i < len; i++) { 120 buf[i] = byte_at(i); 121 } 122 buf[len] = '\0'; 123 } 124 return buf; 125 } 126 127 char* Symbol::as_C_string() const { 128 int len = utf8_length(); 129 char* str = NEW_RESOURCE_ARRAY(char, len + 1); 130 return as_C_string(str, len + 1); 131 } 132 133 char* Symbol::as_C_string_flexible_buffer(Thread* t, 134 char* buf, int size) const { 135 char* str; 136 int len = utf8_length(); 137 int buf_len = len + 1; 138 if (size < buf_len) { 139 str = NEW_RESOURCE_ARRAY(char, buf_len); 140 } else { | 62 63 void* Symbol::operator new(size_t sz, int len, Arena* arena, TRAPS) throw() { 64 int alloc_size = size(len)*wordSize; 65 address res = (address)arena->Amalloc_4(alloc_size); 66 return res; 67 } 68 69 void Symbol::operator delete(void *p) { 70 assert(((Symbol*)p)->refcount() == 0, "should not call this"); 71 FreeHeap(p); 72 } 73 74 // ------------------------------------------------------------------ 75 // Symbol::starts_with 76 // 77 // Tests if the symbol starts with the specified prefix of the given 78 // length. 79 bool Symbol::starts_with(const char* prefix, int len) const { 80 if (len > utf8_length()) return false; 81 while (len-- > 0) { 82 if (prefix[len] != char_at(len)) 83 return false; 84 } 85 assert(len == -1, "we should be at the beginning"); 86 return true; 87 } 88 89 90 // ------------------------------------------------------------------ 91 // Symbol::index_of 92 // 93 // Finds if the given string is a substring of this symbol's utf8 bytes. 94 // Return -1 on failure. Otherwise return the first index where str occurs. 95 int Symbol::index_of_at(int i, const char* str, int len) const { 96 assert(i >= 0 && i <= utf8_length(), "oob"); 97 if (len <= 0) return 0; 98 char first_char = str[0]; 99 address bytes = (address) ((Symbol*)this)->base(); 100 address limit = bytes + utf8_length() - len; // inclusive limit 101 address scan = bytes + i; 102 if (scan > limit) 103 return -1; 104 for (; scan <= limit; scan++) { 105 scan = (address) memchr(scan, first_char, (limit + 1 - scan)); 106 if (scan == NULL) 107 return -1; // not found 108 assert(scan >= bytes+i && scan <= limit, "scan oob"); 109 if (memcmp(scan, str, len) == 0) 110 return (int)(scan - bytes); 111 } 112 return -1; 113 } 114 115 116 char* Symbol::as_C_string(char* buf, int size) const { 117 if (size > 0) { 118 int len = MIN2(size - 1, utf8_length()); 119 for (int i = 0; i < len; i++) { 120 buf[i] = char_at(i); 121 } 122 buf[len] = '\0'; 123 } 124 return buf; 125 } 126 127 char* Symbol::as_C_string() const { 128 int len = utf8_length(); 129 char* str = NEW_RESOURCE_ARRAY(char, len + 1); 130 return as_C_string(str, len + 1); 131 } 132 133 char* Symbol::as_C_string_flexible_buffer(Thread* t, 134 char* buf, int size) const { 135 char* str; 136 int len = utf8_length(); 137 int buf_len = len + 1; 138 if (size < buf_len) { 139 str = NEW_RESOURCE_ARRAY(char, buf_len); 140 } else { |