示例#1
0
文件: xRegex.cpp 项目: BorMor/xEngine
bool xRegex::Match(const xString& input, xMatch& match)
{
	bool result = false;
	if (pImpl->mCompiledPattern)
	{	
		int offsets[OFFSET_COUNT];
			
		int rc = pcre_exec(pImpl->mCompiledPattern, 0, input.c_str(), 0, input.Length(), 0, offsets, OFFSET_COUNT);          
		if (rc > 0)
		{
			if (!result)
				match.Groups.mGroups.Clear();
			result = true;
			match.mSuccess = true;
			for (int i = 0; i < rc; i++)
			{
				xGroup* group;
				if (i == 0)
					group = &match;
				else
					group = new xGroup();
						
				group->mIndex = offsets[2*i];
				group->mLength = offsets[2*i+1] - offsets[2*i];
				group->mValue.Set(input.c_str() + offsets[2*i], offsets[2*i+1] - offsets[2*i]);

				if (i > 0)
					match.Groups.mGroups.AddBack(group);
			}
		}
	}
	
	return result;
}
示例#2
0
文件: xRegex.cpp 项目: BorMor/xEngine
xString xRegex::Replace(const xString& input, const xString replacement)
{
	xString result;
	if (pImpl->mCompiledPattern)
	{	
		int offsets[OFFSET_COUNT];

		unsigned int offset = 0;
		unsigned int len = input.Length();
	
		while (offset < len)
		{
			int rc = pcre_exec(pImpl->mCompiledPattern, 0, input.c_str(), len, offset, 0, offsets, OFFSET_COUNT);          
			if (rc > 0)
			{
				const char* s1 = input.c_str() + offset;
				result += xString(input.c_str() + offset, offsets[0] - offset) + replacement;
				offset = offsets[1];
			}
			else
			{
				result += xString(input.c_str() + offset, len - offset);
				break;
			}
			
		}
	}
	return result;
}
示例#3
0
文件: xRegex.cpp 项目: BorMor/xEngine
bool xRegex::IsMatch(const xString& input)
{
	if (pImpl->mCompiledPattern)
	{
		int offsets[3];
		if (pcre_exec(pImpl->mCompiledPattern, 0, input.c_str(), input.Length(), 0, 0, offsets, 3) > 0)
			return true;
	}

	return false;
}
示例#4
0
文件: xRegex.cpp 项目: BorMor/xEngine
bool xRegex::Matches(const xString& input, xMatchCollection& matches)
{
	bool result = false;
	if (pImpl->mCompiledPattern)
	{	
		int offsets[OFFSET_COUNT];

		unsigned int offset = 0;
		unsigned int len = input.Length();
			
		while (offset < len)
		{
			int rc = pcre_exec(pImpl->mCompiledPattern, 0, input.c_str(), len, offset, 0, offsets, OFFSET_COUNT);          
			if (rc > 0)
			{
				if (!result)
					matches.mMatches.Clear();
				result = true;
				xMatch* match = new xMatch();
				match->mSuccess = true;
				for (int i = 0; i < rc; i++)
				{
					xGroup* group;
					if (i == 0)
						group = match;
					else
						group = new xGroup();
						
					group->mIndex = offsets[2*i];
					group->mLength = offsets[2*i+1] - offsets[2*i];
					group->mValue.Set(input.c_str() + offsets[2*i], offsets[2*i+1] - offsets[2*i]);

					if (i > 0)
						match->Groups.mGroups.AddBack(group);
				}
				matches.mMatches.AddBack(match);
				offset = offsets[1];
			}
			else
				break;
			
		}
	}
	
	return result;
}
示例#5
0
void xBinaryWriter::WriteString(const xString& string)
{
	xUInt16 length = (xUInt16)string.Length();
	WriteUInt16(length);
	mStream->Write((void*)string.c_str(), length);
}