SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Scripting Help > Help Archive

 
 
Thread Tools Display Modes
Old 22/11/2010, 12:32 PM   #1
kLx
Huge Clucker
 
kLx's Avatar
 
Join Date: Feb 2008
Posts: 206
Reputation: 6
Default Letters replace in string?

Hello, I have this code to remove utf8 letters from text, so they can be shown on textdraws:

pawn Code:
stock
    unicode_to_ansi ( const string [ ] )
{
    new
        array [ 2 ][ ] =
    {
        {"ą", "a"},
        {"č", "c"},
        {"ę", "e"},
        {"ė", "e"},
        {"į", "i"},
        {"", "s"},
        {"ų", "u"},
        {"ū", "u"},
        {"", "z"}
    };
   
    new
        string_len = strlen ( string ),
        array_size = sizeof ( array  ),
        i = 0;
   
    while ( i < string_len )
    {
        for ( new c = 0; c < array_size; c++ )
        {
            if (( chrtolower ( string [ i ] ) == array [ c ][ 0 ] ))
            {
                string [ i ] = array [ c ][ 1 ];
            }
        }
        i++;
    }
}
But when i hit compile, compiler crashes, whats the problem?
kLx is offline  
Old 22/11/2010, 12:37 PM   #2
Gamer_Z
Banned
 
Join Date: Jun 2009
Posts: 1,047
Reputation: 1370
Default Re: Letters replace in string?

won't str_replace work for you? (i know it will be slower but now i'm at schoool pc and at home my internet is off...)
Gamer_Z is offline  
Old 22/11/2010, 12:42 PM   #3
LarzI
High-roller
 
LarzI's Avatar
 
Join Date: Apr 2007
Location: Norway
Posts: 2,628
Reputation: 136
Default Re: Letters replace in string?

Why not returning anything? When you've got no variables to hold the information, you have to return something so that the function makes use of itself. (correct me if I'm wrong)

Isn't it better to use strfind instead of doing a normal if == check?
__________________


NEW: [FS] Simple Cellphones

if( strfind( title, "REP", true ) != -1 )
{________________________________________
Sigh();__________________________
}________________________________________


LarzI is offline  
Old 22/11/2010, 12:49 PM   #4
kLx
Huge Clucker
 
kLx's Avatar
 
Join Date: Feb 2008
Posts: 206
Reputation: 6
Default Re: Letters replace in string?

Totally forgot about these ones
kLx is offline  
Old 22/11/2010, 12:52 PM   #5
kLx
Huge Clucker
 
kLx's Avatar
 
Join Date: Feb 2008
Posts: 206
Reputation: 6
Default Re: Letters replace in string?

******,
pawn Code:
stock
    unicode_to_ansi ( string [ ] )

still crash

I think mistake is here, because when i remove this, it doesn`t crash

pawn Code:
new
        array [ 2 ][ ] =
    {
        {"ą", "a"},
        {"č", "c"},
        {"ę", "e"},
        {"ė", "e"},
        {"į", "i"},
        {"", "s"},
        {"ų", "u"},
        {"ū", "u"},
        {"", "z"}
    };
kLx is offline  
Old 22/11/2010, 01:13 PM   #6
kLx
Huge Clucker
 
kLx's Avatar
 
Join Date: Feb 2008
Posts: 206
Reputation: 6
Default Re: Letters replace in string?

Okay, got it working without crashes, but it doesn`t really work.
I put this in OnGameModeInit:
pawn Code:
new
        string [ ] = "ąčędfasf5112";
       
    printf ( "Before: %s", string );
    unicode_to_ansi ( string );
    printf ( "after: %s", string );
And in console i just get:
Code:
[16:22:25] Before: ąčędfasf5112
[16:22:25] after:
Function:
pawn Code:
stock
    unicode_to_ansi ( string [ ] )
{
    new
        array [ ][ ] =
    {
        {"ą", "a"},
        {"č", "c"},
        {"ę", "e"},
        {"ė", "e"},
        {"į", "i"},
        {"", "s"},
        {"ų", "u"},
        {"ū", "u"},
        {"", "z"}
    };
   
    new
        string_len = strlen ( string ),
        array_size = sizeof ( array  ),
        i = 0;
   
    while ( i < string_len )
    {
        for ( new c = 0; c < array_size; c++ )
        {
            if (( chrtolower ( string [ i ] ) == array [ c ][ 0 ] ))
            {
                string [ i ] = array [ c ][ 1 ];
            }
        }
        i++;
    }
}
kLx is offline  
Old 22/11/2010, 02:23 PM   #7
LarzI
High-roller
 
LarzI's Avatar
 
Join Date: Apr 2007
Location: Norway
Posts: 2,628
Reputation: 136
Default Re: Letters replace in string?

Quote:
Originally Posted by ****** View Post
You are trying to modify a constant string.

LarzI: You are wrong, strings are always passed by reference, so modifying an input modifies the original.
Okay, my bad then.
__________________


NEW: [FS] Simple Cellphones

if( strfind( title, "REP", true ) != -1 )
{________________________________________
Sigh();__________________________
}________________________________________


LarzI is offline  
Old 22/11/2010, 03:01 PM   #8
kLx
Huge Clucker
 
kLx's Avatar
 
Join Date: Feb 2008
Posts: 206
Reputation: 6
Default Re: Letters replace in string?

Quote:
Originally Posted by ****** View Post
I'm not sure how well chrtolower deals with unicode characters. This code only handles a very specific subset of unicode characters, any others will still get through. I'd suggest something similar to (though not the same as, this is just a rough demo, not working provided code):

pawn Code:
stock
    unicode_to_ansi(string[])
{
    // Include upper and lower case letters here.
    const static
        sc_chars[][2] =
            {
                {'ą', 'a'},
                {'č', 'c'},
                {'Č', 'C'},
                {'Ą', 'A'}
            };
    new
        ch,
        i = -1
    while ((ch = string[++i]))
    {
        if (!(' ' <= ch <= '~'))
        {
            // Not an ASCII character.
            new
                j = 0;
            while (j != sizeof (sc_chars))
            {
                if (ch == array[j][0])
                {
                    // Found in the list of replacements.
                    string[i] = sc_chars[j][1];
                    break;
                }
                ++j;
            }
            if (j == sizeof (sc_chars))
            {
                // Not found - do a generic replace.
                string[i] = '_';
            }
        }
    }
}
Actualy, it works perfectly!
I have just added the letters and added some ; and the result is the following:
Code:
[18:10:51] Before: ąčędfasf5112
[18:10:51] after: acedfasf5112
Thanks, Alex, I owe you one.
kLx is offline  
 

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Detect the two first letters from a string? Fj0rtizFredde Help Archive 6 10/07/2010 09:12 PM
Detect something from a string and replace it with something else?? Fj0rtizFredde Help Archive 5 20/06/2010 08:22 PM
Counting letters in a string Striker_Moe Help Archive 2 06/01/2010 04:41 PM
String replace Lenny_Carlson Help Archive 2 24/11/2009 09:34 PM
How to replace characters in a string automatically? LTomi Help Archive 2 17/10/2009 01:51 PM


All times are GMT. The time now is 12:55 PM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.