• R/O
  • SSH
  • HTTPS

yash: Commit


Commit MetaInfo

Revision4155 (tree)
Time2020-12-05 21:49:53
Authormagicant

Log Message

Don't quote ordinary characters in regex (#39094)

Change Summary

Incremental Difference

--- yash/trunk/builtins/test.c (revision 4154)
+++ yash/trunk/builtins/test.c (revision 4155)
@@ -95,6 +95,8 @@
9595 static bool quote_removal_and_regex_matching(
9696 const wchar_t *lhs, const wchar_t *rhsvalue, const char *rhscc)
9797 __attribute__((nonnull));
98+static wchar_t *quote_removal_for_regex(const wchar_t *s, const char *cc)
99+ __attribute__((nonnull,malloc,warn_unused_result));
98100 #endif
99101
100102
@@ -857,12 +859,31 @@
857859 bool quote_removal_and_regex_matching(
858860 const wchar_t *lhs, const wchar_t *rhsvalue, const char *rhscc)
859861 {
860- wchar_t *rhs = quote_removal(rhsvalue, rhscc, ES_QUOTED);
862+ wchar_t *rhs = quote_removal_for_regex(rhsvalue, rhscc);
861863 bool result = match_regex(lhs, rhs);
862864 free(rhs);
863865 return result;
864866 }
865867
868+/* Removes all quotation marks in the input string `s' and add backslash escapes
869+ * to quoted characters that would otherwise be treated specially when parsed as
870+ * an extended regular expression pattern. The result is a newly malloced
871+ * string. */
872+wchar_t *quote_removal_for_regex(const wchar_t *s, const char *cc)
873+{
874+ xwcsbuf_T result;
875+ wb_initwithmax(&result, mul(wcslen(s), 2));
876+ for (size_t i = 0; s[i] != L'\0'; i++) {
877+ if (cc[i] & CC_QUOTATION)
878+ continue;
879+ if (cc[i] & CC_QUOTED)
880+ if (wcschr(L"^.[$()|*+?{\\", s[i]) != NULL)
881+ wb_wccat(&result, L'\\');
882+ wb_wccat(&result, s[i]);
883+ }
884+ return wb_towcs(&result);
885+}
886+
866887 #endif /* YASH_ENABLE_DOUBLE_BRACKET */
867888
868889
Show on old repository browser