From a83e470c36328c1eb6a170366e128c6fe79f8837 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=91=A8=E9=91=AB?= Date: Thu, 24 Apr 2025 21:10:30 +0800 Subject: [PATCH 1/5] =?UTF-8?q?readtext=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 周鑫 --- .../js/src/common/ani_helper/type_converter.cpp | 13 +++++++++++++ .../kits/js/src/common/ani_helper/type_converter.h | 1 + .../js/src/mod_fs/properties/ani/read_text_ani.cpp | 3 ++- 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/interfaces/kits/js/src/common/ani_helper/type_converter.cpp b/interfaces/kits/js/src/common/ani_helper/type_converter.cpp index fe336b31e..8ccc2d8f8 100644 --- a/interfaces/kits/js/src/common/ani_helper/type_converter.cpp +++ b/interfaces/kits/js/src/common/ani_helper/type_converter.cpp @@ -108,6 +108,19 @@ std::tuple TypeConverter::ToAniString(ani_env *env, std::strin return { true, std::move(result) }; } +static std::tuple ToAniString(ani_env *env, std::string str, size_t size) +{ + if (env == nullptr) { + return { false, {} }; + } + + ani_string result; + if (ANI_OK != env->String_NewUTF8(str.c_str(), size, &result)) { + return { false, {} }; + } + return { true, std::move(result) }; +} + std::tuple TypeConverter::ToAniString(ani_env *env, const char *str) { if (env == nullptr) { diff --git a/interfaces/kits/js/src/common/ani_helper/type_converter.h b/interfaces/kits/js/src/common/ani_helper/type_converter.h index 24bdd736a..a6d062659 100644 --- a/interfaces/kits/js/src/common/ani_helper/type_converter.h +++ b/interfaces/kits/js/src/common/ani_helper/type_converter.h @@ -33,6 +33,7 @@ public: static std::tuple> ToOptionalInt32(ani_env *env, const ani_object &value); static std::tuple> ToOptionalInt64(ani_env *env, const ani_object &value); static std::tuple ToAniString(ani_env *env, std::string str); + static std::tuple ToAniString(ani_env *env, std::string str, size_t size); static std::tuple ToAniString(ani_env *env, const char *str); static std::tuple> EnumToInt32(ani_env *env, const ani_enum_item &enumOp); static std::tuple ToFileInfo(ani_env *env, const ani_object &pathOrFd); diff --git a/interfaces/kits/js/src/mod_fs/properties/ani/read_text_ani.cpp b/interfaces/kits/js/src/mod_fs/properties/ani/read_text_ani.cpp index dc733c606..7e2fe63ff 100644 --- a/interfaces/kits/js/src/mod_fs/properties/ani/read_text_ani.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/ani/read_text_ani.cpp @@ -92,7 +92,8 @@ ani_string ReadTextAni::ReadTextSync( const auto &resText = ret.GetData().value(); string res = std::get<0>(resText); - auto [succ, result] = TypeConverter::ToAniString(env, res); + size_t size = std::get<1>(resText); + auto [succ, result] = TypeConverter::ToAniString(env, res, size); if (!succ) { HILOGE("Convert result to ani string failed"); ErrorHandler::Throw(env, UNKNOWN_ERR); -- Gitee From 7cc574eed852452e45cbc3e82894286ed992b0d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=91=A8=E9=91=AB?= Date: Thu, 24 Apr 2025 14:35:06 +0000 Subject: [PATCH 2/5] update interfaces/kits/js/src/common/ani_helper/type_converter.cpp. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 周鑫 --- interfaces/kits/js/src/common/ani_helper/type_converter.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/interfaces/kits/js/src/common/ani_helper/type_converter.cpp b/interfaces/kits/js/src/common/ani_helper/type_converter.cpp index 8ccc2d8f8..1206d5877 100644 --- a/interfaces/kits/js/src/common/ani_helper/type_converter.cpp +++ b/interfaces/kits/js/src/common/ani_helper/type_converter.cpp @@ -108,7 +108,7 @@ std::tuple TypeConverter::ToAniString(ani_env *env, std::strin return { true, std::move(result) }; } -static std::tuple ToAniString(ani_env *env, std::string str, size_t size) +static std::tuple TypeConverter::ToAniString(ani_env *env, std::string str, size_t size) { if (env == nullptr) { return { false, {} }; -- Gitee From e61ed615a05ae43681d32197d12ef3d2dec2bf94 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=91=A8=E9=91=AB?= Date: Thu, 24 Apr 2025 16:02:51 +0000 Subject: [PATCH 3/5] update interfaces/kits/js/src/common/ani_helper/type_converter.cpp. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 周鑫 --- interfaces/kits/js/src/common/ani_helper/type_converter.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/interfaces/kits/js/src/common/ani_helper/type_converter.cpp b/interfaces/kits/js/src/common/ani_helper/type_converter.cpp index 1206d5877..bfa635e0e 100644 --- a/interfaces/kits/js/src/common/ani_helper/type_converter.cpp +++ b/interfaces/kits/js/src/common/ani_helper/type_converter.cpp @@ -108,7 +108,7 @@ std::tuple TypeConverter::ToAniString(ani_env *env, std::strin return { true, std::move(result) }; } -static std::tuple TypeConverter::ToAniString(ani_env *env, std::string str, size_t size) +std::tuple TypeConverter::ToAniString(ani_env *env, std::string str, size_t size) { if (env == nullptr) { return { false, {} }; -- Gitee From cc43fe556901a00b9c7dee4a40e6a0e718038ef4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=91=A8=E9=91=AB?= Date: Sun, 27 Apr 2025 07:50:10 +0000 Subject: [PATCH 4/5] update interfaces/kits/js/src/mod_fs/properties/ani/read_text_ani.cpp. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 周鑫 --- .../kits/js/src/mod_fs/properties/ani/read_text_ani.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/interfaces/kits/js/src/mod_fs/properties/ani/read_text_ani.cpp b/interfaces/kits/js/src/mod_fs/properties/ani/read_text_ani.cpp index 7e2fe63ff..125286c9f 100644 --- a/interfaces/kits/js/src/mod_fs/properties/ani/read_text_ani.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/ani/read_text_ani.cpp @@ -91,8 +91,8 @@ ani_string ReadTextAni::ReadTextSync( } const auto &resText = ret.GetData().value(); - string res = std::get<0>(resText); - size_t size = std::get<1>(resText); + string res = std::get<0>(resText); // 取字符串 + size_t size = std::get<1>(resText); // 取读取字符数 auto [succ, result] = TypeConverter::ToAniString(env, res, size); if (!succ) { HILOGE("Convert result to ani string failed"); -- Gitee From aa93c5ea86b7ce3eedfb171acf4f79bd52449d75 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=91=A8=E9=91=AB?= Date: Sun, 27 Apr 2025 07:52:31 +0000 Subject: [PATCH 5/5] update interfaces/kits/js/src/mod_fs/properties/ani/read_text_ani.cpp. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 周鑫 --- .../kits/js/src/mod_fs/properties/ani/read_text_ani.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/interfaces/kits/js/src/mod_fs/properties/ani/read_text_ani.cpp b/interfaces/kits/js/src/mod_fs/properties/ani/read_text_ani.cpp index 125286c9f..05689d3d7 100644 --- a/interfaces/kits/js/src/mod_fs/properties/ani/read_text_ani.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/ani/read_text_ani.cpp @@ -91,8 +91,8 @@ ani_string ReadTextAni::ReadTextSync( } const auto &resText = ret.GetData().value(); - string res = std::get<0>(resText); // 取字符串 - size_t size = std::get<1>(resText); // 取读取字符数 + string res = std::get<0>(resText); // get buffer + size_t size = std::get<1>(resText); // get buffer length auto [succ, result] = TypeConverter::ToAniString(env, res, size); if (!succ) { HILOGE("Convert result to ani string failed"); -- Gitee