- Fixed 16-bit compressed unicode string creation, which was apparently

seg-faulting due to unaligned memory accesses.
- Switched from new to new(nothrow).


git-svn-id: file:///srv/svn/repos/haiku/trunk/current@5880 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Tyler Dauwalder 2004-01-03 01:59:33 +00:00
parent 6a70da44dc
commit 2b215e7a92
1 changed files with 18 additions and 12 deletions

View File

@ -131,7 +131,7 @@ String::~String()
void
String::SetTo(const char *utf8)
{
DEBUG_INIT_ETC("String", ("utf8: %p, strlen(utf8): %ld", utf8,
DEBUG_INIT_ETC("String", ("utf8: `%s', strlen(utf8): %ld", utf8,
utf8 ? strlen(utf8) : 0));
_Clear();
if (!utf8) {
@ -140,7 +140,7 @@ String::SetTo(const char *utf8)
}
uint32 length = strlen(utf8);
// First copy the utf8 string
fUtf8String = new char[length+1];
fUtf8String = new(nothrow) char[length+1];
if (!fUtf8String){
PRINT(("new fUtf8String[%ld] allocation failed\n", length+1));
return;
@ -149,7 +149,7 @@ String::SetTo(const char *utf8)
// analysis to figure out if we have any invalid characters,
// and whether we can get away with compressed 8-bit unicode,
// or have to use burly 16-bit unicode.
uint32 *raw = new uint32[length];
uint32 *raw = new(nothrow) uint32[length];
if (!raw) {
PRINT(("new uint32 raw[%ld] temporary string allocation failed\n", length));
_Clear();
@ -182,7 +182,7 @@ String::SetTo(const char *utf8)
// Build our cs0 string
if (canUse8bit) {
fCs0Length = rawLength+1;
fCs0String = new char[fCs0Length];
fCs0String = new(nothrow) char[fCs0Length];
if (fCs0String) {
fCs0String[0] = '\x08'; // 8-bit compressed unicode
for (uint32 i = 0; i < rawLength; i++)
@ -194,12 +194,18 @@ String::SetTo(const char *utf8)
}
} else {
fCs0Length = rawLength*2+1;
fCs0String = new char[fCs0Length];
fCs0String = new(nothrow) char[fCs0Length];
if (fCs0String) {
fCs0String[0] = '\x10'; // 16-bit unicode
uint16 *string = reinterpret_cast<uint16*>(&fCs0String[1]);
for (uint32 i = 0; i < rawLength; i++)
string[i+1] = uint16(raw[i]);
uint32 pos = 0;
fCs0String[pos++] = '\x10'; // 16-bit unicode
for (uint32 i = 0; i < rawLength; i++) {
// 16-bit unicode chars must be written big endian
uint16 value = uint16(raw[i]);
uint8 high = uint8(value >> 8 & 0xff);
uint8 low = uint8(value & 0xff);
fCs0String[pos++] = high;
fCs0String[pos++] = low;
}
} else {
PRINT(("new fCs0String[%ld] allocation failed\n", fCs0Length));
_Clear();
@ -225,7 +231,7 @@ String::SetTo(const char *cs0, uint32 length)
}
// First copy the Cs0 string and length
fCs0String = new char[length];
fCs0String = new(nothrow) char[length];
if (fCs0String) {
memcpy(fCs0String, cs0, length);
} else {
@ -248,7 +254,7 @@ String::SetTo(const char *cs0, uint32 length)
const uint8 *inputString = reinterpret_cast<const uint8*>(&(cs0[1]));
int32 maxLength = length-1; // Max length of input string in uint8 characters
int32 allocationLength = maxLength*2+1; // Need at most 2 utf8 chars per uint8 char
fUtf8String = new char[allocationLength];
fUtf8String = new(nothrow) char[allocationLength];
if (fUtf8String) {
char *outputString = fUtf8String;
@ -269,7 +275,7 @@ String::SetTo(const char *cs0, uint32 length)
const uint16 *inputString = reinterpret_cast<const uint16*>(&(cs0[1]));
int32 maxLength = (length-1) / 2; // Max length of input string in uint16 characters
int32 allocationLength = maxLength*3+1; // Need at most 3 utf8 chars per uint16 char
fUtf8String = new char[allocationLength];
fUtf8String = new(nothrow) char[allocationLength];
if (fUtf8String) {
char *outputString = fUtf8String;