File
Blob: src/workerd/api/node/i18n.h
| 1 | // Copyright (c) 2017-2022 Cloudflare, Inc. |
| 2 | // Licensed under the Apache 2.0 license found in the LICENSE file or at: |
| 3 | // https://opensource.org/licenses/Apache-2.0 |
| 4 | #pragma once |
| 5 | |
| 6 | #include <workerd/jsg/jsg.h> |
| 7 | |
| 8 | #include <kj/common.h> |
| 9 | #include <kj/string.h> |
| 10 | |
| 11 | #include <cstdint> |
| 12 | |
| 13 | struct UConverter; |
| 14 | |
| 15 | namespace workerd::api::node { |
| 16 | |
| 17 | using EncodingValue = uint8_t; |
| 18 | |
| 19 | enum Encoding : EncodingValue { |
| 20 | ASCII = 0, |
| 21 | LATIN1 = 1, |
| 22 | UTF8 = 2, |
| 23 | UTF16LE = 3, |
| 24 | BASE64 = 4, |
| 25 | BASE64URL = 5, |
| 26 | HEX = 6, |
| 27 | }; |
| 28 | |
| 29 | namespace i18n { |
| 30 | |
| 31 | // Used by BufferUtil::transcode. |
| 32 | constexpr bool canBeTranscoded(Encoding encoding) noexcept { |
| 33 | switch (encoding) { |
| 34 | case Encoding::ASCII: |
| 35 | case Encoding::LATIN1: |
| 36 | case Encoding::UTF16LE: |
| 37 | case Encoding::UTF8: |
| 38 | return true; |
| 39 | default: |
| 40 | return false; |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | class Converter final { |
| 45 | public: |
| 46 | explicit Converter(Encoding encoding, kj::StringPtr substitude = ""_kj); |
| 47 | KJ_DISALLOW_COPY_AND_MOVE(Converter); |
| 48 | |
| 49 | UConverter* conv() const; |
| 50 | size_t maxCharSize() const; |
| 51 | size_t minCharSize() const; |
| 52 | void reset(); |
| 53 | void setSubstituteChars(kj::StringPtr sub); |
| 54 | |
| 55 | private: |
| 56 | kj::Own<UConverter> conv_; |
| 57 | }; |
| 58 | |
| 59 | jsg::JsUint8Array transcode( |
| 60 | jsg::Lock& js, kj::ArrayPtr<kj::byte> source, Encoding fromEncoding, Encoding toEncoding); |
| 61 | |
| 62 | } // namespace i18n |
| 63 | |
| 64 | } // namespace workerd::api::node |
| 65 | |
| 66 | KJ_DECLARE_NON_POLYMORPHIC(UConverter) |