Page 1 of 1

[SOLVED] - Pattern Detection Help

Posted: Wed Jul 09, 2008 4:12 am
by Omega
Hi. I developed this snippet in JavaScript so that I could develop and test it quickly outside of my OS:

Code: Select all

function inString(source, pattern)
{
	var i=0;
	var x=0;
	var match=1;
	for(i=0;i<source.length;i++)
	{
		if(source[i]==pattern[0])
		{
			for(x=0;x<pattern.length;x++)
			{
				if(source[i+x]==pattern[x])
				{
					match=0;
				} else {
					match=1;
				}
			}
		}
		if(match==0) 
		{ 
			return 0; 
		}
	}
	return 1;
}
When I do this:

Code: Select all

<script>alert(inString("The red dog rows quickly","rows"));</script>
The return is 0 (which means it worked), so I translated it to C:

Code: Select all

int inString(const void *source, const void *pattern)
{
	int i=0;
	int x=0;
	int match=1;
	const char *src = (const char*) source;
	const char *ptn = (const char*) pattern;
	for(i=0;i<len(src);i++)
	{
		if(src[i]==ptn[0])
		{
			for(x=0;x<len(ptn);x++)
			{
				if(src[i+x]==ptn[x])
				{
					match=0;
				} else {
					match=1;
				}
			}
		}
		if(match==0) 
		{ 
			return 0; 
		}
	}
	return 1;
}
And it doesn't work. It returns 1 when I do this:

Code: Select all

printf(0x0F,1,1"Return = %d", inString("The red dog rows quickly","rows"));
Any Help? Thanks

Re: Pattern Detection Help

Posted: Wed Jul 09, 2008 7:08 am
by bewing
1. You are overwriting the "success" return value with a "fail" return value every single time you see a ptn[0] character in the string. You need to stop looping and return as soon as you get a "success".

2. First loop is going too far. It should stop at len(src) - len(ptn).

3. In the inner loop, you must wait until you've checked the ENTIRE pattern before you set match=0; (and then return immediately as per #1).

4. If there is a mismatch in the pattern vs source, the inner loop should exit immediately, and not check any more characters.

5. This loop structure is a LOT slower than it could be. Precalculate the len()s. Use the pointers, and not indexes. Count your loops down to 0, and not up to n.

Re: Pattern Detection Help

Posted: Wed Jul 09, 2008 8:05 pm
by Omega
Hmmm.. not sure if you see what I am doing here; maybe, but just in case here it is:

1. I loop the length of source: the red dog rows quickly
2. I then check within that loop if we find a match with the first character of pattern: (r)ows
3. If I find a match, I loop the length of pattern: rows
4. If all match then match will never be 1, else it becomes 1
5. I check if match is 0, if so, return 0, else keep looking
6. If match never becomes 0, it must be false so return 1

The only thing I can think of that it could fail is on number 3, where I could be failing once I hit that space behind it: rows_ <-- fails here, so it would be me overwriting my match=0, with match=1 which would return 1 and fail. I think I will do what you said in the inner for loop (count down to zero, that way I can't shoot past my mark on accident, and I will also pre-calculate the length for speed. I'll let you know what happens. thanks

Re: Pattern Detection Help

Posted: Wed Jul 09, 2008 10:20 pm
by octa
inString("The red dog rows quickly","raas")

your code returns 0 even for this one !!!!!

correct it..

Re: Pattern Detection Help

Posted: Wed Jul 09, 2008 10:25 pm
by Omega
Dude, you won't believe this crazyness. My logic as before was correct, it was that I needed to pre-calculate the length outside the loops:

Code: Select all

int inString(const void *source, const void *pattern)
{
	int i=0;
	int x=0;
	int match=1;
	const char *src = (const char*) source;
	const char *ptn = (const char*) pattern;
	int slen=len(src);
	int plen=len(ptn);
	for(i=0;i<slen;i++)
	{
		if(src[i]==ptn[0])
		{
			for(x=0;x<plen;x++)
			{
				if(src[i+x]==ptn[x])
				{
					match=0;
				} else {
					match=1;
				}
			}
		}
		if(match==0) 
		{ 
			return 0; 
		}
	}
	return 1;
}
Doing this:

Code: Select all

printf(0x0F,1,1,"(rowz) - Return = %d", inString("The red dog rows quickly","rowz"));
printf(0x0F,1,2,"(rows) - Return = %d", inString("The red dog rows quickly","rows"));
produces this:
(rowz) - Return = 1
(rows) - Return = 0
beautiful. thanks :)

EDIT: grrr.. I just tested that and you are right. This is annoying. Be right back.

EDIT: Fixed. :)

Code: Select all

int inString(const void *source, const void *pattern)
{
	int i=0;
	int x=0;
	int match=1;
	const char *src = (const char*) source;
	const char *ptn = (const char*) pattern;
	int slen=len(src);
	int plen=len(ptn);
	for(i=0;i<slen;i++)
	{
		if(src[i]==ptn[0])
		{
			for(x=0;x<plen;x++)
			{
				if(src[i+x]==ptn[x])
				{
					match=0;
				} else {
					match=1;
					break;
				}
			}
		}
		if(match==0) 
		{ 
			return 0; 
		}
	}
	return 1;
}
good eye, thanks.

Re: [SOLVED] - Pattern Detection Help

Posted: Wed Jul 09, 2008 11:56 pm
by Omega
Here is strpos:

Code: Select all

//*******************************
//Find the position of a string within a string
//Return: Position of first char of pattern,
//else if no match the return is -1.
//*******************************
int strpos(const void *source, const void *pattern)
{
	int i=0;
	int x=0;
	int match=1;
	int pos;
	const char *src = (const char*) source;
	const char *ptn = (const char*) pattern;
	int slen=len(src);
	int plen=len(ptn);
	for(i=0;i<slen;i++)
	{
		if(src[i]==ptn[0])
		{
			for(x=0;x<plen;x++)
			{
				if(src[i+x]==ptn[x])
				{
					match=0;
					pos=i;
				} else {
					match=1;
					pos=0;
					break;
				}
			}
		}
		if(match==0) 
		{ 
			return pos+1; 
		}
	}
	return -1;
}

Code: Select all

printf(0x0F,1,1,"(rows) - Return = %d",strpos("The red dog rows quickly.","rows"));
Output: 13