• R/O
  • SSH
  • HTTPS

yash: Commit


Commit MetaInfo

Revision4174 (tree)
Time2021-10-10 22:25:43
Authormagicant

Log Message

Don't remove delimited empty field (#39286)

Change Summary

Incremental Difference

--- yash/trunk/NEWS (revision 4173)
+++ yash/trunk/NEWS (revision 4174)
@@ -10,6 +10,11 @@
1010 ----------------------------------------------------------------------
1111 Yash 2.52
1212
13+ = Unquoted multiple empty fields resulting from a single word
14+ expansion are now removed. For example, the brace expansion {,}
15+ now expands to no fields rather than two empty fields.
16+ * In word expansion, if a field consists of a single non-whitespace
17+ IFS character, it results in an empty field rather than no field.
1318 * "typeset -fp" now correctly distinguishes "<<-FOO" and "<< -FOO".
1419 * Fixed possible memory leak caused by improper use of the realloc
1520 function.
--- yash/trunk/expand.c (revision 4173)
+++ yash/trunk/expand.c (revision 4174)
@@ -220,15 +220,6 @@
220220 &expand.valuelist, &expand.cclist);
221221 assert(expand.valuelist.length == expand.cclist.length);
222222
223- /* empty field removal */
224- if (expand.valuelist.length == 1) {
225- const wchar_t *field = expand.valuelist.contents[0];
226- if (field[0] == L'\0') {
227- pl_clear(&expand.valuelist, free);
228- pl_clear(&expand.cclist, free);
229- }
230- }
231-
232223 /* pathname expansion (and quote removal) */
233224 glob_all(&expand, list);
234225
@@ -919,10 +910,18 @@
919910 /* create `e.cclist' contents */
920911 charcategory_T cc = CC_SOFT_EXPANSION | (indq * CC_QUOTED);
921912 for (size_t i = 0; i < e.valuelist.length; i++) {
922- size_t n = wcslen(e.valuelist.contents[i]);
913+ xwcsbuf_T sbuf;
914+ wb_initwith(&sbuf, e.valuelist.contents[i]);
915+ size_t n = sbuf.length;
923916 xstrbuf_T ccbuf;
924- sb_initwithmax(&ccbuf, n);
917+ sb_initwithmax(&ccbuf, n + 1);
925918 sb_ccat_repeat(&ccbuf, cc, n);
919+ if (indq && e.valuelist.length > 1) {
920+ // keep the field from empty field removal by adding a dummy quote
921+ wb_wccat(&sbuf, L'"');
922+ sb_ccat(&ccbuf, cc | CC_QUOTATION);
923+ }
924+ e.valuelist.contents[i] = wb_towcs(&sbuf);
926925 pl_add(&e.cclist, sb_tostr(&ccbuf));
927926 }
928927
@@ -1590,8 +1589,8 @@
15901589 * The return value is a pointer to the end of the input string (but before
15911590 * trailing IFS whitespaces). */
15921591 /* Split examples (assuming `ifs' = L" -" and `shopt_emptylastfield' is true)
1593- * "" -> ""
1594- * " " -> ""
1592+ * "" -> (nothing)
1593+ * " " -> (nothing)
15951594 * " abc 123 " -> "abc" "123"
15961595 * " abc 123 " -> "abc" "123"
15971596 * "-abc-123-" -> "" "abc" "123" ""
@@ -1642,11 +1641,12 @@
16421641
16431642 /* remove the empty last field */
16441643 size_t newlen = dest->length;
1645- if (!shopt_emptylastfield && newlen - oldlen >= 2 * 2 &&
1646- dest->contents[newlen - 2] == dest->contents[newlen - 1])
1647- pl_truncate(dest, newlen - 2);
1644+ assert(newlen - oldlen >= 2);
1645+ if (!shopt_emptylastfield || newlen - oldlen == 2)
1646+ if (dest->contents[newlen - 2] == dest->contents[newlen - 1])
1647+ pl_truncate(dest, newlen - 2);
16481648
1649- assert(dest->length - oldlen >= 2);
1649+ assert(dest->length >= oldlen);
16501650 return (wchar_t *) &s[ifswhitestartindex];
16511651 }
16521652
Show on old repository browser