String_exploding improvement

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
User avatar
TsT
Party member
Posts: 161
Joined: Thu Sep 25, 2008 7:04 pm
Location: France
Contact:

Re: String_exploding improvement

Post by TsT » Tue Apr 26, 2011 11:13 am

BlackBulletIV wrote:I've added the ability to use plain text (see the split function) and noted your help in the README. Note that by using the division operator, it will default to plain text. Use split for patterns.
Thanks to you!
I see, it's good but I wonder if it's a good idea to have 2 different default one with div operator and the other with split().
I prefer keep in mind both are equal.

I suggest to set the both to plain with :

Code: Select all

function string:split(pat, plain)
  if plain == nil then plain = true end
...
    local pos1, pos2 = self:find(pat, 1, plain or false)
or (little insane)

Code: Select all

function string:split(pat, plain)
  plain = plain == nil and true or plain and true or false
...
    local pos1, pos2 = self:find(pat, 1, plain)
Another question for the fun, is it possible to make a :

Code: Select all

a = "a b c"  / (" ", true)
a = "a b c"  / (" ", false)
It's little ugly but it's just for my curiosity :D
My projects current projects : dragoon-framework (includes lua-newmodule, lua-provide, lovemodular, , classcommons2, and more ...)

User avatar
BlackBulletIV
Inner party member
Posts: 1260
Joined: Wed Dec 29, 2010 8:19 pm
Location: Queensland, Australia
Contact:

Re: String_exploding improvement

Post by BlackBulletIV » Tue Apr 26, 2011 11:29 am

I think it's better to have a convenient way to do a plain text split, rather than specifying true to the split function itself. For example, if I were looking for asterisks (*) in a string, I'd have to do this to find them:

Code: Select all

"hell*o" / '%*'
Whereas with dividing defaulting to plain text, I can do this:

Code: Select all

"hell*o" / '*'
I picture the division operator having a casual use. Patterns are there for more hardcore splitting, and when you get into that, I think the split method is appropriate.
TsT wrote:

Code: Select all

function string:split(pat, plain)
  if plain == nil then plain = true end
...
    local pos1, pos2 = self:find(pat, 1, plain or false)
That makes the 'or false' bit pointless. You could also express it like this

Code: Select all

self:find(pat, 1, plain ~= nil or true)
TsT wrote:or (little insane)

Code: Select all

function string:split(pat, plain)
  plain = plain == nil and true or plain and true or false
...
    local pos1, pos2 = self:find(pat, 1, plain)
Not quite getting what that obscure code does (couldn't be bothered to run it through the Lua console right now).
Another question for the fun, is it possible to make a :

Code: Select all

a = "a b c"  / (" ", true)
a = "a b c"  / (" ", false)
It's little ugly but it's just for my curiosity :D
No it's not possible. Operators can only accept two arguments.

User avatar
TsT
Party member
Posts: 161
Joined: Thu Sep 25, 2008 7:04 pm
Location: France
Contact:

Re: String_exploding improvement

Post by TsT » Tue Apr 26, 2011 11:59 am

BlackBulletIV wrote:That makes the 'or false' bit pointless. You could also express it like this

Code: Select all

self:find(pat, 1, plain ~= nil or true)
But by this way I got always true, even I choose plain = true

Code: Select all

> function x(plain) return plain ~=nil or true end
> = x(false)
true
we want :

Code: Select all

plain = nil => true (the default value, when the argument is omitted)
plain = false => false
plain = other value => true
Isn't it ?
BlackBulletIV wrote:
TsT wrote:Another question for the fun, is it possible to make a :

Code: Select all

a = "a b c"  / (" ", true)
a = "a b c"  / (" ", false)
It's little ugly but it's just for my curiosity :D
No it's not possible. Operators can only accept two arguments.
And it's better like that ! :D
My projects current projects : dragoon-framework (includes lua-newmodule, lua-provide, lovemodular, , classcommons2, and more ...)

User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: String_exploding improvement

Post by Robin » Tue Apr 26, 2011 1:55 pm

What kind of fucked up shit are you guys doing with default arguments?

Code: Select all

function string:split(pat, plain)
  plain = plain == nil or plain
...
    local pos1, pos2 = self:find(pat, 1, plain)
That's enough. It uses plain=true for plain=nil and plain=plain for everything else.
Last edited by Robin on Tue Apr 26, 2011 7:14 pm, edited 1 time in total.
Help us help you: attach a .love.

User avatar
BlackBulletIV
Inner party member
Posts: 1260
Joined: Wed Dec 29, 2010 8:19 pm
Location: Queensland, Australia
Contact:

Re: String_exploding improvement

Post by BlackBulletIV » Tue Apr 26, 2011 6:37 pm

TsT wrote:...
Oh ok, I see my logic error. Anyway, I have no intention of making plain text the default when using split normally, I just couldn't resist having a go at improving code logic.
Robin wrote:

Code: Select all

function string:split(pat, plain)
  plain = plain == nil or plain
...
    local pos1, pos2 = self:find(pat, 1, plain)
Ah, that's it.

User avatar
TsT
Party member
Posts: 161
Joined: Thu Sep 25, 2008 7:04 pm
Location: France
Contact:

Re: String_exploding improvement

Post by TsT » Tue Apr 26, 2011 10:33 pm

Good!
Let's use strong ! :awesome:

Regards,
My projects current projects : dragoon-framework (includes lua-newmodule, lua-provide, lovemodular, , classcommons2, and more ...)

User avatar
BlackBulletIV
Inner party member
Posts: 1260
Joined: Wed Dec 29, 2010 8:19 pm
Location: Queensland, Australia
Contact:

Re: String_exploding improvement

Post by BlackBulletIV » Tue Apr 26, 2011 10:50 pm

Thanks :). The tests are nearly there.

Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 7 guests