I have asked a previous question but at the last moment I realized that the code does not work as intended as it has some small bugs.
I got some ideas for improvement though, but they didn't seem to work as well, so I decided to re-write it. This time, I was following the logic all the time.
char *tbl_getline (char *source, size_t line)
{
size_t current_line = 1, columns;
size_t iter;
for(iter = 0; source[iter] != '\0' && current_line != line; iter++)
if(source[iter] == '\n') current_line++;
for(columns = 0; source[iter + columns] != '\n' && source[iter + columns] != '\0'; columns++);
char *buff = malloc(columns + 1);
memcpy(buff, source + iter * sizeof(char), columns);
buff[columns] = '\0';
return(buff);
}
And that was the code able to return a line from a string in a relatively good speed.
current_line < line
rather thancurrent_line != line
. Deals with the unusual case ofline == 0
. \$\endgroup\$