AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Run-Time Freeze "While" function. (https://forums.alliedmods.net/showthread.php?t=283721)

siriusmd99 06-09-2016 16:24

Run-Time Freeze "While" function.
 
I literally give up, i made a function which will remove all non-letters from a string.
For example i type a string : "H e 1 l l!!o"
And it should clear the string and return me : "Hello"

And the problem is when i add the condition string[i] < 65 (65 by ascii table is 'A' ) , then when i type my string which has one of these symbols up to 65, then it just freezes. In pawno quincy freezes and in cs 1.6 HLDS freezes and it's showing as "no response" window.

Oh my fucking jesus, i first time see that kind of bullshit . I'm very tired, i tried to understand why and nothing...
I sit like 3-4 hours and didn't find out the problem.

Look:

PHP Code:

new str[32], len,i,jtmp;
print(
"Type in your string: "); getstring(str31false);
len strlen(str)
 
0;
    while(
len)
    {
        
tmp str[i];
        while(
tmp 65 || (tmp 90 && tmp 97) || tmp 122 //checking for no-letter case insensitive
        
{
           if(!
tmp//if current symbol is equal to 0 then string finished and we do break;
           
break;
            for(
ilenj++) //moving all characters 1 step back, so if we have "h1ello", then it will be "hello", ' 1 'replaced with ' e ', ' e ' with ' l ' ,' l 'with ' l ', ' l ' with ' o ' and  ' o ' with EOS ( which is 0)
                
str[j] = str[j+1];
        }

        
i++;
        
len strlen(str);
    }
printf("String:%s"str


klippy 06-09-2016 16:44

Re: Run-Time Freeze "While" function.
 
Make it easy for yourself and use different input and output buffers, rather than modifying the input.

PHP Code:

MeowMeow(const szInput[], szOutput[], iOutLen)
{
    new 
00;
    new 
c;
    
    while((
szInput[i++]) != EOS && iOutLen)
    {
        if(
isalpha(c))
            
szOutput[j++] = c;
    }
    
    
szOutput[j] = EOS;
}

// Called as
new szInput[] = "H e 1 l l!!o";
new 
szOutput[sizeof(szInput)];
MeowMeow(szInputszOutputcharsmax(szOutput)); 


siriusmd99 06-09-2016 17:50

Re: Run-Time Freeze "While" function.
 
Ohh, but this way I should create another string .But I want only to remove these characters from the string..
Really don't understand why < 65 throws error ..

siriusmd99 06-10-2016 01:04

Re: Run-Time Freeze "While" function.
 
So if I use this stock and add also another message[192] to copy new string will it be less efficient than editing the actual one?
Because there will be 2 arrays with size 192, instead of 1.

HamletEagle 06-10-2016 01:16

Re: Run-Time Freeze "While" function.
 
Having one or two really doesn't matter. Don't try to micro optimize. It only start to matter on functions called per frame.

siriusmd99 06-10-2016 07:41

Re: Run-Time Freeze "While" function.
 
Quote:

Originally Posted by HamletEagle (Post 2426272)
Having one or two really doesn't matter. Don't try to micro optimize. It only start to matter on functions called per frame.

Ok , i understood. But really , how the heck could this error happen. It's so weird , because it has no sense.

If i use "if(str[i] < 65)" and then don't use one of these characters in my string then it gives error, but as soon as i use space, or other characters which are smaller then 65 regarding ASCII table then it freezes. Really don't understand.

It looks like it can't replace one of this characters with others ??

For example string "Hell o", it replaces letters but when it comes to space it freezes. wtf?
letters are white and spaces are black? (kidding..) :)

HamletEagle 06-10-2016 13:07

Re: Run-Time Freeze "While" function.
 
If you really want to do it with only one string:

Code:
RemoveAllButLetters(InputString[], Size) {     new i, j     for(i = 0; i < Size; i++)     {         if(!isalpha(InputString[i]))         {             for(j = i; j <= Size -1; j++)             {                 InputString[j] = InputString[j+1]             }                         i = i - 1             Size = Size -1         }     } }

A test plugin:
Code:
#include <amxmodx> public plugin_init() {     register_srvcmd("get_result", "ServerCommand_GetResult") } public ServerCommand_GetResult() {     new Argument[32]     read_argv(1, Argument, charsmax(Argument))         RemoveAllButLetters(Argument, strlen(Argument))     server_print("Result: %s", Argument)     } RemoveAllButLetters(InputString[], Size) {     new i, j     for(i = 0; i < Size; i++)     {         if(!isalpha(InputString[i]))         {             for(j = i; j <= Size -1; j++)             {                 InputString[j] = InputString[j+1]             }                         i = i - 1             Size = Size -1         }     } }

Code:

get_result "H e 1 l l!!o"
Result: Hello

Regarding your crash, it's because you get a without-ending loop.
Code:
while(tmp < 65 || (tmp > 90 && tmp < 97) || tmp > 122 ) //checking for no-letter case insensitive
This loop never ends, because tmp is never changed. It should be just an if check. I did not dig much, but the error seems to be obvious.
You could find this by putting some log_to_file messages and see where it crash and why. It was not related with the ASCII condition.
Anyway, your function is missing some stuff(check mine) so it will likely not work. If you want to know why:

Take for example: Hell33o
When you remove the first 3, the second one gets on position i, but you jump to i+1 so the second "3" escape. That's why I'm doing i = i - 1

siriusmd99 06-10-2016 17:08

Re: Run-Time Freeze "While" function.
 
Ok , thank you very much. I needed it to use in my new plugin , you can check it here :

https://forums.alliedmods.net/showth...85#post2426485

Credits to both of you, thanks ...

klippy 06-11-2016 04:36

Re: Run-Time Freeze "While" function.
 
Hamlet, just a little suggestion.
Passing string length to RemoveAllButLetters() is a little bit awkward, because function can track the length by itself. That's like if you had to pass length to remove_quotes() or any function that takes an input string and doesn't add characters to that buffer.

Here is my modified version:
PHP Code:

RemoveAllButLetters(InputString[])
{
    new 
ijc;
    
    while((
InputString[i]) != EOS)
    {
        if(!
isalpha(c))
        {
            
i;
            while(
InputString[j++] != EOS)
                
InputString[1] = InputString[j];
        }
        else
            
i++;
    }



HamletEagle 06-11-2016 04:44

Re: Run-Time Freeze "While" function.
 
I had in mind to do something different but changed in the last moment. Well, not like it matter.


All times are GMT -4. The time now is 15:31.

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