Raised This Month: $ Target: $400
 0% 

Out of bounds


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
shavit
AlliedModders Donor
Join Date: Dec 2011
Location: Israel
Old 04-23-2013 , 19:44   Out of bounds
Reply With Quote #1

I am trying to recreate The RTLer from SourceMod in AMXX but I am getting "Index out of bounds" error in line 58 when I am saying 2 words or more.

Code:
#include <amxmodx>
#include <amxmisc>

#pragma semicolon 1

public plugin_init()
{
    register_plugin("The RTLer", "1.0", "alongub");
    
    register_cvar("amx_rtler_enabled", "1");
    
    register_clcmd("say", "Command_Say", -1, "Registers the say command in The RTLer");
    // register_clcmd("say_team", "Command_SayTeam", -1, "Registers the say_team command in The RTLer");
}

public Command_Say(client)
{
    if(!get_cvar_num("amx_rtler_enabled"))
    {
        return PLUGIN_CONTINUE;
    }
    
    new arg_string[255];
    read_args(arg_string, 255);
    
    client_print(client, print_chat, "RTLed words: %d", _RTLify(arg_string, arg_string));
    
    if(!_RTLify(arg_string, arg_string))
    {
        return PLUGIN_CONTINUE;
    }
    
    return PLUGIN_HANDLED_MAIN;
}

_RTLify(dest[], original[])
{
    new rtledWords;
    
    new casino[32][32];
    new words[sizeof(casino)][sizeof(casino[])];
    
    new n = ExplodeString(casino, 32, 32, original, ' ');
    
    for(new word; word < n; word++)
    {
        if(WordAnalysis(casino[word]) >= 0.1)
        {
            ReverseString(casino[word], sizeof(casino[]), words[n - 1 - word]);
            rtledWords++;
        }
        
        else
        {
            new firstWord = word;
            new lastWord = word;
            
            while(WordAnalysis(casino[lastWord]) < 0.1)
            {
                lastWord++;
            }
            
            for(new t = lastWord - 1; t >= firstWord; t--)
            {
                copy(words[n - 1 - word], sizeof(casino[]), casino[t]);
                
                if(t > firstWord)
                {
                    word++;
                }
            }
        }
    }
    
    ImplodeStrings(words, n, " ", dest, sizeof(words[]));
    
    return rtledWords;
}

stock ExplodeString( p_szOutput[][], p_nMax, p_nSize, p_szInput[], p_szDelimiter ) 
{
    new nIdx, l = strlen(p_szInput) ;
    
    new nLen = (1 + copyc(p_szOutput[nIdx], p_nSize, p_szInput, p_szDelimiter));
    
    while((nLen < l) && (++nIdx < p_nMax)) 
    {
        nLen += (1 + copyc(p_szOutput[nIdx], p_nSize, p_szInput[nLen], p_szDelimiter));
    }
    
    return nIdx;
}

ReverseString(str[], maxlength, buffer[])
{
    for(new character = strlen(str); character >= 0; character--)
    {
        if(str[character] >= 0xD6 && str[character] <= 0xDE)
        {
            continue;
        }
        
        if(character > 0 && str[character - 1] >= 0xD7 && str[character - 1] <= 0xD9)
        {
            format(buffer, maxlength, "%s%c%c", buffer, str[character - 1], str[character]);
        }
        
        else
        {
            format(buffer, maxlength, "%s%c", buffer, str[character]);
        }
    }
}

Float:WordAnalysis(word[])
{
    new count, length = strlen(word);
    
    for(new n; n < length - 1; n++)
    {
        if(IsRTLCharacter(word, n))
        {    
            count++;
            n++;
        }
    }
    
    return float(count) * 2 / length;
}

bool:IsRTLCharacter(str[], n)
{
    return (str[n] >= 0xD6 && str[n] <= 0xDE && str[n + 1] >= 0x80 && str[n + 1] <= 0xBF);
}

ImplodeStrings(const strings[][], numStrings, const join[], buffer[], maxLength)
{
    new total, length, part_length;
    new join_length = strlen(join);
    
    for(new i; i < numStrings; i++)
    {
        length = copy(buffer[total], maxLength - total, strings[i]);
        total += length;
        
        if(length < part_length)
        {
            break;
        }
        
        if(i != numStrings - 1)
        {
            length = copy(buffer[total], maxLength - total, join);
            total += length;
            
            if(length < join_length)
            {
                break;
            }
        }
    }
    
    return total;
}
Help would be appreciated.
__________________
retired
shavit is offline
hornet
AMX Mod X Plugin Approver
Join Date: Mar 2010
Location: Australia
Old 04-23-2013 , 20:05   Re: Out of bounds
Reply With Quote #2

Print some debug messages and see how many iterations it's getting through.
__________________
Quote:
vBulletin Tip #42: Not much would be accomplished by merging this item with itself.
hornet is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 04-23-2013 , 23:31   Re: Out of bounds
Reply With Quote #3

"lastWord" is obviously 32 when this happens. You need to prevent it from getting larger than the cells in the array.

EDIT:
After looking at the SourceMod code, you should notice that the size of that array is much larger than yours.
__________________

Last edited by fysiks; 04-23-2013 at 23:42.
fysiks is offline
shavit
AlliedModders Donor
Join Date: Dec 2011
Location: Israel
Old 04-24-2013 , 00:53   Re: Out of bounds
Reply With Quote #4

Quote:
Originally Posted by fysiks View Post
"lastWord" is obviously 32 when this happens. You need to prevent it from getting larger than the cells in the array.

EDIT:
After looking at the SourceMod code, you should notice that the size of that array is much larger than yours.
For some reason I can't make "casino" too long.
__________________
retired
shavit is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 04-24-2013 , 21:30   Re: Out of bounds
Reply With Quote #5

Quote:
Originally Posted by shavit View Post
For some reason I can't make "casino" too long.
And what do you mean by that?
__________________
fysiks is offline
shavit
AlliedModders Donor
Join Date: Dec 2011
Location: Israel
Old 04-25-2013 , 01:10   Re: Out of bounds
Reply With Quote #6

Quote:
Originally Posted by fysiks View Post
And what do you mean by that?
I can't make the variable "casino" big enough so it won't get out of bounds.
__________________
retired
shavit is offline
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 10:55.


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