• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     ~QuickJSNativeString() override;
28 
29     void* GetInterface(int interfaceId) override;
30 
31     void GetCString(char* buffer, size_t size, size_t* length) override;
32     void GetCString16(char16_t* buffer, size_t size, size_t* length) override;
33     size_t GetLength() override;
34     size_t EncodeWriteUtf8(char* buffer, size_t bufferSize, int32_t* nchars) override;
EncodeWriteChinese(std::string & buffer,const char * encoding)35     void EncodeWriteChinese(std::string& buffer, const char* encoding) override { return; }
36 
37 private:
38     char16_t* Utf8ToUtf16(const char* utf8Str, size_t u8len, char16_t* u16str, size_t u16len);
39     static inline size_t Utf8CodePointLen(uint8_t ch);
40     uint32_t Utf8ToUtf32CodePoint(const char* src, size_t length);
41     static inline void Utf8ShiftAndMask(uint32_t* codePoint, const uint8_t byte);
42     int Utf8ToUtf16Length(const char* str8, size_t str8Len);
43 };
44 
45 #endif /* FOUNDATION_ACE_NAPI_NATIVE_ENGINE_IMPL_QUICKJS_NATIVE_VALUE_QUICKJS_NATIVE_STRING_H */
46