几个常见字符串函数的实现

来源:互联网 发布:俄罗斯 知乎 编辑:程序博客网 时间:2024/04/30 11:58

#ifndef __template_xchar
#define __template_xchar template <class xchar>
#endif

#define __xcharfunc(name)  _t ## name

HRESULT wctoac(const xchar* pwc, char* pac, size_t bufsize)
{
 if (pwc == 0) return E_FAIL;
 size_t i = 0;
 for(; *(pwc+i) != 0; i++)
 {
  *(pac+i) = *((char*)(pwc+i));
  if (i >= bufsize) return S_OK;
 }
 *(pac+i) = 0;
 return S_OK;
}

HRESULT actowc(const char* pac, xchar* pwc, size_t bufsize)
{
 if (pac == 0) return E_FAIL;
 size_t i = 0;
 for(; *(pac+i) != 0; i++)
 {
  *(pwc+i) = *(pac+i);
  if (i >= bufsize) return S_OK;
 }
 *(pwc+i) = 0;
 return S_OK;
}

__template_xchar
xchar * __xdecl __xcharfunc(strcat)(xchar * dst, const xchar* src)
{
        xchar * cp = dst;

        while( *cp )
                cp++;                   /* find end of dst */

        while( *cp++ = *src++ ) ;       /* Copy src to end of dst */

        return( dst );                  /* return dst */

}

__template_xchar
xchar * __xdecl __xcharfunc(strncpy)(
        xchar * dest,
        const xchar * source,
        size_t count
        )
{
        xchar *start = dest;

        while (count && (*dest++ = *source++))    /* copy string */
                count--;

        if (count)                              /* pad out with zeroes */
                while (--count)
                        *dest++ = L'/0';

        return(start);
}

__template_xchar
xchar* __xdecl __xcharfunc(strupr)(xchar* string)
{
    xchar * cp;

    for (cp=string; *cp; ++cp)
    {
        if ('a' <= *cp && *cp <= 'z')
            *cp += 'A' - 'a';
    }
    return(string);
}

__template_xchar
int __xdecl __xcharfunc(strcmp)(
        const xchar * src,
        const xchar * dst
        )
{
        int ret = 0 ;

        while( ! (ret = (int)(*src - *dst)) && *dst)
                ++src, ++dst;

        if ( ret < 0 )
                ret = -1 ;
        else if ( ret > 0 )
                ret = 1 ;

        return( ret );
}


__template_xchar
int __xdecl __xcharfunc(strncmp)(
        const xchar * first,
        const xchar * last,
        size_t count
        )
{
        if (!count)
                return(0);

        while (--count && *first && *first == *last)
        {
                first++;
                last++;
        }

        return((int)(*first - *last));
}

__template_xchar
int __xdecl __xcharfunc(stricmp)(
        const xchar * dst,
        const xchar * src
        )
{
        xchar f,l;
       
  do  {
            f = ((*dst <= 'Z') && (*dst >= 'A'))
                ? *dst + 'a' - 'A'
                : *dst;
            l = ((*src <= 'Z') && (*src >= 'A'))
                ? *src + 'a' - 'A'
                : *src;
            dst++;
            src++;
        } while ( (f) && (f == l) );

        return (int)(f - l);
}

__template_xchar
size_t __xdecl __xcharfunc(strlen)(
        const xchar * wcs
        )
{
  const xchar *eos = wcs;

  while( *eos++ ) ;

  return( (size_t)(eos - wcs - 1) );
}

__template_xchar
xchar * __xdecl __xcharfunc(strlwr)(
        xchar * wsrc
        )
{
        xchar *p;             /* traverses string for C locale conversion */
        xchar *wdst = NULL;   /* wide version of string in alternate case */
        //int dstlen;         /* len of wdst string, wide chars, with null */

        for (p=wsrc; *p; p++)
        {
   if ( (*p >= 'A') && (*p <= 'Z') )
    *p = *p - 'A' + 'a';
  }
        return (wsrc);

原创粉丝点击