A question about \r\n in C...
I have a string that has several carriage returns line feeds in it. Of course a string ends in C when it is NULL terminated.
The pointer to the string I am working with is a const char *. I need to pull out the data between each \r\n. I can't use strtok() because the pointer is a const and strtok changes the string. I don't want to write a function that checks each byte for the \r\n sequence. Are there any other functions that would do this? -Tristan |
I suppose I can make a non const copy of the string and use strtok on that. This is for a packet analyzer of web traffic so I want it to be as efficient as possible. It just seems so expensive to do this on each HTTP request.
-Tristan |
You could also try strcspn(). http://www.cplusplus.com/reference/cstring/strcspn/
|
Great thank you. I will try that.
-Tristan |
Writing a function to perform the check is quite trivial.
All you need to do is literate through the string and check for the first character e.g. "\r" and if it matches then check the current position+1 and see if it matches the second character e.g. "\n" and if that matches return the position. If there is no match, then increment and try again until hitting \0 If you need to find multiple matches in a string you can either add them to a list stored in an array or you can have your search function take an argument as to where to start in the string. |
All times are GMT -5. The time now is 05:18 PM. |