Skip to content
This repository was archived by the owner on Aug 31, 2021. It is now read-only.

Commit 425ae93

Browse files
committed
libfoundation: Avoid unnecessary buffer copy.
1 parent c22616e commit 425ae93

1 file changed

Lines changed: 14 additions & 15 deletions

File tree

libfoundation/src/foundation-string.cpp

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2468,25 +2468,24 @@ bool MCStringConvertToBytes(MCStringRef self, MCStringEncoding p_encoding, bool
24682468
case kMCStringEncodingUTF16BE:
24692469
{
24702470
uindex_t t_char_count;
2471-
unichar_t *t_bytes;
2472-
if (MCStringConvertToUnicode(self, t_bytes, t_char_count))
2471+
unichar_t * t_bytes;
2472+
if (!MCStringConvertToUnicode(self, t_bytes, t_char_count))
2473+
return false;
2474+
2475+
if ((p_encoding == kMCStringEncodingUTF16BE &&
2476+
kMCByteOrderHost != kMCByteOrderBigEndian) ||
2477+
(p_encoding == kMCStringEncodingUTF16LE &&
2478+
kMCByteOrderHost != kMCByteOrderLittleEndian))
24732479
{
2474-
unichar_t *t_buffer;
2475-
MCMemoryAllocate((t_char_count + 1) * sizeof(unichar_t), t_buffer);
2476-
2477-
for (uindex_t i = 0; i < t_char_count; i++)
2480+
for (uindex_t i = 0; i < t_char_count; ++i)
24782481
{
2479-
if (p_encoding == kMCStringEncodingUTF16BE)
2480-
t_buffer[i] = (unichar_t)MCSwapInt16HostToBig((t_bytes)[i]);
2481-
else
2482-
t_buffer[i] = (unichar_t)MCSwapInt16HostToLittle((t_bytes)[i]);
2482+
t_bytes[i] = MCSwapInt(t_bytes[i]);
24832483
}
2484-
MCMemoryDeleteArray (t_bytes);
2485-
r_bytes = (byte_t*&)t_buffer;
2486-
r_byte_count = t_char_count * sizeof(unichar_t);
2487-
return true;
24882484
}
2489-
return false;
2485+
2486+
r_bytes = reinterpret_cast<byte_t*>(t_bytes);
2487+
r_byte_count = t_char_count * sizeof(*t_bytes);
2488+
return true;
24902489
}
24912490
case kMCStringEncodingUTF8:
24922491
return MCStringConvertToUTF8(self, (char*&)r_bytes, r_byte_count);

0 commit comments

Comments
 (0)