add levelize

This commit is contained in:
lixianjing 2023-12-04 18:02:20 +08:00
parent 0758925fdc
commit f5735b887f
7 changed files with 143 additions and 2 deletions

View File

@ -2,7 +2,7 @@
2023/12/04
* 完善combobox的options如果选项第一个字符不是数字里面存在':'也不视为分隔符。比如http://localhost视为一个整体。
* 增加函数tk_levelize。fscript增加levelize函数。
2023/12/03
* 修复编译警告。
* 完善remote ui service

View File

@ -1454,6 +1454,27 @@ clamp(a, min, max) => double
clamp(2, 1, 3)
```
#### 4.5.8 levelize
> levelize 函数。
----------------------------
##### 原型
按指定的区间对值进行等级化。
```js
levelize(levels, value) => int32_t
```
##### 示例
```js
levelize('0-20;21-40;41-60;61-80;81-100', 10) => 0
levelize('0-20;21-40;41-60;61-80;81-100', 30) => 1
levelize('0-20;21-40;41-60;61-80;81-100', 50) => 2
```
### 4.6 时间函数
> 已经移动到扩展模块。

View File

@ -112,6 +112,19 @@ static ret_t func_value_get_binary_size(fscript_t* fscript, fscript_args_t* args
return RET_FAIL;
}
static ret_t func_levelize(fscript_t* fscript, fscript_args_t* args, value_t* result) {
int32_t value = 0;
const char* levels = NULL;
FSCRIPT_FUNC_CHECK(args->size == 2, RET_BAD_PARAMS);
levels = value_str(args->args);
value = value_int32(args->args + 1);
return_value_if_fail(levels != NULL, RET_BAD_PARAMS);
value_set_int32(result, tk_levelize(levels, value));
return RET_OK;
}
static ret_t func_index_of_ex(fscript_t* fscript, fscript_args_t* args, value_t* result,
bool_t last) {
const char* p = NULL;
@ -409,6 +422,7 @@ static ret_t func_define_param(fscript_t* fscript, fscript_args_t* args, value_t
}
FACTORY_TABLE_BEGIN(s_ext_basic)
FACTORY_TABLE_ENTRY("levelize", func_levelize)
FACTORY_TABLE_ENTRY("index_of", func_index_of)
FACTORY_TABLE_ENTRY("last_index_of", func_last_index_of)
FACTORY_TABLE_ENTRY("ulen", func_ulen)

View File

@ -48,7 +48,7 @@ ret_t tk_set_ui_thread(uint64_t ui_thread_id) {
bool_t tk_is_ui_thread(void) {
return s_ui_thread_id == tk_thread_self();
}
#endif/*WITH_WASM*/
#endif /*WITH_WASM*/
const char* tk_skip_to_num(const char* str) {
const char* p = str;
@ -1990,3 +1990,41 @@ int tk_sscanf(const char* str, const char* format, ...) {
return ret;
}
#endif /*HAS_NO_VSSCANF*/
/**
* @method tk_levelize
* value转换成level
* levels为"0-20;21-40;41-60;61-80;81-100"value为502
* @param {const char*} levels
* @param {int32_t} value
* @return {int32_t} level
*/
int32_t tk_levelize(const char* levels, int32_t value) {
int32_t level = 0;
int32_t start = 0;
int32_t end = 0;
const char* p = levels;
return_value_if_fail(levels != NULL, 0);
while (*p) {
if (tk_sscanf(p, "%d-%d", &start, &end) == 2) {
if (value < start) {
break;
}
if (value >= start && value <= end) {
return level;
}
}
p = strchr(p, ';');
if (p != NULL) {
p = p + 1;
level++;
} else {
break;
}
}
return level;
}

View File

@ -1055,6 +1055,16 @@ ret_t tk_free_utf8_argv(int argc, char** argv);
*/
int tk_sscanf_simple(const char* str, const char* format, ...);
/**
* @method tk_levelize
* value转换成level
* levels为"0-20;21-40;41-60;61-80;81-100"value为502
* @param {const char*} levels
* @param {int32_t} value
* @return {int32_t} level
*/
int32_t tk_levelize(const char* levels, int32_t value);
#define TK_STRDUP(str) ((str) != NULL) ? tk_strdup(str) : NULL
#define TK_STRNDUP(str, len) ((str) != NULL) ? tk_strndup(str, len) : NULL

View File

@ -3229,3 +3229,23 @@ TEST(FScript, consts) {
TK_OBJECT_UNREF(obj);
}
TEST(FScript, levelize) {
value_t v;
value_t s;
tk_object_t* obj = object_default_create();
fscript_eval(obj, "var t = levelize('0-20;21-40;41-60;61-80;81-100', 10); t", &v);
ASSERT_EQ(value_int(&v), 0);
fscript_eval(obj, "var t = levelize('0-20;21-40;41-60;61-80;81-100', 30); t", &v);
ASSERT_EQ(value_int(&v), 1);
fscript_eval(obj, "var t = levelize('0-20;21-40;41-60;61-80;81-100', 200); t", &v);
ASSERT_EQ(value_int(&v), 4);
value_reset(&v);
TK_OBJECT_UNREF(obj);
}

View File

@ -1154,3 +1154,41 @@ TEST(Utils, sscanf_simple2) {
ASSERT_EQ(tk_isdigit('-'), FALSE);
ASSERT_EQ(tk_isdigit('+'), FALSE);
}
TEST(Utils, levelize1) {
ASSERT_EQ(tk_levelize("0-20;21-40;41-60;61-80;81-100", -1), 0);
ASSERT_EQ(tk_levelize("0-20;21-40;41-60;61-80;81-100", 0), 0);
ASSERT_EQ(tk_levelize("0-20;21-40;41-60;61-80;81-100", 10), 0);
ASSERT_EQ(tk_levelize("0-20;21-40;41-60;61-80;81-100", 20), 0);
ASSERT_EQ(tk_levelize("0-20;21-40;41-60;61-80;81-100", 21), 1);
ASSERT_EQ(tk_levelize("0-20;21-40;41-60;61-80;81-100", 30), 1);
ASSERT_EQ(tk_levelize("0-20;21-40;41-60;61-80;81-100", 40), 1);
ASSERT_EQ(tk_levelize("0-20;21-40;41-60;61-80;81-100", 41), 2);
ASSERT_EQ(tk_levelize("0-20;21-40;41-60;61-80;81-100", 50), 2);
ASSERT_EQ(tk_levelize("0-20;21-40;41-60;61-80;81-100", 60), 2);
ASSERT_EQ(tk_levelize("0-20;21-40;41-60;61-80;81-100", 61), 3);
ASSERT_EQ(tk_levelize("0-20;21-40;41-60;61-80;81-100", 70), 3);
ASSERT_EQ(tk_levelize("0-20;21-40;41-60;61-80;81-100", 80), 3);
ASSERT_EQ(tk_levelize("0-20;21-40;41-60;61-80;81-100", 81), 4);
ASSERT_EQ(tk_levelize("0-20;21-40;41-60;61-80;81-100", 90), 4);
ASSERT_EQ(tk_levelize("0-20;21-40;41-60;61-80;81-100", 100), 4);
ASSERT_EQ(tk_levelize("0-20;21-40;41-60;61-80;81-100", 1000), 4);
}
TEST(Utils, levelize2) {
ASSERT_EQ(tk_levelize("0-20", -1), 0);
ASSERT_EQ(tk_levelize("0-20", 0), 0);
ASSERT_EQ(tk_levelize("0-20", 10), 0);
ASSERT_EQ(tk_levelize("0-20", 100), 0);
}
TEST(Utils, levelize3) {
ASSERT_EQ(tk_levelize("0-20;21-40", -1), 0);
ASSERT_EQ(tk_levelize("0-20;21-40", 30), 1);
ASSERT_EQ(tk_levelize("0-20;21-40", 100), 1);
}