summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorLars Ingebrigtsen <larsi@gnus.org>2021-07-24 17:22:43 +0200
committerLars Ingebrigtsen <larsi@gnus.org>2021-07-24 17:22:43 +0200
commitb4543dfa9e72deeee607ffa9396a680c51a00968 (patch)
tree09493193d03f4961a8007c709f1b8a8598ac26d6 /src
parent8cd66a3170b4117d3cbcdce7a09837e3c2ea0e43 (diff)
downloademacs-b4543dfa9e72deeee607ffa9396a680c51a00968.tar.gz
Extend directory-append to take an arbitrary number of components
* doc/lispref/files.texi (Directory Names): Document it. * lisp/emacs-lisp/shortdoc.el (file-name): Add new example. * src/fileio.c (Fdirectory_append): Change the function to take an arbitrary number of components.
Diffstat (limited to 'src')
-rw-r--r--src/fileio.c116
1 files changed, 79 insertions, 37 deletions
diff --git a/src/fileio.c b/src/fileio.c
index 277da48315e..a4f08383776 100644
--- a/src/fileio.c
+++ b/src/fileio.c
@@ -749,48 +749,90 @@ For that reason, you should normally use `make-temp-file' instead. */)
empty_unibyte_string, Qnil);
}
-DEFUN ("directory-append", Fdirectory_append, Sdirectory_append, 2, 2, 0,
- doc: /* Return FILE (a string) appended to DIRECTORY (a string).
-DIRECTORY may or may not end with a slash -- the return value from
-this function will be the same. */)
- (Lisp_Object directory, Lisp_Object file)
+DEFUN ("directory-append", Fdirectory_append, Sdirectory_append, 1, MANY, 0,
+ doc: /* Append COMPONENTS to DIRECTORY and return the resulting string.
+COMPONENTS must be a list of strings. DIRECTORY or the non-final
+elements in COMPONENTS may or may not end with a slash -- if they don't
+end with a slash, a slash will be inserted before contatenating.
+usage: (record DIRECTORY &rest COMPONENTS) */)
+ (ptrdiff_t nargs, Lisp_Object *args)
{
- USE_SAFE_ALLOCA;
- char *p;
-
- CHECK_STRING (file);
- CHECK_STRING (directory);
+ ptrdiff_t chars = 0, bytes = 0, multibytes = 0;
+ Lisp_Object *elements = args;
+ Lisp_Object result;
+ ptrdiff_t i;
+
+ /* First go through the list to check the types and see whether
+ they're all of the same multibytedness. */
+ for (i = 0; i < nargs; i++)
+ {
+ Lisp_Object arg = args[i];
+ CHECK_STRING (arg);
+ if (SCHARS (arg) == 0)
+ xsignal1 (Qfile_error, build_string ("Empty file name"));
+ /* Multibyte and non-ASCII. */
+ if (STRING_MULTIBYTE (arg) && SCHARS (arg) != SBYTES (arg))
+ multibytes++;
+ /* We're not adding a slash to the final part. */
+ if (i == nargs - 1
+ || IS_DIRECTORY_SEP (*(SSDATA (arg) + SBYTES (arg) - 1)))
+ {
+ bytes += SBYTES (arg);
+ chars += SCHARS (arg);
+ }
+ else
+ {
+ bytes += SBYTES (arg) + 1;
+ chars += SCHARS (arg) + 1;
+ }
+ }
- if (SCHARS (file) == 0)
- xsignal1 (Qfile_error, build_string ("Empty file name"));
+ /* Convert if needed. */
+ if (multibytes != 0 && multibytes != nargs)
+ {
+ elements = xmalloc (nargs * sizeof *elements);
+ bytes = 0;
+ for (i = 0; i < nargs; i++)
+ {
+ Lisp_Object arg = args[i];
+ if (STRING_MULTIBYTE (arg))
+ elements[i] = arg;
+ else
+ elements[i] = make_multibyte_string (SSDATA (arg), SCHARS (arg),
+ SCHARS (arg));
+ arg = elements[i];
+ /* We have to recompute the number of bytes. */
+ if (i == nargs - 1
+ || IS_DIRECTORY_SEP (*(SSDATA (arg) + SBYTES (arg) - 1)))
+ bytes += SBYTES (arg);
+ else
+ bytes += SBYTES (arg) + 1;
+ }
+ }
- if (SCHARS (directory) == 0)
- return file;
+ /* Allocate an empty string. */
+ if (multibytes == 0)
+ result = make_uninit_string (chars);
+ else
+ result = make_uninit_multibyte_string (chars, bytes);
+ /* Null-terminate the string. */
+ *(SSDATA (result) + SBYTES (result)) = 0;
- /* Make the strings the same multibytedness. */
- if (STRING_MULTIBYTE (file) != STRING_MULTIBYTE (directory))
+ /* Copy over the data. */
+ char *p = SSDATA (result);
+ for (i = 0; i < nargs; i++)
{
- if (STRING_MULTIBYTE (file))
- directory = make_multibyte_string (SSDATA (directory),
- SCHARS (directory),
- SCHARS (directory));
- else
- file = make_multibyte_string (SSDATA (file),
- SCHARS (file),
- SCHARS (file));
- }
-
- /* Allocate enough extra space in case we need to put a slash in
- there. */
- p = SAFE_ALLOCA (SBYTES (file) + SBYTES (directory) + 2);
- ptrdiff_t offset = SBYTES (directory);
- memcpy (p, SSDATA (directory), offset);
- if (! IS_DIRECTORY_SEP (p[offset - 1]))
- p[offset++] = DIRECTORY_SEP;
- memcpy (p + offset, SSDATA (file), SBYTES (file));
- p[offset + SBYTES (file)] = 0;
- Lisp_Object result = build_string (p);
- SAFE_FREE ();
+ Lisp_Object arg = elements[i];
+ memcpy (p, SSDATA (arg), SBYTES (arg));
+ p += SBYTES (arg);
+ /* The last element shouldn't have a slash added at the end. */
+ if (i < nargs -1 && !IS_DIRECTORY_SEP (*(p - 1)))
+ *p++ = DIRECTORY_SEP;
+ }
+
+ if (multibytes != 0 && multibytes != nargs)
+ xfree (elements);
+
return result;
}