Come la sicurezza parse stringa delimitato da tabulazioni?
-
27-09-2019 - |
Domanda
Come fare il parsing della stringa di sicurezza scheda delimitatore? per esempio: test \ TBLA-bla-bla \ t2332?
Soluzione
strtok()
è una funzione standard per l'analisi di stringhe con delimitatori arbitrari. E ', tuttavia, non thread-safe. La vostra libreria C di scelta potrebbe avere una variante thread-safe.
Un altro modo conforme allo standard (appena scritto questo in su, è non testato ):
#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;
}
Altri suggerimenti
Usa strtok_r invece di strtok (se è disponibile). Ha utilizzo simile, tranne che è rientrante, e non modificare la stringa come strtok fa. [ Modifica In realtà, io misspoke. Come sottolinea Christoph, strtok_r non sostituire i delimitatori da '\ 0'. Quindi, si dovrebbe operare su una copia della stringa, se si desidera conservare la stringa originale. Ma è preferibile strtok perché è rientrante e sicuro filo]
strtok lascerà la vostra stringa originale modificato. Esso sostituisce il delimitatore con '\ 0'. E se la stringa sembra essere una costante, memorizzata in un memoria di sola lettura (alcuni compilatori lo farà), si può effettivamente ottenere una violazione di accesso.
Utilizzando strtok()
da 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;
}
Ancora un'altra versione; questo separa la logica in una nuova funzione
#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;
}
Se avete bisogno di un modo sicuro per binario tokenize una data stringa:
#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;
}