Packageorg.servebox.toolbox.search
Classpublic class StringComparator

JUST FOR FUN ! StringComparator performs StartsWith, Boyer-Moore and Zhu-Takaoka string-matching comparison. It has now worst performances that the built-in String.indexOf method, that seems to perform a linear character-match.

The Boyer-Moore algorithm is considered as the most efficient string-matching algorithm in usual applications, and has the following features :
- performs the comparisons from right to left - pre-processing phase in Ο(m+σ) time and space complexity - searching phase in Ο(mn) time complexity - 3n text character comparisons in the worst case when searching for a non-periodic pattern

The ZT algorithm is a variant of the Boyer-Moore algorithm, and has the following features :
- use two consecutive text characters to compute the bad-character shift - pre-processing phase in Ο(m+σ²) time and space complexity - searching phase in Ο(mn) time complexity

References :
- Boyer, R. S., and Moore, J. S., 1977. A fast string searching algorithm. Comm. ACM. 20:762-772. - Charras, C. and Leroq. T., 1998. Exact string matching algorithms. Laboratoire d'Informatique de Rouen, Université de Rouen, 76821 Mont Saint-Aignan Cedex, France.
- Doyle B. Myers, java implementation of Boyer-Moore text search.

Usage :
if you want to search for the string abc inside the string asdadbsrsbbbfbebebrdfvdfbvabc:
StringComparator.prepareZt( "abc" );
var result:Boolean = StringComparator.zhuTakaokaMatch( "asdadbsrsbbbfbebebrdfvdfbvabc" );
// or
StringComparator.prepareBm( "abc" );
var result:Boolean = StringComparator.boyerMooreMatch( "asdadbsrsbbbfbebebrdfvdfbvabc" );
We use the "prepare-and-go" to avoid to pre-process multiple times if you need to search inside more than one string using the same pattern.



Public Properties
 PropertyDefined by
  ALPHABET_SIZE : Number = 256
[static] ASCII alphabet size (64k).
StringComparator
Public Methods
 MethodDefined by
  
boyerMooreMatch(text:String):Boolean
[static] Performs a BM string-matching search for the string pattern inside the string text.
StringComparator
  
prepareBm(pattern:String):void
[static] Pre-process pattern for Boyer-Moore matching.
StringComparator
  
prepareZt(pattern:String):void
[static] Pre-process pattern for Zhu-Takaoka matching.
StringComparator
  
zhuTakaokaMatch(text:String):Boolean
[static] Performs a ZT string-matching search for the string pattern inside the string text.
To use this algorithm, pattern length must be greater than 1 !!!
StringComparator
Property detail
ALPHABET_SIZEproperty
public static var ALPHABET_SIZE:Number = 256

ASCII alphabet size (64k).

Method detail
boyerMooreMatch()method
public static function boyerMooreMatch(text:String):Boolean

Performs a BM string-matching search for the string pattern inside the string text.

Parameters
text:String

Returns
Boolean — true if pattern inside text.
prepareBm()method 
public static function prepareBm(pattern:String):void

Pre-process pattern for Boyer-Moore matching.

Parameters
pattern:String
prepareZt()method 
public static function prepareZt(pattern:String):void

Pre-process pattern for Zhu-Takaoka matching.

Parameters
pattern:String
zhuTakaokaMatch()method 
public static function zhuTakaokaMatch(text:String):Boolean

Performs a ZT string-matching search for the string pattern inside the string text.
To use this algorithm, pattern length must be greater than 1 !!!

Parameters
text:String

Returns
Boolean — true if pattern inside text.