1. Welcome to TechPowerUp Forums, Guest! Please check out our forum guidelines for info related to our community.

Snippet C#: Find/Replace

Discussion in 'Programming & Webmastering' started by FordGT90Concept, Oct 26, 2008.

  1. FordGT90Concept

    FordGT90Concept "I go fast!1!11!1!"

    Joined:
    Oct 13, 2008
    Messages:
    13,596 (6.24/day)
    Thanks Received:
    3,519
    Location:
    IA, USA
    One day I was trying to come up with the best way find a series of bytes inside of a byte array. I concluded a slider method is best. A slider looks at every byte and then "slides" up, down, left, right, or around the byte in order to find the entire sequences of the pattern. For instance, if our bytes include the phrase "jimbob" and we search for "bob," it would see that the first three letters are not a "b" and ignore them. Once it sees the "b," it notes it. It then proceeds to check subsequent letters for the necessary "o" and finishing "b." In the case of "jimbob" it would find all three and mark offset 3 as a found instance.

    Having finished that code, I thought "why not do it backwards, up/down, diagonally, and close/farther away from you?" So I did. Not only can this code find "bob" in "jimbob," it can find it twice: "jimbob" and "bobmij." It can even complete a word search for you given an array of characters and the words to search for. I even go one better and add support for 3D word searches, which, to my knowledge, don't even exist (too much to wrap the brain around and stick on paper I suppose).

    Anyway, to the code. I am not going to spell out how the source works in this one because it is quite short but complex. Instead, I'll give you examples on how to use these features.

    First, there is three files in here: LargeFile, SmallFile, and Search.

    LargeFile uses a buffer to search the file. This uses very little memory and is great for bigger files (over 5 MiB). SmallFile loads the entire file into the memory. Use this especially if the file you are searching is small and you are going to be searching it over and over again. Both LargeFile and SmallFile are binary-safe and case sensitive. They both also require access to the Search class.

    Search contains all the algorithms:
    Code:
    int[] Find(byte[] needle, string path, int buffersize, int limit)
    int[] Find(byte[] needle, byte[] haystack, int limit)
    Found1D[] Find1D(byte[] needle, byte[] haystack)
    Found2D[] Find2D(byte[] needle, byte[,] haystack)
    Found3D[] Find3D(byte[] needle, byte[,,] haystack)
    The first Find is for searching in files. The second Find is for searching in memory. Both of these only go from left to right. Example:
    Code:
    int[] results = Search.Find(System.Text.Encoding.ASCCI.GetBytes("bob"), "myfile.txt", 4096, -1);
    Code:
    int[] results = Search.Find(System.Text.Encoding.ASCCI.GetBytes("bob"), System.Text.Encoding.ASCCI.GetBytes("bobobobobobob"), -1);
    Find1D searches memory from left to right and right to left.
    Code:
    byte[] haystack = System.Text.Encoding.ASCCI.GetBytes("bobobobobobob");
    Found1D[] results = Find1D(System.Text.Encoding.ASCCI.GetBytes("bob"), haystack);
    Find2D searches memory from bottom center clockwise to bottom right.
    Code:
    byte[,] haystack = new byte[3, 3];
    haystack[0] = System.Text.Encoding.ASCCI.GetBytes("bob");
    haystack[1] = System.Text.Encoding.ASCCI.GetBytes("bob");
    haystack[2] = System.Text.Encoding.ASCCI.GetBytes("bob");
    Found2D[] results = Find2D(System.Text.Encoding.ASCCI.GetBytes("bob"), haystack);
    Find3D searches memory in a random order from the top layer (above the needle) through the bottom layer (below the needle).
    Code:
    byte[,,] haystack = new byte[3, 3, 3];
    haystack[0, 0] = System.Text.Encoding.ASCCI.GetBytes("bob");
    haystack[0, 1] = System.Text.Encoding.ASCCI.GetBytes("bob");
    haystack[0, 2] = System.Text.Encoding.ASCCI.GetBytes("bob");
    haystack[1, 0] = System.Text.Encoding.ASCCI.GetBytes("bob");
    haystack[1, 1] = System.Text.Encoding.ASCCI.GetBytes("bob");
    haystack[1, 2] = System.Text.Encoding.ASCCI.GetBytes("bob");
    haystack[2, 0] = System.Text.Encoding.ASCCI.GetBytes("bob");
    haystack[2, 1] = System.Text.Encoding.ASCCI.GetBytes("bob");
    haystack[2, 2] = System.Text.Encoding.ASCCI.GetBytes("bob");
    Found3D[] results = Find3D(System.Text.Encoding.ASCCI.GetBytes("bob"), haystack);

    I cannot be held account for students using this to complete their word search assignments. :cool:
     

    Attached Files:

    Last edited: Jun 7, 2012
    Oliver_FF and Kreij say thanks.
    Crunching for Team TPU

Currently Active Users Viewing This Thread: 1 (0 members and 1 guest)

Share This Page