1 /* 2 * Copyright (c) 2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef UTIL_JS_TEXTENCODER_H_ 17 #define UTIL_JS_TEXTENCODER_H_ 18 19 #include <string> 20 21 #include "napi/native_api.h" 22 #include "napi/native_node_api.h" 23 namespace OHOS::Util { 24 class TextEncoder { 25 public: 26 /** 27 * Constructor of textdecoder. 28 * 29 * @param encoding Encoding format 30 */ TextEncoder(const std::string & encoding)31 explicit TextEncoder(const std::string &encoding) : encoding_(encoding) {} 32 33 /** 34 * Destructor of textencoder. 35 */ ~TextEncoder()36 virtual ~TextEncoder() {} 37 38 /** 39 * Get encoding format. 40 * 41 * @param env NAPI environment parameters. 42 */ 43 napi_value GetEncoding(napi_env env) const; 44 45 /** 46 * Output the corresponding text after encoding the input parameters. 47 * 48 * @param env NAPI environment parameters. 49 * @param src A string that needs to be encoded. 50 */ 51 napi_value Encode(napi_env env, napi_value src) const; 52 53 /** 54 * Place the generated UTF-8 encoded text. 55 * 56 * @param env NAPI environment parameters. 57 * @param src A string that needs to be encoded. 58 * @param dest Uint8array object instance, which is used to put the generated UTF-8 encoded text into it. 59 */ 60 napi_value EncodeInto(napi_env env, napi_value src, napi_value dest) const; 61 62 private: 63 std::string encoding_; 64 }; 65 } 66 #endif // UTIL_JS_TEXTENCODER_H_ 67