Page 2 of 2
Re: String_exploding improvement
Posted: Tue Apr 26, 2011 11:13 am
by TsT
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
Re: String_exploding improvement
Posted: Tue Apr 26, 2011 11:29 am
by BlackBulletIV
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:
Whereas with dividing defaulting to plain text, I can do this:
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
No it's not possible. Operators can only accept two arguments.
Re: String_exploding improvement
Posted: Tue Apr 26, 2011 11:59 am
by TsT
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
No it's not possible. Operators can only accept two arguments.
And it's better like that !
Re: String_exploding improvement
Posted: Tue Apr 26, 2011 1:55 pm
by Robin
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.
Re: String_exploding improvement
Posted: Tue Apr 26, 2011 6:37 pm
by BlackBulletIV
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.
Re: String_exploding improvement
Posted: Tue Apr 26, 2011 10:33 pm
by TsT
Good!
Let's use strong !
Regards,
Re: String_exploding improvement
Posted: Tue Apr 26, 2011 10:50 pm
by BlackBulletIV
Thanks
. The tests are nearly there.