refactor(api): make cstr_as_string accept "const char*"

In the context a String inside an Object/Dictionary etc is consumed,
it is considered to be read-only.
This commit is contained in:
bfredl 2024-02-09 11:42:40 +01:00
parent 4788abf2da
commit e0e5b7f0ba
16 changed files with 29 additions and 30 deletions

View File

@ -425,12 +425,12 @@ String cstrn_as_string(char *str, size_t maxsize)
/// @param str the C string to use /// @param str the C string to use
/// @return The resulting String, or an empty String if /// @return The resulting String, or an empty String if
/// str was NULL /// str was NULL
String cstr_as_string(char *str) FUNC_ATTR_PURE String cstr_as_string(const char *str) FUNC_ATTR_PURE
{ {
if (str == NULL) { if (str == NULL) {
return (String)STRING_INIT; return (String)STRING_INIT;
} }
return (String){ .data = str, .size = strlen(str) }; return (String){ .data = (char *)str, .size = strlen(str) };
} }
/// Return the owned memory of a ga as a String /// Return the owned memory of a ga as a String

View File

@ -789,7 +789,7 @@ void remote_ui_hl_attr_define(UI *ui, Integer id, HlAttrs rgb_attrs, HlAttrs cte
// system. So we add them here. // system. So we add them here.
if (rgb_attrs.url >= 0) { if (rgb_attrs.url >= 0) {
const char *url = hl_get_url((uint32_t)rgb_attrs.url); const char *url = hl_get_url((uint32_t)rgb_attrs.url);
PUT_C(rgb, "url", STRING_OBJ(cstr_as_string((char *)url))); PUT_C(rgb, "url", CSTR_AS_OBJ(url));
} }
ADD_C(args, DICTIONARY_OBJ(rgb)); ADD_C(args, DICTIONARY_OBJ(rgb));
@ -857,7 +857,7 @@ void remote_ui_put(UI *ui, const char *cell)
UIData *data = ui->data; UIData *data = ui->data;
data->client_col++; data->client_col++;
Array args = data->call_buf; Array args = data->call_buf;
ADD_C(args, CSTR_AS_OBJ((char *)cell)); ADD_C(args, CSTR_AS_OBJ(cell));
push_call(ui, "put", args); push_call(ui, "put", args);
} }

View File

@ -2289,7 +2289,7 @@ Dictionary nvim_eval_statusline(String str, Dict(eval_statusline) *opts, Arena *
opts->use_winbar, stc_hl_id); opts->use_winbar, stc_hl_id);
PUT_C(hl_info, "start", INTEGER_OBJ(0)); PUT_C(hl_info, "start", INTEGER_OBJ(0));
PUT_C(hl_info, "group", CSTR_AS_OBJ((char *)grpname)); PUT_C(hl_info, "group", CSTR_AS_OBJ(grpname));
ADD_C(hl_values, DICTIONARY_OBJ(hl_info)); ADD_C(hl_values, DICTIONARY_OBJ(hl_info));
} }
@ -2308,7 +2308,7 @@ Dictionary nvim_eval_statusline(String str, Dict(eval_statusline) *opts, Arena *
snprintf(user_group, sizeof(user_group), "User%d", sp->userhl); snprintf(user_group, sizeof(user_group), "User%d", sp->userhl);
grpname = arena_memdupz(arena, user_group, strlen(user_group)); grpname = arena_memdupz(arena, user_group, strlen(user_group));
} }
PUT_C(hl_info, "group", CSTR_AS_OBJ((char *)grpname)); PUT_C(hl_info, "group", CSTR_AS_OBJ(grpname));
ADD_C(hl_values, DICTIONARY_OBJ(hl_info)); ADD_C(hl_values, DICTIONARY_OBJ(hl_info));
} }
PUT_C(result, "highlights", ARRAY_OBJ(hl_values)); PUT_C(result, "highlights", ARRAY_OBJ(hl_values));

View File

@ -626,7 +626,7 @@ Dict(float_config) nvim_win_get_config(Window window, Arena *arena, Error *err)
PUT_KEY_X(rv, bufpos, pos); PUT_KEY_X(rv, bufpos, pos);
} }
} }
PUT_KEY_X(rv, anchor, cstr_as_string((char *)float_anchor_str[config->anchor])); PUT_KEY_X(rv, anchor, cstr_as_string(float_anchor_str[config->anchor]));
PUT_KEY_X(rv, row, config->row); PUT_KEY_X(rv, row, config->row);
PUT_KEY_X(rv, col, config->col); PUT_KEY_X(rv, col, config->col);
PUT_KEY_X(rv, zindex, config->zindex); PUT_KEY_X(rv, zindex, config->zindex);
@ -659,12 +659,12 @@ Dict(float_config) nvim_win_get_config(Window window, Arena *arena, Error *err)
PUT_KEY_X(rv, width, wp->w_width); PUT_KEY_X(rv, width, wp->w_width);
PUT_KEY_X(rv, height, wp->w_height); PUT_KEY_X(rv, height, wp->w_height);
WinSplit split = win_split_dir(wp); WinSplit split = win_split_dir(wp);
PUT_KEY_X(rv, split, cstr_as_string((char *)win_split_str[split])); PUT_KEY_X(rv, split, cstr_as_string(win_split_str[split]));
} }
const char *rel = (wp->w_floating && !config->external const char *rel = (wp->w_floating && !config->external
? float_relative_str[config->relative] : ""); ? float_relative_str[config->relative] : "");
PUT_KEY_X(rv, relative, cstr_as_string((char *)rel)); PUT_KEY_X(rv, relative, cstr_as_string(rel));
return rv; return rv;
} }

View File

@ -121,7 +121,7 @@ static void augroup_map_del(int id, const char *name)
{ {
if (name != NULL) { if (name != NULL) {
String key; String key;
map_del(String, int)(&map_augroup_name_to_id, cstr_as_string((char *)name), &key); map_del(String, int)(&map_augroup_name_to_id, cstr_as_string(name), &key);
api_free_string(key); api_free_string(key);
} }
if (id > 0) { if (id > 0) {
@ -476,7 +476,7 @@ void augroup_del(char *name, bool stupid_legacy_mode)
int augroup_find(const char *name) int augroup_find(const char *name)
FUNC_ATTR_PURE FUNC_ATTR_WARN_UNUSED_RESULT FUNC_ATTR_PURE FUNC_ATTR_WARN_UNUSED_RESULT
{ {
int existing_id = map_get(String, int)(&map_augroup_name_to_id, cstr_as_string((char *)name)); int existing_id = map_get(String, int)(&map_augroup_name_to_id, cstr_as_string(name));
if (existing_id == AUGROUP_DELETED) { if (existing_id == AUGROUP_DELETED) {
return existing_id; return existing_id;
} }

View File

@ -977,12 +977,12 @@ void decor_to_dict_legacy(Dictionary *dict, DecorInline decor, bool hl_name, Are
} }
if (sh_hl.url != NULL) { if (sh_hl.url != NULL) {
PUT_C(*dict, "url", STRING_OBJ(cstr_as_string((char *)sh_hl.url))); PUT_C(*dict, "url", STRING_OBJ(cstr_as_string(sh_hl.url)));
} }
if (virt_text) { if (virt_text) {
if (virt_text->hl_mode) { if (virt_text->hl_mode) {
PUT_C(*dict, "hl_mode", CSTR_AS_OBJ((char *)hl_mode_str[virt_text->hl_mode])); PUT_C(*dict, "hl_mode", CSTR_AS_OBJ(hl_mode_str[virt_text->hl_mode]));
} }
Array chunks = virt_text_to_array(virt_text->data.virt_text, hl_name, arena); Array chunks = virt_text_to_array(virt_text->data.virt_text, hl_name, arena);
@ -992,7 +992,7 @@ void decor_to_dict_legacy(Dictionary *dict, DecorInline decor, bool hl_name, Are
if (virt_text->pos == kVPosWinCol) { if (virt_text->pos == kVPosWinCol) {
PUT_C(*dict, "virt_text_win_col", INTEGER_OBJ(virt_text->col)); PUT_C(*dict, "virt_text_win_col", INTEGER_OBJ(virt_text->col));
} }
PUT_C(*dict, "virt_text_pos", CSTR_AS_OBJ((char *)virt_text_pos_str[virt_text->pos])); PUT_C(*dict, "virt_text_pos", CSTR_AS_OBJ(virt_text_pos_str[virt_text->pos]));
priority = virt_text->priority; priority = virt_text->priority;
} }

View File

@ -2093,8 +2093,8 @@ static void f_feedkeys(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
flags = tv_get_string_buf(&argvars[1], nbuf); flags = tv_get_string_buf(&argvars[1], nbuf);
} }
nvim_feedkeys(cstr_as_string((char *)keys), nvim_feedkeys(cstr_as_string(keys),
cstr_as_string((char *)flags), true); cstr_as_string(flags), true);
} }
/// "filereadable()" function /// "filereadable()" function
@ -4528,7 +4528,7 @@ static void f_luaeval(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
return; return;
} }
nlua_typval_eval(cstr_as_string((char *)str), &argvars[1], rettv); nlua_typval_eval(cstr_as_string(str), &argvars[1], rettv);
} }
static void find_some_match(typval_T *const argvars, typval_T *const rettv, static void find_some_match(typval_T *const argvars, typval_T *const rettv,

View File

@ -135,7 +135,7 @@ void ui_send_all_hls(UI *ui)
api_free_array(inspect); api_free_array(inspect);
} }
for (size_t hlf = 0; hlf < HLF_COUNT; hlf++) { for (size_t hlf = 0; hlf < HLF_COUNT; hlf++) {
remote_ui_hl_group_set(ui, cstr_as_string((char *)hlf_names[hlf]), remote_ui_hl_group_set(ui, cstr_as_string(hlf_names[hlf]),
highlight_attr[hlf]); highlight_attr[hlf]);
} }
} }

View File

@ -2241,7 +2241,7 @@ void highlight_changed(void)
HlAttrs attrs = syn_attr2entry(highlight_attr[hlf]); HlAttrs attrs = syn_attr2entry(highlight_attr[hlf]);
msg_grid.blending = attrs.hl_blend > -1; msg_grid.blending = attrs.hl_blend > -1;
} }
ui_call_hl_group_set(cstr_as_string((char *)hlf_names[hlf]), ui_call_hl_group_set(cstr_as_string(hlf_names[hlf]),
highlight_attr[hlf]); highlight_attr[hlf]);
highlight_attr_last[hlf] = highlight_attr[hlf]; highlight_attr_last[hlf] = highlight_attr[hlf];
} }

View File

@ -3017,7 +3017,7 @@ void msg_ext_ui_flush(void)
msg_ext_emit_chunk(); msg_ext_emit_chunk();
if (msg_ext_chunks.size > 0) { if (msg_ext_chunks.size > 0) {
ui_call_msg_show(cstr_as_string((char *)msg_ext_kind), ui_call_msg_show(cstr_as_string(msg_ext_kind),
msg_ext_chunks, msg_ext_overwrite); msg_ext_chunks, msg_ext_overwrite);
if (!msg_ext_overwrite) { if (!msg_ext_overwrite) {
msg_ext_visible++; msg_ext_visible++;

View File

@ -547,7 +547,7 @@ static void send_error(Channel *chan, MsgpackRpcRequestHandler handler, MessageT
static void send_request(Channel *channel, uint32_t id, const char *name, Array args) static void send_request(Channel *channel, uint32_t id, const char *name, Array args)
{ {
const String method = cstr_as_string((char *)name); const String method = cstr_as_string(name);
channel_write(channel, serialize_request(channel->id, channel_write(channel, serialize_request(channel->id,
id, id,
method, method,
@ -558,7 +558,7 @@ static void send_request(Channel *channel, uint32_t id, const char *name, Array
static void send_event(Channel *channel, const char *name, Array args) static void send_event(Channel *channel, const char *name, Array args)
{ {
const String method = cstr_as_string((char *)name); const String method = cstr_as_string(name);
channel_write(channel, serialize_request(channel->id, channel_write(channel, serialize_request(channel->id,
0, 0,
method, method,
@ -583,7 +583,7 @@ static void broadcast_event(const char *name, Array args)
goto end; goto end;
} }
const String method = cstr_as_string((char *)name); const String method = cstr_as_string(name);
WBuffer *buffer = serialize_request(0, WBuffer *buffer = serialize_request(0,
0, 0,
method, method,

View File

@ -95,7 +95,7 @@ int os_chdir(const char *path)
} }
int err = uv_chdir(path); int err = uv_chdir(path);
if (err == 0) { if (err == 0) {
ui_call_chdir(cstr_as_string((char *)path)); ui_call_chdir(cstr_as_string(path));
} }
return err; return err;
} }

View File

@ -488,7 +488,7 @@ void pum_redraw(void)
if (ui_has(kUIMultigrid)) { if (ui_has(kUIMultigrid)) {
const char *anchor = pum_above ? "SW" : "NW"; const char *anchor = pum_above ? "SW" : "NW";
int row_off = pum_above ? -pum_height : 0; int row_off = pum_above ? -pum_height : 0;
ui_call_win_float_pos(pum_grid.handle, -1, cstr_as_string((char *)anchor), pum_anchor_grid, ui_call_win_float_pos(pum_grid.handle, -1, cstr_as_string(anchor), pum_anchor_grid,
pum_row - row_off, pum_left_col, false, pum_grid.zindex); pum_row - row_off, pum_left_col, false, pum_grid.zindex);
} }

View File

@ -84,7 +84,7 @@ static int64_t group_get_ns(const char *group)
return UINT32_MAX; // All namespaces return UINT32_MAX; // All namespaces
} }
// Specific or non-existing namespace // Specific or non-existing namespace
int ns = map_get(String, int)(&namespace_ids, cstr_as_string((char *)group)); int ns = map_get(String, int)(&namespace_ids, cstr_as_string(group));
return ns ? ns : -1; return ns ? ns : -1;
} }

View File

@ -229,7 +229,7 @@ void ui_refresh(void)
} }
ui_ext[i] = ext_widgets[i]; ui_ext[i] = ext_widgets[i];
if (i < kUIGlobalCount) { if (i < kUIGlobalCount) {
ui_call_option_set(cstr_as_string((char *)ui_ext_names[i]), ui_call_option_set(cstr_as_string(ui_ext_names[i]),
BOOLEAN_OBJ(ext_widgets[i])); BOOLEAN_OBJ(ext_widgets[i]));
} }
} }
@ -451,8 +451,7 @@ void ui_set_ext_option(UI *ui, UIExtension ext, bool active)
return; return;
} }
if (ui_ext_names[ext][0] != '_' || active) { if (ui_ext_names[ext][0] != '_' || active) {
remote_ui_option_set(ui, cstr_as_string((char *)ui_ext_names[ext]), remote_ui_option_set(ui, cstr_as_string(ui_ext_names[ext]), BOOLEAN_OBJ(active));
BOOLEAN_OBJ(active));
} }
if (ext == kUITermColors) { if (ext == kUITermColors) {
ui_default_colors_set(); ui_default_colors_set();

View File

@ -796,7 +796,7 @@ void ui_ext_win_position(win_T *wp, bool validate)
wp->w_grid_alloc.zindex = wp->w_float_config.zindex; wp->w_grid_alloc.zindex = wp->w_float_config.zindex;
if (ui_has(kUIMultigrid)) { if (ui_has(kUIMultigrid)) {
String anchor = cstr_as_string((char *)float_anchor_str[c.anchor]); String anchor = cstr_as_string(float_anchor_str[c.anchor]);
if (!c.hide) { if (!c.hide) {
ui_call_win_float_pos(wp->w_grid_alloc.handle, wp->handle, anchor, ui_call_win_float_pos(wp->w_grid_alloc.handle, wp->handle, anchor,
grid->handle, row, col, c.focusable, grid->handle, row, col, c.focusable,