AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Reverse UTF-8 String! Arabic Chat! (https://forums.alliedmods.net/showthread.php?t=287613)

raheem.1 09-10-2016 22:15

Reverse UTF-8 String! Arabic Chat!
 
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.

klippy 09-10-2016 22:23

Re: Reverse UTF-8 Word!
 
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.

raheem.1 09-10-2016 22:44

Re: Reverse UTF-8 Word!
 
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?

fysiks 09-11-2016 00:41

Re: Reverse UTF-8 Word!
 
Quote:

Originally Posted by KliPPy (Post 2452938)
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.

klippy 09-11-2016 04:38

Re: Reverse UTF-8 Word!
 
Quote:

Originally Posted by fysiks (Post 2452956)
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.

PRoSToTeM@ 09-11-2016 07:42

Re: Reverse UTF-8 Word!
 
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;



raheem.1 09-11-2016 10:56

Re: Reverse UTF-8 Word!
 
@PRoSToTeM@, Worked thank you very much. Now i'll continue supporting my language :) .

abdobiskra 09-11-2016 13:33

Re: Reverse UTF-8 Word!
 
raheem.1
Is it possible to put the full code?

raheem.1 09-12-2016 04:23

Re: Reverse UTF-8 Word!
 
Quote:

Originally Posted by abdobiskra (Post 2453119)
raheem.1
Is it possible to put the full code?

I updated the first post see it.

abdobiskra 09-12-2016 08:07

Re: Reverse UTF-8 String! Arabic Chat!
 
Thx evry one for this plugin

PS:
did not work in HL ?


All times are GMT -4. The time now is 10:50.

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