Raised This Month: $300 Target: $400
 75% 

String parsing problem


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Ludak
Member
Join Date: Oct 2014
Old 04-04-2020 , 14:51   String parsing problem
Reply With Quote #1

Hello!
I have ported this code from the AMXX:
PHP Code:
#include <sourcemod>

#pragma semicolon 1
#pragma newdecls required

public Plugin myinfo =  {
    
name "Test Parse"
    
author "Ludak"
    
description "Test Parse"
    
version "1.0.0"
    
url ""
};

public 
void OnPluginStart() {
    
AddCommandListener(Chat_Handle"say");
}

public 
Action Chat_Handle(int player, const char[] commandint argCommands) {
    if (
IsClientInGame(player)) {
        
char message[256];
        
        
GetCmdArgString(messagesizeof(message));
        
StripQuotes(message);
        
        if (!
strlen(message))
            return 
Plugin_Handled;
        
        if (
StrEqual(message"/parse")) {
            
PrintToChat(player"---------> Test <--------- ");
            
            
char results[12][256];
            
int number ParseTest("(some,(weird,string),list,1234,,(more,text,in,a,substring),some,more,$$$,###,@annotation)"results256);
            
            for (
int i 0numberi++)
            
PrintToChat(player"Part %d: %s"1results[i]);
            
            
PrintToChat(player"---------> Test <--------- ");
            return 
Plugin_Handled;
        }
    }
    
    return 
Plugin_Continue;
}

public 
int ParseTest(const char[] textchar[][] resultsint maxLength) {
    
int index 0;
    
int items 0;
    
int startIndex 0;
    
int len 0;
    
int parenthesesCounter 0;
    
    while (
text[index] != EOS) {
        if (
text[index] == '(')
            
parenthesesCounter++;
        
        if (
text[index] == ')')
            
parenthesesCounter--;
        
        if ((
text[index] == ',' || text[index 1] == EOS) && parenthesesCounter == 0) {
            
len index startIndex + (text[index 1] == EOS 0);
            
strcopy(results[items++], maxLength len maxLength lentext[startIndex]);
            
            
items++;
            
startIndex index 1;
        }
        
        
index++;
    }
    
    return 
items;

But the problem that it is not working, it just prints this output:

Instead of:
PHP Code:
Part 1some
Part 2
: (weird,string)
Part 3: list
Part 41234
Part 5

Part 6: (more,text,in,a,substring)
Part 7some
Part 8
more
Part 9
: $$$
Part 10###
Part 11: @annotation 
The original AMXX Code: https://forums.alliedmods.net/showpo...99&postcount=4

I do not know that is wrong with it, everything seems normal.
I have tried using '\0' instead of EOS, it does not work.
Can someone help me, thank you.
Also, it is possible to pass ArrayList by a reference and push the resulting string into it?
I am new to source pawn and programming in general.
Ludak is offline
MAGNAT2645
Senior Member
Join Date: Nov 2015
Location: AlliedMods.net
Old 04-04-2020 , 15:54   Re: String parsing problem
Reply With Quote #2

I think you don't need to pass ArrayList by reference just to push something in it because ArrayList is a child from Handle, Handle itself is like a pointer to object in memory. You work with Handle object and its data via its pointer.
So you can just pass ArrayList (and any other Handle-based thing) as value, not a reference.
__________________

Last edited by MAGNAT2645; 04-04-2020 at 15:58.
MAGNAT2645 is online now
Fyren
FyrenFyrenFyrenFyrenFyren FyrenFyrenFyrenFyrenFyren FyrenFyrenFyrenFyrenFyren FyrenFyrenFyrenFyrenFyren
Join Date: Feb 2106
Old 04-04-2020 , 20:01   Re: String parsing problem
Reply With Quote #3

Code:
text[index] == ',' || text[index + 1] == EOS) && parenthesesCounter == 0
This is the expression you're using to determine whether to copy something over into your results. It can only possibly be true when parentheses are balanced. Since your sample input starts with an open parentheses, that means it'll only actually be true at the very end, when the parentheses balance out (and the next character is null for the end of the string).

The code from fysiks doesn't appear to do what you want.
Fyren is offline
Ludak
Member
Join Date: Oct 2014
Old 04-05-2020 , 06:17   Re: String parsing problem
Reply With Quote #4

Quote:
Originally Posted by MAGNAT2645 View Post
I think you don't need to pass ArrayList by reference just to push something in it because ArrayList is a child from Handle, Handle itself is like a pointer to object in memory. You work with Handle object and its data via its pointer.
So you can just pass ArrayList (and any other Handle-based thing) as value, not a reference.
Thanks
Ludak is offline
Ludak
Member
Join Date: Oct 2014
Old 04-05-2020 , 06:19   Re: String parsing problem
Reply With Quote #5

Quote:
Originally Posted by Fyren View Post
Code:
text[index] == ',' || text[index + 1] == EOS) && parenthesesCounter == 0
This is the expression you're using to determine whether to copy something over into your results. It can only possibly be true when parentheses are balanced. Since your sample input starts with an open parentheses, that means it'll only actually be true at the very end, when the parentheses balance out (and the next character is null for the end of the string).

The code from fysiks doesn't appear to do what you want.
It has worked in AMXX as intended, weird. So I will have to port my own, my own does not parse the last value for some reason.

Last edited by Ludak; 04-05-2020 at 06:20.
Ludak is offline
milutinke
AlliedModders Donor
Join Date: Jun 2012
Location: Serbia
Old 04-05-2020 , 09:58   Re: String parsing problem
Reply With Quote #6

PHP Code:
#include <sourcemod>

#pragma semicolon 1
#pragma newdecls required

public Plugin myinfo =  {
    
name "Test Parse"
    
author "Ludak"
    
description "Test Parse"
    
version "1.0.0"
    
url ""
};

public 
void OnPluginStart() {
    
ArrayList list = new ArrayList(256);
    
char text[256];
    
    
ParseTest("(some,(weird,string),list,1234,,(more,text,in,a,substring),some,more,$$$,###,@annotation)", list, 256);
    
    for (
int i 0< list.Lengthi++) {
        list.
GetString(itextsizeof(text));
        
PrintToServer("[%d] >>> %s"1text);
    }
}

public 
void OnPluginEnd() {
    
}

public 
int ParseTest(const char[] textArrayList resultsint partLength) {
    
int textLength strlen(text);
    
int icurrentChar;
    
bool foundFirst false;
    
bool inASubstring false;
    
    
int tempNumber 0;
    
int tempLength 0;
    
char[] tempPart = new char[partLength];
    
    for (
0textLengthi++) {
        
currentChar text[i];
        
        
// Skip first '('
        
if (!foundFirst && currentChar == '(') {
            
foundFirst true;
            continue;
        }
        
// Enter to the substring
        
else if (foundFirst && currentChar == '(')
            
inASubstring true;
        
        
// Skip last ')'
        
if (== textLength && currentChar == ')') {
            
tempPart[tempLength] = EOS;
            
results.PushString(tempPart);
            
tempLength 0;
            
tempNumber++;
            break;
        }
        
        
// Exit the substring
        
else if (currentChar == ')')
            
inASubstring false;
        
        
// Skip ',' and move on to the next part if not in the substring
        
if (!inASubstring && currentChar == ',') {
            
tempPart[tempLength] = EOS;
            
results.PushString(tempPart);
            
tempLength 0;
            
tempNumber++;
            continue;
        }
        
        
// Truncate over partLength
        
if (tempLength partLength) {
            
tempLength partLength;
            
tempPart[tempLength] = EOS;
            
results.PushString(tempPart);
            
tempLength 0;
            
tempNumber++;
            continue;
        }
        
        
tempPart[tempLength++] = currentChar;
    }
    
    return 
tempNumber;

PHP Code:
[1] >>> some
[2] >>> (weird,string)
[
3] >>> list
[
4] >>> 1234
[5] >>>
[
6] >>> (more,text,in,a,substring)
[
7] >>> some
[8] >>> more
[9] >>> $$$
[
10] >>> ###
[11] >>> @annotation 
__________________
milutinke is offline
Send a message via Skype™ to milutinke
Reply


Thread Tools
Display Modes

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 08:05.


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