[Plugin] Text tools: beforeLast() afterLast()

Post your work in progress addons and get feedback

Post » Mon Apr 25, 2016 1:17 am

I'm working on a text utility plugin, and just wanted to get feedback on some function (C2-expression) names I'm planning to use.

beforeLast( source , slicer )
afterLast( source , slicer )

These return the portion of the source string before or after (respectively) the last occurrence of the slicer substring. If slicer is not found, the source is returned unmodified.

I think it might be a little more niche than what usually shows up in generic text libraries, but I'm not sure, so I just wanted to double check on the forums to see if anyone knew if there was a conventional name for this functionality. Or if anyone has a suggestion for a better name.

afterLast( "root/trunk/branch.jpg" , "/" )
// Slices on: root/trunk / branch.jpg
// Returns: "branch.jpg"

beforeLast( "branch.jpg" , "." )
// Slices on: branch . jpg
// Returns: "branch"
Posts: 351
Reputation: 6,733

Post » Mon Apr 25, 2016 6:17 am

You can do the same with tokenat(src, index, separator) and tokencount(src, separator) :
afterLast (src,separator) = tokenat (src, tokencount(src,separator)-1,separator)
beforeLast (src,separator)=tokenat(src,tokencount(src,separator)-2,separator)
Image Image
Posts: 894
Reputation: 83,261

Post » Mon Apr 25, 2016 7:03 am

Thanks for the reply @MadSpy, :)

Yeah, the afterLast() and beforeLast() functions are kind of a shorthand for stuff that can certainly be done with more functions.
One of my main reasons for incorporating them into my plugin, is to make certain expressions a bit easier to understand at a glance.

The afterLast() function especially is pretty straightforward to recreate with built-in text expressions.

I may not have explained the beforeLast() that clearly, but it returns everything before the last slicer string found, rather than just the previous token.
So I think
beforeLast( src , separator )
is not quite the same as
tokenat( src , tokencount( src , separator ) - 2 , separator )

for instance, given
var src = "root/trunk/branch.jpg"
var separator = "/"

beforeLast( src , separator ) // returns "root/trunk"
tokenat( src , tokencount( src , separator ) - 2 , separator ) // returns "trunk"

This is actually the same issue I ran into when trying to create that functionality with the built-in text expressions, and the reason I thought it might be handy to have a beforeLast() in particular. Otherwise you have to either get the index of the last token, (which can be tricky with "find()" if there's another identical token elsewhere), and crop from the start of the src string to it's index; or you have to recombine all but the last token, reinserting the separators back in-between the remaining tokens. Both of those are doable, but they start getting a bit convoluted looking compared to the beforeLast() expression.
Posts: 351
Reputation: 6,733

Return to Work in Progress Addons

Who is online

Users browsing this forum: No registered users and 0 guests