env_iter: Learn delim parameter.

This commit is contained in:
Justin M. Keyes
2017-05-15 03:54:52 +02:00
parent 5bda5c5bf2
commit cc5a42a774
4 changed files with 24 additions and 20 deletions

View File

@@ -1685,7 +1685,7 @@ static bool do_user_initialization(void)
do { do {
const char *dir; const char *dir;
size_t dir_len; size_t dir_len;
iter = vim_colon_env_iter(config_dirs, iter, &dir, &dir_len); iter = vim_env_iter(':', config_dirs, iter, &dir, &dir_len);
if (dir == NULL || dir_len == 0) { if (dir == NULL || dir_len == 0) {
break; break;
} }

View File

@@ -341,7 +341,7 @@ static inline size_t compute_double_colon_len(const char *const val,
do { do {
size_t dir_len; size_t dir_len;
const char *dir; const char *dir;
iter = vim_colon_env_iter(val, iter, &dir, &dir_len); iter = vim_env_iter(':', val, iter, &dir, &dir_len);
if (dir != NULL && dir_len > 0) { if (dir != NULL && dir_len > 0) {
ret += ((dir_len + memcnt(dir, ',', dir_len) + common_suf_len ret += ((dir_len + memcnt(dir, ',', dir_len) + common_suf_len
+ !after_pathsep(dir, dir + dir_len)) * 2 + !after_pathsep(dir, dir + dir_len)) * 2
@@ -385,8 +385,8 @@ static inline char *add_colon_dirs(char *dest, const char *const val,
do { do {
size_t dir_len; size_t dir_len;
const char *dir; const char *dir;
iter = (forward ? vim_colon_env_iter : vim_colon_env_iter_rev)( iter = (forward ? vim_env_iter : vim_env_iter_rev)(':', val, iter, &dir,
val, iter, &dir, &dir_len); &dir_len);
if (dir != NULL && dir_len > 0) { if (dir != NULL && dir_len > 0) {
dest = strcpy_comma_escaped(dest, dir, dir_len); dest = strcpy_comma_escaped(dest, dir, dir_len);
if (!after_pathsep(dest - 1, dest)) { if (!after_pathsep(dest - 1, dest)) {

View File

@@ -521,10 +521,11 @@ static char *remove_tail(char *path, char *pend, char *dirname)
return pend; return pend;
} }
/// Iterate over colon-separated list /// Iterate over a delimited list.
/// ///
/// @note Environment variables must not be modified during iteration. /// @note Environment variables must not be modified during iteration.
/// ///
/// @param[in] delim Delimiter character.
/// @param[in] val Value of the environment variable to iterate over. /// @param[in] val Value of the environment variable to iterate over.
/// @param[in] iter Pointer used for iteration. Must be NULL on first /// @param[in] iter Pointer used for iteration. Must be NULL on first
/// iteration. /// iteration.
@@ -533,18 +534,19 @@ static char *remove_tail(char *path, char *pend, char *dirname)
/// @param[out] len Location where current directory length should be saved. /// @param[out] len Location where current directory length should be saved.
/// ///
/// @return Next iter argument value or NULL when iteration should stop. /// @return Next iter argument value or NULL when iteration should stop.
const void *vim_colon_env_iter(const char *const val, const void *vim_env_iter(const char delim,
const char *const val,
const void *const iter, const void *const iter,
const char **const dir, const char **const dir,
size_t *const len) size_t *const len)
FUNC_ATTR_NONNULL_ARG(1, 3, 4) FUNC_ATTR_WARN_UNUSED_RESULT FUNC_ATTR_NONNULL_ARG(2, 4, 5) FUNC_ATTR_WARN_UNUSED_RESULT
{ {
const char *varval = (const char *) iter; const char *varval = (const char *) iter;
if (varval == NULL) { if (varval == NULL) {
varval = val; varval = val;
} }
*dir = varval; *dir = varval;
const char *const dirend = strchr(varval, ':'); const char *const dirend = strchr(varval, delim);
if (dirend == NULL) { if (dirend == NULL) {
*len = strlen(varval); *len = strlen(varval);
return NULL; return NULL;
@@ -554,10 +556,11 @@ const void *vim_colon_env_iter(const char *const val,
} }
} }
/// Iterate over colon-separated list in reverse order /// Iterate over a delimited list in reverse order.
/// ///
/// @note Environment variables must not be modified during iteration. /// @note Environment variables must not be modified during iteration.
/// ///
/// @param[in] delim Delimiter character.
/// @param[in] val Value of the environment variable to iterate over. /// @param[in] val Value of the environment variable to iterate over.
/// @param[in] iter Pointer used for iteration. Must be NULL on first /// @param[in] iter Pointer used for iteration. Must be NULL on first
/// iteration. /// iteration.
@@ -566,18 +569,19 @@ const void *vim_colon_env_iter(const char *const val,
/// @param[out] len Location where current directory length should be saved. /// @param[out] len Location where current directory length should be saved.
/// ///
/// @return Next iter argument value or NULL when iteration should stop. /// @return Next iter argument value or NULL when iteration should stop.
const void *vim_colon_env_iter_rev(const char *const val, const void *vim_env_iter_rev(const char delim,
const char *const val,
const void *const iter, const void *const iter,
const char **const dir, const char **const dir,
size_t *const len) size_t *const len)
FUNC_ATTR_NONNULL_ARG(1, 3, 4) FUNC_ATTR_WARN_UNUSED_RESULT FUNC_ATTR_NONNULL_ARG(2, 4, 5) FUNC_ATTR_WARN_UNUSED_RESULT
{ {
const char *varend = (const char *) iter; const char *varend = (const char *) iter;
if (varend == NULL) { if (varend == NULL) {
varend = val + strlen(val) - 1; varend = val + strlen(val) - 1;
} }
const size_t varlen = (size_t)(varend - val) + 1; const size_t varlen = (size_t)(varend - val) + 1;
const char *const colon = xmemrchr(val, ':', varlen); const char *const colon = xmemrchr(val, (uint8_t)delim, varlen);
if (colon == NULL) { if (colon == NULL) {
*len = varlen; *len = varlen;
*dir = val; *dir = val;

View File

@@ -2265,7 +2265,7 @@ void path_guess_exepath(const char *argv0, char *buf, size_t bufsize)
do { do {
const char *dir; const char *dir;
size_t dir_len; size_t dir_len;
iter = vim_colon_env_iter(path, iter, &dir, &dir_len); iter = vim_env_iter(ENV_SEPCHAR, path, iter, &dir, &dir_len);
if (dir == NULL || dir_len == 0) { if (dir == NULL || dir_len == 0) {
break; break;
} }