65.9K
CodeProject is changing. Read more.
Home

XMemString - Extended memory and string search functions

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.68/5 (12 votes)

May 12, 2003

CPOL

1 min read

viewsIcon

67932

downloadIcon

806

Extended string and memory search functions: memmem(), memimem(), memichr(), _tcsistr()

  • Download demo project - 21 Kb

    Introduction

    XMemString is a collection of memory and string search functions I have been using for many years. They are all modelled on the functions available in the standard C runtime, so they are fairly self-explanatory.

    Function Descriptions

    • memichr() - Find character in a buffer (case insensitive).
      ///////////////////////////////////////////////////////////////////////////////
      //
      // memichr()
      //
      // Purpose:     Find character in a buffer (case insensitive)
      //
      // Parameters:  buf      - pointer to buffer
      //              c        - character to look for
      //              buf_len  - size of buffer in bytes
      //
      // Returns:     void *   - if successful, returns a pointer to the first
      //                         occurrence of c in buf;  otherwise, returns NULL
      //
      // Notes;       memichr() will search by ignoring the case of those characters
      //              that fall in the ANSI range a-z and A-Z.
      // 
    • memimem() - Find a byte sequence within a memory buffer (case insensitive)
      ///////////////////////////////////////////////////////////////////////////////
      //
      // memimem()
      //
      // Purpose:     Find a byte sequence within a memory buffer (case insensitive)
      //
      // Parameters:  buf               - pointer to buffer
      //              buf_len           - size of buffer in bytes
      //              byte_sequence     - byte sequence to look for
      //              byte_sequence_len - size of byte sequence in bytes
      //
      // Returns:     void * - if successful, returns a pointer to the first
      //                       occurrence of byte_sequence in buf;  otherwise,
      //                       returns NULL
      //
      // Notes;       memimem() will search by ignoring the case of those characters
      //              that fall in the ANSI range a-z and A-Z.
      //
      
    • memmem() - Find a byte sequence within a memory buffer
      ///////////////////////////////////////////////////////////////////////////////
      //
      // memmem()
      //
      // Purpose:     Find a byte sequence within a memory buffer
      //
      // Parameters:  buf               - pointer to buffer
      //              buf_len           - size of buffer in bytes
      //              byte_sequence     - byte sequence to look for
      //              byte_sequence_len - size of byte sequence in bytes
      //
      // Returns:     void * - if successful, returns a pointer to the first
      //                       occurrence of byte_sequence in buf;  otherwise,
      //                       returns NULL
      //
      // Notes;       Characters in byte_sequence and characters in buf will be
      //              compared "as is", with no case conversion.
      // 
    • _tcsistr() - Find a substring within a string (case insensitive)
      ///////////////////////////////////////////////////////////////////////////////
      //
      // _tcsistr()
      //
      // Purpose:     Find a substring within a string (case insensitive)
      //
      // Parameters:  string     - nul-terminated string to search
      //              strCharSet - nul-terminated string to search for
      //
      // Returns:     void * - if successful, returns a pointer to the first
      //                       occurrence of strCharSet in string;  otherwise,
      //                       returns NULL
      //
      // Notes;       _tcsistr() will search by ignoring the case of those characters
      //              that fall in the ANSI range a-z and A-Z.
      // 

    How To Use

    To integrate XMemString functions into your app, you first need to add following files to your project:

    • XMemString.cpp
    • XMemString.h

    If you include XMemString in project that uses precompiled headers, you must change C/C++ Precompiled Headers settings to Not using precompiled headers for XMemString.cpp.

    Next, include the header file XMemString.h in appropriate project files. Now you are ready to start using XMemString. Please see XMemStringTest.cpp for examples.

    Revision History

    Version 1.0 - 2003 May 11

    • Initial public release

    Usage

    This software is released into the public domain. You are free to use it in any way you like. If you modify it or extend it, please to consider posting new code here for everyone to share. This software is provided "as is" with no expressed or implied warranty. I accept no liability for any damage or loss of business that this software may cause.