Passing text/strings as a byte ptr?

BlitzMax Forums/BlitzMax Beginners Area/Passing text/strings as a byte ptr?

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

[edit] Actually, you can ignore this. I've decided to bite the bullet and just go in and modify the String source to suit my needs.


Okay, so I'm working on implementing my own string type and I've got a question regarding the underlying way BMax is going to handle something.

Here's the beginning of my type
Type TString

	Field m_data:Byte Ptr
	Field m_length:Int
	
	Function Constructor:TString( data:Byte Ptr, length:Int )
		
		Local l_NewTString:TString
		l_NewTString = New TString
		l_NewTString.m_data = data
		l_NewTString.m_length = length
		
		Return l_NewTString	
	EndFunction

	Method ReturnString:String( )
		Return string.frombytes( m_data, m_length )
	EndMethod
	
EndType


Now, if I do this
Global test$ = "This is only a test"
Global str:TString
str = TString.Constructor( test , test.length )
Print str.ReturnString()


The code does, for some reason, work. The line

Print str.ReturnString()

Does in fact return the expected text. However, modifying the m_data field has no effect whatsoever on the original string "test$'. This leads me to believe that when a string is passed into a function expecting a byte ptr (as Constructor() does), it converts itself to a normal C-string. That's fine.

What I'm curious about tho, is - if I simply pass an undefined and undeclared chunk of text into the function - how will that be handled. What will happen if I do this
str = TString.Constructor( "random text", 11 )


Calling ReturnString() works as expected. The code works. I'm just worried that when I pass in a character array, BMax is actually creating a String object out of it, and then calling .ToCString to return a byte ptr to the function.

I'm trying to avoid the extra copying and overhead of the ToCString() call, so if what I suspect above is actually happening, how might I pass some text into the function without the overhead?