// Create a modified UTF-8 encoded std::string from a java/lang/String object.
std::string String::ToModifiedUtf8() { if (IsCompressed()) { return std::string(reinterpret_cast<constchar*>(GetValueCompressed()), GetLength());
} else {
size_t byte_count = GetModifiedUtf8Length();
std::string result(byte_count, static_cast<char>(0));
ConvertUtf16ToModifiedUtf8(&result[0], byte_count, GetValue(), GetLength()); return result;
}
}
int32_t String::CompareTo(ObjPtr<String> rhs) { // Quick test for comparison of a string with itself.
ObjPtr<String> lhs = this; if (lhs == rhs) { return0;
}
int32_t lhs_count = lhs->GetLength();
int32_t rhs_count = rhs->GetLength();
int32_t count_diff = lhs_count - rhs_count;
int32_t min_count = (count_diff < 0) ? lhs_count : rhs_count; if (lhs->IsCompressed() && rhs->IsCompressed()) { const uint8_t* lhs_chars = lhs->GetValueCompressed(); const uint8_t* rhs_chars = rhs->GetValueCompressed(); for (int32_t i = 0; i < min_count; ++i) {
int32_t char_diff = static_cast<int32_t>(lhs_chars[i]) - static_cast<int32_t>(rhs_chars[i]); if (char_diff != 0) { return char_diff;
}
}
} elseif (lhs->IsCompressed() || rhs->IsCompressed()) { const uint8_t* compressed_chars =
lhs->IsCompressed() ? lhs->GetValueCompressed() : rhs->GetValueCompressed(); const uint16_t* uncompressed_chars = lhs->IsCompressed() ? rhs->GetValue() : lhs->GetValue(); for (int32_t i = 0; i < min_count; ++i) {
int32_t char_diff = static_cast<int32_t>(compressed_chars[i]) - static_cast<int32_t>(uncompressed_chars[i]); if (char_diff != 0) { return lhs->IsCompressed() ? char_diff : -char_diff;
}
}
} else { const uint16_t* lhs_chars = lhs->GetValue(); const uint16_t* rhs_chars = rhs->GetValue(); // FIXME: The MemCmp16() name is misleading. It returns the char difference on mismatch // where memcmp() only guarantees that the returned value has the same sign.
int32_t char_diff = MemCmp16(lhs_chars, rhs_chars, min_count); if (char_diff != 0) { return char_diff;
}
} return count_diff;
}
ObjPtr<CharArray> String::ToCharArray(Handle<String> h_this, Thread* self) {
ObjPtr<CharArray> result = CharArray::Alloc(self, h_this->GetLength()); if (result != nullptr) { if (h_this->IsCompressed()) {
int32_t length = h_this->GetLength(); const uint8_t* src = h_this->GetValueCompressed();
uint16_t* dest = result->GetData(); for (int i = 0; i < length; ++i) {
dest[i] = src[i];
}
} else {
memcpy(result->GetData(), h_this->GetValue(), h_this->GetLength() * sizeof(uint16_t));
}
} else {
self->AssertPendingOOMException();
} return result;
}
void String::GetChars(int32_t start, int32_t end, Handle<CharArray> array, int32_t index) {
uint16_t* data = array->GetData() + index;
DCHECK_LE(start, end);
int32_t length = end - start; if (IsCompressed()) { const uint8_t* value = GetValueCompressed() + start; for (int i = 0; i < length; ++i) {
data[i] = value[i];
}
} else {
uint16_t* value = GetValue() + start;
memcpy(data, value, length * sizeof(uint16_t));
}
}
void String::FillBytesLatin1(Handle<ByteArray> array, int32_t index) {
int8_t* data = array->GetData() + index;
int32_t length = GetLength(); if (IsCompressed()) { const uint8_t* value = GetValueCompressed();
memcpy(data, value, length * sizeof(uint8_t));
} else { // Drop the high byte of the characters. // The caller should check that all dropped high bytes are zeros. const uint16_t* value = GetValue(); for (int32_t i = 0; i < length; ++i) {
data[i] = static_cast<int8_t>(dchecked_integral_cast<uint8_t>(value[i]));
}
}
}
void String::FillBytesUTF16(Handle<ByteArray> array, int32_t index) {
int8_t* data = array->GetData() + index;
int32_t length = GetLength(); if (IsCompressed()) { const uint8_t* value = GetValueCompressed();
uint32_t d_index = 0; for (int i = 0; i < length; ++i) {
data[d_index++] = static_cast<int8_t>(value[i]);
data[d_index++] = 0;
}
} else { const uint16_t* value = GetValue();
memcpy(data, value, length * sizeof(uint16_t));
}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.