git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH v12 03/10] strbuf: add a case insensitive starts_with()

From
MWMorten Welinder <mwelinder@gmail.com>
Date
Mar 16, 2018, 17:33 UTC
Message-ID
<CANv4PNmx9ok0pYxQD1TLfTveWyF8LqhhvT_JpvqrnP8q6+UbKg@mail.gmail.com>
In-Reply-To
<20180315225746.18119-4-lars.schneider@autodesk.com>

You need to cast those arguments of tolower to (unsigned char). That's arguably a bug of the language.

Character values less than zero aren't valid for tolower, unless they happen to equal EOF in which case the tolower calls don't mean what you want them to mean. Per the man page:

"If c is neither an unsigned char value nor EOF, the behavior of these functions is undefined."

M.
On Thu, Mar 15, 2018 at 6:57 PM,  <lars.schneider@autodesk.com> wrote:
Show 48 quoted lines
> From: Lars Schneider <larsxschneider@gmail.com>
>
> Check in a case insensitive manner if one string is a prefix of another
> string.
>
> This function is used in a subsequent commit.
>
> Signed-off-by: Lars Schneider <larsxschneider@gmail.com>
> ---
>  git-compat-util.h | 1 +
>  strbuf.c          | 9 +++++++++
>  2 files changed, 10 insertions(+)
>
> diff --git a/git-compat-util.h b/git-compat-util.h
> index 68b2ad531e..95c9b34832 100644
> --- a/git-compat-util.h
> +++ b/git-compat-util.h
> @@ -455,6 +455,7 @@ extern void (*get_warn_routine(void))(const char *warn, va_list params);
>  extern void set_die_is_recursing_routine(int (*routine)(void));
>
>  extern int starts_with(const char *str, const char *prefix);
> +extern int istarts_with(const char *str, const char *prefix);
>
>  /*
>   * If the string "str" begins with the string found in "prefix", return 1.
> diff --git a/strbuf.c b/strbuf.c
> index b635f0bdc4..99812b8488 100644
> --- a/strbuf.c
> +++ b/strbuf.c
> @@ -11,6 +11,15 @@ int starts_with(const char *str, const char *prefix)
>                         return 0;
>  }
>
> +int istarts_with(const char *str, const char *prefix)
> +{
> +       for (; ; str++, prefix++)
> +               if (!*prefix)
> +                       return 1;
> +               else if (tolower(*str) != tolower(*prefix))
> +                       return 0;
> +}
> +
>  int skip_to_optional_arg_default(const char *str, const char *prefix,
>                                  const char **arg, const char *def)
>  {
> --
> 2.16.2
>
Previous: lars.schneider@autodesk.comNext: Junio C Hamano
Message 5 of 22 in “convert: add support for different encodings”
  1. 00/10 convert: add support for different encodingslars.schneider@autodesk.com, Mar 15, 2018
  2. 01/10 strbuf: remove unnecessary NUL assignment in xstrdup_tolower()lars.schneider@autodesk.com, Mar 15, 2018
  3. 02/10 strbuf: add xstrdup_toupper()lars.schneider@autodesk.com, Mar 15, 2018
  4. 03/10 strbuf: add a case insensitive starts_with()lars.schneider@autodesk.com, Mar 15, 2018
  5. Morten WelinderMar 16, 2018
  6. Junio C HamanoMar 16, 2018
  7. 04/10 utf8: teach same_encoding() alternative UTF encoding nameslars.schneider@autodesk.com, Mar 15, 2018
  8. Eric SunshineMar 15, 2018
  9. Lars SchneiderMar 15, 2018
  10. Eric SunshineMar 15, 2018
  11. Junio C HamanoMar 16, 2018
  12. Eric SunshineMar 16, 2018
  13. Lars SchneiderApr 1, 2018
  14. 05/10 utf8: add function to detect prohibited UTF-16/32 BOMlars.schneider@autodesk.com, Mar 15, 2018
  15. 06/10 utf8: add function to detect a missing UTF-16/32 BOMlars.schneider@autodesk.com, Mar 15, 2018
  16. 07/10 convert: add 'working-tree-encoding' attributelars.schneider@autodesk.com, Mar 15, 2018
  17. 08/10 convert: check for detectable errors in UTF encodingslars.schneider@autodesk.com, Mar 15, 2018
  18. 10/10 convert: add round trip check based on 'core.checkRoundtripEncoding'lars.schneider@autodesk.com, Mar 15, 2018
  19. 09/10 convert: add tracing for 'working-tree-encoding' attributelars.schneider@autodesk.com, Mar 15, 2018
  20. Junio C HamanoMar 29, 2018
  21. Lars SchneiderApr 2, 2018
  22. Lars SchneiderApr 3, 2018

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.