不区分大小写的前n个字符字符串比较

C++: case-insensitive first-n-characters string comparison

本文关键字:字符 字符串 比较 大小写 不区      更新时间:2023-10-16

我的问题与此类似,但我有两个字符串(如char *),任务是用boost::iequals之类的东西替换strnicmp函数(仅适用于MS VC)。

注意strnicmp不是stricmp -它只比较前n个字符。

还有比这更简单的解决方案吗?

void foo(const char *s1, const char *s2) 
{
    ...
    std::string str1 = s1;
    std::string str2 = s2;
    int n = 7;
    if (boost::iequals(str1.substr(0, n), str2)) {
        ...
    }
}

如果确实有必要,可以自己编写函数:

bool mystrnicmp(char const* s1, char const* s2, int n){
  for(int i=0; i < n; ++i){
    unsigned char c1 = static_cast<unsigned char>(s1[i]);
    unsigned char c2 = static_cast<unsigned char>(s2[i]);
    if(tolower(c1) != tolower(c2))
      return false;
    if(c1 == '' || c2 == '')
      break;
  }
  return true;
}

对于大小写不敏感,您需要一个自定义比较函数(或函子):

struct EqIgnoreCase
{
    bool operator()( char lhs, char rhs ) const
    {
        return ::tolower( static_cast<unsigned char>( lhs ) )
            == ::tolower( static_cast<unsigned char>( rhs ) );
    }
};

如果我理解正确的话,你是在检查前缀。的最简单的方法是:

bool
isPrefix( std::string const& s1, std::string const& s2 )
{
    return s1.size() <= s2.size()
        && std::equals( s1.begin(), s1.end(), s2.begin(), EqIgnoreCase() );
}

(注意尺寸的检查。s1不能是s2的前缀它比s2长吗?当然,std::equals也会如果s1大于s2)。

对于用C字符串(字符指针)定义的函数,"向上"到STL字符串似乎非常低效,但也许这对我来说是完全不成熟的想法。

我认为直接的C语言解决方案"更简单",但这又取决于个人的观点。

#include <ctype.h>
void foo(const char *s1, const char *s2)
{
  size_t i, n = 7;
  for(i = 0; i < n; i++)
  {
    if(tolower(s1[i]) != tolower(s2[i]))
      return;
    if(s[i] == '' && s2[i] == '')
      break;
  }
  /* Strings are equal, do the work. */
  ...
}

这假设如果两个字符串在前缀长度耗尽之前结束,则是匹配的。

当然,上面的假设是ASCII字符串,其中tolower()是有意义的。

我建议你自己写这个函数,像这样:

bool strnicmp2(const char *s, const char *t, size_t n) {
    while (n > 0 && *s && *t && tolower(*s) == tolower(*t)) {
        ++s;
        ++t;
        --n;
    }
    return n == 0 || !*s || !*t;
}

这样的东西应该可以工作。

#include <iostream>
#include <string>
#include <cctype>
#include <cstring>
#include <algorithm>
struct isequal
{
  bool operator()(int l, int r) const
  {
    return std::tolower(l) == std::tolower(r);
  }
};
bool istrncmp(const char* s1, const char* s2, size_t n)
{
  size_t ls1 = std::strlen(s1);
  size_t ls2 = std::strlen(s2);
  // this is strict, but you can change
  if (ls1 < n || ls2 < n)
    return false;
  return std::equal(s1, s1 + n, s2, isequal()); 
}
int main(void)
{
  std::cout << istrncmp("fooB", "fooA", 3) << std::endl;
  std::cout << istrncmp("fooB", "fooA", 5) << std::endl;
  std::cout << istrncmp("fooB", "f1oA", 3) << std::endl;
  return 0;
}

我不知道这算不算更简单,但它的行数更少,速度也应该相当不错。

#include <boost/iterator/transform_iterator.hpp>
#include <algorithm>
#include <cctype>
bool equal_insensitive_n( char const *a, char const *b, size_t n ) {
    n = std::min( n, std::min( ::strlen( a ) + 1, ::strlen( b ) + 1 ) );
    #define tilc(S) boost::make_transform_iterator( (S), ::tolower )
    return std::equals( tilc(a), tilc(a) + n, tilc(b) );
    #undef tilc
}