SILC_STR_REGEX

NAME

    #define SILC_STR_REGEX() ...

DESCRIPTION

Regular expression matching within the buffer.

     Formatting:    SILC_STR_REGEX(char *regex, SilcBufferRegexFlags flags)
     Unformatting:  SILC_STR_REGEX(char *regex, SilcBufferRegexFlags flags)

SILC_STR_REGEX can be used to do regular expression matching within the SilcBuffer. When the string in the buffer matches the regular expression the position of the buffer is advanced to the position of the first match (rest of the buffer remains intact). If the regular expression does not match it is skipped, unless the flags specify otherwise. If flags are not needed they can be set to 0.

In addition of matching regular expressions it can be used in a Stream Editor (sed) and Awk like fashion. The regular expression can be matched and then edited by any of the SILC_STR_* macros. The flags can be used to perform complex operations on the data. Some sed features that cannot be directly done with the flags can be done with SILC_STR_FUNC and other macros (the SILC_STR_FUNC could do anything after the match).

The SILC_STR_REGEX itself is used as an opening of a block of encoding macros and must be closed with SILC_STR_END. This means that for each SILC_STR_REGEX there must be one SILC_STR_END. See examples for more information.

The SILC_STR_REGEX can be used in buffer unformatting also to do string matching and parsing, but not editing, except with SILC_STR_FUNC macro, which can do anything caller wants.

Typically the following encoding macros are used with SILC_STR_REGEX: SILC_STR_REPLACE, SILC_STR_STRING and SILC_STR_FUNC. If you use SILC_STR_REPLACE always set SILC_STR_REGEX_INCLUSIVE.

EXAMPLE

    // sed 's/foo/bar/', replace first foo with bar
    silc_buffer_format(buffer,
                       SILC_STR_REGEX("foo", 0),
                         SILC_STR_STRING("bar"),
                       SILC_STR_END, SILC_STR_END);

    // sed 's/foo/barbar/g', replace all foo's with barbar, without
    // overwriting any data in the buffer, but appending it.  The match
    // must be inclusive to make appending work.
    silc_buffer_format(buffer,
                       SILC_STR_REGEX("foo", SILC_STR_REGEX_ALL |
                                             SILC_STR_REGEX_INCLUSIVE),
                         SILC_STR_REPLACE("barbar", 5),
                       SILC_STR_END, SILC_STR_END);

    // sed 's/foo/B/', replace foo with B, deleting rest of the match from
    // the buffer.  The match must be inclusive to make deleting work.
    silc_buffer_format(buffer,
                       SILC_STR_REGEX("foo", SILC_STR_REGEX_ALL |
                                             SILC_STR_REGEX_INCLUSIVE),
                         SILC_STR_REPLACE("B", 1),
                       SILC_STR_END, SILC_STR_END);

    // sed '/baz/s/foo/bar/g, replace all foo's with bar on lines with baz
    silc_buffer_format(buffer,
                       SILC_STR_REGEX("baz", SILC_STR_REGEX_NL),
                         SILC_STR_REGEX("foo", SILC_STR_REGEX_ALL),
                           SILC_STR_STRING("bar"),
                         SILC_STR_END,
                       SILC_STR_END, SILC_STR_END);

    // Print all lines that start with 'R'
    int print(SilcStack stack, SilcBuffer buf, void *value, void *context)
    {
      return fwrite(silc_buffer_data(buf), 1, silc_buffer_len(buf), stdout);
    }

    silc_buffer_unformat(buffer,
                         SILC_STR_REGEX("^R", SILC_STR_REGEX_NL),
                           SILC_STR_FUNC(print, NULL, NULL),
                         SILC_STR_END, SILC_STR_END);