How to safety parse tab-delimited string ?

ⅰ亾dé卋堺 提交于 2019-12-23 05:00:06

问题


How to safety parse tab-delimiter string ? for example: test\tbla-bla-bla\t2332 ?


回答1:


strtok() is a standard function for parsing strings with arbitrary delimiters. It is, however, not thread-safe. Your C library of choice might have a thread-safe variant.

Another standard-compliant way (just wrote this up, it is not tested):

#include <string.h>
#include <stdio.h>

int main()
{
    char string[] = "foo\tbar\tbaz";
    char * start = string;
    char * end;
    while ( ( end = strchr( start, '\t' ) ) != NULL )
    {
        // %s prints a number of characters, * takes number from stack
        // (your token is not zero-terminated!)
        printf( "%.*s\n", end - start, start );
        start = end + 1;
    }
    // start points to last token, zero-terminated
    printf( "%s", start );
    return 0;
}



回答2:


Use strtok_r instead of strtok (if it is available). It has similar usage, except it is reentrant, and it does not modify the string like strtok does. [Edit: Actually, I misspoke. As Christoph points out, strtok_r does replace the delimiters by '\0'. So, you should operate on a copy of the string if you want to preserve the original string. But it is preferable to strtok because it is reentrant and thread safe]

strtok will leave your original string modified. It replaces the delimiter with '\0'. And if your string happens to be a constant, stored in a read only memory (some compilers will do that), you may actually get a access violation.




回答3:


Using strtok() from string.h.

#include <stdio.h>
#include <string.h>

int main ()
{
    char str[] = "test\tbla-bla-bla\t2332";
    char * pch;
    pch = strtok (str," \t");
    while (pch != NULL)
    {
        printf ("%s\n",pch);
        pch = strtok (NULL, " \t");
    }
    return 0;
}



回答4:


You can use any regex library or even the GLib GScanner, see here and here for more information.




回答5:


Yet another version; this one separates the logic into a new function

#include <stdio.h>

static _Bool next_token(const char **start, const char **end)
{
    if(!*end) *end = *start;    // first call
    else if(!**end)             // check for terminating zero
        return 0;
    else *start = ++*end;       // skip tab

    // advance to terminating zero or next tab
    while(**end && **end != '\t')
        ++*end;

    return 1;
}

int main(void)
{
    const char *string = "foo\tbar\tbaz";

    const char *start = string;
    const char *end = NULL; // NULL value indicates first call

    while(next_token(&start, &end))
    {
        // print substring [start,end[
        printf("%.*s\n", end - start, start);
    }

    return 0;
}



回答6:


If you need a binary safe way to tokenize a given string:

#include <string.h>
#include <stdio.h>

void tokenize(const char *str, const char delim, const size_t size)
{
        const char *start = str, *next;
        const char *end = str + size;

        while (start < end) {
                if ((next = memchr(start, delim, end - start)) == NULL) {
                        next = end;
                }

                printf("%.*s\n", next - start, start);
                start = next + 1;
        }
}

int main(void)
{
        char str[] = "test\tbla-bla-bla\t2332";
        int len = strlen(str);

        tokenize(str, '\t', len);

        return 0;
}


来源:https://stackoverflow.com/questions/3839664/how-to-safety-parse-tab-delimited-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!