Raised This Month: $51 Target: $400
 12% 

Reverse UTF-8 String! Arabic Chat!


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
raheem.1
Junior Member
Join Date: Sep 2016
Location: Egypt
Old 09-10-2016 , 22:15   Reverse UTF-8 String! Arabic Chat!
Reply With Quote #1

Hello, I have problem i try to Reverse a UTF-8 word but it's don't appear correct i have tried 2 codes.
The language i test in utf-8 arabic. First i try this code:

PHP Code:
#include <amxmodx>

public plugin_init()
{
    
register_clcmd("say""hook")
}

public 
hook(id)
{
    
    new 
said[192], cmd[10]
    
read_args(said191)
    
    
ReverseString(said)
    
    
read_argv(1cmd9)
    
engclient_cmd (id ,cmd ,said)
}

stock ReverseString(toggle[])
{
    for(new 
strlen(toggle) - 10temp i--, j++)
    {
        
temp toggle[i];
        
toggle[i] = toggle[j];
        
toggle[j] = temp;
    }

This first code only Reverse english normal characters but don't work in any other utf-8 language.

Second code:

PHP Code:
#include <amxmodx>

public plugin_init()
{
    
register_message(get_user_msgid("SayText"), "msgSayText")
}

public 
msgSayText()
{
    new 
said[192]
    
get_msg_arg_string(4said191);
    
    
ReverseString(said)
    
    
set_msg_arg_string(4said);
}

stock ReverseString(toggle[])
{
    for(new 
strlen(toggle) - 10temp i--, j++)
    {
        
temp toggle[i];
        
toggle[i] = toggle[j];
        
toggle[j] = temp;
    }

Second code works for english and when i try arabic letters some of them appear and some not appear. I have tried to change this stock that reverse the word but faild all time. So now the problem in this code or in amxmodx when it process the letters?

Solved: HERE.

Last edited by raheem.1; 09-12-2016 at 04:22. Reason: Solved!
raheem.1 is offline
klippy
AlliedModders Donor
Join Date: May 2013
Location: Serbia
Old 09-10-2016 , 22:23   Re: Reverse UTF-8 Word!
Reply With Quote #2

You are only reversing bytes which will work only for ASCII characters. UTF-8 characters may consist of multiple bytes. I think you may have to use AMXX 1.8.3-dev, there are some improvements regarding UTF-8 strings.
klippy is offline
raheem.1
Junior Member
Join Date: Sep 2016
Location: Egypt
Old 09-10-2016 , 22:44   Re: Reverse UTF-8 Word!
Reply With Quote #3

Thanks man i understand the bytes difference. I have tested in latest dev version of amxmodx and not work also.

I forget to say that first code when write arabic nothing apear as if i don't write anything.

I have see plugin you write but not works also. Here.

If the problem regards amxmodx why not be fixed in next dev versions?
raheem.1 is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 09-11-2016 , 00:41   Re: Reverse UTF-8 Word!
Reply With Quote #4

Quote:
Originally Posted by KliPPy View Post
You are only reversing bytes which will work only for ASCII characters. UTF-8 characters may consist of multiple bytes. I think you may have to use AMXX 1.8.3-dev, there are some improvements regarding UTF-8 strings.
He's reversing cells. Cells are 32-bits each. I'm not sure why it wouldn't work though.
__________________
fysiks is offline
klippy
AlliedModders Donor
Join Date: May 2013
Location: Serbia
Old 09-11-2016 , 04:38   Re: Reverse UTF-8 Word!
Reply With Quote #5

Quote:
Originally Posted by fysiks View Post
He's reversing cells. Cells are 32-bits each. I'm not sure why it wouldn't work though.
Yes, but each of these cells really contain a single byte (a character). UTF-8 characters are actually spread across multiple cells, they don't take a single one.
klippy is offline
PRoSToTeM@
Veteran Member
Join Date: Jan 2010
Location: Russia, Ivanovo
Old 09-11-2016 , 07:42   Re: Reverse UTF-8 Word!
Reply With Quote #6

You should convert the string to UTF-16, then reverse. After that, convert the string back to UTF-8.
You can do that with this functions:
PHP Code:
// Converts MultiByte (UTF-8) to WideChar (UTF-16, UCS-2)
// Supports only 1-byte, 2-byte and 3-byte UTF-8 (unicode chars from 0x0000 to 0xFFFF), because client can't display 2-byte UTF-16
// charsmax(wcszOutput) should be >= strlen(mbszInput)
stock MultiByteToWideChar(const mbszInput[], wcszOutput[]) {
    new 
nOutputChars 0;
    for (new 
0mbszInput[n] != EOSn++) {
        if (
mbszInput[n] < 0x80) { // 0... 1-byte ASCII
            
wcszOutput[nOutputChars] = mbszInput[n];
        } else if ((
mbszInput[n] & 0xE0) == 0xC0) { // 110... 2-byte UTF-8
            
wcszOutput[nOutputChars] = (mbszInput[n] & 0x1F) << 6// Upper 5 bits
            
            
if ((mbszInput[1] & 0xC0) == 0x80) { // Is 10... ?
                
wcszOutput[nOutputChars] |= mbszInput[++n] & 0x3F// Lower 6 bits
            
} else { // Decode error
                
wcszOutput[nOutputChars] = '?';
            }
        } else if ((
mbszInput[n] & 0xF0) == 0xE0) { // 1110... 3-byte UTF-8
            
wcszOutput[nOutputChars] = (mbszInput[n] & 0xF) << 12// Upper 4 bits
            
            
if ((mbszInput[1] & 0xC0) == 0x80) { // Is 10... ?
                
wcszOutput[nOutputChars] |= (mbszInput[++n] & 0x3F) << 6// Middle 6 bits
                
                
if ((mbszInput[1] & 0xC0) == 0x80) { // Is 10... ?
                    
wcszOutput[nOutputChars] |= mbszInput[++n] & 0x3F// Lower 6 bits
                
} else { // Decode error
                    
wcszOutput[nOutputChars] = '?';
                }
            } else { 
// Decode error
                
wcszOutput[nOutputChars] = '?';
            }
        } else { 
// Decode error
            
wcszOutput[nOutputChars] = '?';
        }
        
        
nOutputChars++;
    }
    
wcszOutput[nOutputChars] = EOS;
}

// Converts WideChar (UTF-16, UCS-2) to MultiByte (UTF-8)
// Supports only 1-byte UTF-16 (0x0000 to 0xFFFF), because client can't display 2-byte UTF-16
// charsmax(mbszOutput) should be >= wcslen(wcszInput) * 3
stock WideCharToMultiByte(const wcszInput[], mbszOutput[]) {
    new 
nOutputChars 0;
    for (new 
0wcszInput[n] != EOSn++) {
        if (
wcszInput[n] < 0x80) {
            
mbszOutput[nOutputChars++] = wcszInput[n];
        } else if (
wcszInput[n] < 0x800) {
            
mbszOutput[nOutputChars++] = (wcszInput[n] >> 6) | 0xC0;
            
mbszOutput[nOutputChars++] = (wcszInput[n] & 0x3F) | 0x80;
        } else {
            
mbszOutput[nOutputChars++] = (wcszInput[n] >> 12) | 0xE0;
            
mbszOutput[nOutputChars++] = ((wcszInput[n] >> 6) & 0x3F) | 0x80;
            
mbszOutput[nOutputChars++] = (wcszInput[n] & 0x3F) | 0x80;
        }
    }
    
mbszOutput[nOutputChars] = EOS;

__________________

Last edited by PRoSToTeM@; 09-11-2016 at 07:44.
PRoSToTeM@ is offline
Send a message via ICQ to PRoSToTeM@ Send a message via Skype™ to PRoSToTeM@
raheem.1
Junior Member
Join Date: Sep 2016
Location: Egypt
Old 09-11-2016 , 10:56   Re: Reverse UTF-8 Word!
Reply With Quote #7

@PRoSToTeM@, Worked thank you very much. Now i'll continue supporting my language .
raheem.1 is offline
abdobiskra
Veteran Member
Join Date: Jul 2014
Location: Algeria
Old 09-11-2016 , 13:33   Re: Reverse UTF-8 Word!
Reply With Quote #8

raheem.1
Is it possible to put the full code?
__________________
abdobiskra is offline
Send a message via Skype™ to abdobiskra
raheem.1
Junior Member
Join Date: Sep 2016
Location: Egypt
Old 09-12-2016 , 04:23   Re: Reverse UTF-8 Word!
Reply With Quote #9

Quote:
Originally Posted by abdobiskra View Post
raheem.1
Is it possible to put the full code?
I updated the first post see it.
raheem.1 is offline
abdobiskra
Veteran Member
Join Date: Jul 2014
Location: Algeria
Old 09-12-2016 , 08:07   Re: Reverse UTF-8 String! Arabic Chat!
Reply With Quote #10

Thx evry one for this plugin

PS:
did not work in HL ?
__________________

Last edited by abdobiskra; 09-12-2016 at 10:48.
abdobiskra is offline
Send a message via Skype™ to abdobiskra
Reply



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

Forum Jump


All times are GMT -4. The time now is 05:39.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode