2
0

rawstr.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2009, Daniel Stenberg, <daniel@haxx.se>, 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 http://curl.haxx.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. * $Id$
  22. ***************************************************************************/
  23. #include "setup.h"
  24. #include "rawstr.h"
  25. /* Portable, consistent toupper (remember EBCDIC). Do not use toupper() because
  26. its behavior is altered by the current locale. */
  27. char Curl_raw_toupper(char in)
  28. {
  29. switch (in) {
  30. case 'a':
  31. return 'A';
  32. case 'b':
  33. return 'B';
  34. case 'c':
  35. return 'C';
  36. case 'd':
  37. return 'D';
  38. case 'e':
  39. return 'E';
  40. case 'f':
  41. return 'F';
  42. case 'g':
  43. return 'G';
  44. case 'h':
  45. return 'H';
  46. case 'i':
  47. return 'I';
  48. case 'j':
  49. return 'J';
  50. case 'k':
  51. return 'K';
  52. case 'l':
  53. return 'L';
  54. case 'm':
  55. return 'M';
  56. case 'n':
  57. return 'N';
  58. case 'o':
  59. return 'O';
  60. case 'p':
  61. return 'P';
  62. case 'q':
  63. return 'Q';
  64. case 'r':
  65. return 'R';
  66. case 's':
  67. return 'S';
  68. case 't':
  69. return 'T';
  70. case 'u':
  71. return 'U';
  72. case 'v':
  73. return 'V';
  74. case 'w':
  75. return 'W';
  76. case 'x':
  77. return 'X';
  78. case 'y':
  79. return 'Y';
  80. case 'z':
  81. return 'Z';
  82. }
  83. return in;
  84. }
  85. /*
  86. * Curl_raw_equal() is for doing "raw" case insensitive strings. This is meant
  87. * to be locale independent and only compare strings we know are safe for
  88. * this. See http://daniel.haxx.se/blog/2008/10/15/strcasecmp-in-turkish/ for
  89. * some further explanation to why this function is necessary.
  90. *
  91. * The function is capable of comparing a-z case insensitively even for
  92. * non-ascii.
  93. */
  94. int Curl_raw_equal(const char *first, const char *second)
  95. {
  96. while(*first && *second) {
  97. if(Curl_raw_toupper(*first) != Curl_raw_toupper(*second))
  98. /* get out of the loop as soon as they don't match */
  99. break;
  100. first++;
  101. second++;
  102. }
  103. /* we do the comparison here (possibly again), just to make sure that if the
  104. loop above is skipped because one of the strings reached zero, we must not
  105. return this as a successful match */
  106. return (Curl_raw_toupper(*first) == Curl_raw_toupper(*second));
  107. }
  108. int Curl_raw_nequal(const char *first, const char *second, size_t max)
  109. {
  110. while(*first && *second && max) {
  111. if(Curl_raw_toupper(*first) != Curl_raw_toupper(*second)) {
  112. break;
  113. }
  114. max--;
  115. first++;
  116. second++;
  117. }
  118. if(0 == max)
  119. return 1; /* they are equal this far */
  120. return Curl_raw_toupper(*first) == Curl_raw_toupper(*second);
  121. }
  122. /* Copy an upper case version of the string from src to dest. The
  123. * strings may overlap. No more than n characters of the string are copied
  124. * (including any NUL) and the destination string will NOT be
  125. * NUL-terminated if that limit is reached.
  126. */
  127. void Curl_strntoupper(char *dest, const char *src, size_t n)
  128. {
  129. if (n < 1)
  130. return;
  131. do {
  132. *dest++ = Curl_raw_toupper(*src);
  133. } while (*src++ && --n);
  134. }