如何在 Kotlin/Native 中将 const char* 转换为 KString?
How to convert const char* to KString in Kotlin/Native?
在 C++ 文件中,我想将 const char* 转换为 KString,这样我就可以使用 Kotlin/Native.
将 KString 传递给 Kotlin 文件
我相信答案就在函数中
OBJ_GETTER(utf8ToUtf16, const char* rawString, size_t rawStringLength)
我在 KString.cpp. But even though I discovered the used define statements in Memory.h 中找到的,我还没有设法从我自己的 C++ 文件中正确调用函数 utf8ToUtf16
来获取 KString。感谢任何帮助。
我最终把碎片写成了我自己的函数:
KString getKString(const char* rawString) {
size_t rawStringLength = strlen(rawString);
ObjHeader** OBJ_RESULT;
uint32_t charCount = utf8::unchecked::distance(rawString, rawString + rawStringLength);
ArrayHeader* result = AllocArrayInstance(theStringTypeInfo, charCount, OBJ_RESULT)->array();
KChar* rawResult = CharArrayAddressOfElementAt(result, 0);
auto convertResult =
utf8::unchecked::utf8to16(rawString, rawString + rawStringLength, rawResult);
ObjHeader* obj = result->obj();
UpdateReturnRef(OBJ_RESULT, obj);
return (const ArrayHeader*)obj;
}
在我的测试代码(C++)中,我是这样使用的:
...
RuntimeState* state = InitRuntime();
KString inMessage;
{
ObjHolder args;
inMessage = getKString("Hello from C++");
}
...
DeinitRuntime(state);
并包括 Memory.h、Natives.h、Runtime.h、KString.h、utf8.h、stdlib.h 和字符串。您也许可以摆脱其中的一些。
附带说明一下,您可能会意识到函数中如何使用 AllocArrayInstance。如果可以简单地做同样的事情来获取 KString 就好了,比如:
ObjHeader** OBJ_RESULT;
KString kstr = utf8ToUtf16(rawString, rawStringLength, OBJ_RESULT);
这在我的函数中不起作用,因为未找到 utf8ToUtf16。我相信原因是,(在撰写本文时)KString.cpp 中的相应函数位于命名空间 {...} 块内,因此无法从另一个文件使用它。这就是为什么我最终模仿了上面显示的功能。
这取决于您希望如何与 Kotlin 代码交互。如果你用 -produce dynamic
生成动态库,那么字符串会自动转换,例如 https://github.com/JetBrains/kotlin-native/blob/adf8614889e8cf5038a79960aa9651ca7d45e409/samples/python_extension/src/main/c/kotlin_bridge.c#L72。
所以根本不需要额外的魔法。与 Objective-C 字符串和 -produce framework
相同。对于其他情况,无需传递字符串 C -> Kotlin(使用 staticCFunction
产生的回调也进行自动转换)。
在 C++ 文件中,我想将 const char* 转换为 KString,这样我就可以使用 Kotlin/Native.
将 KString 传递给 Kotlin 文件我相信答案就在函数中
OBJ_GETTER(utf8ToUtf16, const char* rawString, size_t rawStringLength)
我在 KString.cpp. But even though I discovered the used define statements in Memory.h 中找到的,我还没有设法从我自己的 C++ 文件中正确调用函数 utf8ToUtf16
来获取 KString。感谢任何帮助。
我最终把碎片写成了我自己的函数:
KString getKString(const char* rawString) {
size_t rawStringLength = strlen(rawString);
ObjHeader** OBJ_RESULT;
uint32_t charCount = utf8::unchecked::distance(rawString, rawString + rawStringLength);
ArrayHeader* result = AllocArrayInstance(theStringTypeInfo, charCount, OBJ_RESULT)->array();
KChar* rawResult = CharArrayAddressOfElementAt(result, 0);
auto convertResult =
utf8::unchecked::utf8to16(rawString, rawString + rawStringLength, rawResult);
ObjHeader* obj = result->obj();
UpdateReturnRef(OBJ_RESULT, obj);
return (const ArrayHeader*)obj;
}
在我的测试代码(C++)中,我是这样使用的:
...
RuntimeState* state = InitRuntime();
KString inMessage;
{
ObjHolder args;
inMessage = getKString("Hello from C++");
}
...
DeinitRuntime(state);
并包括 Memory.h、Natives.h、Runtime.h、KString.h、utf8.h、stdlib.h 和字符串。您也许可以摆脱其中的一些。
附带说明一下,您可能会意识到函数中如何使用 AllocArrayInstance。如果可以简单地做同样的事情来获取 KString 就好了,比如:
ObjHeader** OBJ_RESULT;
KString kstr = utf8ToUtf16(rawString, rawStringLength, OBJ_RESULT);
这在我的函数中不起作用,因为未找到 utf8ToUtf16。我相信原因是,(在撰写本文时)KString.cpp 中的相应函数位于命名空间 {...} 块内,因此无法从另一个文件使用它。这就是为什么我最终模仿了上面显示的功能。
这取决于您希望如何与 Kotlin 代码交互。如果你用 -produce dynamic
生成动态库,那么字符串会自动转换,例如 https://github.com/JetBrains/kotlin-native/blob/adf8614889e8cf5038a79960aa9651ca7d45e409/samples/python_extension/src/main/c/kotlin_bridge.c#L72。
所以根本不需要额外的魔法。与 Objective-C 字符串和 -produce framework
相同。对于其他情况,无需传递字符串 C -> Kotlin(使用 staticCFunction
产生的回调也进行自动转换)。