Time for an 2014 update…
This commit is contained in:
parent
372c577f83
commit
09687c4926
2520 changed files with 34227 additions and 7318 deletions
|
|
@ -2,7 +2,7 @@ The task is to remove text that follow any of a set of comment markers, (in thes
|
|||
|
||||
'''Whitespace debacle:''' There is some confusion about whether to remove any whitespace from the input line. As of [http://rosettacode.org/mw/index.php?title=Strip_comments_from_a_string&oldid=119409 2 September 2011], at least 8 languages (C, C++, Java, Perl, Python, Ruby, sed, UNIX Shell) were incorrect, out of 36 total languages, because they did not trim whitespace by 29 March 2011 rules. Some other languages might be incorrect for the same reason. '''Please discuss this issue at [[{{TALKPAGENAME}}]].'''
|
||||
|
||||
* From [http://rosettacode.org/mw/index.php?title=Strip_comments_from_a_string&oldid=103978 29 March 2011], this task required that: ''"The comment marker and any whitespace at the beginning or ends of the resultant line should be removed. A line without comments should be trimmed of any leading or training whitespace before being produced as a result."'' The task had 28 languages, which did not all meet this new requirement.
|
||||
* From [http://rosettacode.org/mw/index.php?title=Strip_comments_from_a_string&oldid=103978 29 March 2011], this task required that: ''"The comment marker and any whitespace at the beginning or ends of the resultant line should be removed. A line without comments should be trimmed of any leading or trailing whitespace before being produced as a result."'' The task had 28 languages, which did not all meet this new requirement.
|
||||
* From [http://rosettacode.org/mw/index.php?title=Strip_comments_from_a_string&oldid=103978 28 March 2011], this task required that: ''"Whitespace before the comment marker should be removed."''
|
||||
* From [http://rosettacode.org/mw/index.php?title=Strip_comments_from_a_string&offset=20101206204307&action=history 30 October 2010], this task did not specify whether or not to remove whitespace.
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,16 @@
|
|||
-module( strip_comments_from_string ).
|
||||
|
||||
-export( [task/0] ).
|
||||
|
||||
task() ->
|
||||
io:fwrite( "~s~n", [keep_until_comment("apples, pears and bananas")] ),
|
||||
io:fwrite( "~s~n", [keep_until_comment("apples, pears # and bananas")] ),
|
||||
io:fwrite( "~s~n", [keep_until_comment("apples, pears ; and bananas")] ).
|
||||
|
||||
|
||||
|
||||
keep_until_comment( String ) -> lists:takewhile( fun not_comment/1, String ).
|
||||
|
||||
not_comment( $# ) -> false;
|
||||
not_comment( $; ) -> false;
|
||||
not_comment( _ ) -> true.
|
||||
|
|
@ -0,0 +1 @@
|
|||
def stripComments = { it.replaceAll(/\s*[#;].*$/, '') }
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
assert 'apples, pears' == stripComments('apples, pears # and bananas')
|
||||
assert 'apples, pears' == stripComments('apples, pears ; and bananas')
|
||||
|
|
@ -1,10 +1,19 @@
|
|||
class String
|
||||
def strip_comment( markers = ['#',';'] )
|
||||
re = Regexp.union( markers ) # construct a regular expression which will match any of the markers
|
||||
self[0, self =~ re] # slice the string where the regular expression matches, and return it.
|
||||
if index = (self =~ re)
|
||||
self[0, index].rstrip # slice the string where the regular expression matches, and return it.
|
||||
else
|
||||
rstrip
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
puts 'apples, pears # and bananas'.strip_comment
|
||||
p 'apples, pears # and bananas'.strip_comment
|
||||
str = 'apples, pears ; and bananas'
|
||||
puts str.strip_comment
|
||||
p str.strip_comment
|
||||
str = 'apples, pears and bananas '
|
||||
p str.strip_comment
|
||||
p str.strip_comment('and')
|
||||
p " \t \n ;".strip_comment
|
||||
p "".strip_comment
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue