开发者

Source code for strncasecmp() function

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


Of course I tried google first, but haven't found solution, also function from "strn开发者_如何学运维casecmp.c" takes only 2 instead of 3 parameters.

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); }


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


EDIT After 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?!?. _tcsnicmp is 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.

Under Windows (using Microsoft Compilers at least, the platform is not really the issue here), you can use the strnicmp family of functions instead. 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".


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 == '\0' || *s2 == '\0')
    break;
      s1++;
      s2++;
    }

  return tolower(*(unsigned char *) s1) - tolower(*(unsigned char *) s2);
}


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

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

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


There is a portable version the function code located here:

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

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

strncasecmp.h:

/*
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

strncasecmp.c:

/*
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 == '\0' || *s2 == '\0')
            break;
        s1++;
        s2++;
    }

    return tolower(*(const unsigned char *)s1)
        - tolower(*(const unsigned char *)s2);
}

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.

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.


based on code from here:

int strncasecmp(const char *s1, const char *s2, int n)
{
    if (n && s1 != s2)
    {
        do {
            int d = tolower(*s1) - tolower(*s2);
            if (d || *s1 == '\0' || *s2 == '\0') return d;
            s1++;
            s2++;
        } while (--n);
    }
    return 0;
}

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜