CODECUBE VENTURES

Holy Streaming Strings Batman!

I was browsing around and found this article on a cool use of streams. Within the sample code, there was this nifty little class that extends a MemoryStream so you can easily pass a string into memory ...

public class StringStream : MemoryStream
{
public StringStream(string text) : base()
{
StreamWriter sw = new StreamWriter(this);
sw.Write(text);
sw.Flush();
Position = 0;
}

public StringStream(string text, params object[] args) : base()
{
StreamWriter sw = new StreamWriter(this);
sw.Write(text, args);
sw.Flush();
Position = 0;
}
}

So the brain gets to thinking ... Rico warns us about the use of strings (Principle #4), where String.SubString and String.Split might hurt performance. I wonder if there's a way to extend this concept further and write a mutable string class where things like substring and such wouldn't be such a drain.

So, every "MutableString" would simply point to a memory string of the original ... any subsequent operations (such as SubString and Split) would spawn new MutableStrings that merely point to the original data in memory, but operate using offsets.

Hmm ... this would be a lot easier if pointers weren't an unsafe construct

Latest post: Digging Up the First Version of CodeCube

See more in the archives