c ++ CAtlRegExp.Parse&匹配wstring [英] c++ CAtlRegExp.Parse & Match wstring
本文介绍了c ++ CAtlRegExp.Parse&匹配wstring的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用这个类,因为它是WinHttpClient的一部分,但我得到以下错误(它适用于Unicode,但是然后unicode打破了我的所有代码,所以我想我会修复它):
I'm trying to use this class as it's part of WinHttpClient, but I get the following errors (It works on Unicode, but then unicode breaks all my code so I figured I'd fix that instead):
Erreur 2 error C2664: 'ATL::REParseError ATL::CAtlRegExp<ATL::CAtlRECharTraits>::Parse(const char *,BOOL)' : impossible de convertir l'argument 1 de 'const wchar_t *' en 'const char *' c:\users\david\desktop\directx\overlay\Project\RegExp.h 52 1 Output
Erreur 3 error C2664: 'BOOL ATL::CAtlRegExp<ATL::CAtlRECharTraits>::Match(const char *,ATL::CAtlREMatchContext<CharTraits> *,const char **)' : impossible de convertir l'argument 1 de 'const wchar_t *' en 'const char *' c:\users\david\desktop\directx\overlay\Project\RegExp.h 70 1 Output
Erreur 4 error C2664: 'void ATL::CAtlREMatchContext<CharTraits>::GetMatch(UINT,ATL::CAtlREMatchContext<CharTraits>::MatchGroup *)' : impossible de convertir l'argument 2 de 'const wchar_t **' en 'const char **' c:\users\david\desktop\directx\overlay\Project\RegExp.h 78 1 Output
5 IntelliSense : l'argument de type "const wchar_t *" est incompatible avec le paramètre de type "const ATL::CAtlRECharTraitsA::RECHARTYPE *" c:\users\david\desktop\directx\overlay\Project\RegExp.h 52 32 Output
6 IntelliSense : l'argument de type "const wchar_t *" est incompatible avec le paramètre de type "const ATL::CAtlRECharTraitsA::RECHARTYPE *" c:\users\david\desktop\directx\overlay\Project\RegExp.h 70 23 Output
7 IntelliSense : l'argument de type "const wchar_t **" est incompatible avec le paramètre de type "const ATL::CAtlRECharTraitsA::RECHARTYPE **" c:\users\david\desktop\directx\overlay\Project\RegExp.h 70 35 Output
8 IntelliSense : aucune instance de fonction surchargée "ATL::CAtlREMatchContext<CharTraits>::GetMatch [avec CharTraits=ATL::CAtlRECharTraits]" ne correspond à la liste d'arguments
les types d'arguments sont : (int, const wchar_t **, const wchar_t **)
le type d'objet est : ATL::CAtlREMatchContext<ATL::CAtlRECharTraits> c:\users\david\desktop\directx\overlay\Project\RegExp.h 78 7 Output
/**
* Copyright 2008-2009 Cheng Shi. All rights reserved.
* Email: shicheng107@hotmail.com
*/
#ifndef REGEXP_H
#define REGEXP_H
#include <iostream>
#include <string>
#include <vector>
using namespace std;
#pragma warning(push)
#pragma warning(disable: 6385 6011 4127)
#include "atlrx.h"
#pragma warning(pop)
/*
* Parameters
* [in] regExp: Value of type string which is the input regular expression.
* [in] caseSensitive: Value of type bool which indicate whether the parse is case sensitive.
* [in] groupCount: Value of type int which is the group count of the regular expression.
* [in] source: Value of type string reference which is the source to parse.
* [out] result: Value of type vecotr of strings which is the output of the parse.
* [in] allowDuplicate: Value of type bool which indicates whether duplicate items are added to the output result.
*
* Return Value
* Returns true if the function succeeds, or false otherwise.
*
* Remarks
* The output result is devided into groups. User should get the groups according to the group count. For example:
* 1. RegExp = L"{ab}", source = L"abcabe", then result = L"ab", L"ab".
* 2. RegExp = L"{ab}{cd}", source = L"abcdeabecd", then result = L"ab", L"cd", L"ab", L"cd".
*/
inline bool ParseRegExp(const wstring ®Exp, bool caseSensitive, int groupCount, const wstring &source, vector<wstring> &result, bool allowDuplicate = false)
{
result.clear();
if (regExp.size() <= 0)
{
return false;
}
if (groupCount <= 0)
{
return false;
}
if (source.size() <= 0)
{
return false;
}
CAtlRegExp<> re;
REParseError error = re.Parse(regExp.c_str(), caseSensitive);
if (error != REPARSE_ERROR_OK)
{
return false;
}
wchar_t *pSource = new wchar_t[source.size() + 1];
wchar_t *pSourceEnd = pSource + source.size();
if (pSource == NULL)
{
return false;
}
wcscpy_s(pSource, source.size() + 1, source.c_str());
BOOL bSucceed = TRUE;
CAtlREMatchContext<> mc;
const wchar_t *pFrom = pSource;
const wchar_t *pTo = NULL;
while (bSucceed)
{
bSucceed = re.Match(pFrom, &mc, &pTo);
if (bSucceed)
{
const wchar_t *pStart = NULL;
const wchar_t *pEnd = NULL;
vector<wstring> tempMatch;
for (int i = 0; i < groupCount; i++)
{
mc.GetMatch(i, &pStart, &pEnd);
if (pStart != NULL && pEnd != NULL)
{
wstring match(pStart, pEnd - pStart);
tempMatch.push_back(match);
}
else
{
break;
}
}
bool bAdd = true;
if (!allowDuplicate)
{
// Check whether this match already exists in the vector.
for (vector<wstring>::iterator it = result.begin(); it != result.end();)
{
bool bEqual = true;
for (vector<wstring>::iterator tempMatchIt = tempMatch.begin(); tempMatchIt != tempMatch.end(); tempMatchIt++, it++)
{
bool bGroupEqual = true;
if (caseSensitive)
{
bGroupEqual = (wcscmp(it->c_str(), tempMatchIt->c_str()) == 0);
}
else
{
bGroupEqual = (_wcsicmp(it->c_str(), tempMatchIt->c_str()) == 0);
}
if (!bGroupEqual)
{
bEqual = false;
}
}
if (bEqual)
{
bAdd = false;
break;
}
}
}
if (bAdd)
{
for (vector<wstring>::iterator tempMatchIt = tempMatch.begin(); tempMatchIt != tempMatch.end(); tempMatchIt++)
{
result.push_back(*tempMatchIt);
}
}
if (pTo < pSourceEnd)
{
pFrom = pTo;
}
else
{
break;
}
}
else
{
break;
}
}
delete[] pSource;
return true;
}
#endif // REGEXP_H
有谁可以帮我解决这个问题?尝试按字符串等更改wstring但它不起作用。
Does anyone can help me fix that? Tried change wstring by string etc but it didn't work.
推荐答案
您可以尝试使用W2A或W2CA宏,参见 ATL和MFC字符串转换宏 [ ^ ]
You can try to use the W2A or W2CA macro, see ATL and MFC String Conversion Macros[^]
这篇关于c ++ CAtlRegExp.Parse&匹配wstring的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文