* single replace and its example

This commit is contained in:
Vovanium 2023-04-24 22:40:26 +03:00
parent bdcc30bf21
commit 550b972c78
4 changed files with 64 additions and 4 deletions

View file

@ -9,4 +9,4 @@ maintainers-logins = ["vovanium"]
project-files = ["gpr/encodings.gpr", "gpr/test.gpr", "gpr/examples.gpr"]
[[depends-on]]
aunit = "^23.0.0"
aunit = ">=22.0.0"

View file

@ -7,4 +7,6 @@ project Examples extends "common" is
package Builder is
For Default_Switches("Ada") use Common.Default_Switches;
end Builder;
for Main use ("add_cr", "strip_cr", "replace");
end Examples;

View file

@ -0,0 +1,46 @@
--
-- Program reads standard input, replaces string in first parameter
-- with one in second, then writes to standard output
--
with Ada.Streams, Ada.Text_IO, Ada.Text_IO.Text_Streams;
use Ada.Streams, Ada.Text_IO, Ada.Text_IO.Text_Streams;
with Ada.Command_Line;
use Ada.Command_Line;
with Encodings.Generic_Single_Replace;
with Encodings.Utility, Encodings.Converters;
use Encodings.Utility, Encodings.Converters;
procedure Replace is
Input_Stream : Stream_Access := Stream (Standard_Input);
Output_Stream : Stream_Access := Stream (Standard_Output);
Input_Buffer : String (1 .. 100);
Output_Buffer : String (1 .. 100);
Input_Last,
Input_Read_Last : Natural;
Output_Last : Natural;
begin
if Argument_Count < 2 then
return;
end if;
declare
package Coders is new Encodings.Generic_Single_Replace (
Character_Type => Character,
String_Type => String,
Original => Argument (1),
Replacement => Argument (2),
Converter_Base => Encodings.Converters.Character_To_Character);
Coder : Coders.Converter;
begin
while not End_Of_File (Standard_Input) loop
Read_String (Input_Stream.all, Input_Buffer, Input_Read_Last);
Input_Last := Input_Buffer'First - 1;
while Input_Last < Input_Read_Last loop
Coder.Process (Input_Buffer (Input_Last + 1 .. Input_Read_Last),
Input_Last, Output_Buffer, Output_Last);
String'Write (Output_Stream, Output_Buffer (Output_Buffer'First .. Output_Last));
end loop;
end loop;
end;
end Replace;

View file

@ -32,8 +32,17 @@ package body Encodings.Generic_Single_Replace is
C := Source (Source_Cursor);
if C = Original (This.SI) then -- original match
if This.SI = Original'Last then -- full string matched
This.TI := Replacement'First - 1;
This.S := Output_Replacement;
case Replacement'Length is
when 0 =>
This.SI := Original'First;
when 1 =>
Put (Replacement (Replacement'First));
This.SI := Original'First;
when others =>
Put (Replacement (Replacement'First));
This.TI := Replacement'First;
This.S := Output_Replacement;
end case;
else
This.SI := This.SI + 1;
end if;
@ -41,7 +50,8 @@ package body Encodings.Generic_Single_Replace is
if This.SI = Original'First then -- no matched prefix
Put (C);
else
This.TI := Original'First - 1;
This.TI := Original'First;
Put (Original (Original'First));
This.C := C;
This.S := Output_Original;
end if;
@ -50,6 +60,7 @@ package body Encodings.Generic_Single_Replace is
This.TI := This.TI + 1;
if This.TI = This.SI then -- whole matched part already written
Put (This.C);
This.SI := Original'First;
This.S := Initial;
else
Put (Original (This.TI));
@ -58,6 +69,7 @@ package body Encodings.Generic_Single_Replace is
This.TI := This.TI + 1;
Put (Replacement (This.TI));
if This.TI = Replacement'Last then
This.SI := Original'First;
This.S := Initial;
end if;
end case;