The deserialization library (messagepack) I am using does not provide null-terminated strings. Instead, I get a pointer to the beginning of the string and a length. What is the fastest way to compare this string to a normal null-terminated string?
相关问题
- Multiple sockets for clients to connect to
- how to split a list into a given number of sub-lis
- What is the best way to do a search in a large fil
- glDrawElements only draws half a quad
- Generate string from integer with arbitrary base i
The fastest way is
strncmp()
which limits the length to be compared.This assumes however that the length you use is the maximum length of the two strings. If the null terminated string could have a bigger length, you first have to compare the length.
Note that the strlen() is checked on purpose after the comparison, to avoid unnecessarily iterating through all the characters o fthe null terminated string if the first caracters don't even match.
A final variant is:
Here is one way:
usage: