Comment la sécurité parse string délimité par des tabulations?
-
27-09-2019 - |
Question
Comment Parse de sécurité chaîne tabulations delimiter? par exemple: test \ TBLA-Bla-Bla \ t2332?
La solution
strtok()
est une fonction standard pour analyser des chaînes avec des délimiteurs arbitraires. Il est cependant pas thread-safe. Votre bibliothèque C de choix pourrait avoir une variante de thread-safe.
Une autre façon conforme aux normes (juste écrit ça, il est non testé ):
#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;
}
Autres conseils
Utilisation strtok_r au lieu de strtok (si elle est disponible). Il a l'usage similaire, sauf qu'il est rentrante, et ne pas modifier la chaîne comme strtok fait. [ Modifier En fait, je me suis trompé. Comme le souligne Christoph, strtok_r ne remplace les délimiteurs par « \ 0 ». Donc, vous devez fonctionner sur une copie de la chaîne si vous souhaitez conserver la chaîne d'origine. Mais il est préférable de strtok parce qu'il est rentrante et sûr de fil]
strtok modifié laissera votre chaîne d'origine. Il remplace le délimiteur par « \ 0 ». Et si votre chaîne se trouve être une constante, stockée dans une mémoire morte (certains compilateurs faire), vous pouvez réellement obtenir une violation d'accès.
Utilisation strtok()
de 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;
}
Vous pouvez utiliser une bibliothèque regex ou même le GScanner
GLib, voir ici et pour plus d'informations.
Pourtant, une autre version; celui-ci sépare la logique en une nouvelle fonction
#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;
}
Si vous avez besoin d'un moyen sûr de binaire tokenizer une chaîne donnée:
#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;
}