MCPcopy Create free account
hub / github.com/coreutils/coreutils / docolon

Function docolon

src/expr.c:565–637  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

563 PV is the VALUE for the rhs (the pattern). */
564
565static VALUE *
566docolon (VALUE *sv, VALUE *pv)
567{
568 tostring (sv);
569 tostring (pv);
570
571 struct re_registers re_regs;
572 re_regs.num_regs = 0;
573 re_regs.start = NULL;
574 re_regs.end = NULL;
575
576 struct re_pattern_buffer re_buffer;
577 char fastmap[UCHAR_MAX + 1];
578 re_buffer.buffer = NULL;
579 re_buffer.allocated = 0;
580 re_buffer.fastmap = fastmap;
581 re_buffer.translate = NULL;
582 re_syntax_options = (RE_SYNTAX_POSIX_BASIC & ~RE_CONTEXT_INVALID_DUP
583 & ~RE_NO_EMPTY_RANGES);
584 char const *errmsg = re_compile_pattern (pv->u.s, strlen (pv->u.s),
585 &re_buffer);
586 if (errmsg)
587 error (EXPR_INVALID, 0, "%s", (errmsg));
588 re_buffer.newline_anchor = 0;
589
590 VALUE *v;
591 regoff_t matchlen = re_match (&re_buffer, sv->u.s, strlen (sv->u.s), 0,
592 &re_regs);
593 if (0 <= matchlen)
594 {
595 /* Were \(...\) used? */
596 if (re_buffer.re_nsub > 0)
597 {
598 if (re_regs.end[1] < 0)
599 v = str_value ("");
600 else
601 {
602 sv->u.s[re_regs.end[1]] = '\0';
603 v = str_value (sv->u.s + re_regs.start[1]);
604 }
605 }
606 else
607 {
608 /* In multibyte locales, convert the matched offset (=number of bytes)
609 to the number of matched characters. */
610 size_t i = (MB_CUR_MAX == 1
611 ? matchlen
612 : mbs_offset_to_chars (sv->u.s, matchlen));
613 v = int_value (i);
614 }
615 }
616 else if (matchlen == -1)
617 {
618 /* Match failed -- return the right kind of null. */
619 if (re_buffer.re_nsub > 0)
620 v = str_value ("");
621 else
622 v = int_value (0);

Callers 2

eval6Function · 0.85
eval5Function · 0.85

Calls 4

tostringFunction · 0.85
str_valueFunction · 0.85
mbs_offset_to_charsFunction · 0.85
int_valueFunction · 0.85

Tested by

no test coverage detected