|
| 1 | +/*++ |
| 2 | +Copyright (c) Microsoft Corporation |
| 3 | +Licensed under the MIT license. |
| 4 | +
|
| 5 | +Module Name: |
| 6 | +- UTF8OutPipeReader.hpp |
| 7 | +
|
| 8 | +Abstract: |
| 9 | +- This reads a UTF-8 stream and gives back a buffer that contains complete code points only |
| 10 | +- Partial UTF-8 code points at the end of the buffer read are cached and prepended to the next chunk read |
| 11 | +
|
| 12 | +Author(s): |
| 13 | +- Steffen Illhardt (german-one) 12-July-2019 |
| 14 | +--*/ |
| 15 | + |
| 16 | +#pragma once |
| 17 | + |
| 18 | +#ifndef WIN32_LEAN_AND_MEAN |
| 19 | +#define WIN32_LEAN_AND_MEAN |
| 20 | +#endif |
| 21 | + |
| 22 | +#include <windows.h> |
| 23 | +#include <wil\common.h> |
| 24 | +#include <wil\resource.h> |
| 25 | +#include <string_view> |
| 26 | + |
| 27 | +class UTF8OutPipeReader final |
| 28 | +{ |
| 29 | +public: |
| 30 | + UTF8OutPipeReader(wil::unique_hfile& outPipe); |
| 31 | + [[nodiscard]] HRESULT Read(_Out_ std::string_view& strView); |
| 32 | + |
| 33 | +private: |
| 34 | + wil::unique_hfile& _outPipe; |
| 35 | + |
| 36 | + enum _Utf8BitMasks : BYTE |
| 37 | + { |
| 38 | + IsAsciiByte = 0b0'0000000, // Any byte representing an ASCII character has the MSB set to 0 |
| 39 | + MaskAsciiByte = 0b1'0000000, // Bit mask to be used in a bitwise AND operation to find out whether or not a byte match the IsAsciiByte pattern |
| 40 | + IsContinuationByte = 0b10'000000, // Continuation bytes of any UTF-8 non-ASCII character have the MSB set to 1 and the adjacent bit set to 0 |
| 41 | + MaskContinuationByte = 0b11'000000, // Bit mask to be used in a bitwise AND operation to find out whether or not a byte match the IsContinuationByte pattern |
| 42 | + IsLeadByteTwoByteSequence = 0b110'00000, // A lead byte that indicates a UTF-8 non-ASCII character consisting of two bytes has the two highest bits set to 1 and the adjacent bit set to 0 |
| 43 | + MaskLeadByteTwoByteSequence = 0b111'00000, // Bit mask to be used in a bitwise AND operation to find out whether or not a lead byte match the IsLeadByteTwoByteSequence pattern |
| 44 | + IsLeadByteThreeByteSequence = 0b1110'0000, // A lead byte that indicates a UTF-8 non-ASCII character consisting of three bytes has the three highest bits set to 1 and the adjacent bit set to 0 |
| 45 | + MaskLeadByteThreeByteSequence = 0b1111'0000, // Bit mask to be used in a bitwise AND operation to find out whether or not a lead byte match the IsLeadByteThreeByteSequence pattern |
| 46 | + IsLeadByteFourByteSequence = 0b11110'000, // A lead byte that indicates a UTF-8 non-ASCII character consisting of four bytes has the four highest bits set to 1 and the adjacent bit set to 0 |
| 47 | + MaskLeadByteFourByteSequence = 0b11111'000 // Bit mask to be used in a bitwise AND operation to find out whether or not a lead byte match the IsLeadByteFourByteSequence pattern |
| 48 | + }; |
| 49 | + |
| 50 | + // array of bitmasks |
| 51 | + constexpr const static BYTE _cmpMasks[]{ |
| 52 | + 0, // unused |
| 53 | + _Utf8BitMasks::MaskContinuationByte, |
| 54 | + _Utf8BitMasks::MaskLeadByteTwoByteSequence, |
| 55 | + _Utf8BitMasks::MaskLeadByteThreeByteSequence, |
| 56 | + }; |
| 57 | + |
| 58 | + // array of values for the comparisons |
| 59 | + constexpr const static BYTE _cmpOperands[]{ |
| 60 | + 0, // unused |
| 61 | + _Utf8BitMasks::IsAsciiByte, // intentionally conflicts with MaskContinuationByte |
| 62 | + _Utf8BitMasks::IsLeadByteTwoByteSequence, |
| 63 | + _Utf8BitMasks::IsLeadByteThreeByteSequence, |
| 64 | + }; |
| 65 | + |
| 66 | + BYTE _buffer[4096]{ 0 }; // buffer for the chunk read |
| 67 | + BYTE _utf8Partials[4]{ 0 }; // buffer for code units of a partial UTF-8 code point that have to be cached |
| 68 | + DWORD _dwPartialsLen{}; // number of cached UTF-8 code units |
| 69 | +}; |
0 commit comments