improve text selector to support escape char

This commit is contained in:
lixianjing 2022-06-17 16:28:04 +08:00
parent 63b9a58b49
commit dd54687a52
4 changed files with 66 additions and 4 deletions

View File

@ -1,6 +1,7 @@
# 最新动态
2022/06/17
* 完善str_unescape
* text selector的options支持字符转义方便使用特殊字符。
2022/06/16
* 完善draggable

View File

@ -294,11 +294,13 @@ static ret_t text_selector_on_destroy(widget_t* widget) {
}
ret_t text_selector_parse_options(widget_t* widget, const char* str) {
str_t s;
int32_t i = 0;
tokenizer_t tokenizer;
tokenizer_t* t = &tokenizer;
text_selector_t* text_selector = TEXT_SELECTOR(widget);
return_value_if_fail(widget != NULL && text_selector != NULL, RET_BAD_PARAMS);
return_value_if_fail(str_init(&s, 100) != NULL, RET_OOM);
text_selector->options = tk_strdup(str);
tokenizer_init(t, str, strlen(str), ";");
@ -316,10 +318,13 @@ ret_t text_selector_parse_options(widget_t* widget, const char* str) {
value = i;
}
text_selector_append_option(widget, value, text);
str_set(&s, text);
str_unescape(&s);
text_selector_append_option(widget, value, s.str);
i++;
}
}
str_reset(&s);
tokenizer_deinit(t);
return RET_OK;
@ -349,11 +354,13 @@ ret_t text_selector_set_options(widget_t* widget, const char* options) {
text_selector_reset_options(widget);
if (strchr(options, ':') == NULL && strchr(options, '-') != NULL) {
str_t s;
int nr = 0;
int end = 0;
int step = 1;
int start = 0;
char format[41];
ret_t ret = RET_FAIL;
memset(format, 0x00, sizeof(format));
nr = tk_sscanf(options, "%d-%d-%40s", &start, &end, format);
@ -369,7 +376,13 @@ ret_t text_selector_set_options(widget_t* widget, const char* options) {
}
}
return text_selector_set_range_options_ex(widget, start, end - start + 1, step, format);
if (str_init(&s, sizeof(format)) != NULL) {
str_set(&s, format);
str_unescape(&s);
ret = text_selector_set_range_options_ex(widget, start, end - start + 1, step, s.str);
str_reset(&s);
}
return ret;
} else {
return text_selector_parse_options(widget, options);
}

View File

@ -98,9 +98,13 @@ typedef struct _text_selector_t {
/**
* @property {char*} options
* @annotation ["set_prop","get_prop","readable","persitent","design","scriptable"]
* (:1:red;2:green;3:blue)
* -
* ((:)(;):1:red;2:green;3:blue)
* (-)
* "1-7-%02d"1702d%d
* > (:)(;)(-)16
* > * (:)XML中写为\x3aC代码中写为\\x3a
* > * (;)XML中写为\x3bC代码中写为\\x3b
* > * (-)XML中写为\x2dC代码中写为\\x2d
*/
char* options;

View File

@ -281,3 +281,47 @@ TEST(TextSelector, localize_options) {
widget_destroy(win);
}
TEST(TextSelector, parse_options_special_chars) {
widget_t* w = text_selector_create(NULL, 10, 20, 30, 40);
text_selector_parse_options(w, "1:r\\x3a\\x3b;2:gree\\x6e;3:\\x3ablue");
ASSERT_EQ(text_selector_count_options(w), 3);
text_selector_set_selected_index(w, 0);
ASSERT_EQ(text_selector_get_value(w), 1);
ASSERT_EQ(string(text_selector_get_text(w)), string("r:;"));
text_selector_set_selected_index(w, 1);
ASSERT_EQ(text_selector_get_value(w), 2);
ASSERT_EQ(string(text_selector_get_text(w)), string("green"));
text_selector_set_selected_index(w, 2);
ASSERT_EQ(text_selector_get_value(w), 3);
ASSERT_EQ(string(text_selector_get_text(w)), string(":blue"));
text_selector_reset_options(w);
ASSERT_EQ(text_selector_count_options(w), 0);
widget_destroy(w);
}
TEST(TextSelector, parse_options_special_chars1) {
widget_t* w = text_selector_create(NULL, 10, 20, 30, 40);
text_selector_set_options(w, "-12-14-GTM%+02d\\x3a00");
ASSERT_EQ(text_selector_count_options(w), 27);
text_selector_set_selected_index(w, 0);
ASSERT_EQ(text_selector_get_value(w), -12);
ASSERT_STREQ(text_selector_get_text(w), "GTM-12:00");
text_selector_set_selected_index(w, 13);
ASSERT_EQ(text_selector_get_value(w), 1);
ASSERT_STREQ(text_selector_get_text(w), "GTM+1:00");
text_selector_reset_options(w);
ASSERT_EQ(text_selector_count_options(w), 0);
widget_destroy(w);
}