Skip to content

Commit

Permalink
improve ulen
Browse files Browse the repository at this point in the history
  • Loading branch information
xianjimli committed Dec 9, 2023
1 parent f87edb2 commit 0e29ab4
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 5 deletions.
1 change: 1 addition & 0 deletions docs/changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

2023/12/09
* 增加函数 date\_time\_parse\_date/date\_time\_parse\_time/date\_time\_parse\_date\_time
* 完善 fscript ulen。

2023/12/07
* csv file object 支持通过 MVVM 来查询。
Expand Down
15 changes: 10 additions & 5 deletions src/fscript_ext/fscript_ext.c
Original file line number Diff line number Diff line change
Expand Up @@ -322,12 +322,17 @@ static ret_t func_ulen(fscript_t* fscript, fscript_args_t* args, value_t* result
wstr_t wstr;
const char* str = NULL;
FSCRIPT_FUNC_CHECK(args->size == 1, RET_BAD_PARAMS);
str = value_str(args->args);

wstr_init(&wstr, 0);
return_value_if_fail(wstr_set_utf8(&wstr, str) == RET_OK, RET_OOM);
value_set_int32(result, wstr.size);
wstr_reset(&wstr);
if (args->args->type == VALUE_TYPE_WSTRING) {
value_set_int32(result, wcs_len(value_wstr(args->args)));
} else {
char buff[64] = {0};
str = value_str_ex(args->args, buff, sizeof(buff));
wstr_init(&wstr, 0);
return_value_if_fail(wstr_set_utf8(&wstr, str) == RET_OK, RET_OOM);
value_set_int32(result, wstr.size);
wstr_reset(&wstr);
}

return RET_OK;
}
Expand Down
14 changes: 14 additions & 0 deletions tests/fscript_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3431,3 +3431,17 @@ TEST(FExr, abs) {
TK_OBJECT_UNREF(obj);
}

TEST(FExr, ulen) {
value_t v;
tk_object_t* obj = object_default_create();

fscript_eval(obj, "ulen(1)", &v);
ASSERT_EQ(value_int32(&v), 1);
value_reset(&v);

fscript_eval(obj, "ulen('abc')", &v);
ASSERT_EQ(value_int32(&v), 3);
value_reset(&v);

TK_OBJECT_UNREF(obj);
}
21 changes: 21 additions & 0 deletions tests/fscript_widget_test.cc
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#include "tkc/fscript.h"
#include "tkc/object_default.h"
#include "gtest/gtest.h"
#include "widgets/edit.h"
#include "widgets/button.h"
#include "widgets/progress_bar.h"
#include "base/window.h"
#include "base/window_manager.h"
#include "base/object_widget.h"

TEST(FScriptWidget, basic) {
value_t v;
Expand Down Expand Up @@ -59,3 +61,22 @@ TEST(FScriptWidget, layout) {
widget_close_window(win);
TK_OBJECT_UNREF(obj);
}

TEST(FScriptWidget, ulen) {
value_t v;
widget_t* w = edit_create(NULL, 0, 0, 100, 20);
tk_object_t* obj = object_widget_create(w);

widget_set_text(w, L"中文");
fscript_eval(obj, "ulen(text)", &v);
ASSERT_EQ(value_int(&v), 2);
value_reset(&v);

widget_set_text(w, L"abc");
fscript_eval(obj, "ulen(text)", &v);
ASSERT_EQ(value_int(&v), 3);
value_reset(&v);

widget_destroy(w);
TK_OBJECT_UNREF(obj);
}

0 comments on commit 0e29ab4

Please sign in to comment.