+ In_Initial_State function

This commit is contained in:
Vovanium 2023-04-26 17:17:30 +03:00
parent d37628b673
commit 4d25e47dc8
13 changed files with 73 additions and 10 deletions

View file

@ -5,10 +5,18 @@ generic
type Target_String is array (Positive range <>) of Target_Character'Base;
package Encodings.Generic_Converters with Pure is
type Converter is interface;
procedure Reset (State : in out Converter) is null;
-- Reset the converter to initial state (if any)
function In_Initial_State (This : Converter) return Boolean is abstract;
-- Query if converter in initial state, :
-- both source and target stream in their initial states
-- (as defined in their encoding specifiactions)
-- and there's no data in internal buffers that have to be output
procedure Reset (This : in out Converter) is null;
-- Reset the converter to the initial state (if any)
procedure Process (
State : in out Converter; -- Converter
This : in out Converter; -- Converter
Source : in Source_String; -- Character sequence to convert
Source_Cursor : out Natural; -- Last character consumed by converter
Target : out Target_String; -- Converted character sequence
@ -16,7 +24,7 @@ package Encodings.Generic_Converters with Pure is
) is abstract;
-- Procedure converts a (part of) string
-- Procedure must consume whole source string or
-- fill whole target string.
-- fill whole target string (or both).
-- Source (Source_Cursor + 1 .. Source'Last)
-- Note: this is most basic conversion interface

View file

@ -1,5 +1,8 @@
package body Encodings.Generic_Single_Replace is
function In_Initial_State (State : Converter) return Boolean
is (State.S = Initial and then State.SI = Original'First);
procedure Reset (
This : in out Converter)
is

View file

@ -18,6 +18,8 @@ package Encodings.Generic_Single_Replace is
type Converter is new Converter_Base.Converter with private;
overriding function In_Initial_State (State : Converter) return Boolean;
overriding procedure Reset (
This : in out Converter);

View file

@ -1,4 +1,15 @@
package body Encodings.Line_Endings.Generic_Add_CR is
function In_Initial_State (State : Converter) return Boolean
is (State.State = Initial);
procedure Reset (
This : in out Converter)
is
begin
This.State := Initial;
end;
procedure Process (
This : in out Converter;
Source : in String_Type;
@ -38,4 +49,5 @@ package body Encodings.Line_Endings.Generic_Add_CR is
Target (Target_Cursor) := C;
end loop;
end Process;
end Encodings.Line_Endings.Generic_Add_CR;

View file

@ -12,13 +12,19 @@ generic
Target_String => String_Type); -- Base package
package Encodings.Line_Endings.Generic_Add_CR is
type Converter is new Converter_Base.Converter with private;
overriding function In_Initial_State (State : Converter) return Boolean;
overriding procedure Reset (
This : in out Converter); -- Converter state
overriding procedure Process (
This : in out Converter; -- Converter state
Source : in String_Type; -- String to be converted
Source_Cursor : out Natural; -- Last index of source string read
Target : out String_Type; -- Converted string
Target_Cursor : out Natural -- Last Index of destination string written
);
Target_Cursor : out Natural); -- Last Index of destination string written
private
type Converter_State is (
Initial,

View file

@ -1,13 +1,24 @@
with Ada.Assertions;
use Ada.Assertions;
package body Encodings.Line_Endings.Generic_Strip_CR is
overriding function In_Initial_State (State : Converter) return Boolean
is (not State.Have_CR);
procedure Reset (
This : in out Converter)
is
begin
This.Have_CR := False;
end Reset;
procedure Process (
This : in out Converter;
Source : in String_Type;
Source_Cursor : out Natural;
Target : out String_Type;
Target_Cursor : out Natural
) is
Target_Cursor : out Natural)
is
C : Character_Type;
begin
Source_Cursor := Source'First - 1;
@ -38,4 +49,5 @@ package body Encodings.Line_Endings.Generic_Strip_CR is
Source_Cursor := Source_Cursor + 1;
end loop;
end Process;
end Encodings.Line_Endings.Generic_Strip_CR;

View file

@ -12,13 +12,18 @@ generic
Target_String => String_Type); -- Base package
package Encodings.Line_Endings.Generic_Strip_CR is
type Converter is new Converter_Base.Converter with private;
overriding function In_Initial_State (State : Converter) return Boolean;
overriding procedure Reset (
This : in out Converter);
overriding procedure Process (
This : in out Converter; -- Coder state
Source : in String_Type; -- String to be converted
Source_Cursor : out Natural; -- Last index of source string read
Target : out String_Type; -- Converted string
Target_Cursor : out Natural -- Last Index of destination string written
);
Target_Cursor : out Natural); -- Last Index of destination string written
private
type Converter is new Converter_Base.Converter with record
Have_CR : Boolean := False;

View file

@ -1,5 +1,8 @@
package body Encodings.Unicode.UTF_16.From_UTF_32 is
function In_Initial_State (State : Converter) return Boolean
is (State.Surrogate not in Low_Surrogate);
procedure Reset (State : in out Converter) is
begin
State.Surrogate := UTF_16_Character'Val (0);

View file

@ -9,6 +9,8 @@ package Encodings.Unicode.UTF_16.From_UTF_32 is
type Converter is new Converters.Wide_Wide_to_Wide.Converter with private;
overriding function In_Initial_State (State : Converter) return Boolean;
overriding procedure Reset (State : in out Converter);
overriding procedure Process (

View file

@ -1,5 +1,8 @@
package body Encodings.Unicode.UTF_32.From_UTF_8 is
function In_Initial_State (State : Converter) return Boolean
is (State.L = 0);
procedure Reset (State : in out Converter) is
begin
State.L := 0;

View file

@ -6,6 +6,8 @@ package Encodings.Unicode.UTF_32.From_UTF_8 is
type Converter is new Converters.Narrow_to_Wide_Wide.Converter with private;
overriding function In_Initial_State (State : Converter) return Boolean;
overriding procedure Reset (State : in out Converter);
overriding procedure Process (

View file

@ -1,5 +1,8 @@
package body Encodings.Unicode.UTF_8.From_UTF_32 is
overriding function In_Initial_State (State : Converter) return Boolean
is (State.L = 0);
procedure Reset (State : in out Converter) is
begin
State.L := 0;

View file

@ -6,6 +6,8 @@ package Encodings.Unicode.UTF_8.From_UTF_32 is
type Converter is new Converters.Wide_Wide_to_Narrow.Converter with private;
overriding function In_Initial_State (State : Converter) return Boolean;
overriding procedure Reset (State : in out Converter);
overriding procedure Process (