strcase.c
Go to the documentation of this file.
00001 /***************************************************************************
00002  *                                  _   _ ____  _
00003  *  Project                     ___| | | |  _ \| |
00004  *                             / __| | | | |_) | |
00005  *                            | (__| |_| |  _ <| |___
00006  *                             \___|\___/|_| \_\_____|
00007  *
00008  * Copyright (C) 1998 - 2016, Daniel Stenberg, <daniel@haxx.se>, et al.
00009  *
00010  * This software is licensed as described in the file COPYING, which
00011  * you should have received as part of this distribution. The terms
00012  * are also available at https://curl.haxx.se/docs/copyright.html.
00013  *
00014  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
00015  * copies of the Software, and permit persons to whom the Software is
00016  * furnished to do so, under the terms of the COPYING file.
00017  *
00018  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
00019  * KIND, either express or implied.
00020  *
00021  ***************************************************************************/
00022 
00023 #include "curl_setup.h"
00024 
00025 #include <curl/curl.h>
00026 
00027 #include "strcase.h"
00028 
00029 /* Portable, consistent toupper (remember EBCDIC). Do not use toupper() because
00030    its behavior is altered by the current locale. */
00031 char Curl_raw_toupper(char in)
00032 {
00033 #if !defined(CURL_DOES_CONVERSIONS)
00034   if(in >= 'a' && in <= 'z')
00035     return (char)('A' + in - 'a');
00036 #else
00037   switch(in) {
00038   case 'a':
00039     return 'A';
00040   case 'b':
00041     return 'B';
00042   case 'c':
00043     return 'C';
00044   case 'd':
00045     return 'D';
00046   case 'e':
00047     return 'E';
00048   case 'f':
00049     return 'F';
00050   case 'g':
00051     return 'G';
00052   case 'h':
00053     return 'H';
00054   case 'i':
00055     return 'I';
00056   case 'j':
00057     return 'J';
00058   case 'k':
00059     return 'K';
00060   case 'l':
00061     return 'L';
00062   case 'm':
00063     return 'M';
00064   case 'n':
00065     return 'N';
00066   case 'o':
00067     return 'O';
00068   case 'p':
00069     return 'P';
00070   case 'q':
00071     return 'Q';
00072   case 'r':
00073     return 'R';
00074   case 's':
00075     return 'S';
00076   case 't':
00077     return 'T';
00078   case 'u':
00079     return 'U';
00080   case 'v':
00081     return 'V';
00082   case 'w':
00083     return 'W';
00084   case 'x':
00085     return 'X';
00086   case 'y':
00087     return 'Y';
00088   case 'z':
00089     return 'Z';
00090   }
00091 #endif
00092 
00093   return in;
00094 }
00095 
00096 /*
00097  * Curl_raw_equal() is for doing "raw" case insensitive strings. This is meant
00098  * to be locale independent and only compare strings we know are safe for
00099  * this.  See https://daniel.haxx.se/blog/2008/10/15/strcasecmp-in-turkish/ for
00100  * some further explanation to why this function is necessary.
00101  *
00102  * The function is capable of comparing a-z case insensitively even for
00103  * non-ascii.
00104  *
00105  * @unittest: 1301
00106  */
00107 
00108 int Curl_strcasecompare(const char *first, const char *second)
00109 {
00110   while(*first && *second) {
00111     if(Curl_raw_toupper(*first) != Curl_raw_toupper(*second))
00112       /* get out of the loop as soon as they don't match */
00113       break;
00114     first++;
00115     second++;
00116   }
00117   /* we do the comparison here (possibly again), just to make sure that if the
00118      loop above is skipped because one of the strings reached zero, we must not
00119      return this as a successful match */
00120   return (Curl_raw_toupper(*first) == Curl_raw_toupper(*second));
00121 }
00122 
00123 int Curl_safe_strcasecompare(const char *first, const char *second)
00124 {
00125   if(first && second)
00126     /* both pointers point to something then compare them */
00127     return Curl_strcasecompare(first, second);
00128   else
00129     /* if both pointers are NULL then treat them as equal */
00130     return (NULL == first && NULL == second);
00131 }
00132 
00133 /*
00134  * @unittest: 1301
00135  */
00136 int Curl_strncasecompare(const char *first, const char *second, size_t max)
00137 {
00138   while(*first && *second && max) {
00139     if(Curl_raw_toupper(*first) != Curl_raw_toupper(*second)) {
00140       break;
00141     }
00142     max--;
00143     first++;
00144     second++;
00145   }
00146   if(0 == max)
00147     return 1; /* they are equal this far */
00148 
00149   return Curl_raw_toupper(*first) == Curl_raw_toupper(*second);
00150 }
00151 
00152 /* Copy an upper case version of the string from src to dest.  The
00153  * strings may overlap.  No more than n characters of the string are copied
00154  * (including any NUL) and the destination string will NOT be
00155  * NUL-terminated if that limit is reached.
00156  */
00157 void Curl_strntoupper(char *dest, const char *src, size_t n)
00158 {
00159   if(n < 1)
00160     return;
00161 
00162   do {
00163     *dest++ = Curl_raw_toupper(*src);
00164   } while(*src++ && --n);
00165 }
00166 
00167 /* --- public functions --- */
00168 
00169 int curl_strequal(const char *first, const char *second)
00170 {
00171   return Curl_strcasecompare(first, second);
00172 }
00173 int curl_strnequal(const char *first, const char *second, size_t max)
00174 {
00175   return Curl_strncasecompare(first, second, max);
00176 }


rc_visard_driver
Author(s): Heiko Hirschmueller , Christian Emmerich , Felix Ruess
autogenerated on Thu Jun 6 2019 20:43:06