1 /* 2 * Copyright (c) 2021 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 FOUNDATION_ACE_NAPI_NATIVE_ENGINE_IMPL_QUICKJS_NATIVE_VALUE_QUICKJS_NATIVE_STRING_H 17 #define FOUNDATION_ACE_NAPI_NATIVE_ENGINE_IMPL_QUICKJS_NATIVE_VALUE_QUICKJS_NATIVE_STRING_H 18 19 #include "quickjs_native_value.h" 20 21 class QuickJSNativeString : public QuickJSNativeValue, public NativeString { 22 public: 23 QuickJSNativeString(QuickJSNativeEngine* engine, JSValue value); 24 QuickJSNativeString(QuickJSNativeEngine* engine, JSAtom value); 25 QuickJSNativeString(QuickJSNativeEngine* engine, const char* value, size_t length); 26 QuickJSNativeString(QuickJSNativeEngine* engine, const char16_t* value, size_t length); 27 virtual ~QuickJSNativeString(); 28 29 virtual void* GetInterface(int interfaceId) override; 30 31 virtual void GetCString(char* buffer, size_t size, size_t* length) override; 32 virtual void GetCString16(char16_t* buffer, size_t size, size_t* length) override; 33 virtual size_t GetLength() override; 34 virtual size_t EncodeWriteUtf8(char* buffer, size_t bufferSize, int32_t* nchars) override; 35 36 private: 37 char16_t* Utf8ToUtf16(const char* utf8Str, size_t u8len, char16_t* u16str, size_t u16len); 38 static inline size_t Utf8CodePointLen(uint8_t ch); 39 uint32_t Utf8ToUtf32CodePoint(const char* src, size_t length); 40 static inline void Utf8ShiftAndMask(uint32_t* codePoint, const uint8_t byte); 41 int Utf8ToUtf16Length(const char* str8, size_t str8Len); 42 }; 43 44 #endif /* FOUNDATION_ACE_NAPI_NATIVE_ENGINE_IMPL_QUICKJS_NATIVE_VALUE_QUICKJS_NATIVE_STRING_H */ 45