Text between: Difference between revisions

Content added Content deleted
m (→‎{{header|Phix}}: added syntax colouring the hard way)
(Ada version)
Line 128: Line 128:
V endString = :argv[3]
V endString = :argv[3]
print(textBetween(thisText, startString, endString))</lang>
print(textBetween(thisText, startString, endString))</lang>

=={{header|Ada}}==
<lang Ada>with Ada.Text_Io;
with Ada.Strings.Fixed;

procedure Text_Between is

Default_Start : constant String := "start";
Default_End : constant String := "end";

function Between (Item : String;
First : String := Default_Start;
Last : String := Default_End) return String
is
use Ada.Strings.Fixed;
First_Pos : Natural;
Last_Pos : Natural;
begin
if First = Default_Start then
First_Pos := Item'First;
else
First_Pos := Index (Item, First);
if First_Pos = 0 then
return "";
else
First_Pos := First_Pos + First'Length;
end if;
end if;

if Last = Default_End then
return Item (First_Pos .. Item'Last);
else
Last_Pos := Index (Item (First_Pos .. Item'Last), Last);
if Last_Pos = 0 then
return Item (First_Pos .. Item'Last);
else
return Item (First_Pos .. Last_Pos - 1);
end if;
end if;
end Between;

procedure Test_Between (Text, First, Last : String) is
use Ada.Text_Io;
function Quote (Item : String) return String is ("'" & Item & "'");
Result : String renames Between (Text, First, Last);
begin
Put ("Text: "); Put_Line (Quote (Text));
Put ("Start: "); Put_Line (Quote (First));
Put ("End: "); Put_Line (Quote (Last));
Put ("Result: "); Put_Line (Quote (Result));
New_Line;
end Test_Between;

begin
Test_Between ("Hello Rosetta Code world", First => "Hello ", Last => " world");
Test_Between ("Hello Rosetta Code world", First => Default_Start, Last => " world");
Test_Between ("Hello Rosetta Code world", First => "Hello ", Last => Default_End);
Test_Between ("</div><div style=\""chinese\"">你好嗎</div>",
First => "<div style=\""chinese\"">", Last => "</div>");
Test_Between ("<text>Hello <span>Rosetta Code</span> world</text><table style=\""myTable\"">",
First => "<text>", Last => "<table>");
Test_Between ("<table style=\""myTable\""><tr><td>hello world</td></tr></table>",
First => "<table>", Last => "</table>");
Test_Between ("The quick brown fox jumps over the lazy other fox",
First => "quick ", Last => " fox");
Test_Between ("One fish two fish red fish blue fish", First => "fish ", Last => " red");
Test_Between ("FooBarBazFooBuxQuux", First => "Foo", Last => "Foo");
end Text_Between;</lang>
{{out}}
<pre>
Text: 'Hello Rosetta Code world'
Start: 'Hello '
End: ' world'
Result: 'Rosetta Code'

Text: 'Hello Rosetta Code world'
Start: 'start'
End: ' world'
Result: 'Hello Rosetta Code'

Text: 'Hello Rosetta Code world'
Start: 'Hello '
End: 'end'
Result: 'Rosetta Code world'

Text: '</div><div style=\"chinese\">你好嗎</div>'
Start: '<div style=\"chinese\">'
End: '</div>'
Result: '你好嗎'

Text: '<text>Hello <span>Rosetta Code</span> world</text><table style=\"myTable\">'
Start: '<text>'
End: '<table>'
Result: 'Hello <span>Rosetta Code</span> world</text><table style=\"myTable\">'

Text: '<table style=\"myTable\"><tr><td>hello world</td></tr></table>'
Start: '<table>'
End: '</table>'
Result: ''

Text: 'The quick brown fox jumps over the lazy other fox'
Start: 'quick '
End: ' fox'
Result: 'brown'

Text: 'One fish two fish red fish blue fish'
Start: 'fish '
End: ' red'
Result: 'two fish'

Text: 'FooBarBazFooBuxQuux'
Start: 'Foo'
End: 'Foo'
Result: 'BarBaz'
</pre>


=={{header|ALGOL 68}}==
=={{header|ALGOL 68}}==