Find (search) the specified string in stream stream and byte[]

Source: Internet
Author: User
Tags static class

Find (search) the specified string in stream stream and byte[]

Here is a look at two Search extension methods, one is the extension of the stream type, the other is the extension of the byte[] type,

If everyone has a better "algorithm", please give the reply, we optimize together.

--common extension code, need this part of the code support.

Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using System.IO;
Using System.Drawing;

Namespace IMS.BLL
{
<summary>
Transformation extension method classes between stream, String, byte[]
</summary>
public static Class Streamextend
{
#region Stream Extension
<summary>
Stream stream is converted to a byte array
</summary>
<returns></returns>
public static byte[] Tobytearray (the This stream stream)
{
byte[] bytes = new Byte[stream. Length];
Stream. Read (bytes, 0, bytes. Length);
Sets the current stream's position as the start of the stream
Stream. Seek (0, seekorigin.begin);
return bytes;
}
<summary>
Stream converted to image image
</summary>
<returns></returns>
public static Image Toimage (the This stream stream)
{
Image img = new Bitmap (stream);
return img;
}
<summary>
Stream into string, using Encoding.default encoding
</summary>
<returns></returns>
public static string Tostr (the This stream stream)
{
Return System.Text.Encoding.Default.GetString (stream. Tobytearray ());
}
<summary>
Searches for the specified byte[in the current stream]
</summary>
<param name= "Arr" ></param>
<param name= "key" > Search keyword </param>
<param name= "Beginposition" > Search start position </param>
<returns> returns the first occurrence of byte[in the stream if present, otherwise returns -1</returns>
public static long Search (this stream stream, long beginposition, byte[] key)
{
if (stream = = NULL | | stream. Length <= beginposition)
return-1;

if (key = null | | stream. Length < key. Length)
return-1;

Long I=-1;
Long j =-1;
int currentbyte = Int. MinValue;
For (I=beginposition;i<stream. length;i++)
{
if (stream. Length < key. Length + i)
Break

Stream. Seek (I, seekorigin.begin);
for (j = 0; J < Key. Length; J + +)
{
Currentbyte = stream. ReadByte ();
if (Currentbyte!= key[j])
Break
}
if (j = = key. Length)
return i;

if (Currentbyte = = 1)
Break
}
return-1;
}
#endregion

#region byte[] Extension
<summary>
Byte[] Convert to stream stream
</summary>
<returns></returns>
public static Stream Tostream (this byte[] arr)
{
Stream stream = new MemoryStream (arr);
Sets the position of the current stream as the start of the stream www.2cto.com
Stream. Seek (0, seekorigin.begin);
return stream;
}
<summary>
Byte[] Convert to Image
</summary>
<returns></returns>
public static Image Toimage (this byte[] arr)
{
Return Image.fromstream (arr. Tostream ());
}
<summary>
Convert to string, using Encoding.default encoding
</summary>
<returns></returns>
public static string Tostr (this byte[] arr)
{
return System.Text.Encoding.Default.GetString (arr);
}
<summary>
Search
</summary>
<param name= "Arr" ></param>
<param name= "key" > Search keyword </param>
<param name= "Beginpos" > Search start position </param>
<returns></returns>
public static int Search (this byte[] arr, int beginposition, byte[] key)
{
if (arr = null | | arr. Length <= beginposition)
return-1;

if (key = = NULL | | arr. Length < key. Length)
return-1;

int i =-1;
Int j =-1;
for (i = Beginposition i < arr. Length; i++)
{
if (arr. Length < key. Length + i)
Break

for (j = 0; J < Key. Length; J + +)
{
if (Arr[i+j]!= key[j])
Break
}
if (j = = key. Length)
return i;
}
return-1;
}
#endregion

#region String Extension
<summary>
string conversion to byte[]
</summary>
<returns></returns>
public static byte[] Tobytearray (This string str)
{
return System.Text.Encoding.Default.GetBytes (str);
}
<summary>
String converted to Stream
</summary>
<returns></returns>
public static Stream Tostream (This string str)
{
Stream stream = new MemoryStream (str. Tobytearray ());
Sets the current stream's position as the start of the stream
Stream. Seek (0, seekorigin.begin);
return stream;
}
#endregion
}
}

------------------------

--Test scripts

byte[] arr = "0123456789111". Tobytearray ();
byte[] Key1 = "123". Tobytearray ();
byte[] Key2 = "678". Tobytearray ();
byte[] Key3 = "911". Tobytearray ();
byte[] Key4 = "111". Tobytearray ();
In-stream search test
Stream sm = arr. Tostream ();
Long index1 = Sm. Search (0, Key1);
Long index2 = Sm. Search (0, Key2);
Long index3 = Sm. Search (0, Key3);
Long index4 = Sm. Search (0, Key4);
Byte[] Internal Search test
Long index10 = arr. Search (0, Key1);
Long index20 = arr. Search (0, Key2);
Long index30 = arr. Search (0, Key3);
Long index40 = arr. Search (0, Key4);

-----Excerpt from the Green Studio column





Original: http://www.2cto.com/kf/201204/129171.html Please respect the original author copyright

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.