June 2018 Update

This commit is contained in:
Ingy döt Net 2018-06-22 20:57:24 +00:00
parent ba8067c3b7
commit 22f33d4004
5278 changed files with 84726 additions and 14379 deletions

View file

@ -0,0 +1,43 @@
begin
% determines the non-comment portion of the string s, startPos and endPos are %
% returned set to the beginning and ending character positions (indexed from 0) %
% of the non-comment text in s. If there is no non-comment text in s, startPos %
% will be greater than endPos %
% note that in Algol W, strings can be at most 256 characters long %
procedure stripComments ( string(256) value s; integer result startPos, endPos ) ;
begin
integer MAX_LENGTH;
MAX_LENGTH := 256;
startPos := 0;
endPos := -1;
% find the first non-blank character in s %
while startPos < MAX_LENGTH and s( startPos // 1 ) = " " do startPos := startPos + 1;
if startPos < MAX_LENGTH then begin
% have a non-blank character in the string %
if s( startPos // 1 ) not = "#" and s( startPos // 1 ) not = ";" then begin
% the non-blank character is not a comment delimiter %
integer cPos;
cPos := endPos := startPos;
while cPos < MAX_LENGTH and s( cPos // 1 ) not = "#" and s( cPos // 1 ) not = ";" do begin
if s( cPos // 1 ) not = " " then endPos := cPos;
cPos := cPos + 1
end while_not_a_comment
end if_not_a_comment
end if_startPos_lt_MAX_LENGTH
end stripComments ;
% tests the stripComments procedure %
procedure testStripComments( string(256) value s ) ;
begin
integer startPos, endPos;
stripComments( s, startPos, endPos );
write( """" );
for cPos := startPos until endPos do writeon( s( cPos // 1 ) );
writeon( """" )
end testStripComments ;
begin % test cases - should all print "apples, pears" %
testStripComments( "apples, pears # and bananas" );
testStripComments( "apples, pears ; and bananas" );
testStripComments( "apples, pears " );
testStripComments( " apples, pears" )
end
end.

View file

@ -0,0 +1,29 @@
use System.IO.File;
class StripComments {
function : Main(args : String[]) ~ Nil {
reader : FileReader;
if(args->Size() = 1) {
reader := FileReader->New(args[0]);
line := reader->ReadString();
while(line <> Nil) {
index := line->FindLast(';');
if(index < 0) {
index := line->FindLast('#');
};
if(index > -1) {
line->SubString(index)->PrintLine();
};
line := reader->ReadString();
};
};
leaving {
if(reader <> Nil) {
reader->Close();
};
};
}
}

View file

@ -1,7 +1,10 @@
>>> marker, line = '#', 'apples, pears # and bananas'
>>> line[:line.index(marker)].strip()
'apples, pears'
>>>
>>> marker, line = ';', ' apples, pears ; and bananas'
>>> line[:line.index(marker)].strip()
'apples, pears'
def remove_comments(line, sep):
for s in sep:
i = line.find(s)
if i >= 0:
line = line[:i]
return line.strip()
# test
print remove_comments('apples ; pears # and bananas', ';#')
print remove_comments('apples ; pears # and bananas', '!')

View file

@ -1,8 +1,5 @@
def remove_comments(line, sep):
for s in sep:
line = line.split(s)[0]
return line.strip()
import re
# test
print remove_comments('apples ; pears # and bananas', ';#')
print remove_comments('apples ; pears # and bananas', '!')
m = re.match(r'^([^#]*)#(.*)$', line)
if m: # The line contains a hash / comment
line = m.group(1)

View file

@ -0,0 +1,4 @@
>> parse s: "apples, pears ; and bananas" [to [any space ";"] remove thru end]
== true
>> s
== "apples, pears"

View file

@ -0,0 +1,10 @@
s: "apples, pears ; and bananas"
dlms: charset "#;"
trim head clear find s dlms
== "apples, pears"
s: "apples, pears # and bananas"
trim head clear find s dlms
== "apples, pears"