windows strncasecmp() 函数的源代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7299119/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 17:59:37  来源:igfitidea点击:

Source code for strncasecmp() function

cwindows

提问by Neomex

Does someone have source for this function? I think it is avaiable under Unix, but I'm using Windows.

有人有这个功能的来源吗?我认为它在 Unix 下是可用的,但我使用的是 Windows。



Of course I tried google first, but haven't found solution, also function from "strncasecmp.c" takes only 2 instead of 3 parameters.

当然,我首先尝试了 google,但还没有找到解决方案,“strncasecmp.c”中的函数也只需要 2 个而不是 3 个参数。

Can't provide much more information, as it is not my code.

无法提供更多信息,因为它不是我的代码。

That's how he use it:

他是这样使用的:

int _tcsnicmp(const char *c1, const char *c2, int l) { return strncasecmp(c1,c2,l); }

回答by Tanguy

#define strncasecmp(x,y,z) _strnicmp(x,y,z)

回答by Christian.K

EDITAfter you have updated your question it looks like you're doing a backport from something that was original written for Windows, than ported to UNIX and now back to Windows?!?. _tcsnicmpis actually the function to call on Windows (see my link above). There is no point in redirecting it back to strncasecmp(or your own version thereof) on Windows.

编辑更新您的问题后,看起来您正在从最初为 Windows 编写的东西进行反向移植,而不是移植到 UNIX 现在又回到 Windows?!?。 _tcsnicmp实际上是在 Windows 上调用的函数(见我上面的链接)。strncasecmp在 Windows 上将其重定向回(或您自己的版本)是没有意义的。

Under Windows (using Microsoft Compilers at least, the platform is not really the issue here), you can use the strnicmp family of functionsinstead. If you still need the sourcecode either google for it, as others have suggested or look into the CRT source code that is distributed with Visual Studio and installed under "\VC\crt\src".

在 Windows 下(至少使用 Microsoft 编译器,这里的平台并不是真正的问题),您可以使用strnicmp 系列函数代替。如果您仍然需要源代码,请按照其他人的建议使用谷歌搜索,或者查看随 Visual Studio 分发并安装在“\VC\crt\src”下的 CRT 源代码。

回答by Tony The Lion

I have found it here

在这里找到

#include <string.h>
#include <ctype.h>

int 
_DEFUN (strncasecmp, (s1, s2, n),
    _CONST char *s1 _AND
    _CONST char *s2 _AND
    size_t n)
{
  if (n == 0)
    return 0;

  while (n-- != 0 && tolower(*s1) == tolower(*s2))
    {
      if (n == 0 || *s1 == '
int strncasecmp(const char *s1, const char *s2, int n)
{
    if (n && s1 != s2)
    {
        do {
            int d = tolower(*s1) - tolower(*s2);
            if (d || *s1 == '
/*
Copyright (c) 2015, Logicista / H Roberts
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the distribution.
3. All advertising materials mentioning features or use of this software
   must display the following acknowledgement:
   This product includes software developed by the <organization>.
4. Neither the name of the <organization> nor the
   names of its contributors may be used to endorse or promote products
   derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY <COPYRIGHT HOLDER> ''AS IS'' AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef PORTABLE_STRNCASECMP_H_
#define PORTABLE_STRNCASECMP_H_

#include <stddef.h>

int strncasecmp(const char *s1, const char *s2, size_t n);

#endif
' || *s2 == '
/*
Copyright (c) 2015, Logicista / H Roberts
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the distribution.
3. All advertising materials mentioning features or use of this software
   must display the following acknowledgement:
   This product includes software developed by the <organization>.
4. Neither the name of the <organization> nor the
   names of its contributors may be used to endorse or promote products
   derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY <COPYRIGHT HOLDER> ''AS IS'' AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "strncasecmp.h"

#include <ctype.h>

int
strncasecmp(const char *s1, const char *s2, size_t n)
{
    if (n == 0) return 0;

    while (n-- != 0 && tolower(*s1) == tolower(*s2)) {
        if (n == 0 || *s1 == '##代码##' || *s2 == '##代码##')
            break;
        s1++;
        s2++;
    }

    return tolower(*(const unsigned char *)s1)
        - tolower(*(const unsigned char *)s2);
}
') return d; s1++; s2++; } while (--n); } return 0; }
' || *s2 == '##代码##') break; s1++; s2++; } return tolower(*(unsigned char *) s1) - tolower(*(unsigned char *) s2); }

回答by selbie

If you have Visual C++ installed, the CRT source code is in your installation directory.

如果您安装了 Visual C++,则 CRT 源代码位于您的安装目录中。

Look in this directory: (Path to Visual Studio Install)\VC\crt\src

在此目录中查找:(Visual Studio 安装路径)\VC\crt\src

Look at all the *cmp.c files in this directory (e.g. wcsnicmp.c, strnicmp.c, etc...)

查看此目录中的所有 *cmp.c 文件(例如 wcsnicmp.c、strnicmp.c 等...)

回答by tutejszy

based on code from here:

基于此处的代码:

##代码##

Tested. Comparing the original Intel version I've added comparing identical pointers.

测试。比较原始的 Intel 版本,我添加了比较相同的指针。

回答by C.D.

There is a portable version the function code located here:

有一个便携式版本,功能代码位于此处:

https://github.com/HarryR/logpipe/blob/51b51b211ee2b961dd096a5f481b3c4d71a0863a/src/portable/strncasecmp.chttps://github.com/HarryR/logpipe/blob/51b51b211ee2b961dd096a5f481b3c4d71a0863a/src/portable/strncasecmp.h

https://github.com/HarryR/logpipe/blob/51b51b211ee2b961dd096a5f481b3c4d71a0863a/src/portable/strncasecmp.c https://github.com/HarryR/logpipe/blob/51b51b261ee6dcase281b2615dc861b2615dc861b2615dc861b3c4dcmp.c

For further completeness sake, I will post their contents here:

为了更完整起见,我将在此处发布它们的内容:

strncasecmp.h:

strncasecmp.h:

##代码##

strncasecmp.c:

strncasecmp.c:

##代码##

Either copy/paste from here or download from that repository link. Then place the files into your project, and adjust any other C/C++ related files you have to use #include "strncasecmp.h" (instead of #include ). This will reference the local files you have.

从这里复制/粘贴或从该存储库链接下载。然后将文件放入您的项目中,并调整您必须使用 #include "strncasecmp.h" (而不是 #include )的任何其他 C/C++ 相关文件。这将引用您拥有的本地文件。

I used this approach for my Visual Studio 2017 project. Seemed to work just fine so far. If I discover a problem I will adjust my post.

我在我的 Visual Studio 2017 项目中使用了这种方法。到目前为止似乎工作得很好。如果我发现问题,我会调整我的帖子。