Click here to Skip to main content
13,291,690 members (56,404 online)
Click here to Skip to main content


36 bookmarked
Posted 15 Jun 2002

A Multiple Substring Search Class: CIVStringSet

, 25 Apr 2010
A string array class using MFC or STL that performs very fast multiple string searches
// StringSet.H: interface for the CIVStringSet class.
// Written 12 June 2002 by Scot T Brennecke
// Thanks to Moishe Halibard and Moshe Rubin for their article,
//    "A Multiple Substring Search Algorithm" in the June 2002
//    edition of C/C++ Users Journal.  This class is based on
//    the algorthim therein described, but extended to return
//    all strings and use MFC classes.

#pragma once

class CIVStringSet : public CStringArray
        CIVStringSet( WORD wInitialWidth = 64 ) ;  // Initial width of FSM
        virtual ~CIVStringSet() ;

        bool     Add( LPCTSTR pszWord ) ;               // add a single word
        bool     Add( const CString & rstrWord ) ;      // add a single word
        int      Add( LPCTSTR pszzWords, int nWords ) ; // add nWords words, each 0 term'd, with extra 0 at end
        int      Add( CStringArray astrWords ) ;        // add all the elements of a CStringArray
        int      Add( CStringList  lstrWords ) ;        // add all the elements of a CStringList

        bool     FindFirstIn( CString strText, CString & rstrFirst ) ;    // Begin iteration
        bool     FindNext( CString & rstrNext ) ;                         // Continue interation
        int      FindAllIn( CString strText, CStringList & rlstrWords ) ; // Iterate all at once and make list

        DWORD (* m_apnFSM)[128] ;   // Finite State Machine. Array of 128 char arrays
        size_t   m_nCurDim ;        // Dimension of allocated width of FSM
        size_t   m_nUsedCols ;      // Used portion of allocated width
        WORD     m_wMaxUsedState ;  // largest state value used
        CString  m_strSearch ;      // Current search string
        UINT     m_nCurTextChar ;   // Current position in search string

        bool     InsertWord( LPCTSTR pszWord, WORD wIndex ) ; // put the new word into the FSM for given index
        bool     SetColDim( size_t nNewDim ) ;                // set the current width to at least nNewDim columns

        // Hide several base class members that shouldn't be used
        // Using these members could cause the index numbers of strings to change, throwing
        //     off the embedded index entries in the FSM
        void SetAt( int nIndex, LPCTSTR newElement ) ;
        void SetAt( int nIndex, const CString & newElement ) ;
        void SetAtGrow( int nIndex, LPCTSTR newElement ) ;
        void SetAtGrow( int nIndex, const CString & newElement ) ;
        int Append( const CStringArray & src ) ;
        void Copy( const CStringArray & src ) ;
        void InsertAt( int nIndex, LPCTSTR newElement, int nCount = 1 ) ;
        void InsertAt( int nIndex, const CString & newElement, int nCount = 1 ) ;
        void RemoveAt( int nIndex, int nCount = 1 ) ;
        void InsertAt( int nStartIndex, CStringArray * pNewArray ) ;
} ;

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Scot Brennecke
Software Developer (Senior) Microsoft
United States United States
Scot is a Sr. Escalation Engineer for the Microsoft Developer Support Languages team. He helps software developers who are Microsoft customers find bugs in their own, or Microsoft's, code.

Scot spends most of his time writing, reading, or thinking about C++ software, thereby classifying him as a geek.

You may also be interested in...

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.171207.1 | Last Updated 25 Apr 2010
Article Copyright 2002 by Scot Brennecke
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid