2016 Update
This commit is contained in:
parent
948b86eafa
commit
dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions
|
|
@ -1,16 +1,27 @@
|
|||
The task is to remove text that follow any of a set of comment markers, (in these examples either a hash or a semicolon) from a string or input line.
|
||||
|
||||
'''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}}]].'''
|
||||
|
||||
'''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 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.
|
||||
|
||||
The following examples will be truncated to either "apples, pears " or "apples, pears". (This example has flipped between "apples, pears " and "apples, pears" in the past.)
|
||||
<br>
|
||||
The following examples will be truncated to either "apples, pears " or "apples, pears".
|
||||
|
||||
(This example has flipped between "apples, pears " and "apples, pears" in the past.)
|
||||
|
||||
<pre>
|
||||
apples, pears # and bananas
|
||||
apples, pears ; and bananas
|
||||
</pre>
|
||||
|
||||
Cf. [[Strip block comments]]
|
||||
|
||||
;Related task:
|
||||
* [[Strip block comments]]
|
||||
<br><br>
|
||||
|
|
|
|||
|
|
@ -0,0 +1,26 @@
|
|||
10 LET C$ = ";#"
|
||||
20 S$(1)="APPLES, PEARS # AND BANANAS"
|
||||
30 S$(2)="APPLES, PEARS ; AND BANANAS"
|
||||
40 FOR Q = 1 TO 2
|
||||
50 LET S$ = S$(Q)
|
||||
60 GOSUB 100"STRIP COMMENTS
|
||||
70 PRINT S$
|
||||
80 NEXT Q
|
||||
90 END
|
||||
|
||||
100 IF S$ = "" THEN RETURN
|
||||
110 FOR I = 1 TO LEN(S$)
|
||||
120 LET A$ = MID$(S$, I, 1)
|
||||
130 FOR J = 1 TO LEN(C$)
|
||||
140 LET F$ = MID$(C$, J, 1)
|
||||
150 IF A$ <> F$ THEN NEXT J
|
||||
160 IF A$ = F$ THEN 200
|
||||
170 NEXT I
|
||||
200 LET I = I - 1
|
||||
210 GOSUB 260"STRIP
|
||||
220 IF S$ = "" THEN RETURN
|
||||
230 FOR I = I TO 0 STEP -1
|
||||
240 LET A$ = MID$(S$, I, 1)
|
||||
250 IF A$ = " " THEN NEXT I
|
||||
260 LET S$ = MID$(S$, 1, I)
|
||||
270 RETURN
|
||||
|
|
@ -1,41 +1,41 @@
|
|||
/*REXX program strips a string delineated by a hash (#) or a semicolon (;). */
|
||||
old1=' apples, pears # and bananas' ; say ' old ───►'old1"◄───"
|
||||
new1=stripCom1(old1) ; say '1st version new ───►'new1"◄───"
|
||||
new2=stripCom2(old1) ; say '2nd version new ───►'new2"◄───"
|
||||
new3=stripCom3(old1) ; say '3rd version new ───►'new3"◄───"
|
||||
new4=stripCom4(old1) ; say '4th version new ───►'new4"◄───"
|
||||
say copies('═',55)
|
||||
old2=' apples, pears ; and bananas' ; say ' old ───►'old2"◄───"
|
||||
new1=stripCom1(old2) ; say '1st version new ───►'new1"◄───"
|
||||
new2=stripCom2(old2) ; say '2nd version new ───►'new2"◄───"
|
||||
new3=stripCom3(old2) ; say '3rd version new ───►'new3"◄───"
|
||||
new4=stripCom4(old2) ; say '4th version new ───►'new4"◄───"
|
||||
exit /*stick a fork in it, we're all done. */
|
||||
/*────────────────────────────────────────────────────────────────────────────*/
|
||||
stripCom1: procedure; parse arg x /*obtain the argument (the X string).*/
|
||||
x=translate(x, '#', ";") /*translate semicolons to a hash (#). */
|
||||
parse var x x '#' /*parse the X string, ending in hash. */
|
||||
return strip(x) /*return the striped shortened string. */
|
||||
/*────────────────────────────────────────────────────────────────────────────*/
|
||||
stripCom2: procedure; parse arg x /*obtain the argument (the X string).*/
|
||||
d = ';#' /*this is the delimiter list to be used*/
|
||||
d1=left(d,1) /*get the first character in delimiter.*/
|
||||
x=translate(x,copies(d1,length(d)),d) /*translates delimiters ──► 1st delim.*/
|
||||
parse var x x (d1) /*parse the string, ending in a hash. */
|
||||
return strip(x) /*return the striped shortened string. */
|
||||
/*────────────────────────────────────────────────────────────────────────────*/
|
||||
stripCom3: procedure; parse arg x /*obtain the argument (the X string).*/
|
||||
d = ';#' /*this is the delimiter list to be used*/
|
||||
do j=1 for length(d) /*process each of the delimiters singly*/
|
||||
_=substr(d,j,1) /*use only one delimiter at a time. */
|
||||
parse var x x (_) /*parse the X string for each delim. */
|
||||
end /*j*/ /* [↑] (_) means stop parsing at _ */
|
||||
return strip(x) /*return the striped shortened string. */
|
||||
/*────────────────────────────────────────────────────────────────────────────*/
|
||||
stripCom4: procedure; parse arg x /*obtain the argument (the X string).*/
|
||||
d = ';#' /*this is the delimiter list to be used*/
|
||||
do k=1 for length(d) /*process each of the delimiters singly*/
|
||||
p=pos(substr(d,k,1), x) /*see if a delimiter is in the X string*/
|
||||
if p\==0 then x=left(x,p-1) /*shorten the X string by one character*/
|
||||
end /*k*/ /* [↑] If p==0, then char wasn't found*/
|
||||
return strip(x) /*return the striped shortened string. */
|
||||
/*REXX program strips a string delineated by a hash (#) or a semicolon (;). */
|
||||
old1= ' apples, pears # and bananas' ; say ' old ───►'old1"◄───"
|
||||
new1= stripCom1(old1) ; say ' 1st version new ───►'new1"◄───"
|
||||
new2= stripCom2(old1) ; say ' 2nd version new ───►'new2"◄───"
|
||||
new3= stripCom3(old1) ; say ' 3rd version new ───►'new3"◄───"
|
||||
new4= stripCom4(old1) ; say ' 4th version new ───►'new4"◄───"
|
||||
say copies('▒', 62)
|
||||
old2= ' apples, pears ; and bananas' ; say ' old ───►'old2"◄───"
|
||||
new1= stripCom1(old2) ; say ' 1st version new ───►'new1"◄───"
|
||||
new2= stripCom2(old2) ; say ' 2nd version new ───►'new2"◄───"
|
||||
new3= stripCom3(old2) ; say ' 3rd version new ───►'new3"◄───"
|
||||
new4= stripCom4(old2) ; say ' 4th version new ───►'new4"◄───"
|
||||
exit /*stick a fork in it, we're all done. */
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
stripCom1: procedure; parse arg x /*obtain the argument (the X string).*/
|
||||
x=translate(x, '#', ";") /*translate semicolons to a hash (#). */
|
||||
parse var x x '#' /*parse the X string, ending in hash. */
|
||||
return strip(x) /*return the stripped shortened string.*/
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
stripCom2: procedure; parse arg x /*obtain the argument (the X string).*/
|
||||
d= ';#' /*this is the delimiter list to be used*/
|
||||
d1=left(d,1) /*get the first character in delimiter.*/
|
||||
x=translate(x,copies(d1,length(d)),d) /*translates delimiters ──► 1st delim.*/
|
||||
parse var x x (d1) /*parse the string, ending in a hash. */
|
||||
return strip(x) /*return the stripped shortened string.*/
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
stripCom3: procedure; parse arg x /*obtain the argument (the X string).*/
|
||||
d= ';#' /*this is the delimiter list to be used*/
|
||||
do j=1 for length(d) /*process each of the delimiters singly*/
|
||||
_=substr(d,j,1) /*use only one delimiter at a time. */
|
||||
parse var x x (_) /*parse the X string for each delim. */
|
||||
end /*j*/ /* [↑] (_) means stop parsing at _ */
|
||||
return strip(x) /*return the stripped shortened string.*/
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
stripCom4: procedure; parse arg x /*obtain the argument (the X string).*/
|
||||
d= ';#' /*this is the delimiter list to be used*/
|
||||
do k=1 for length(d) /*process each of the delimiters singly*/
|
||||
p=pos(substr(d,k,1), x) /*see if a delimiter is in the X string*/
|
||||
if p\==0 then x=left(x,p-1) /*shorten the X string by one character*/
|
||||
end /*k*/ /* [↑] If p==0, then char wasn't found*/
|
||||
return strip(x) /*return the stripped shortened string.*/
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue