GameMonkey Script

GameMonkey Script Forums
It is currently Mon Nov 20, 2017 3:37 pm

All times are UTC




Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: Wed Oct 20, 2010 6:45 pm 
Offline

Joined: Wed May 26, 2010 11:08 am
Posts: 4
Hi all,
I have rather newbie question.

I in need to replace one character in an gm string.

I've figured out, that I can't simply write in the script:
Code:
a[0]='b';

So I decided to write my own library function putChar(myChar,index):

Code:
static int GM_CDECL gm_putChar(gmThread * a_thread)
{

/* type checking omitted */

    GM_CHECK_INT_PARAM(myChar,0);
    GM_CHECK_INT_PARAM(index,1);

/* code ripped from gmStringLib.cpp */

    const gmVariable * var = a_thread->GetThis();
    GM_ASSERT(var->m_type == GM_STRING);
    gmStringObject * strObj = (gmStringObject *) GM_OBJECT(var->m_value.m_ref);
    const char * str = (const char *) *strObj;

    char *s=(char*)str;

    int length = strObj->GetLength();

    if(index>=length) {
        a_thread->GetMachine()->GetLog().LogEntry("ERROR: index exceeds string length.","");
        return GM_EXCEPTION;
    }
    s[index]=(char)myChar;
    return GM_OK;
}


When I run this code, the function works fine, character is replaced but
I'm getting error message, which is generated in gmMachine destructor on line 305:
Code:
  GM_ASSERT(m_strings.Count() == 0);

The m_strings.Count() is equal to 1 (should be 0).
When I remove this line:
Code:
    s[index]=(char)myChar;

from my function, I'm not getting any errors, however I'm not getting the desired result :lol:

So my question is: Have I missed something?
Is there an easy way to replace particular character in the gm string?

Thanks,
Mike


Top
 Profile  
Reply with quote  
PostPosted: Thu Oct 21, 2010 2:53 am 
Offline

Joined: Mon Dec 15, 2003 1:38 pm
Posts: 698
gmStrings are immutable, so the behavior you experience is by design.
If you look at the existing gmStringLib you will notice that strings are never modified, instead, a new string with the modification is created and returned / assigned.
There are various reasons for this, but it is fairly common logic in managed languages.
The obvious question here is performance! When modifying strings, either changing the characters in an existing string or appending sub strings, string operations are not efficient.
What is needed here is a new type... StringBuilder, a new utility type that would help efficiently work with strings. (.Net Framework has such a thing also.) Unfortunately noone has written or shared such a binding for GM thus far.
In the mean time, make your binding return a modified copy of the string and see how it performs. If gm string operations slow your application, consider implementing a StringBuilder or perhaps other native helpers.


Top
 Profile  
Reply with quote  
PostPosted: Thu Oct 21, 2010 8:17 am 
Offline

Joined: Wed May 26, 2010 11:08 am
Posts: 4
Oh, I see the point.

Speed in my app is not a problem, I was rather
concerned about memory, which is occupied
by the old, unneeded string.

So by now I can only assume, that the garbage
collector at some point will take care of it.

Anyway, thanks for the explanation.

-Mike

UPDATE:
I've finally figured out, how to write the string a way
that it contains the desired character (0x06):
"\006"


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group