Skip to content

Commit 80eee20

Browse files
print numbers instead of chars
Co-authored-by: Caleb Aikens <[email protected]>
1 parent 2a5ba78 commit 80eee20

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

src/PyBytesProxyHandler.cc

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,25 @@ static bool array_valueOf(JSContext *cx, unsigned argc, JS::Value *vp) {
3434
bool isSharedMemory;
3535
JS::AutoCheckCannotGC autoNoGC(cx);
3636
uint8_t *data = JS::GetArrayBufferData(rootedArrayBuffer, &isSharedMemory, autoNoGC);
37-
38-
const size_t STRING_LENGTH = byteLength*2 - 1;
37+
size_t numberOfDigits = 0;
38+
for (size_t i = 0; i < byteLength; i++) {
39+
numberOfDigits += data[i] < 10 ? 1 : data[i] < 100 ? 2 : 3;
40+
}
41+
const size_t STRING_LENGTH = byteLength + numberOfDigits;
3942
JS::Latin1Char* buffer = (JS::Latin1Char *)malloc(sizeof(JS::Latin1Char) * STRING_LENGTH);
40-
41-
buffer[0] = data[0];
42-
for (Py_ssize_t index = 1; index < byteLength; index++) {
43-
buffer[index*2 - 1] = ',';
44-
buffer[index*2] = data[index];
43+
44+
size_t charIndex = 0;
45+
sprintf((char*)&buffer[charIndex], "%d", data[0]);
46+
charIndex += data[0] < 10 ? 1 : data[0] < 100 ? 2 : 3;
47+
for (size_t dataIndex = 1; dataIndex < byteLength; dataIndex++) {
48+
buffer[charIndex] = ',';
49+
charIndex++;
50+
sprintf((char*)&buffer[charIndex], "%d", data[dataIndex]);
51+
charIndex += data[dataIndex] < 10 ? 1 : data[dataIndex] < 100 ? 2 : 3;
4552
}
4653

4754
JS::UniqueLatin1Chars str(buffer);
48-
args.rval().setString(JS_NewLatin1String(cx, std::move(str), STRING_LENGTH));
55+
args.rval().setString(JS_NewLatin1String(cx, std::move(str), STRING_LENGTH - 1)); // don't include null byte
4956
return true;
5057
}
5158

0 commit comments

Comments
 (0)