I highly doubt a regex is faster than each_byte. each_byte has very
little code and is very fast (looping over the array in C and casting
the chars to fixnums), where as with a regex it has to pass through the
regex parser, get pulled back out as an object, pushed back into split,
which there in turn returns a potentially huge array which you pull back
again to run over with each. Then you've done another comparison with a
regex within the block which i guarantee is much slower then comparing 2
Fixnums.
My initial version didnt do \n, only white space, so here's my updated
version that even does tabs.
class String
def only_ws?
each_byte { |b| return false unless [9,10,32].include?(b) }
true
end
end
Evan Webb //
On Thu, 2004-09-23 at 01:11, MiG wrote:
> I think regexp should be is faster than each_byte. What about this?
>
> class String
>
> def whitespace_only? str
> str.split(/\n/).each { |x|
> return false unless x =~ /^\s*$/
> }
> true
> end
>
> end
>
>
> MiG
>