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

Run-Time Freeze "While" function.


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
siriusmd99
Veteran Member
Join Date: Oct 2013
Location: Republic of Moldova
Old 06-09-2016 , 16:24   Run-Time Freeze "While" function.
Reply With Quote #1

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

Last edited by siriusmd99; 06-09-2016 at 16:30.
siriusmd99 is offline
klippy
AlliedModders Donor
Join Date: May 2013
Location: Serbia
Old 06-09-2016 , 16:44   Re: Run-Time Freeze "While" function.
Reply With Quote #2

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)); 

Last edited by klippy; 06-09-2016 at 16:46.
klippy is offline
siriusmd99
Veteran Member
Join Date: Oct 2013
Location: Republic of Moldova
Old 06-09-2016 , 17:50   Re: Run-Time Freeze "While" function.
Reply With Quote #3

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 is offline
siriusmd99
Veteran Member
Join Date: Oct 2013
Location: Republic of Moldova
Old 06-10-2016 , 01:04   Re: Run-Time Freeze "While" function.
Reply With Quote #4

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.

Last edited by siriusmd99; 06-10-2016 at 01:05.
siriusmd99 is offline
HamletEagle
AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
Old 06-10-2016 , 01:16   Re: Run-Time Freeze "While" function.
Reply With Quote #5

Having one or two really doesn't matter. Don't try to micro optimize. It only start to matter on functions called per frame.
__________________
HamletEagle is offline
siriusmd99
Veteran Member
Join Date: Oct 2013
Location: Republic of Moldova
Old 06-10-2016 , 07:41   Re: Run-Time Freeze "While" function.
Reply With Quote #6

Quote:
Originally Posted by HamletEagle View Post
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..)
siriusmd99 is offline
HamletEagle
AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
Old 06-10-2016 , 13:07   Re: Run-Time Freeze "While" function.
Reply With Quote #7

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
__________________

Last edited by HamletEagle; 06-10-2016 at 13:10.
HamletEagle is offline
siriusmd99
Veteran Member
Join Date: Oct 2013
Location: Republic of Moldova
Old 06-10-2016 , 17:08   Re: Run-Time Freeze "While" function.
Reply With Quote #8

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 ...
siriusmd99 is offline
klippy
AlliedModders Donor
Join Date: May 2013
Location: Serbia
Old 06-11-2016 , 04:36   Re: Run-Time Freeze "While" function.
Reply With Quote #9

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++;
    }

klippy is offline
HamletEagle
AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
Old 06-11-2016 , 04:44   Re: Run-Time Freeze "While" function.
Reply With Quote #10

I had in mind to do something different but changed in the last moment. Well, not like it matter.
__________________
HamletEagle 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 20:51.


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