|
| 1 | +#include "i18n.h" |
| 2 | + |
| 3 | +#include <kj/common.h> |
| 4 | +#include <kj/debug.h> |
| 5 | +#include <kj/one-of.h> |
| 6 | + |
| 7 | +#include <workerd/jsg/exception.h> |
| 8 | + |
| 9 | +#include <string> |
| 10 | + |
| 11 | +namespace workerd::api::node { |
| 12 | + |
| 13 | +namespace i18n { |
| 14 | + |
| 15 | +namespace { |
| 16 | + |
| 17 | +const char* getEncodingName(Encoding input) { |
| 18 | + switch (input) { |
| 19 | + case Encoding::ASCII: |
| 20 | + return "us-ascii"; |
| 21 | + case Encoding::LATIN1: |
| 22 | + return "iso8859-1"; |
| 23 | + case Encoding::UCS2: |
| 24 | + return "utf16le"; |
| 25 | + case Encoding::UTF8: |
| 26 | + return "utf-8"; |
| 27 | + default: |
| 28 | + KJ_UNREACHABLE; |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +typedef TranscodeResult (*TranscodeImpl)(kj::ArrayPtr<kj::byte> source, Encoding fromEncoding, |
| 33 | + Encoding toEncoding); |
| 34 | + |
| 35 | +TranscodeResult TranscodeDefault(kj::ArrayPtr<kj::byte> source, Encoding fromEncoding, |
| 36 | + Encoding toEncoding) { |
| 37 | + Converter to(toEncoding); |
| 38 | + std::string substitude(to.minSize(), '?'); |
| 39 | + to.setSubstitudeChars(substitude); |
| 40 | + |
| 41 | + Converter from(fromEncoding); |
| 42 | + |
| 43 | + auto limit = source.size() + to.maxSize(); |
| 44 | + KJ_STACK_ARRAY(kj::byte, out, limit, 0, limit); |
| 45 | + char* target = out.asChars().begin(); |
| 46 | + const char* source_ = source.asChars().begin(); |
| 47 | + UErrorCode status{}; |
| 48 | + ucnv_convertEx(to.conv(), from.conv(), &target, target + limit, &source_, source_ + source.size(), |
| 49 | + nullptr, nullptr, nullptr, nullptr, true, true, &status); |
| 50 | + |
| 51 | + if (U_SUCCESS(status)) { |
| 52 | + return out.slice(0, target - out.asChars().begin()).attach(); |
| 53 | + } |
| 54 | + |
| 55 | + return status; |
| 56 | +} |
| 57 | + |
| 58 | +TranscodeResult TranscodeToUCS2(kj::ArrayPtr<kj::byte> source, Encoding fromEncoding, |
| 59 | + Encoding toEncoding) { |
| 60 | + UErrorCode status{}; |
| 61 | + const size_t length_in_chars = source.size() * sizeof(UChar); |
| 62 | + Converter from(fromEncoding); |
| 63 | + KJ_STACK_ARRAY(UChar, out, source.size(), 0, source.size()); |
| 64 | + const auto source_ = source.asChars().begin(); |
| 65 | + ucnv_toUChars(from.conv(), out.begin(), length_in_chars, source_, source.size(), &status); |
| 66 | + |
| 67 | + if (U_SUCCESS(status)) { |
| 68 | + return out.asBytes().attach(); |
| 69 | + } |
| 70 | + return status; |
| 71 | +} |
| 72 | + |
| 73 | +TranscodeResult TranscodeFromUCS2(kj::ArrayPtr<kj::byte> source, Encoding fromEncoding, |
| 74 | + Encoding toEncoding) { |
| 75 | + UErrorCode status{}; |
| 76 | + const size_t length_in_chars = source.size() * sizeof(UChar); |
| 77 | + // Transcode from UCS2. |
| 78 | + Converter to(toEncoding); |
| 79 | + // KJ_STACK_ARRAY(kj::byte, dest, length_in_chars, 0, length_in_chars); |
| 80 | + // dest.copyFrom(source.asConst().slice(0, length_in_chars)); |
| 81 | + |
| 82 | + // const uint32_t len = ucnv_fromUChars(to.conv(), dest.begin(), length_in_chars, |
| 83 | + // *sourcebuf, length_in_chars, status); |
| 84 | + |
| 85 | + // if (U_SUCCESS(status)) { |
| 86 | + // return out.asBytes().attach(); |
| 87 | + // } |
| 88 | + return status; |
| 89 | +} |
| 90 | + |
| 91 | +TranscodeResult TranscodeUcs2FromUtf8(kj::ArrayPtr<kj::byte> source, Encoding fromEncoding, Encoding toEncoding) { |
| 92 | + |
| 93 | +} |
| 94 | + |
| 95 | +TranscodeResult TranscodeUtf8FromUcs2(kj::ArrayPtr<kj::byte> source, Encoding fromEncoding, Encoding toEncoding) { |
| 96 | + |
| 97 | +} |
| 98 | + |
| 99 | +} // namespace |
| 100 | + |
| 101 | +Converter::Converter(Encoding encoding, std::string_view substitude) { |
| 102 | + UErrorCode status = U_ZERO_ERROR; |
| 103 | + auto name = getEncodingName(encoding); |
| 104 | + auto conv = ucnv_open(name, &status); |
| 105 | + KJ_ASSERT(U_SUCCESS(status)); |
| 106 | + conv_.reset(conv); |
| 107 | + setSubstitudeChars(substitude); |
| 108 | +} |
| 109 | + |
| 110 | +Converter::Converter(UConverter* converter, std::string_view substitude) : conv_(converter) { |
| 111 | + setSubstitudeChars(substitude); |
| 112 | +} |
| 113 | + |
| 114 | +kj::Array<kj::byte> transcode(kj::ArrayPtr<kj::byte> source, Encoding fromEncoding, |
| 115 | + Encoding toEncoding) { |
| 116 | + TranscodeImpl transcode_function = &TranscodeDefault; |
| 117 | + switch (fromEncoding) { |
| 118 | + case Encoding::ASCII: |
| 119 | + case Encoding::LATIN1: |
| 120 | + if (toEncoding == Encoding::UCS2) { |
| 121 | + transcode_function = &TranscodeToUCS2; |
| 122 | + } |
| 123 | + break; |
| 124 | + case Encoding::UTF8: |
| 125 | + if (toEncoding == Encoding::UCS2) { |
| 126 | + transcode_function = &TranscodeUcs2FromUtf8; |
| 127 | + } |
| 128 | + break; |
| 129 | + case Encoding::UCS2: |
| 130 | + switch (toEncoding) { |
| 131 | + case Encoding::UCS2: |
| 132 | + transcode_function = &TranscodeDefault; |
| 133 | + break; |
| 134 | + case Encoding::UTF8: |
| 135 | + transcode_function = &TranscodeUtf8FromUcs2; |
| 136 | + break; |
| 137 | + default: |
| 138 | + transcode_function = &TranscodeFromUCS2; |
| 139 | + } |
| 140 | + default: |
| 141 | + KJ_UNREACHABLE; |
| 142 | + } |
| 143 | + |
| 144 | + auto result = transcode_function(source, fromEncoding, toEncoding); |
| 145 | + KJ_SWITCH_ONEOF(result) { |
| 146 | + KJ_CASE_ONEOF(value, UErrorCode) { |
| 147 | + JSG_FAIL_REQUIRE(Error, "Unable to transcode Buffer"); |
| 148 | + } |
| 149 | + KJ_CASE_ONEOF(v, kj::Array<kj::byte>) { |
| 150 | + return kj::mv(v); |
| 151 | + } |
| 152 | + } |
| 153 | + KJ_UNREACHABLE; |
| 154 | +} |
| 155 | + |
| 156 | +} // namespace i18n |
| 157 | + |
| 158 | +} // namespace workerd::api::node |
0 commit comments