RESOLVED FIXED 193274
Implement TextEncoder's encodeInto()
https://bugs.webkit.org/show_bug.cgi?id=193274
Summary Implement TextEncoder's encodeInto()
Anne van Kesteren
Reported 2019-01-08 23:56:55 PST
This API allows for encoding a string into UTF-8 bytes using a preexisting target buffer. See https://github.com/whatwg/encoding/pull/166 for the standard change and https://github.com/web-platform-tests/wpt/pull/14505 for tests.
Attachments
Patch (81.22 KB, patch)
2020-09-02 20:05 PDT, Alex Christensen
darin: review+
Alex Christensen
Comment 1 2020-09-02 20:05:39 PDT
Darin Adler
Comment 2 2020-09-02 20:51:50 PDT
Comment on attachment 407853 [details] Patch View in context: https://bugs.webkit.org/attachment.cgi?id=407853&action=review > Source/WebCore/dom/TextEncoder.cpp:64 > + uint8_t buffer[U8_MAX_LENGTH]; > + unsigned offset = 0; > + U8_APPEND(buffer, offset, sizeof(buffer), token, sawError); > + if (sawError) > + break; > + if (written + offset > capacity) > + break; > + memcpy(destinationBytes + written, buffer, offset); > + written += offset; Since U8_APPEND has bounds checking built in we don’t need to keep using a buffer every time. We could do something more like this: auto offset = written; U8_APPEND(destinationBytes, offset, capacity, token, sawError); if (sawError) break; written = offset; > Source/WebCore/dom/TextEncoder.cpp:68 > + if (U_IS_BMP(token)) > + read++; > + else > + read += 2; This could be: read += U16_LENGTH(token);
Darin Adler
Comment 3 2020-09-02 20:52:51 PDT
Comment on attachment 407853 [details] Patch View in context: https://bugs.webkit.org/attachment.cgi?id=407853&action=review >> Source/WebCore/dom/TextEncoder.cpp:64 >> + written += offset; > > Since U8_APPEND has bounds checking built in we don’t need to keep using a buffer every time. We could do something more like this: > > auto offset = written; > U8_APPEND(destinationBytes, offset, capacity, token, sawError); > if (sawError) > break; > written = offset; Reviewed the U8_APPEND implementation and we can do better than that: U8_APPEND(destinationBytes, written, capacity, token, sawError); if (sawError) break;
youenn fablet
Comment 4 2020-09-03 01:33:54 PDT
Comment on attachment 407853 [details] Patch View in context: https://bugs.webkit.org/attachment.cgi?id=407853&action=review > Source/WebCore/dom/TextEncoder.idl:31 > +}; Seems like there is an issue with the binding generator here in identifying that TextEncoder should be exposed to worker. Just move the dictionary declaration in another idl file or at the end of this file to circumvent the issue. We should probably file a bug for this issue.
Alex Christensen
Comment 5 2020-09-03 10:10:42 PDT
Adding Exposed=(Window,Worker) to the dictionary works, too.
youenn fablet
Comment 6 2020-09-03 10:36:47 PDT
(In reply to Alex Christensen from comment #5) > Adding Exposed=(Window,Worker) to the dictionary works, too. Sure but I do not think dictionaries have Exposed in WebIDL.
Alex Christensen
Comment 7 2020-09-03 10:51:07 PDT
(In reply to Darin Adler from comment #3) > U8_APPEND(destinationBytes, written, capacity, token, sawError); > if (sawError) > break; This works, but I first have to check if written == capacity and break. It is a precondition of U8_APPEND that written is strictly less than capacity.
Alex Christensen
Comment 8 2020-09-03 10:53:13 PDT
Radar WebKit Bug Importer
Comment 9 2020-09-03 10:54:24 PDT
Alex Christensen
Comment 10 2020-09-04 11:06:37 PDT
Note You need to log in before you can comment on or make changes to this bug.