quickjs-tart

quickjs-based runtime for wallet-core logic
Log | Files | Refs | README | LICENSE

strequal.c (3277B)


      1 /***************************************************************************
      2  *                                  _   _ ____  _
      3  *  Project                     ___| | | |  _ \| |
      4  *                             / __| | | | |_) | |
      5  *                            | (__| |_| |  _ <| |___
      6  *                             \___|\___/|_| \_\_____|
      7  *
      8  * Copyright (C) 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 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 
     25 #include "curl_setup.h"
     26 
     27 #include <curl/curl.h>
     28 #include "strcase.h"
     29 
     30 /*
     31  * curl_strequal() is for doing "raw" case insensitive strings. This is meant
     32  * to be locale independent and only compare strings we know are safe for
     33  * this. See https://daniel.haxx.se/blog/2008/10/15/strcasecmp-in-turkish/ for
     34  * further explanations as to why this function is necessary.
     35  */
     36 
     37 static int casecompare(const char *first, const char *second)
     38 {
     39   while(*first) {
     40     if(Curl_raw_toupper(*first) != Curl_raw_toupper(*second))
     41       /* get out of the loop as soon as they do not match */
     42       return 0;
     43     first++;
     44     second++;
     45   }
     46   /* If we are here either the strings are the same or the length is different.
     47      We can just test if the "current" character is non-zero for one and zero
     48      for the other. Note that the characters may not be exactly the same even
     49      if they match, we only want to compare zero-ness. */
     50   return !*first == !*second;
     51 }
     52 
     53 static int ncasecompare(const char *first, const char *second, size_t max)
     54 {
     55   while(*first && max) {
     56     if(Curl_raw_toupper(*first) != Curl_raw_toupper(*second))
     57       return 0;
     58     max--;
     59     first++;
     60     second++;
     61   }
     62   if(0 == max)
     63     return 1; /* they are equal this far */
     64 
     65   return Curl_raw_toupper(*first) == Curl_raw_toupper(*second);
     66 }
     67 
     68 /*
     69  * Only "raw" case insensitive strings. This is meant to be locale independent
     70  * and only compare strings we know are safe for this.
     71  *
     72  * The function is capable of comparing a-z case insensitively.
     73  *
     74  * Result is 1 if text matches and 0 if not.
     75  */
     76 
     77 /* --- public function --- */
     78 int curl_strequal(const char *first, const char *second)
     79 {
     80   if(first && second)
     81     /* both pointers point to something then compare them */
     82     return casecompare(first, second);
     83 
     84   /* if both pointers are NULL then treat them as equal */
     85   return NULL == first && NULL == second;
     86 }
     87 
     88 /* --- public function --- */
     89 int curl_strnequal(const char *first, const char *second, size_t max)
     90 {
     91   if(first && second)
     92     /* both pointers point to something then compare them */
     93     return ncasecompare(first, second, max);
     94 
     95   /* if both pointers are NULL then treat them as equal if max is non-zero */
     96   return NULL == first && NULL == second && max;
     97 }