summaryrefslogtreecommitdiff
path: root/src/regexp.c
diff options
context:
space:
mode:
authorBram Moolenaar <Bram@vim.org>2014-04-02 19:00:58 +0200
committerBram Moolenaar <Bram@vim.org>2014-04-02 19:00:58 +0200
commit41571769c9a236fd07b333a5eb98c461636b466c (patch)
treeec1ea68205a7a81587973c381cfe55f4188924d1 /src/regexp.c
parentfe5aab63feb2b03656700d3738d46a19e99edde0 (diff)
downloadvim-git-41571769c9a236fd07b333a5eb98c461636b466c.tar.gz
updated for version 7.4.241v7.4.241
Problem: The string returned by submatch() does not distinguish between a NL from a line break and a NL that stands for a NUL character. Solution: Add a second argument to return a list. (ZyX)
Diffstat (limited to 'src/regexp.c')
-rw-r--r--src/regexp.c79
1 files changed, 79 insertions, 0 deletions
diff --git a/src/regexp.c b/src/regexp.c
index 32a51884b..0a590f66d 100644
--- a/src/regexp.c
+++ b/src/regexp.c
@@ -7897,6 +7897,85 @@ reg_submatch(no)
return retval;
}
+
+/*
+ * Used for the submatch() function with the optional non-zero argument: get
+ * the list of strings from the n'th submatch in allocated memory with NULs
+ * represented in NLs.
+ * Returns a list of allocated strings. Returns NULL when not in a ":s"
+ * command, for a non-existing submatch and for any error.
+ */
+ list_T *
+reg_submatch_list(no)
+ int no;
+{
+ char_u *s;
+ linenr_T slnum;
+ linenr_T elnum;
+ colnr_T scol;
+ colnr_T ecol;
+ int i;
+ list_T *list;
+ int error = FALSE;
+
+ if (!can_f_submatch || no < 0)
+ return NULL;
+
+ if (submatch_match == NULL)
+ {
+ slnum = submatch_mmatch->startpos[no].lnum;
+ elnum = submatch_mmatch->endpos[no].lnum;
+ if (slnum < 0 || elnum < 0)
+ return NULL;
+
+ scol = submatch_mmatch->startpos[no].col;
+ ecol = submatch_mmatch->endpos[no].col;
+
+ list = list_alloc();
+ if (list == NULL)
+ return NULL;
+
+ s = reg_getline_submatch(slnum) + scol;
+ if (slnum == elnum)
+ {
+ if (list_append_string(list, s, ecol - scol) == FAIL)
+ error = TRUE;
+ }
+ else
+ {
+ if (list_append_string(list, s, -1) == FAIL)
+ error = TRUE;
+ for (i = 1; i < elnum - slnum; i++)
+ {
+ s = reg_getline_submatch(slnum + i);
+ if (list_append_string(list, s, -1) == FAIL)
+ error = TRUE;
+ }
+ s = reg_getline_submatch(elnum);
+ if (list_append_string(list, s, ecol) == FAIL)
+ error = TRUE;
+ }
+ }
+ else
+ {
+ s = submatch_match->startp[no];
+ if (s == NULL || submatch_match->endp[no] == NULL)
+ return NULL;
+ list = list_alloc();
+ if (list == NULL)
+ return NULL;
+ if (list_append_string(list, s,
+ (int)(submatch_match->endp[no] - s)) == FAIL)
+ error = TRUE;
+ }
+
+ if (error)
+ {
+ list_free(list, TRUE);
+ return NULL;
+ }
+ return list;
+}
#endif
static regengine_T bt_regengine =