View Single Post
Ambitious contributor
18Aug2010,02:30  
pein87's Avatar
I tried to port the Boyer-Moore string matching algorithm from C to php, I have not tested it out yet because I have a project thats taking all of my time. I was wondering if someone would give it a go and let me know how it works and provide some feed back/help improve it. I picked them because they are said to be the fastest methods to do string matches. Yeah I know since php and C are so much alike I'm hoping that it works perfectly else I write my own algorithm for it using php.

Boyer-Moore string match algorithm php version
PHP Code:
<?php
 
define
('ALPHABET_SIZE',1);
 
function 
compute_prefix($str,$size,&$result=0
{
    
$result $size 1;
    
$q;
    
$k;
    
$result[0] = 0;
 
    
$k 0;
    for (
$q 1$q $size$q++) 
    {
        while (
$k && $str[$k] != $str[$q])
            
$k $result[$k-1];
 
        if (
$str[$k] == $str[$q])
            
$k++;
        
$result[$q] = $k;
    }
}
 
function 
prepare_badcharacter_heuristic($str,$size,$result=ALPHABET_SIZE)
{
 
    
$i 0;
 
    for (
$i 0$i ALPHABET_SIZE$i++)
        
$result[$i] = -1;
 
    for (
$i 0$i $size$i++)
        
$result[(int) $str[$i]] = $i;
}
 
function 
prepare_goodsuffix_heuristic($normal$size, &$result=1
{
    
$result $size 1;
    
$left = (string) $normal;
    
$right $left $size;
    
$reversed[$size+1];
    
$tmp $reversed $size;
    
$i;
 
    
/* reverse string */
    
$tmp 0;
    while (
$left $right)
        
$tmp $left++;
 
    
$prefix_normal[$size];
    
$prefix_reversed[$size];
 
    
compute_prefix($normal$size$prefix_normal);
    
compute_prefix($reversed$size$prefix_reversed);
 
    for (
$i 0$i <= $size$i++) 
    {
        
$result[$i] = $size $prefix_normal[$size-1];
    }
 
    for (
$i 0$i $size$i++) 
    {
        
$j $size $prefix_reversed[$i];
        
$k $i $prefix_reversed[$i]+1;
 
        if (
$result[$j] > $k) { $result[$j] = $k; }
    }
}
/*
 * Boyer-Moore search algorithm
 */
function boyermoore_search($haystack,$needle) {
    
/*
     * Calc string sizes
     */
    
$needle_len
    
$haystack_len;
    
$needle_len strlen($needle);
    
$haystack_len strlen($haystack);
 
    
/*
     * Simple checks
     */
    
if($haystack_len == 0) { return NULL; }
    if(
$needle_len == 0) { return $haystack; }
 
    
/*
     * Initialize heuristics
     */
    
$badcharacter[ALPHABET_SIZE];
    
$goodsuffix[$needle_len+1];
 
    
prepare_badcharacter_heuristic($needle$needle_len$badcharacter);
    
prepare_goodsuffix_heuristic($needle$needle_len$goodsuffix);
 
    
/*
     * Boyer-Moore search
     */
    
$s 0;
    while(
$s <= ($haystack_len $needle_len))
    {
        
$j $needle_len;
        while(
$j && $needle[$j-1] == $haystack[$s+$j-1])
            
$j--;
 
        if(
$j 0)
        {
            
$k $badcharacter[(int) $haystack[$s+$j-1]];
            
$m;
            if(
$k < (int)$j && ($m $j-$k-1) > $goodsuffix[$j])
                
$s += $m;
            else
                
$s += $goodsuffix[$j];
        }
        else
        {
            return 
$haystack $s;
        }
    }
 
    return 
NULL;
}
?>
Boyer-Moore-Horspool string matching algorithm php version
PHP Code:
<?php
define
('UCHAR_MAX',255);
function 
boyermoore_horspool_memmem($haystack,$needle)
{
    
$hlen strlen($haystack);
    
$nlen strlen($needle);
    
$scan 0;
    
$bad_char_skip[UCHAR_MAX 1];
 
    if (
$nlen <= || !$haystack || !$needle) { return NULL; }
 
    for (
$scan 0$scan <= UCHAR_MAX$scan $scan 1)
        
$bad_char_skip[$scan] = $nlen;
 
    
$last $nlen 1;
 
    for (
$scan 0$scan $last$scan $scan 1)
        
$bad_char_skip[$needle[$scan]] = $last $scan;
 
    while (
$hlen >= $nlen)
    {
        for (
$scan $last$haystack[$scan] == $needle[$scan]; $scan $scan 1)
            if (
$scan == 0) { return $haystack; }
 
       
        
$hlen -= $bad_char_skip[$haystack[$last]];
        
$haystack += $bad_char_skip[$haystack[$last]];
    }
    
    return 
NULL;
}


?>
Boyer-Moore string match C version
Code:
#include <limits.h>
#include <string.h>
 
#define ALPHABET_SIZE (1 << CHAR_BIT)
 
static void compute_prefix(const char* str, size_t size, int result[size]) {
	size_t q;
	int k;
	result[0] = 0;
 
	k = 0;
	for (q = 1; q < size; q++) {
		while (k > 0 && str[k] != str[q])
			k = result[k-1];
 
		if (str[k] == str[q])
			k++;
		result[q] = k;
	}
}
 
static void prepare_badcharacter_heuristic(const char *str, size_t size,
		int result[ALPHABET_SIZE]) {
 
	size_t i;
 
	for (i = 0; i < ALPHABET_SIZE; i++)
		result[i] = -1;
 
	for (i = 0; i < size; i++)
		result[(size_t) str[i]] = i;
}
 
void prepare_goodsuffix_heuristic(const char *normal, size_t size,
		int result[size + 1]) {
 
	char *left = (char *) normal;
	char *right = left + size;
	char reversed[size+1];
	char *tmp = reversed + size;
	size_t i;
 
	/* reverse string */
	*tmp = 0;
	while (left < right)
		*(--tmp) = *(left++);
 
	int prefix_normal[size];
	int prefix_reversed[size];
 
	compute_prefix(normal, size, prefix_normal);
	compute_prefix(reversed, size, prefix_reversed);
 
	for (i = 0; i <= size; i++) {
		result[i] = size - prefix_normal[size-1];
	}
 
	for (i = 0; i < size; i++) {
		const int j = size - prefix_reversed[i];
		const int k = i - prefix_reversed[i]+1;
 
		if (result[j] > k)
			result[j] = k;
	}
}
/*
 * Boyer-Moore search algorithm
 */
const char *boyermoore_search(const char *haystack, const char *needle) {
	/*
	 * Calc string sizes
	 */
	size_t needle_len, haystack_len;
	needle_len = strlen(needle);
	haystack_len = strlen(haystack);
 
	/*
	 * Simple checks
	 */
	if(haystack_len == 0)
		return NULL;
	if(needle_len == 0)
		return haystack;
 
	/*
	 * Initialize heuristics
	 */
	int badcharacter[ALPHABET_SIZE];
	int goodsuffix[needle_len+1];
 
	prepare_badcharacter_heuristic(needle, needle_len, badcharacter);
	prepare_goodsuffix_heuristic(needle, needle_len, goodsuffix);
 
	/*
	 * Boyer-Moore search
	 */
	size_t s = 0;
	while(s <= (haystack_len - needle_len))
	{
		size_t j = needle_len;
		while(j > 0 && needle[j-1] == haystack[s+j-1])
			j--;
 
		if(j > 0)
		{
			int k = badcharacter[(size_t) haystack[s+j-1]];
			int m;
			if(k < (int)j && (m = j-k-1) > goodsuffix[j])
				s+= m;
			else
				s+= goodsuffix[j];
		}
		else
		{
			return haystack + s;
		}
	}
 
	return NULL; // not found
}
Boyer-Moore-Horspool string matching in C
Code:
#include <string.h>
#include <limits.h>
 
/* The constant UCHAR_MAX is assumed to contain the maximum
 * value of the input character type. Typically it's 255.
 * size_t is an unsigned type for representing sizes.
 * If your system doesn't have it, replace with
 * unsigned int.
 */
 
/* Returns a pointer to the first occurrence of "needle"
 * within "haystack", or NULL if not found. Works like 
 * memmem().
 */
const unsigned char *
boyermoore_horspool_memmem(const unsigned char* haystack, ssize_t hlen,
                           const unsigned char* needle,   ssize_t nlen)
{
    size_t scan = 0;
    size_t bad_char_skip[UCHAR_MAX + 1]; /* Officially called:
                                          * bad character shift */
 
    /* Sanity checks on the parameters */
    if (nlen <= 0 || !haystack || !needle)
        return NULL;
 
    /* ---- Preprocess ---- */
    /* Initialize the table to default value */
    /* When a character is encountered that does not occur
     * in the needle, we can safely skip ahead for the whole
     * length of the needle.
     */
    for (scan = 0; scan <= UCHAR_MAX; scan = scan + 1)
        bad_char_skip[scan] = nlen;
 
    /* C arrays have the first byte at [0], therefore:
     * [nlen - 1] is the last byte of the array. */
    size_t last = nlen - 1;
 
    /* Then populate it with the analysis of the needle */
    for (scan = 0; scan < last; scan = scan + 1)
        bad_char_skip[needle[scan]] = last - scan;
 
    /* ---- Do the matching ---- */
 
    /* Search the haystack, while the needle can still be within it. */
    while (hlen >= nlen)
    {
        /* scan from the end of the needle */
        for (scan = last; haystack[scan] == needle[scan]; scan = scan - 1)
            if (scan == 0) /* If the first byte matches, we've found it. */
                return haystack;
 
        /* otherwise, we need to skip some bytes and start again. 
           Note that here we are getting the skip value based on the last byte
           of needle, no matter where we didn't match. So if needle is: "abcd"
           then we are skipping based on 'd' and that value will be 4, and
           for "abcdd" we again skip on 'd' but the value will be only 1.
           The alternative of pretending that the mismatched character was 
           the last character is slower in the normal case (Eg. finding 
           "abcd" in "...azcd..." gives 4 by using 'd' but only 
           4-2==2 using 'z'. */
        hlen     -= bad_char_skip[haystack[last]];
        haystack += bad_char_skip[haystack[last]];
    }
 
    return NULL;
}

Last edited by pein87; 18Aug2010 at 02:32..
Seansa Seven like this