sf_sed(3)

NAME
sed_compile, sed_exec, sed_free - string editor

SYNOPSIS
#include <strfunc.h>

sed_t *
sed_compile(char *expr);
char *
sed_exec(sed_t *se, char *string);
svect *
sed_results(sed_t *se);
void
sed_free(sed_t *se);

DESCRIPTION
These routines implement a subset of sed(1) or Perl's s///, y/// and // functionality.

You must compule your expression with sed_compile() in order to evaluate it later. Once compiled, it can be evaluated multiple times. See the EXPRESSIONS block to know about expressions semantics.

sed_free() used to destroy the compiled structure and free the allocated memory.

sed_exec() takes the source string and transforms it according to the compiled rules. Resulting string stored in the internal buffer within the specified sed_t structure.

sed_results() May be invoked multiple times after sed_exec() to obtain last match results. An 'r' flag should be specified within the expression string.

EXPRESSIONS
Currently, this library supports two types of string transformations and one type of string match.

Substitutions

Expressions of this type are defined in the following BNF:

    <delim>	:=	'/' | <other_character>
    <regex>	:=	<regular_expression, re_format(7)>
    <to>	:=	<string>
    <flags>	:=	*( 'g' | 'i' | 'e' | 'r' | 'm' | 'n' )
    <expr>	:= 	s <delim> <regex> <delim> <to> <delim> <flags>

Refer to sed(1) manual page to know other details.

Table lookup

    <delim>	:=	'/' | <other_character>
    <flags>	:=	*( 'i' )
    <expr>	:= 	y <delim> <string> <delim> <string> <delim> <flags>

String match

    <delim>	:=	'/' | <other_character>
    <flags>	:=	*( 'i' | 'r' | 'm' | 'n' )
    <reply>	:=	<string>
    <expr>	:= 	<delim> <string> <delim> [ <reply> <delim>] <flags>

In the last case, if string does not match, sed_exec() will return a NULL pointer, <reply> otherwise. s/// and y/// functions will never return a NULL pointer.

Flags are common to those transformations.

'i' case-insensitive matches.

'e' compile in extended mode (REG_EXTENDED).

'g' Make the substitution for all non-overlapping matches of the regular expression, not just the first one.

'r' Remember last match results to allow use of sed_results().

'm' Compile for newline-sensitive matching (REG_NEWLINE).

'n' Don't include zero regexec(3) match (a whole substring) into results list.

EXAMPLE

    void main() {
    	sed_t *se1;
    	sed_t *se2;
    	sed_t *se3;
    	char *r1, *r2, r3;
    	/* Compile expressions */
    	se1 = sed_compile("s/(tree) (apple)/\\2 \\1/igr");
    	se2 = sed_compile("y/abc/AbC/i");
    	se3 = sed_compile("/apple/i");
    	r1 = sed_exec(se1, "Tree Apple");
    	r2 = sed_exec(se2, "abcabc");
    	r3 = sed_exec(se3, "another apple tree");
    	/*
    	** This will produce:
    	** "Apple Tree\nAbCAbC\n1\n"
    	*/
    	printf("%s\n%s\n%d\n", r1, r2, r3?1:0);
    	/*
    	** This will produce:
    	** "[Tree Apple], [Tree], [Apple]\n"
    	*/
    	printf("[%s]\n", sjoin(sed_results(se1), "], ["));
    	/* Free the resources */
    	sed_free(se1);
    	sed_free(se2);
    	sed_free(se3);
    };

SEE ALSO
strfunc(3).

AUTHORS
Lev Walkin <vlm@spelio.net.ru>