Merge branch 'xstrjoin3'

This commit is contained in:
Craig Barnes 2024-08-03 10:42:37 +01:00
commit b27841e1b5
7 changed files with 44 additions and 31 deletions

View file

@ -356,9 +356,9 @@ open_config(void)
/* First, check XDG_CONFIG_HOME (or .config, if unset) */ /* First, check XDG_CONFIG_HOME (or .config, if unset) */
if (xdg_config_home != NULL && xdg_config_home[0] != '\0') if (xdg_config_home != NULL && xdg_config_home[0] != '\0')
path = xstrjoin(xdg_config_home, "/foot/foot.ini", 0); path = xstrjoin(xdg_config_home, "/foot/foot.ini");
else if (home_dir != NULL) else if (home_dir != NULL)
path = xstrjoin(home_dir, "/.config/foot/foot.ini", 0); path = xstrjoin(home_dir, "/.config/foot/foot.ini");
if (path != NULL) { if (path != NULL) {
LOG_DBG("checking for %s", path); LOG_DBG("checking for %s", path);
@ -383,7 +383,7 @@ open_config(void)
conf_dir = strtok(NULL, ":")) conf_dir = strtok(NULL, ":"))
{ {
free(path); free(path);
path = xstrjoin(conf_dir, "/foot/foot.ini", 0); path = xstrjoin(conf_dir, "/foot/foot.ini");
LOG_DBG("checking for %s", path); LOG_DBG("checking for %s", path);
int fd = open(path, O_RDONLY | O_CLOEXEC); int fd = open(path, O_RDONLY | O_CLOEXEC);
@ -843,7 +843,7 @@ parse_section_main(struct context *ctx)
return false; return false;
} }
_include_path = xasprintf("%s/%s", home_dir, value + 2); _include_path = xstrjoin3(home_dir, "/", value + 2);
include_path = _include_path; include_path = _include_path;
} else } else
include_path = value; include_path = value;
@ -2931,7 +2931,7 @@ get_server_socket_path(void)
const char *wayland_display = getenv("WAYLAND_DISPLAY"); const char *wayland_display = getenv("WAYLAND_DISPLAY");
if (wayland_display == NULL) { if (wayland_display == NULL) {
return xstrjoin(xdg_runtime, "/foot.sock", 0); return xstrjoin(xdg_runtime, "/foot.sock");
} }
return xasprintf("%s/foot-%s.sock", xdg_runtime, wayland_display); return xasprintf("%s/foot-%s.sock", xdg_runtime, wayland_display);

18
main.c
View file

@ -258,7 +258,7 @@ main(int argc, char *const *argv)
break; break;
case 't': case 't':
tll_push_back(overrides, xstrjoin("term=", optarg, 0)); tll_push_back(overrides, xstrjoin("term=", optarg));
break; break;
case 'L': case 'L':
@ -266,11 +266,11 @@ main(int argc, char *const *argv)
break; break;
case 'T': case 'T':
tll_push_back(overrides, xstrjoin("title=", optarg, 0)); tll_push_back(overrides, xstrjoin("title=", optarg));
break; break;
case 'a': case 'a':
tll_push_back(overrides, xstrjoin("app-id=", optarg, 0)); tll_push_back(overrides, xstrjoin("app-id=", optarg));
break; break;
case 'D': { case 'D': {
@ -284,7 +284,7 @@ main(int argc, char *const *argv)
} }
case 'f': { case 'f': {
char *font_override = xstrjoin("font=", optarg, 0); char *font_override = xstrjoin("font=", optarg);
tll_push_back(overrides, font_override); tll_push_back(overrides, font_override);
break; break;
} }
@ -658,15 +658,19 @@ out:
UNITTEST UNITTEST
{ {
char *s = xstrjoin("foo", "bar", 0); char *s = xstrjoin("foo", "bar");
xassert(streq(s, "foobar")); xassert(streq(s, "foobar"));
free(s); free(s);
s = xstrjoin("foo", "bar", ' '); s = xstrjoin3("foo", " ", "bar");
xassert(streq(s, "foo bar")); xassert(streq(s, "foo bar"));
free(s); free(s);
s = xstrjoin("foo", "bar", ','); s = xstrjoin3("foo", ",", "bar");
xassert(streq(s, "foo,bar")); xassert(streq(s, "foo,bar"));
free(s); free(s);
s = xstrjoin3("foo", "bar", "baz");
xassert(streq(s, "foobarbaz"));
free(s);
} }

View file

@ -77,7 +77,7 @@ write_icon_file(const void *data, size_t data_sz, int *fd, char **filename,
LOG_DBG("wrote icon data to %s", name); LOG_DBG("wrote icon data to %s", name);
*filename = xstrdup(name); *filename = xstrdup(name);
*symbolic_name = xasprintf("file://%s", *filename); *symbolic_name = xstrjoin("file://", *filename);
return true; return true;
} }

8
osc.c
View file

@ -797,7 +797,7 @@ kitty_notification(struct terminal *term, char *string)
else { else {
/* Append, comma separated */ /* Append, comma separated */
char *old_category = category; char *old_category = category;
category = xstrjoin(old_category, decoded, ','); category = xstrjoin3(old_category, ",", decoded);
free(decoded); free(decoded);
free(old_category); free(old_category);
} }
@ -940,7 +940,7 @@ kitty_notification(struct terminal *term, char *string)
category = NULL; /* Prevent double free */ category = NULL; /* Prevent double free */
} else { } else {
/* Append, comma separated */ /* Append, comma separated */
char *new_category = xstrjoin(notif->category, category, ','); char *new_category = xstrjoin3(notif->category, ",", category);
free(notif->category); free(notif->category);
notif->category = new_category; notif->category = new_category;
} }
@ -959,7 +959,7 @@ kitty_notification(struct terminal *term, char *string)
payload = NULL; payload = NULL;
} else { } else {
char *old = *ptr; char *old = *ptr;
*ptr = xstrjoin(old, payload, 0); *ptr = xstrjoin(old, payload);
free(old); free(old);
} }
break; break;
@ -1032,7 +1032,7 @@ kitty_notification(struct terminal *term, char *string)
alive_ids = xstrdup(item_id); alive_ids = xstrdup(item_id);
else { else {
char *old_alive_ids = alive_ids; char *old_alive_ids = alive_ids;
alive_ids = xstrjoin(old_alive_ids, item_id, ','); alive_ids = xstrjoin3(old_alive_ids, ",", item_id);
free(old_alive_ids); free(old_alive_ids);
} }
} }

View file

@ -55,7 +55,7 @@ find_file_in_path(const char *file)
path != NULL; path != NULL;
path = strtok(NULL, ":")) path = strtok(NULL, ":"))
{ {
char *full = xasprintf("%s/%s", path, file); char *full = xstrjoin3(path, "/", file);
if (access(full, F_OK) == 0) { if (access(full, F_OK) == 0) {
free(path_list); free(path_list);
return full; return full;
@ -329,7 +329,7 @@ add_to_env(struct environ *env, const char *name, const char *value)
if (env->envp == NULL) if (env->envp == NULL)
setenv(name, value, 1); setenv(name, value, 1);
else { else {
char *e = xasprintf("%s=%s", name, value); char *e = xstrjoin3(name, "=", value);
/* Search for existing variable. If found, replace it with the /* Search for existing variable. If found, replace it with the
new value */ new value */

View file

@ -994,7 +994,7 @@ reload_fonts(struct terminal *term, bool resize_grid)
snprintf(size, sizeof(size), ":size=%.2f", snprintf(size, sizeof(size), ":size=%.2f",
term->font_sizes[i][j].pt_size * scale); term->font_sizes[i][j].pt_size * scale);
names[i][j] = xstrjoin(font->pattern, size, 0); names[i][j] = xstrjoin(font->pattern, size);
} }
} }
@ -1021,9 +1021,9 @@ reload_fonts(struct terminal *term, bool resize_grid)
char *attrs[4] = { char *attrs[4] = {
[0] = dpi, /* Takes ownership */ [0] = dpi, /* Takes ownership */
[1] = xstrjoin(dpi, !custom_bold ? ":weight=bold" : "", 0), [1] = xstrjoin(dpi, !custom_bold ? ":weight=bold" : ""),
[2] = xstrjoin(dpi, !custom_italic ? ":slant=italic" : "", 0), [2] = xstrjoin(dpi, !custom_italic ? ":slant=italic" : ""),
[3] = xstrjoin(dpi, !custom_bold_italic ? ":weight=bold:slant=italic" : "", 0), [3] = xstrjoin(dpi, !custom_bold_italic ? ":weight=bold:slant=italic" : ""),
}; };
struct fcft_font *fonts[4]; struct fcft_font *fonts[4];

View file

@ -25,16 +25,25 @@ xmemdup(const void *ptr, size_t size)
} }
static inline char * static inline char *
xstrjoin(const char *s1, const char *s2, char delim) xstrjoin(const char *s1, const char *s2)
{ {
size_t n1 = strlen(s1); size_t n1 = strlen(s1);
size_t n2 = delim > 0 ? 1 : 0; size_t n2 = strlen(s2);
size_t n3 = strlen(s2); char *joined = xmalloc(n1 + n2 + 1);
char *joined = xmalloc(n1 + n2 + n3 + 1);
memcpy(joined, s1, n1); memcpy(joined, s1, n1);
if (delim > 0) memcpy(joined + n1, s2, n2 + 1);
joined[n1] = delim; return joined;
memcpy(joined + n1 + n2, s2, n3 + 1); }
static inline char *
xstrjoin3(const char *s1, const char *s2, const char *s3)
{
size_t n1 = strlen(s1);
size_t n2 = strlen(s2);
size_t n3 = strlen(s3);
char *joined = xmalloc(n1 + n2 + n3 + 1);
memcpy(joined, s1, n1);
memcpy(joined + n1, s2, n2);
memcpy(joined + n1 + n2, s3, n3 + 1);
return joined; return joined;
} }