[RFC 2/7] Add libiberty/concat styled concat_path function
Pedro Alves
palves@redhat.com
Thu Jan 12 12:00:00 GMT 2017
On 01/12/2017 11:32 AM, Philipp Rudo wrote:
> static inline int
> -startswith (const char *string, const char *pattern)
> +startswith (std::string str, std::string pattern)
NAK.
This is passing by copy, so will force unnecessary deep
string copies all over the place.
> {
> - return strncmp (string, pattern, strlen (pattern)) == 0;
> + return (str.find (pattern) == 0);
> +}
> +
I.e., before, this caused 0 copies:
startswith ("foo, "f");
After, you force a deep string copy for "foo", and another
for "f". It's as if you wrote:
startswith (xstrdup ("foo), xstrdup ("f"));
Also, this function is a static inline in a header so that
the compiler can see when "pattern" is a string literal, and
thus strlen can be optimized to a plain 'sizeof (pattern) - 1',
which is very frequent.
If you want to add overloads that can take "const std::string &"
for convenience, to avoid str.c_str(), that's maybe fine,
but you'd have to add all the combinations of
'const char *' x 'const std::string &' in the parameters, I
suppose.
Thanks,
Pedro Alves
More information about the Gdb-patches
mailing list