From f63f6e54fa911272b0984efed628f13c680d9d64 Mon Sep 17 00:00:00 2001 From: Fabian Homborg Date: Tue, 31 May 2016 23:14:03 +0200 Subject: [PATCH] Make string match -rnv work Fixes #3098. --- src/builtin_string.cpp | 19 ++++++++++--------- tests/string.in | 2 ++ tests/string.out | 1 + 3 files changed, 13 insertions(+), 9 deletions(-) diff --git a/src/builtin_string.cpp b/src/builtin_string.cpp index 3e03faf2e..ae97f7cde 100644 --- a/src/builtin_string.cpp +++ b/src/builtin_string.cpp @@ -450,15 +450,16 @@ class pcre2_matcher_t: public string_matcher_t const wchar_t *argv0; compiled_regex_t regex; - int report_match(const wchar_t *arg, int pcre2_rc) - { - // Return values: -1 = error, 0 = no match, 1 = match - if (pcre2_rc == PCRE2_ERROR_NOMATCH) - { - if (opts.invert_match && !opts.quiet) - { - streams.out.append(arg); - streams.out.push_back(L'\n'); + int report_match(const wchar_t *arg, int pcre2_rc) { + // Return values: -1 = error, 0 = no match, 1 = match. + if (pcre2_rc == PCRE2_ERROR_NOMATCH) { + if (opts.invert_match && !opts.quiet) { + if (opts.index) { + streams.out.append_format(L"1 %lu\n", wcslen(arg)); + } else { + streams.out.append(arg); + streams.out.push_back(L'\n'); + } } return opts.invert_match ? 1 : 0; diff --git a/tests/string.in b/tests/string.in index ac858618d..15feb4f55 100644 --- a/tests/string.in +++ b/tests/string.in @@ -88,3 +88,5 @@ string length 2>/dev/null; or echo "missing argument returns 0" string match -r -v "[dcantg].*" dog can cat diz; or echo "no regexp invert match" string match -v "???" dog can cat diz; or echo "no glob invert match" + +string match -rvn a bbb diff --git a/tests/string.out b/tests/string.out index bd3fff457..7afc267ff 100644 --- a/tests/string.out +++ b/tests/string.out @@ -62,3 +62,4 @@ invalid argument error missing argument returns 0 no regexp invert match no glob invert match +1 3