How to resolve the algorithm Strip comments from a string step by step in the XProfan programming language
How to resolve the algorithm Strip comments from a string step by step in the XProfan programming language
Table of Contents
Problem Statement
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 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 Talk:Strip comments from a string.
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.)
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Strip comments from a string step by step in the XProfan programming language
Source code in the xprofan programming language
Proc Min
Declare int PC, i, float e, t
PC = %PCount
e = @!(1)
If PC > 1
For i, 2, PC
t = @!(i)
e = if( (e == 0.0) and (t == 0.0), -(-e - t), if(t < e, t, e) )
EndFor
EndIf
Return e
EndProc
Proc Odd
Parameters int n
Return TestBit(n,0)
EndProc
Proc strip_comments
Parameters string s, delim
Declare int posi[]
Declare int i, min_p, p
min_p = $7FFFFFFF
For i, 1, Len(delim)
posi[ i ] = InStr( mid$(delim,i,1), s )
Case posi[ i ] > 0 : min_p = Min( posi[ i ], min_p )
EndFor
posi[ 0 ] = InStr( chr$(34), s )
// if there is a string delimiter on the left side...
If (posi[0] > 0) and (posi[0] < min_p)
// ...and counting of delimiter is odd, then the sign is part of a string
If Odd( Len( Left$(s,min_p) ) - Len( translate$( Left$(s,min_p), Chr$(34), "" )) )
p = posi[ 0 ] + 1
min_p = $7FFFFFFF
Repeat
// closing quote
posi[ 0 ] = InStr( chr$(34), s, p )
'Case posi[0] > 0 : posi[0] = posi[0] + p
p = posi[ 0 ] + 1
// find new positions after that
For i, 1, Len(delim)
posi[ i ] = InStr( mid$(delim,i,1), s, p )
Case posi[ i ] > 0 : min_p = Min( posi[ i ], min_p )
EndFor
posi[ 0 ] = InStr( chr$(34), s, p )
// if there is a string delimiter on the left side...
If (posi[0] > 0) and (posi[0] < min_p)
// ...and counting of delimiter is odd, then the sign is part of a string
If Odd( Len( Left$(s,min_p) ) - Len( translate$( Left$(s,min_p), Chr$(34), "" )) )
p = posi[ 0 ] + 1
min_p = $7FFFFFFF
// and again....
CONTINUE
EndIf
EndIf
BREAK
Until min_p = 0
EndIf
EndIf
Return Trim$( Left$( s, min_p - 1 ) )
EndProc
cls
declare string s, t
s = " apples, pears # and bananas"
t = strip_comments( s, "#;" )
Print s + "|\n-> [" + t + "]\n"
s = " apples, pears ; and bananas"
t = strip_comments( s, "#;" )
Print s + "|\n-> [" + t + "]\n"
s = " apples, pears \t "
t = strip_comments( s, "#;" )
Print s + "|\n-> [" + t + "]\n"
s = " " + chr$(34) + " #oh, my god " + chr$(34) + " apples, pears # and bananas"
t = strip_comments( s, "#;" )
Print s + "|\n-> [" + t + "]\n"
waitkey
end
You may also check:How to resolve the algorithm Unicode variable names step by step in the Rust programming language
You may also check:How to resolve the algorithm Go Fish step by step in the D programming language
You may also check:How to resolve the algorithm Conditional structures step by step in the QB64 programming language
You may also check:How to resolve the algorithm Pathological floating point problems step by step in the Raku programming language
You may also check:How to resolve the algorithm Dutch national flag problem step by step in the Perl programming language