Skip to content
This repository was archived by the owner on Oct 15, 2020. It is now read-only.

Commit 5c6f12a

Browse files
MSLaguanakfarnung
authored andcommitted
chakrashim: using new jsrt interface
Several scenarios involve moving buffers of 8-bit chars around. Previously the only way to get 8-bit chars out of jsrt was to get 16-bit chars and then marshal them manually. To assist with this, we've added a new JsCopyStringOneByte function to directly extract 8-bit strings without needing intermediate storage. PR-URL: #344 Reviewed-By: Kunal Pathak <kunal.pathak@microsoft.com> Reviewed-By: Hitesh Kanwathirtha <hiteshk@microsoft.com> Reviewed-By: Kyle Farnung <kfarnung@microsoft.com>
1 parent 72b7dc2 commit 5c6f12a

File tree

1 file changed

+5
-14
lines changed

1 file changed

+5
-14
lines changed

deps/chakrashim/src/v8string.cc

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -101,23 +101,14 @@ int String::Write(uint16_t *buffer, int start, int length, int options) const {
101101

102102
int String::WriteOneByte(
103103
uint8_t* buffer, int start, int length, int options) const {
104-
// The JSRT API only supports utf8 and utf16 encoded strings. In order to get
105-
// 8 bit bytes from the string (i.e. Latin1) we can get the utf16 string and
106-
// cast each character down to a uint8_t. This will only work for characters
107-
// between U+0000 and U+00FF in the source string.
108-
uint16_t* tmpBuffer = new uint16_t[length];
109104
size_t count = 0;
110-
if (JsCopyStringUtf16((JsValueRef)this, start, length,
111-
tmpBuffer, &count) == JsNoError) {
112-
for (size_t i = 0; i < count; i++) {
113-
buffer[i] = (uint8_t)tmpBuffer[i];
114-
}
115-
116-
if (!(options & String::NO_NULL_TERMINATION)) {
117-
buffer[count] = 0;
105+
if (JsCopyStringOneByte((JsValueRef)this, start, length,
106+
(char *)buffer, &count) == JsNoError) {
107+
if (!(options & String::NO_NULL_TERMINATION) &&
108+
(length == -1 || count < length)) {
109+
buffer[count] = '\0';
118110
}
119111
}
120-
delete[] tmpBuffer;
121112
return count;
122113
}
123114

0 commit comments

Comments
 (0)