81 } 82 83 84 // ------------------------------------------------------------------ 85 // Symbol::starts_with 86 // 87 // Tests if the symbol starts with the specified prefix of the given 88 // length. 89 bool Symbol::starts_with(const char* prefix, int len) const { 90 if (len > utf8_length()) return false; 91 while (len-- > 0) { 92 if (prefix[len] != (char) byte_at(len)) 93 return false; 94 } 95 assert(len == -1, "we should be at the beginning"); 96 return true; 97 } 98 99 100 // ------------------------------------------------------------------ 101 // Symbol::index_of 102 // 103 // Finds if the given string is a substring of this symbol's utf8 bytes. 104 // Return -1 on failure. Otherwise return the first index where str occurs. 105 int Symbol::index_of_at(int i, const char* str, int len) const { 106 assert(i >= 0 && i <= utf8_length(), "oob"); 107 if (len <= 0) return 0; 108 char first_char = str[0]; 109 address bytes = (address) ((Symbol*)this)->base(); 110 address limit = bytes + utf8_length() - len; // inclusive limit 111 address scan = bytes + i; 112 if (scan > limit) 113 return -1; 114 for (; scan <= limit; scan++) { 115 scan = (address) memchr(scan, first_char, (limit + 1 - scan)); 116 if (scan == NULL) 117 return -1; // not found 118 assert(scan >= bytes+i && scan <= limit, "scan oob"); 119 if (memcmp(scan, str, len) == 0) 120 return (int)(scan - bytes); | 81 } 82 83 84 // ------------------------------------------------------------------ 85 // Symbol::starts_with 86 // 87 // Tests if the symbol starts with the specified prefix of the given 88 // length. 89 bool Symbol::starts_with(const char* prefix, int len) const { 90 if (len > utf8_length()) return false; 91 while (len-- > 0) { 92 if (prefix[len] != (char) byte_at(len)) 93 return false; 94 } 95 assert(len == -1, "we should be at the beginning"); 96 return true; 97 } 98 99 100 // ------------------------------------------------------------------ 101 // Symbol::ends_with 102 // 103 // Tests if the symbol ends with the specified suffix of the given 104 // length. 105 bool Symbol::ends_with(const char* suffix, int len) const { 106 const int utf8_len = utf8_length(); 107 if (len > utf8_len) return false; 108 int pos = utf8_len; 109 while (len-- > 0) { 110 pos--; 111 if (suffix[len] != (char) byte_at(pos)) 112 return false; 113 } 114 assert(len == -1, "we should be at the beginning"); 115 return true; 116 } 117 118 119 // ------------------------------------------------------------------ 120 // Symbol::index_of 121 // 122 // Finds if the given string is a substring of this symbol's utf8 bytes. 123 // Return -1 on failure. Otherwise return the first index where str occurs. 124 int Symbol::index_of_at(int i, const char* str, int len) const { 125 assert(i >= 0 && i <= utf8_length(), "oob"); 126 if (len <= 0) return 0; 127 char first_char = str[0]; 128 address bytes = (address) ((Symbol*)this)->base(); 129 address limit = bytes + utf8_length() - len; // inclusive limit 130 address scan = bytes + i; 131 if (scan > limit) 132 return -1; 133 for (; scan <= limit; scan++) { 134 scan = (address) memchr(scan, first_char, (limit + 1 - scan)); 135 if (scan == NULL) 136 return -1; // not found 137 assert(scan >= bytes+i && scan <= limit, "scan oob"); 138 if (memcmp(scan, str, len) == 0) 139 return (int)(scan - bytes); |