File: stringstrip.c

package info (click to toggle)
yodl 4.04.00-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,720 kB
  • sloc: ansic: 7,803; perl: 683; cpp: 570; sh: 411; xml: 190; makefile: 164
file content (43 lines) | stat: -rw-r--r-- 907 bytes parent folder | download | duplicates (10)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include "string.ih"

static bool     s_initialized;
static regex_t  s_strip;
static regmatch_t pmatch[2];

char *string_strip(char **str)
{
    if (!s_initialized)
    {
        s_initialized = true;
        if
        (
            regcomp
            (
                &s_strip,
                    "^[[:space:]]*"
                "("
                    "([^[:space:]].*[^[:space:]])"
                    "|"
                    "[^[:space:]]*"
                ")"
                    "[[:space:]]*$"
                ,
                REG_EXTENDED | REG_NEWLINE
            )
        )
        {
            message_show(MSG_EMERG);
            message("string_strip() regcomp() failed");
        }
    }

    if (regexec(&s_strip, *str, 2, pmatch, 0))      /* no match */
        **str = 0;
    else
    {
        (*str)[pmatch[1].rm_eo] = 0;
        *str += pmatch[1].rm_so;
    }

    return *str;
}