It is currently Wed Oct 18, 2017 5:19 pm

All times are UTC - 7 hours





Post new topic Reply to topic  [ 22 posts ]  Go to page Previous  1, 2
Author Message
PostPosted: Mon Aug 07, 2017 12:37 pm 
Offline
User avatar

Joined: Mon Jan 03, 2005 10:36 am
Posts: 2962
Location: Tampere, Finland
GradualGames wrote:
Not sure I understand all the syntax there, particularly the double %%'s.

Those are there because of the inner sprintf constructs the format string for the outer sprintf. So, for example .sprintf("%%.%ds", 9) would turn into "%.9s" which is used as the format string in the outer sprintf.

(Note that unlike C sprintf, ca65 does not support specifying the field precision as an argument with "*", so nested sprintfs are needed.)

_________________
Download STREEMERZ for NES from fauxgame.com! — Some other stuff I've done: kkfos.aspekt.fi


Top
 Profile  
 
PostPosted: Mon Aug 07, 2017 12:39 pm 
Offline
User avatar

Joined: Sun Nov 09, 2008 9:18 pm
Posts: 983
Location: Pennsylvania, USA
thefox wrote:
GradualGames wrote:
Not sure I understand all the syntax there, particularly the double %%'s.

Those are there because of the inner sprintf constructs the format string for the outer sprintf. So, for example .sprintf("%%.%ds", 9) would turn into "%.9s" which is used as the format string in the outer sprintf.

(Note that unlike C sprintf, ca65 does not support specifying the field precision as an argument with "*", so nested sprintfs are needed.)

Ah, %% is an escape for itself then? Gotcha. Cool! There's a couple of other things I don't understand in there, it looks like you're doing a multiply on strlens somewhere, too, what's that for? *edit* I think I understand now that < is returning 0 or 1 so you can prevent it from parameterizing sprintf with a negative number, the trickery you mentioned, I assume. Neat.


Top
 Profile  
 
PostPosted: Mon Aug 07, 2017 12:43 pm 
Offline
User avatar

Joined: Mon Jan 03, 2005 10:36 am
Posts: 2962
Location: Tampere, Finland
GradualGames wrote:
There's a couple of other things I don't understand in there, it looks like you're doing a multiply on strlens somewhere, too, what's that for?

That's the "trickery" I mentioned. (.strlen(ends_with) < .strlen(string)) evaluates to 0 if the ends_with is shorter than string (and 1 otherwise). The multiplication then forces the expression value to 0 to avoid a negative value going into the format string. E.g., "%.-7s" would not be a valid format string.

_________________
Download STREEMERZ for NES from fauxgame.com! — Some other stuff I've done: kkfos.aspekt.fi


Top
 Profile  
 
PostPosted: Mon Aug 07, 2017 12:49 pm 
Offline
User avatar

Joined: Sun Nov 09, 2008 9:18 pm
Posts: 983
Location: Pennsylvania, USA
thefox wrote:
GradualGames wrote:
There's a couple of other things I don't understand in there, it looks like you're doing a multiply on strlens somewhere, too, what's that for?

That's the "trickery" I mentioned. (.strlen(ends_with) < .strlen(string)) evaluates to 0 if the ends_with is shorter than string (and 1 otherwise). The multiplication then forces the expression value to 0 to avoid a negative value going into the format string. E.g., "%.-7s" would not be a valid format string.

I can't seem to get ca65 to be happy with it. Trying to compile the following:

Wondering if it's a linecont issue. Like if I use .linecont + and then add some \ 's if it'll work.

Code:
.define string_ends_with(string, ends_with) .xmatch(.sprintf(.sprintf("%%.%ds%%s", (.strlen(ends_with) < .strlen(string)) * (.strlen(string) - .strlen(ends_with))), string, ends_with), string)
.if string_ends_with("hello", "llo")
    .out "yes"
.else
    .out "no"
.endif


Last edited by GradualGames on Mon Aug 07, 2017 1:02 pm, edited 1 time in total.

Top
 Profile  
 
PostPosted: Mon Aug 07, 2017 12:55 pm 
Offline
User avatar

Joined: Mon Jan 03, 2005 10:36 am
Posts: 2962
Location: Tampere, Finland
GradualGames wrote:
thefox wrote:
GradualGames wrote:
There's a couple of other things I don't understand in there, it looks like you're doing a multiply on strlens somewhere, too, what's that for?

That's the "trickery" I mentioned. (.strlen(ends_with) < .strlen(string)) evaluates to 0 if the ends_with is shorter than string (and 1 otherwise). The multiplication then forces the expression value to 0 to avoid a negative value going into the format string. E.g., "%.-7s" would not be a valid format string.


I can't seem to get ca65 to be happy with it. Trying to compile the following:

Wondering if it's a linecont issue. Like if I use .linecont + and then add some \ 's if it'll work.

Code:
.define string_ends_with(string, ends_with) .xmatch(.sprintf(.sprintf("%%.%ds%%s", (.strlen(ends_with) < .strlen(string)) * (.strlen(string) - .strlen(ends_with))), string, ends_with), string)
.if string_ends_with("hello", "llo")
    .out "yes"
.else
    .out "no"
.endif

You should not use parens when calling macros. Try:
Code:
.if string_ends_with "hello", "llo"
    .out "yes"
.else
    .out "no"
.endif

_________________
Download STREEMERZ for NES from fauxgame.com! — Some other stuff I've done: kkfos.aspekt.fi


Top
 Profile  
 
PostPosted: Mon Aug 07, 2017 12:57 pm 
Offline
User avatar

Joined: Sun Nov 09, 2008 9:18 pm
Posts: 983
Location: Pennsylvania, USA
thefox wrote:
You should not use parens when calling macros. Try:
Code:
.if string_ends_with "hello", "llo"
    .out "yes"
.else
    .out "no"
.endif

Oh! :oops: Probably just force of habit with C style macros... I haven't thus far used any in my ca65 programs, only normal ca65 macros.


Top
 Profile  
 
PostPosted: Tue Aug 08, 2017 12:59 am 
Online
User avatar

Joined: Sat Feb 12, 2005 9:43 pm
Posts: 10055
Location: Rio de Janeiro - Brazil
thefox wrote:
It works by stripping off .strlen(string) - .strlen(ends_with) characters from the end of string, then appends ends_with to it, and compares to the original string.

Very clever!


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 22 posts ]  Go to page Previous  1, 2

All times are UTC - 7 hours


Who is online

Users browsing this forum: tokumaru and 9 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group