Added to-str-delim

This commit is contained in:
Benjamin Vedder 2022-11-10 13:01:13 +01:00
parent 08b8ba73c3
commit 7ea006882b
2 changed files with 35 additions and 4 deletions

View File

@ -1892,6 +1892,19 @@ Convert LBM-types to their string representation and return that string. Example
> "aAa 4 (a 2 3) 2 3 Hello"
```
#### to-str-delim
```clj
(to-str-delim delimiter arg1 ... argN)
```
Same as [to-str](#to-str), but with a custom delimiter. Example:
```clj
(to-str-delim "::" "aAa" 4 '(a 2 3) 2 3 "Hello")
> "aAa::4::(a 2 3)::2::3::Hello"
```
## Events
Events can be used to execute code for certain events, such as when CAN-frames are received. To use events you must first register an event handler, then enable the events you want to receive. As the event handler blocks until the event arrives it is useful to spawn a thread to handle events so that other things can be done in the main thread at the same time.

View File

@ -2774,7 +2774,7 @@ static lbm_value ext_str_cmp(lbm_value *args, lbm_uint argn) {
}
// TODO: This is very similar to ext-print. Maybe they can share code.
static lbm_value ext_to_str(lbm_value *args, lbm_uint argn) {
static lbm_value to_str(char *delimiter, lbm_value *args, lbm_uint argn) {
const int str_len = 300;
char *str = lispif_malloc(str_len);
if (!str) {
@ -2795,7 +2795,7 @@ static lbm_value ext_to_str(lbm_value *args, lbm_uint argn) {
if (str_ofs == 0) {
chars = snprintf(str + str_ofs, max, "%s", (char*)array->data);
} else {
chars = snprintf(str + str_ofs, max, " %s", (char*)array->data);
chars = snprintf(str + str_ofs, max, "%s%s", delimiter, (char*)array->data);
}
if (chars >= max) {
str_ofs += max;
@ -2812,7 +2812,7 @@ static lbm_value ext_to_str(lbm_value *args, lbm_uint argn) {
if (str_ofs == 0) {
chars = snprintf(str + str_ofs, max, "%c", lbm_dec_char(t));
} else {
chars = snprintf(str + str_ofs, max, " %c", lbm_dec_char(t));
chars = snprintf(str + str_ofs, max, "%s%c", delimiter, lbm_dec_char(t));
}
if (chars >= max) {
str_ofs += max;
@ -2826,7 +2826,7 @@ static lbm_value ext_to_str(lbm_value *args, lbm_uint argn) {
if (str_ofs == 0) {
chars = snprintf(str + str_ofs, max, "%s", print_val_buffer);
} else {
chars = snprintf(str + str_ofs, max, " %s", print_val_buffer);
chars = snprintf(str + str_ofs, max, "%s%s", delimiter, print_val_buffer);
}
if (chars >= max) {
str_ofs += max;
@ -2847,6 +2847,23 @@ static lbm_value ext_to_str(lbm_value *args, lbm_uint argn) {
}
}
static lbm_value ext_to_str(lbm_value *args, lbm_uint argn) {
return to_str(" ", args, argn);
}
static lbm_value ext_to_str_delim(lbm_value *args, lbm_uint argn) {
if (argn < 1) {
return ENC_SYM_EERROR;
}
char *delim = lbm_dec_str(args[0]);
if (!delim) {
return ENC_SYM_EERROR;
}
return to_str(delim, args + 1, argn - 1);
}
// Configuration
static lbm_value ext_conf_set(lbm_value *args, lbm_uint argn) {
@ -4317,6 +4334,7 @@ void lispif_load_vesc_extensions(void) {
lbm_add_extension("str-to-upper", ext_str_to_upper);
lbm_add_extension("str-cmp", ext_str_cmp);
lbm_add_extension("to-str", ext_to_str);
lbm_add_extension("to-str-delim", ext_to_str_delim);
// Configuration
lbm_add_extension("conf-set", ext_conf_set);