/////////////////////////////////////////////////
// Description: //
// C Style VB Str Func //
// Name:cvbstrfun.h //
// Auther:Cyycoish //
// ID:201506282102A //
// (C) 2015 www.0xaa55.com //
// All Rights Reserved. //
/////////////////////////////////////////////////
#include <locale.h>
#include <wchar.h>
#include <tchar.h>
#include <malloc.h>
#define WCHR wchar_t
//________________________
//| <Function Table> |
//|______________________|
//|C Style |VB Style |
//|-----------|----------|
//|wslen |Len |
//|wsucase |UCase |
//|wsleft |Left |
//|wsinverse |StrReverse|
//|wsright |Right |
//|wsmid |Mid |
//|wsinstr |InStr |
//|wsltrim |LTrim |
//|wsrtrim |RTrim |
//|wsinstrrev |InStrRev |
//|___________|__________|
/*
int wslen (const WCHR* s);
void wsucase (WCHR* s);
void wslcase (WCHR* s);
WCHR* wsleft (const WCHR* szTar, int n);
void wsinverse (WCHR* szTar);
WCHR* wsright (const WCHR* szTar, int n);
WCHR* wsmid (const WCHR* szTar, int st, int n);
int wsinstr (const WCHR* szTar, const WCHR* szCmp, int st);
void wsltrim (WCHR* szTar);
void wsrtrim (WCHR* szTar);
int wsinstrrev(const WCHR* szTar, const WCHR* szCmp, int st);
*/
//String Functions
int wslen(const WCHR* s)
{
return wcslen(s);
}
void wsucase(WCHR* s)
{
int i;
for (i = 0; i <= wcslen(s) - 1; i++)
s[i] = towupper(s[i]);
return;
}
void wslcase(WCHR* s)
{
int i;
for (i = 0; i <= wcslen(s) - 1; i++)
s[i] = towlower(s[i]);
return;
}
WCHR* wsleft(const WCHR* szTar, int n)
{
int i;
WCHR* rtn;
WCHR tmpWChr[2] = {'\0', '\0'};
if (n > wcslen(szTar))
return L"";
rtn = (WCHR*)malloc(sizeof(WCHR) * (wcslen(szTar) + 1));
wcscpy(rtn, L"");
for (i = 0; i <= n - 1; i++)
{
tmpWChr[0] = szTar[i];
wcscat(rtn, tmpWChr);
}
return rtn;
}
void wsinverse(WCHR* szTar)
{
int i, j, k;
WCHR* rtn;
k = wcslen(szTar);
rtn = (WCHR*)malloc(sizeof(WCHR) * (k + 1));
for (i = 0, j = k - 1; i < wcslen(szTar); i++, j--)
{
rtn[j] = szTar[i];
}
rtn[i] = '\0';
wcscpy(szTar, rtn);
free(rtn);
}
WCHR* wsright(const WCHR* szTar, int n)
{
int i, j, k;
WCHR* rtn;
WCHR tmpWChr[2] = {'\0', '\0'};
k = wcslen(szTar);
if (n > k)
return L"";
rtn = (WCHR*)malloc(sizeof(WCHR) * (k + 1));
wcscpy(rtn, L"");
for (i = k - 1, j = 1; j <= n; i--, j++)
{
tmpWChr[0] = szTar[i];
wcscat(rtn, tmpWChr);
}
wsinverse(rtn);
return rtn;
}
WCHR* wsmid(const WCHR* szTar, int st, int n)
{
int i;
WCHR* rtn;
WCHR tmpWChr[2] = {'\0', '\0'};
if ((st - 1 + n) > wcslen(szTar))
return L"";
rtn = (WCHR*)malloc(sizeof(WCHR) * (wcslen(szTar) + 1));
wcscpy(rtn, L"");
for (i = st - 1; i <= st + n - 2; i++)
{
tmpWChr[0] = szTar[i];
wcscat(rtn, tmpWChr);
}
return rtn;
}
int wsinstr(const WCHR* szTar, const WCHR* szCmp, int st)
{
int i;
if (st >= wcslen(szTar) - wcslen(szCmp))
return -1;
for (i = st; i <= wcslen(szTar); i++)
{
if (!wcscmp(szCmp, wsmid(szTar, i, wcslen(szCmp))))
return i;
}
return -1;
}
void wsltrim(WCHR* szTar)
{
int i;
int b = 0;
WCHR* rtn;
WCHR tmpWChr[2] = {'\0', '\0'};
rtn = (WCHR*)malloc(sizeof(WCHR) * (wcslen(szTar) + 1));
wcscpy(rtn, L"");
for (i = 0; i < wcslen(szTar); i++)
{
tmpWChr[0] = szTar[i];
if (tmpWChr[0] != ' ' || b != 0)
{
wcscat(rtn, tmpWChr);
if (tmpWChr[0] != ' ' && b == 0)
b = !(b);
}
}
wcscpy(szTar, rtn);
free(rtn);
}
void wsrtrim(WCHR* szTar)
{
int i, j, k;
int b = 0;
WCHR* rtn;
WCHR tmpWChr[2] = {'\0', '\0'};
k = wcslen(szTar);
rtn = (WCHR*)malloc(sizeof(WCHR) * (k + 1));
wcscpy(rtn, L"");
for (i = k - 1, j = 0; j < k; i--, j++)
{
tmpWChr[0] = szTar[i];
if (tmpWChr[0] != ' ' || b != 0)
{
wcscat(rtn, tmpWChr);
if (tmpWChr[0] != ' ' && b == 0)
b = !(b);
}
}
wsinverse(rtn);
wcscpy(szTar, rtn);
free(rtn);
}
int wsinstrrev(const WCHR* szTar, const WCHR* szCmp, int st)
{
int i;
for (i = wcslen(szTar) - wcslen(szCmp) + 1; i >= st; i--)
if (!wcscmp(szCmp, wsmid(szTar, i, wcslen(szCmp))))
return i;
return -1;
}
//End String Functions