Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > whitespace string only

Reply
Thread Tools

whitespace string only

 
 
Henrik Horneber
Guest
Posts: n/a
 
      09-23-2004
Hi!

What's the best way to test if a string only consists of whitespaces and
newlines?

best I could come up with is


class String

def is_whitespace_only?
strings_to_test = split("\n")
whitespace = /^\s+$/
is_whitespace_only = true
strings_to_test.each{ |str|
unless whitespace.match(str) or str.empty?
is_whitespace_only = false
break
end
}
is_whitespace_only
end

end

But somehow I think there should be a better way to do it. Any ideas?
Is it okay to add such methods to class String itself?

Any advices appreciated.

regards,
Henrik


 
Reply With Quote
 
 
 
 
Evan Webb
Guest
Posts: n/a
 
      09-23-2004
def only_whitespace?
each_byte { |b| return false if b != 32 }
true
end


On Thu, 2004-09-23 at 00:54, Henrik Horneber wrote:
> Hi!
>
> What's the best way to test if a string only consists of whitespaces and
> newlines?
>
> best I could come up with is
>
>
> class String
>
> def is_whitespace_only?
> strings_to_test = split("\n")
> whitespace = /^\s+$/
> is_whitespace_only = true
> strings_to_test.each{ |str|
> unless whitespace.match(str) or str.empty?
> is_whitespace_only = false
> break
> end
> }
> is_whitespace_only
> end
>
> end
>
> But somehow I think there should be a better way to do it. Any ideas?
> Is it okay to add such methods to class String itself?
>
> Any advices appreciated.
>
> regards,
> Henrik
>




 
Reply With Quote
 
 
 
 
MiG
Guest
Posts: n/a
 
      09-23-2004

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



 
Reply With Quote
 
Evan Webb
Guest
Posts: n/a
 
      09-23-2004
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 // http://www.velocityreviews.com/forums/(E-Mail Removed)


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
>




 
Reply With Quote
 
trans. (T. Onoma)
Guest
Posts: n/a
 
      09-23-2004
On Thursday 23 September 2004 03:54 am, Henrik Horneber wrote:
> Hi!
>
> What's the best way to test if a string only consists of whitespaces and
> newlines?


Unless you're being more specific:

str.strip.length == 0

Also matching against something like

/\A\s*\z/m

but I'm no Regexp expert by a long shot

T.

--
( o _ カラチ
// trans.
/ \ (E-Mail Removed)

I don't give a damn for a man that can only spell a word one way.
-Mark Twain



 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      09-23-2004

"Henrik Horneber" <(E-Mail Removed)> schrieb im Newsbeitrag
news:(E-Mail Removed)...
> Hi!
>
> What's the best way to test if a string only consists of whitespaces and
> newlines?
>
> best I could come up with is
>
>
> class String
>
> def is_whitespace_only?
> strings_to_test = split("\n")
> whitespace = /^\s+$/
> is_whitespace_only = true
> strings_to_test.each{ |str|
> unless whitespace.match(str) or str.empty?
> is_whitespace_only = false
> break
> end
> }
> is_whitespace_only
> end
>
> end
>
> But somehow I think there should be a better way to do it. Any ideas?
> Is it okay to add such methods to class String itself?
>
> Any advices appreciated.
>
> regards,
> Henrik
>
>


>> rx = %r{\A\s*\z}

=> /\A\s*\z/
>> rx =~ ""

=> 0
>> rx =~ " "

=> 0
>> rx =~ " a"

=> nil
>> rx =~ " \n a"

=> nil
>> rx =~ " \n "

=> 0
>> rx =~ " \n \n"

=> 0

Regards

robert

 
Reply With Quote
 
Henrik Horneber
Guest
Posts: n/a
 
      09-23-2004
Hi!


> if "#{s}".chomp.strip.length == 0


...

> rx = %r{\A\s*\z}



Obviously there is more than one way to do it ...and all are better than
mine.

Thanks everybody!


 
Reply With Quote
 
Mikael Brockman
Guest
Posts: n/a
 
      09-23-2004
Henrik Horneber <(E-Mail Removed)> writes:

> Hi!
>
> What's the best way to test if a string only consists of whitespaces
> and newlines?


class String
def is_whitespace_only?
self !~ /[\s\n]/m
end
end



 
Reply With Quote
 
ts
Guest
Posts: n/a
 
      09-23-2004
>>>>> "M" == Mikael Brockman <(E-Mail Removed)> writes:

M> self !~ /[\s\n]/m

1) \n is in \s with a character class, /m is useless
2) you are testing that it don't exist a whitespace character in the string


Guy Decoux







 
Reply With Quote
 
Mikael Brockman
Guest
Posts: n/a
 
      09-23-2004
ts <(E-Mail Removed)> writes:

> >>>>> "M" == Mikael Brockman <(E-Mail Removed)> writes:

>
> M> self !~ /[\s\n]/m
>
> 1) \n is in \s with a character class, /m is useless
> 2) you are testing that it don't exist a whitespace character in the string


self !~ /[^\s]/



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Splitting text at whitespace but keeping the whitespace in thereturned list MRAB Python 3 01-26-2010 11:36 PM
regex for multiple whitespace-only lines Phil Mayes Python 0 04-13-2009 02:53 AM
Structure using whitespace vs logical whitespace cmdrrickhunter@yaho.com Python 10 12-16-2008 03:51 PM
Whitespace where I don't want whitespace! Oli Filth HTML 9 01-17-2005 08:47 PM
Rf. : whitespace string only tad.bochan@bnpparibas.com Ruby 0 09-23-2004 08:09 AM



Advertisments