strequal.c 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) Daniel Stenberg, <[email protected]>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at https://curl.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. #include "curl_setup.h"
  25. #include <curl/curl.h>
  26. #include "strcase.h"
  27. /*
  28. * curl_strequal() is for doing "raw" case insensitive strings. This is meant
  29. * to be locale independent and only compare strings we know are safe for
  30. * this. See https://daniel.haxx.se/blog/2008/10/15/strcasecmp-in-turkish/ for
  31. * further explanations as to why this function is necessary.
  32. */
  33. static int casecompare(const char *first, const char *second)
  34. {
  35. while(*first) {
  36. if(Curl_raw_toupper(*first) != Curl_raw_toupper(*second))
  37. /* get out of the loop as soon as they do not match */
  38. return 0;
  39. first++;
  40. second++;
  41. }
  42. /* If we are here either the strings are the same or the length is different.
  43. We can just test if the "current" character is non-zero for one and zero
  44. for the other. Note that the characters may not be exactly the same even
  45. if they match, we only want to compare zero-ness. */
  46. return !*first == !*second;
  47. }
  48. static int ncasecompare(const char *first, const char *second, size_t max)
  49. {
  50. while(*first && max) {
  51. if(Curl_raw_toupper(*first) != Curl_raw_toupper(*second))
  52. return 0;
  53. max--;
  54. first++;
  55. second++;
  56. }
  57. if(max == 0)
  58. return 1; /* they are equal this far */
  59. return Curl_raw_toupper(*first) == Curl_raw_toupper(*second);
  60. }
  61. /*
  62. * Only "raw" case insensitive strings. This is meant to be locale independent
  63. * and only compare strings we know are safe for this.
  64. *
  65. * The function is capable of comparing a-z case insensitively.
  66. *
  67. * Result is 1 if text matches and 0 if not.
  68. */
  69. /* --- public function --- */
  70. int curl_strequal(const char *first, const char *second)
  71. {
  72. if(first && second)
  73. /* both pointers point to something then compare them */
  74. return casecompare(first, second);
  75. /* if both pointers are NULL then treat them as equal */
  76. return NULL == first && NULL == second;
  77. }
  78. /* --- public function --- */
  79. int curl_strnequal(const char *first, const char *second, size_t max)
  80. {
  81. if(first && second)
  82. /* both pointers point to something then compare them */
  83. return ncasecompare(first, second, max);
  84. /* if both pointers are NULL then treat them as equal if max is non-zero */
  85. return NULL == first && NULL == second && max;
  86. }