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

#define string in string


Post New Thread Reply   
 
Thread Tools Display Modes
psychonic

BAFFLED
Join Date: May 2008
Old 01-29-2010 , 19:22   Re: #define string in string
Reply With Quote #11

Quote:
Originally Posted by Greyscale View Post
I had no idea overloading was possible in Pawn.

But the problem is it takes the return value. Pawn can't return strings.

berni: Did you end up using Format() to work around this?
I thought strings could be returned in non-public methods.
psychonic is offline
Greyscale
SourceMod Plugin Approver
Join Date: Dec 2007
Location: strYoMommasHouse[you];
Old 01-29-2010 , 19:48   Re: #define string in string
Reply With Quote #12

You're saying it's possible to do:

Code:
String:ReturnString()
{
    return "Hello";
}

SomeFunc()
{
    PrintToServer("%s", ReturnString());

    // or even

    decl String:mystr[32];
    mystr = ReturnString();
}

If that's possible then I have a lot of work to do...


EDIT: I don't think I've ever seen any plugin do this, but it amazingly works. This should be made known, because I don't think people know about this. Well that or I've just been oblivious to it for the past couple years.

EDIT2: This actually doesn't help us.

Code:
public String:operator+(String:oper1[], String:oper2[])
{
    StrCat(oper1, 512, oper2);
    return oper1[0];
}
This fires when using + to concatenate but it's not working at all.
__________________

Last edited by Greyscale; 01-29-2010 at 20:12.
Greyscale is offline
p3tsin
Senior Member
Join Date: Sep 2005
Location: Finland
Old 01-30-2010 , 05:51   Re: #define string in string
Reply With Quote #13

Quote:
Originally Posted by Greyscale View Post
EDIT: I don't think I've ever seen any plugin do this, but it amazingly works. This should be made known, because I don't think people know about this. Well that or I've just been oblivious to it for the past couple years.
Its known but very much discouraged. You can easily overflow the buffer if the function returns an array larger than what you've allocated.
__________________
plop
p3tsin is offline
Frus
Senior Member
Join Date: Aug 2009
Old 01-30-2010 , 11:22   Re: #define string in string
Reply With Quote #14

Quote:
Originally Posted by p3tsin View Post
Its known but very much discouraged. You can easily overflow the buffer if the function returns an array larger than what you've allocated.
Can't you put a parameter in the function to specify the maximum length of the returned string?

EDIT: grey, isn't that function returning a single character, not a string?

Last edited by Frus; 01-30-2010 at 11:30.
Frus is offline
p3tsin
Senior Member
Join Date: Sep 2005
Location: Finland
Old 01-30-2010 , 11:41   Re: #define string in string
Reply With Quote #15

Quote:
Originally Posted by Frus View Post
Can't you put a parameter in the function to specify the maximum length of the returned string?
How do you exactly do that? Sample code:

PHP Code:
String:DoSomething(maxlen) {
    
decl String:buffer[128]
    new 
len FormatEx(buffer,sizeof(buffer),"blabla")

    if(
len maxlen) {
        
buffer[maxlen] = 0
    
}

    return 
buffer

Wouldnt that still return the whole 128 byte array?

Quote:
Originally Posted by Frus View Post
EDIT: grey, isn't that function returning a single character, not a string?
It returns the string.
__________________
plop
p3tsin is offline
Downtown1
Veteran Member
Join Date: Mar 2004
Old 01-30-2010 , 12:13   Re: #define string in string
Reply With Quote #16

Good luck with this, I've been racking my brains over it a bit myself..

but honestly concatenating 2 strings is not a big of a deal as stringizing a token (e.g. #define FOO(x) #foo ala C). Just use StrCat to concatenate 2 strings at runtime, the performance isn't going to be that big of a deal.

The problem is that string arrays are passed in by reference, so you can't just return a newly allocated string, though I guess you could just have a static concatenate buffer if the string was consumed right away (or reused to concatenate to something else).
Downtown1 is offline
Frus
Senior Member
Join Date: Aug 2009
Old 01-30-2010 , 13:06   Re: #define string in string
Reply With Quote #17

PHP Code:
String:DoSomething(maxlen) {
    
decl String:buffer[maxlen];
    
FormatEx(buffer,maxlen,"blabla");

    return 
buffer;

Shouldn't that work?
Frus is offline
p3tsin
Senior Member
Join Date: Sep 2005
Location: Finland
Old 01-30-2010 , 14:36   Re: #define string in string
Reply With Quote #18

Quote:
Originally Posted by Frus View Post
PHP Code:
String:DoSomething(maxlen) {
    
decl String:buffer[maxlen];
    
FormatEx(buffer,maxlen,"blabla");

    return 
buffer;

Shouldn't that work?
Yeah I thought of that but left it out from my post

I was actually under the impression that returning arrays was bad for a reason I couldnt remember but seems I remembered wrong. So, carry on.
__________________
plop
p3tsin is offline
Greyscale
SourceMod Plugin Approver
Join Date: Dec 2007
Location: strYoMommasHouse[you];
Old 01-30-2010 , 15:38   Re: #define string in string
Reply With Quote #19

Quote:
Originally Posted by Frus View Post
EDIT: grey, isn't that function returning a single character, not a string?
Actually yes it is, but without the [0] the compiler errors. And oddly it's returning a random letter, not a the first letter of the string.
__________________
Greyscale is offline
p3tsin
Senior Member
Join Date: Sep 2005
Location: Finland
Old 01-30-2010 , 16:57   Re: #define string in string
Reply With Quote #20

Quote:
Originally Posted by Greyscale View Post
Actually yes it is, but without the [0] the compiler errors. And oddly it's returning a random letter, not a the first letter of the string.
What errors?

PHP Code:
public OnPluginStart() {
    
decl String:str[32];
    
str SomeFunc();
    
PrintToServer("Text: %s",str);
}

String:SomeFunc() {
    new 
String:mystr[32] = "oh hai there";
    return 
mystr;

This compiles just fine. And runs nicely too.
Also, the compiler throws an error if str[] is too small to hold the return value. Interesting.

EDIT: at least when the array size is 28 or lower... weird.
__________________
plop

Last edited by p3tsin; 01-30-2010 at 17:01.
p3tsin is offline
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 05:01.


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