Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a string from char array without copying it?

I have a very big char array that I need to convert to string in order to use Regex on it.
But it's so big that I get OutOfMemoryException when I pass that to string constructor.

I know that string is immutable and therefore it shouldn't be possible to specify its underlying character collection but I need a way to use regular expressions on that without copying the whole thing.

How do I get that array?

  • I get it from a file using StreamReader. I know the starting position and the length of the content to read, Read and ReadBlock methods need me to supply a char[] buffer.

So here are the things I want to know:

  • Is there a way to specify a string's underlaying collection? (Does it even keep its chars in an array?)
  • ...or using Regex directly on a char array?
  • ...or getting the part of the file directly as string?
like image 704
Şafak Gür Avatar asked Oct 30 '12 19:10

Şafak Gür


1 Answers

If you have a character or pattern that you could search for that is guaranteed NOT to be in the pattern you're trying to find, you could scan the array for that character and create smaller strings to process individually. Process would be something like:

char token = '|';
int start = 0;
int length = 0;
for(int i = 0; i < charArray.Length; i++;)
{
    if(charArray[i] == token)
    {
        string split = new string(charArray,start,length);
        // check the string using the regex

        // reset the length
        length = 0;
    }
    else
    {
        length++;
    }
}

That way you're copying smaller segments of the string that would be GCed after each attempt versus the entire string.

like image 108
D Stanley Avatar answered Sep 27 '22 20:09

D Stanley