implemented some functions for Elastic_String
This commit is contained in:
parent
0bdc581467
commit
f5311ecb69
@ -1,10 +1,10 @@
|
|||||||
with Ada.Unchecked_Deallocation;
|
with Ada.Unchecked_Deallocation;
|
||||||
|
|
||||||
with ada.text_io;
|
|
||||||
|
|
||||||
package body H3.Strings is
|
package body H3.Strings is
|
||||||
BUFFER_ALIGN: constant := 16;
|
BUFFER_ALIGN: constant := 16;
|
||||||
|
|
||||||
|
type Shift_Direction is (SHIFT_LEFT, SHIFT_RIGHT);
|
||||||
|
|
||||||
function To_Character_Array (Str: in Elastic_String) return Character_Array is
|
function To_Character_Array (Str: in Elastic_String) return Character_Array is
|
||||||
begin
|
begin
|
||||||
return Str.Buffer.Slot(Str.Buffer.Slot'First .. Str.Buffer.Last);
|
return Str.Buffer.Slot(Str.Buffer.Slot'First .. Str.Buffer.Last);
|
||||||
@ -128,7 +128,7 @@ package body H3.Strings is
|
|||||||
end Prepare_Buffer;
|
end Prepare_Buffer;
|
||||||
|
|
||||||
-- prepare the buffer for writing
|
-- prepare the buffer for writing
|
||||||
procedure Prepare_Buffer (Str: in out Elastic_String; Req_Hard_Capa: in System_Size; Shift_Pos: in System_Size := 0; Shift_Size: in System_Size := 0) is
|
procedure Prepare_Buffer (Str: in out Elastic_String; Req_Hard_Capa: in System_Size; Shift_Pos: in System_Size := 0; Shift_Size: in System_Size := 0; Shift_Dir: in Shift_Direction := Shift_Right) is
|
||||||
Tmp: Elastic_String;
|
Tmp: Elastic_String;
|
||||||
First, Last: System_Size;
|
First, Last: System_Size;
|
||||||
Hard_Capa: System_Size;
|
Hard_Capa: System_Size;
|
||||||
@ -136,28 +136,6 @@ package body H3.Strings is
|
|||||||
First := Get_First_Index(Str);
|
First := Get_First_Index(Str);
|
||||||
Last := Get_Last_Index(Str);
|
Last := Get_Last_Index(Str);
|
||||||
|
|
||||||
-- if Str.Buffer /= Empty_Buffer'Access and then Is_Shared(Str) then
|
|
||||||
-- if Req_Hard_Capa < Get_Hard_Capacity(Str) then
|
|
||||||
-- Hard_Capa := Get_Hard_Capacity(Str);
|
|
||||||
-- else
|
|
||||||
-- Hard_Capa := Req_Hard_Capa;
|
|
||||||
-- end if;
|
|
||||||
--
|
|
||||||
-- Tmp := New_Buffer_Container(Hard_Capa);
|
|
||||||
-- Tmp.Buffer.Slot(First .. Last + 1) := Str.Buffer.Slot(First .. Last + 1);
|
|
||||||
-- Tmp.Buffer.Last := Last;
|
|
||||||
--
|
|
||||||
-- Str := Tmp;
|
|
||||||
-- else
|
|
||||||
-- if Req_Hard_Capa > Get_Hard_Capacity(Str) then
|
|
||||||
-- Tmp := Str;
|
|
||||||
-- Str := New_Buffer_Container(Req_Hard_Capa);
|
|
||||||
--
|
|
||||||
-- Str.Buffer.Slot(First .. Last + 1) := Tmp.Buffer.Slot(First .. Last + 1);
|
|
||||||
-- Str.Buffer.Last := Last;
|
|
||||||
-- end if;
|
|
||||||
-- end if;
|
|
||||||
|
|
||||||
if Str.Buffer /= Empty_Buffer'Access and then Is_Shared(Str) then
|
if Str.Buffer /= Empty_Buffer'Access and then Is_Shared(Str) then
|
||||||
if Req_Hard_Capa < Get_Hard_Capacity(Str) then
|
if Req_Hard_Capa < Get_Hard_Capacity(Str) then
|
||||||
Hard_Capa := Get_Hard_Capacity(Str);
|
Hard_Capa := Get_Hard_Capacity(Str);
|
||||||
@ -175,27 +153,38 @@ package body H3.Strings is
|
|||||||
goto COPY_OVER;
|
goto COPY_OVER;
|
||||||
elsif Shift_Pos > 0 then
|
elsif Shift_Pos > 0 then
|
||||||
Tmp := Str;
|
Tmp := Str;
|
||||||
goto COPY_OVER;
|
goto COPY_OVER_WITH_SHIFT;
|
||||||
|
else
|
||||||
|
-- no shift, no change in the buffer
|
||||||
|
null;
|
||||||
end if;
|
end if;
|
||||||
end if;
|
end if;
|
||||||
|
|
||||||
return;
|
return;
|
||||||
|
|
||||||
<<COPY_OVER>>
|
<<COPY_OVER>>
|
||||||
if Shift_Pos > 0 then
|
if Shift_Pos <= 0 then
|
||||||
|
-- no shift is required. copy the entire string including th
|
||||||
|
Str.Buffer.Slot(First .. Last + 1) := Tmp.Buffer.Slot(First .. Last + 1);
|
||||||
|
Str.Buffer.Last := Last;
|
||||||
|
return;
|
||||||
|
end if;
|
||||||
|
<<COPY_OVER_WITH_SHIFT>>
|
||||||
-- it is an internal function. perform no sanity check.
|
-- it is an internal function. perform no sanity check.
|
||||||
-- if Shift_Pos or Shift_Size is beyond the allocated capacity,
|
-- if Shift_Pos or Shift_Size is beyond the allocated capacity,
|
||||||
-- it will end up in an exception.
|
-- it will end up in an exception.
|
||||||
|
if Shift_Dir = SHIFT_LEFT then
|
||||||
|
declare
|
||||||
|
Mid: System_Size := Shift_Pos - Shift_Size;
|
||||||
|
begin
|
||||||
|
Str.Buffer.Slot(First .. Mid) := Tmp.Buffer.Slot(First .. Mid);
|
||||||
|
Str.Buffer.Slot(Mid + 1 .. Last - Shift_Size + 1) := Tmp.Buffer.Slot(Shift_Pos + 1 .. Last + 1);
|
||||||
|
Str.Buffer.Last := Last - Shift_Size;
|
||||||
|
end;
|
||||||
|
else
|
||||||
Str.Buffer.Slot(First .. Shift_Pos - 1) := Tmp.Buffer.Slot(First .. Shift_Pos - 1);
|
Str.Buffer.Slot(First .. Shift_Pos - 1) := Tmp.Buffer.Slot(First .. Shift_Pos - 1);
|
||||||
ada.text_io.put_line ("Shift_Pos " & Shift_Pos'Img);
|
|
||||||
ada.text_io.put_line ("Shift_Size " & Shift_Size'Img);
|
|
||||||
ada.text_io.put_line ("Last " & Last'Img);
|
|
||||||
ada.text_io.put_line ("Capa " & Get_Hard_Capacity(Tmp)'Img);
|
|
||||||
Str.Buffer.Slot(Shift_Pos + Shift_Size .. Last + Shift_Size + 1) := Tmp.Buffer.Slot(Shift_Pos .. Last + 1);
|
Str.Buffer.Slot(Shift_Pos + Shift_Size .. Last + Shift_Size + 1) := Tmp.Buffer.Slot(Shift_Pos .. Last + 1);
|
||||||
Str.Buffer.Last := Last + Shift_Size;
|
Str.Buffer.Last := Last + Shift_Size;
|
||||||
else
|
|
||||||
Str.Buffer.Slot(First .. Last + 1) := Tmp.Buffer.Slot(First .. Last + 1);
|
|
||||||
Str.Buffer.Last := Last;
|
|
||||||
end if;
|
end if;
|
||||||
end Prepare_Buffer;
|
end Prepare_Buffer;
|
||||||
|
|
||||||
@ -211,51 +200,117 @@ package body H3.Strings is
|
|||||||
Str.Buffer := Empty_Buffer'Access;
|
Str.Buffer := Empty_Buffer'Access;
|
||||||
end Purge;
|
end Purge;
|
||||||
|
|
||||||
-- TODO: operator "&"
|
procedure Insert (Str: in out Elastic_String; Pos: in System_Index; V: in Character_Type; Repeat: in System_Size := 1) is
|
||||||
|
Act_Pos: System_Index := Pos;
|
||||||
|
Act_Inc: System_Size := Repeat;
|
||||||
|
begin
|
||||||
|
if Act_Pos > Str.Buffer.Last then
|
||||||
|
Act_Pos := Str.Buffer.Last + 1;
|
||||||
|
end if;
|
||||||
|
|
||||||
|
Prepare_Buffer (Str, H3.Align(Get_Length(Str) + Act_Inc + 1, BUFFER_ALIGN), Act_Pos, Act_Inc);
|
||||||
|
Str.Buffer.Slot(Act_Pos .. Act_Pos + Act_Inc - 1) := (others => V);
|
||||||
|
end Insert;
|
||||||
|
|
||||||
|
procedure Insert (Str: in out Elastic_String; Pos: in System_Index; V: in Character_Array) is
|
||||||
|
Act_Pos: System_Index := Pos;
|
||||||
|
begin
|
||||||
|
if Act_Pos > Str.Buffer.Last then
|
||||||
|
Act_Pos := Str.Buffer.Last + 1;
|
||||||
|
end if;
|
||||||
|
|
||||||
|
Prepare_Buffer (Str, H3.Align(Get_Length(Str) + V'Length + 1, BUFFER_ALIGN), Act_Pos, V'Length);
|
||||||
|
Str.Buffer.Slot(Act_Pos .. Act_Pos + V'Length - 1) := V;
|
||||||
|
end Insert;
|
||||||
|
|
||||||
|
-- TODO: operator "&" that returns a new Elastic_String
|
||||||
|
procedure Append (Str: in out Elastic_String; V: in Character_Type; Repeat: in System_Size := 1) is
|
||||||
|
begin
|
||||||
|
Insert (Str, Get_Last_Index(Str) + 1, V, Repeat);
|
||||||
|
end Append;
|
||||||
|
|
||||||
procedure Append (Str: in out Elastic_String; V: in Character_Array) is
|
procedure Append (Str: in out Elastic_String; V: in Character_Array) is
|
||||||
begin
|
begin
|
||||||
if V'Length > 0 then
|
Insert (Str, Get_Last_Index(Str) + 1, V);
|
||||||
Prepare_Buffer (Str, H3.Align(Get_Length(Str) + V'Length + 1, BUFFER_ALIGN));
|
end Append;
|
||||||
Str.Buffer.Slot(Str.Buffer.Last + 1 .. Str.Buffer.Last + V'Length) := V;
|
|
||||||
Str.Buffer.Last := Str.Buffer.Last + V'Length;
|
procedure Prepend (Str: in out Elastic_String; V: in Character_Type; Repeat: in System_Size := 1) is
|
||||||
Str.Buffer.Slot(Str.Buffer.Last + 1) := Null_Character;
|
begin
|
||||||
|
Insert (Str, Get_First_Index(Str), V, Repeat);
|
||||||
|
end Prepend;
|
||||||
|
|
||||||
|
procedure Prepend (Str: in out Elastic_String; V: in Character_Array) is
|
||||||
|
begin
|
||||||
|
Insert (Str, Get_First_Index(Str), V);
|
||||||
|
end Prepend;
|
||||||
|
|
||||||
|
procedure Replace (Str: in out Elastic_String; From_Pos: in System_Index; To_Pos: in System_Size; V: in Character_Type; Repeat: in System_Size := 1) is
|
||||||
|
Act_To_Pos, Repl_Len: System_Size;
|
||||||
|
begin
|
||||||
|
if From_Pos <= To_Pos and then From_Pos <= Str.Buffer.Last then
|
||||||
|
Act_To_Pos := To_Pos;
|
||||||
|
if Act_To_Pos > Str.Buffer.Last then
|
||||||
|
Act_To_Pos := Str.Buffer.Last;
|
||||||
end if;
|
end if;
|
||||||
end Append;
|
|
||||||
|
|
||||||
procedure Append (Str: in out Elastic_String; V: in Character_Type) is
|
Repl_Len := Act_To_Pos - From_Pos + 1;
|
||||||
Tmp: Character_Array(1 .. 1) := (1 => V);
|
if Repeat < Repl_Len then
|
||||||
begin
|
Prepare_Buffer (Str, Get_Hard_Capacity(Str), Act_To_Pos, Repl_Len - Repeat, SHIFT_LEFT);
|
||||||
Append (Str, Tmp);
|
Act_To_Pos := From_Pos + Repeat - 1;
|
||||||
end Append;
|
elsif Repeat > Repl_Len then
|
||||||
|
Prepare_Buffer (Str, Get_Hard_Capacity(Str), From_Pos, Repeat - Repl_Len, SHIFT_RIGHT);
|
||||||
procedure Delete (Str: in out Elastic_String; Pos: in System_Index; Length: in System_Size) is
|
Act_To_Pos := From_Pos + Repeat - 1;
|
||||||
begin
|
|
||||||
if Pos <= Str.Buffer.Last then
|
|
||||||
Prepare_Buffer (Str);
|
|
||||||
else
|
else
|
||||||
raise Standard.Constraint_Error;
|
Prepare_Buffer (Str);
|
||||||
|
end if;
|
||||||
|
Str.Buffer.Slot(From_Pos .. Act_To_Pos) := (others => V);
|
||||||
|
end if;
|
||||||
|
end Replace;
|
||||||
|
|
||||||
|
procedure Replace (Str: in out Elastic_String; From_Pos: in System_Index; To_Pos: in System_Size; V: in Character_Array) is
|
||||||
|
Act_To_Pos, Repl_Len: System_Size;
|
||||||
|
begin
|
||||||
|
if From_Pos <= To_Pos and then From_Pos <= Str.Buffer.Last then
|
||||||
|
Act_To_Pos := To_Pos;
|
||||||
|
if Act_To_Pos > Str.Buffer.Last then
|
||||||
|
Act_To_Pos := Str.Buffer.Last;
|
||||||
|
end if;
|
||||||
|
|
||||||
|
Repl_Len := Act_To_Pos - From_Pos + 1;
|
||||||
|
if V'Length < Repl_Len then
|
||||||
|
Prepare_Buffer (Str, Get_Hard_Capacity(Str), Act_To_Pos, Repl_Len - V'Length, SHIFT_LEFT);
|
||||||
|
Act_To_Pos := From_Pos + V'Length - 1;
|
||||||
|
elsif V'Length > Repl_Len then
|
||||||
|
Prepare_Buffer (Str, Get_Hard_Capacity(Str), From_Pos, V'Length - Repl_Len, SHIFT_RIGHT);
|
||||||
|
Act_To_Pos := From_Pos + V'Length - 1;
|
||||||
|
else
|
||||||
|
Prepare_Buffer (Str);
|
||||||
|
end if;
|
||||||
|
Str.Buffer.Slot(From_Pos .. Act_To_Pos) := V;
|
||||||
|
end if;
|
||||||
|
end Replace;
|
||||||
|
|
||||||
|
procedure Delete (Str: in out Elastic_String; From_Pos: in System_Index; To_Pos: in System_Size) is
|
||||||
|
Act_To_Pos: System_Size;
|
||||||
|
begin
|
||||||
|
if From_Pos <= To_Pos and then From_Pos <= Str.Buffer.Last then
|
||||||
|
Act_To_Pos := To_Pos;
|
||||||
|
if Act_To_Pos > Str.Buffer.Last then
|
||||||
|
Act_To_Pos := Str.Buffer.Last;
|
||||||
|
end if;
|
||||||
|
Prepare_Buffer (Str, Get_Hard_Capacity(Str), Act_To_Pos, Act_To_Pos - From_Pos + 1, SHIFT_LEFT);
|
||||||
end if;
|
end if;
|
||||||
end Delete;
|
end Delete;
|
||||||
|
|
||||||
procedure Insert (Str: in out Elastic_String; Pos: in System_Index; New_Char: in Character_Type) is
|
function "=" (Str: in Elastic_String; Str2: in Elastic_String) return Standard.Boolean is
|
||||||
begin
|
begin
|
||||||
if Pos <= Str.Buffer.Last then
|
return Str.Buffer = Str2.Buffer or else Str.Buffer.Slot(Get_First_Index(Str) .. Get_Last_Index(Str)) = Str2.Buffer.Slot(Get_First_Index(Str2) .. Get_Last_Index(Str2));
|
||||||
ada.text_io.put_line ( "INSERT ");
|
end "=";
|
||||||
Prepare_Buffer (Str, H3.Align(Get_Length(Str) + 1, BUFFER_ALIGN), Pos, 1);
|
|
||||||
Str.Buffer.Slot(Pos) := New_Char;
|
|
||||||
end if;
|
|
||||||
end Insert;
|
|
||||||
|
|
||||||
procedure Replace (Str: in out Elastic_String; Pos: in System_Index; New_Char: in Character_Type) is
|
function "=" (Str: in Elastic_String; Str2: in Character_Array) return Standard.Boolean is
|
||||||
begin
|
begin
|
||||||
if Pos <= Str.Buffer.Last then
|
return Str.Buffer.Slot(Get_First_Index(Str) .. Get_Last_Index(Str)) = Str2;
|
||||||
Prepare_Buffer (Str);
|
end "=";
|
||||||
Str.Buffer.Slot(Pos) := New_Char;
|
|
||||||
else
|
|
||||||
-- raise Index_Error;
|
|
||||||
raise Standard.Constraint_Error;
|
|
||||||
end if;
|
|
||||||
end Replace;
|
|
||||||
|
|
||||||
-- ---------------------------------------------------------------------
|
-- ---------------------------------------------------------------------
|
||||||
-- Controlled Management
|
-- Controlled Management
|
||||||
|
@ -3,7 +3,7 @@ with Ada.Finalization;
|
|||||||
generic
|
generic
|
||||||
--type Character_Type is private;
|
--type Character_Type is private;
|
||||||
type Character_Type is (<>);
|
type Character_Type is (<>);
|
||||||
Null_Character: Character_Type;
|
Terminator: Character_Type;
|
||||||
package H3.Strings is
|
package H3.Strings is
|
||||||
|
|
||||||
type Elastic_String is private;
|
type Elastic_String is private;
|
||||||
@ -42,11 +42,22 @@ package H3.Strings is
|
|||||||
procedure Purge (Str: in out Elastic_String);
|
procedure Purge (Str: in out Elastic_String);
|
||||||
procedure Clear (Str: in out Elastic_String);
|
procedure Clear (Str: in out Elastic_String);
|
||||||
|
|
||||||
procedure Append (Str: in out Elastic_String; V: in Character_Array);
|
procedure Insert (Str: in out Elastic_String; Pos: in System_Index; V: in Character_Type; Repeat: in System_Size := 1);
|
||||||
procedure Append (Str: in out Elastic_String; V: in Character_Type);
|
procedure Insert (Str: in out Elastic_String; Pos: in System_Index; V: in Character_Array);
|
||||||
|
|
||||||
procedure Insert (Str: in out Elastic_String; Pos: in System_Index; New_Char: in Character_Type);
|
procedure Append (Str: in out Elastic_String; V: in Character_Type; Repeat: in System_Size := 1);
|
||||||
procedure Replace (Str: in out Elastic_String; Pos: in System_Index; New_Char: in Character_Type);
|
procedure Append (Str: in out Elastic_String; V: in Character_Array);
|
||||||
|
|
||||||
|
procedure Prepend (Str: in out Elastic_String; V: in Character_Type; Repeat: in System_Size := 1);
|
||||||
|
procedure Prepend (Str: in out Elastic_String; V: in Character_Array);
|
||||||
|
|
||||||
|
procedure Replace (Str: in out Elastic_String; From_Pos: in System_Index; To_Pos: in System_Size; V: in Character_Type; Repeat: in System_Size := 1);
|
||||||
|
procedure Replace (Str: in out Elastic_String; From_Pos: in System_Index; To_Pos: in System_Size; V: in Character_Array);
|
||||||
|
|
||||||
|
procedure Delete (Str: in out Elastic_String; From_Pos: in System_Index; To_Pos: in System_Size);
|
||||||
|
|
||||||
|
function "=" (Str: in Elastic_String; Str2: in Elastic_String) return Standard.Boolean;
|
||||||
|
function "=" (Str: in Elastic_String; Str2: in Character_Array) return Standard.Boolean;
|
||||||
|
|
||||||
private
|
private
|
||||||
type Buffer_Record(Capa: System_Size) is limited record
|
type Buffer_Record(Capa: System_Size) is limited record
|
||||||
@ -58,7 +69,7 @@ private
|
|||||||
type Buffer_Pointer is access all Buffer_Record;
|
type Buffer_Pointer is access all Buffer_Record;
|
||||||
|
|
||||||
--Empty_Buffer: aliased Buffer_Record(1);
|
--Empty_Buffer: aliased Buffer_Record(1);
|
||||||
Empty_Buffer: aliased Buffer_Record := (Capa => 1, Refs => 0, Slot => (1 => Null_Character), Last => 0);
|
Empty_Buffer: aliased Buffer_Record := (Capa => 1, Refs => 0, Slot => (1 => Terminator), Last => 0);
|
||||||
|
|
||||||
type Elastic_String is new Ada.Finalization.Controlled with record
|
type Elastic_String is new Ada.Finalization.Controlled with record
|
||||||
Buffer: Buffer_Pointer := Empty_Buffer'Access;
|
Buffer: Buffer_Pointer := Empty_Buffer'Access;
|
||||||
|
266
lib2/hello.adb
266
lib2/hello.adb
@ -49,6 +49,22 @@ procedure hello is
|
|||||||
|
|
||||||
SS: S.Elastic_String;
|
SS: S.Elastic_String;
|
||||||
|
|
||||||
|
procedure print_string_info (Str: in S.Elastic_String; Name: in Standard.String) is
|
||||||
|
len: H3.System_Size;
|
||||||
|
capa: H3.System_Size;
|
||||||
|
first: H3.System_Size;
|
||||||
|
last: H3.System_Size;
|
||||||
|
begin
|
||||||
|
len := S.Get_Length(Str);
|
||||||
|
capa := S.Get_Capacity(Str);
|
||||||
|
first := S.Get_First_Index(Str);
|
||||||
|
last := S.Get_Last_Index(Str);
|
||||||
|
Ada.Text_IO.Put (Name & " len:" & len'Img & " capa:" & capa'Img & " first:" & first'img & " last:" & last'img & " => ");
|
||||||
|
Ada.Wide_Text_IO.Put_line (Standard.Wide_String(S.To_Character_Array(Str)));
|
||||||
|
|
||||||
|
pragma Assert (S.Get_Item(Str, S.Get_Last_Index(Str) + 1) = Wide_Character'Val(0));
|
||||||
|
end print_string_info;
|
||||||
|
|
||||||
begin
|
begin
|
||||||
declare
|
declare
|
||||||
TTT: H3.System_Size := H3.System_Size'Last;
|
TTT: H3.System_Size := H3.System_Size'Last;
|
||||||
@ -103,81 +119,235 @@ begin
|
|||||||
declare
|
declare
|
||||||
str: S.Elastic_String;
|
str: S.Elastic_String;
|
||||||
str2: S.Elastic_String;
|
str2: S.Elastic_String;
|
||||||
len: H3.System_Size;
|
|
||||||
capa: H3.System_Size;
|
|
||||||
first: H3.System_Size;
|
|
||||||
last: H3.System_Size;
|
|
||||||
begin
|
begin
|
||||||
len := S.Get_Length(Str);
|
print_string_info (Str, "Str");
|
||||||
capa := S.Get_Capacity(Str);
|
pragma Assert (S.Get_Length(Str) = 0);
|
||||||
first := S.Get_First_Index(Str);
|
pragma Assert (S.Get_First_Index(Str) = 1);
|
||||||
last := S.Get_Last_Index(Str);
|
pragma Assert (S.Get_Last_Index(Str) = 0);
|
||||||
Ada.Text_IO.Put_Line ("length=>" & len'Img & " Capacity=>" & capa'Img & " First=>" & first'img & " Last=>" & last'img);
|
|
||||||
|
|
||||||
S.Append(Str, "Hello, world");
|
|
||||||
len := S.Get_Length(Str);
|
|
||||||
capa := S.Get_Capacity(Str);
|
|
||||||
first := S.Get_First_Index(Str);
|
|
||||||
last := S.Get_Last_Index(Str);
|
|
||||||
Ada.Text_IO.Put_Line ("length=>" & len'Img & " Capacity=>" & capa'Img & " First=>" & first'img & " Last=>" & last'img);
|
|
||||||
|
|
||||||
|
S.Append(Str, "Hello, world!");
|
||||||
|
print_string_info (Str, "Str");
|
||||||
|
pragma Assert (S.Get_Length(Str) = 13);
|
||||||
|
pragma Assert (S.Get_First_Index(Str) = 1);
|
||||||
|
pragma Assert (S.Get_Last_Index(Str) = 13);
|
||||||
|
|
||||||
S.Append(Str, "");
|
S.Append(Str, "");
|
||||||
len := S.Get_Length(Str);
|
print_string_info (Str, "Str");
|
||||||
capa := S.Get_Capacity(Str);
|
pragma Assert (S.Get_Length(Str) = 13);
|
||||||
first := S.Get_First_Index(Str);
|
pragma Assert (S.Get_First_Index(Str) = 1);
|
||||||
last := S.Get_Last_Index(Str);
|
pragma Assert (S.Get_Last_Index(Str) = 13);
|
||||||
Ada.Text_IO.Put_Line ("length=>" & len'Img & " Capacity=>" & capa'Img & " First=>" & first'img & " Last=>" & last'img);
|
|
||||||
|
|
||||||
-- S.Append(Str, "donkey");
|
S.Append(Str, ' ', 0);
|
||||||
len := S.Get_Length(Str);
|
print_string_info (Str, "Str");
|
||||||
capa := S.Get_Capacity(Str);
|
pragma Assert (S.Get_Length(Str) = 13);
|
||||||
first := S.Get_First_Index(Str);
|
pragma Assert (S.Get_First_Index(Str) = 1);
|
||||||
last := S.Get_Last_Index(Str);
|
pragma Assert (S.Get_Last_Index(Str) = 13);
|
||||||
Ada.Text_IO.Put_Line ("length=>" & len'Img & " Capacity=>" & capa'Img & " First=>" & first'img & " Last=>" & last'img);
|
|
||||||
|
|
||||||
|
S.Prepend(Str, ' ', 0);
|
||||||
|
print_string_info (Str, "Str");
|
||||||
|
pragma Assert (S.Get_Length(Str) = 13);
|
||||||
|
pragma Assert (S.Get_First_Index(Str) = 1);
|
||||||
|
pragma Assert (S.Get_Last_Index(Str) = 13);
|
||||||
|
|
||||||
|
S.Append(Str, ' ', 2);
|
||||||
|
print_string_info (Str, "Str");
|
||||||
|
pragma Assert (S.Get_Length(Str) = 15);
|
||||||
|
pragma Assert (S.Get_First_Index(Str) = 1);
|
||||||
|
pragma Assert (S.Get_Last_Index(Str) = 15);
|
||||||
|
|
||||||
|
S.Append(Str, "donkey");
|
||||||
|
print_string_info (Str, "Str");
|
||||||
|
pragma Assert (S.Get_Length(Str) = 21);
|
||||||
|
pragma Assert (S.Get_First_Index(Str) = 1);
|
||||||
|
pragma Assert (S.Get_Last_Index(Str) = 21);
|
||||||
|
|
||||||
|
S.Prepend(Str, "Oh! ");
|
||||||
|
print_string_info (Str, "Str");
|
||||||
|
pragma Assert (S.Get_Length(Str) = 25);
|
||||||
|
pragma Assert (S.Get_First_Index(Str) = 1);
|
||||||
|
pragma Assert (S.Get_Last_Index(Str) = 25);
|
||||||
|
|
||||||
declare
|
declare
|
||||||
arr: constant S.Character_Array := S.To_Character_Array(str);
|
-- unsafe way to access the internal buffer.
|
||||||
|
arr: constant S.Character_Array := S.To_Character_Array(Str);
|
||||||
begin
|
begin
|
||||||
Ada.Wide_Text_IO.Put ("[");
|
Ada.Wide_Text_IO.Put ("STR[1] => [");
|
||||||
for i in arr'Range loop
|
for i in arr'Range loop
|
||||||
Ada.Wide_Text_IO.Put (arr(i));
|
Ada.Wide_Text_IO.Put (arr(i));
|
||||||
end loop;
|
end loop;
|
||||||
Ada.Wide_Text_IO.Put_Line ("]");
|
Ada.Wide_Text_IO.Put_Line ("]");
|
||||||
|
|
||||||
Ada.Wide_Text_IO.Put ("PRINTING AGAIN [");
|
Ada.Wide_Text_IO.Put ("STR[2] => [");
|
||||||
|
for i in S.Get_First_Index(Str) .. S.Get_Last_Index(Str) loop
|
||||||
|
Ada.Wide_Text_IO.Put (S.Get_Item(Str, i));
|
||||||
|
end loop;
|
||||||
|
Ada.Wide_Text_IO.Put_Line ("]");
|
||||||
|
|
||||||
|
Ada.Wide_Text_IO.Put ("STR[3] => [");
|
||||||
Ada.Wide_Text_IO.Put (Standard.Wide_String(arr));
|
Ada.Wide_Text_IO.Put (Standard.Wide_String(arr));
|
||||||
Ada.Wide_Text_IO.Put_Line ("]");
|
Ada.Wide_Text_IO.Put_Line ("]");
|
||||||
end;
|
end;
|
||||||
|
|
||||||
-- unsafe way to access the internal buffer.
|
pragma Assert (S."="(Str, "Oh! Hello, world! donkey"));
|
||||||
S.Append (Str, 'X');
|
|
||||||
S.Append(Str, "donkeyX");
|
S.Append (Str, '>');
|
||||||
S.Append(Str, "ABCDE");
|
print_string_info (Str, "Str");
|
||||||
|
pragma Assert (S.Get_Length(Str) = 26);
|
||||||
|
pragma Assert (S.Get_First_Index(Str) = 1);
|
||||||
|
pragma Assert (S.Get_Last_Index(Str) = 26);
|
||||||
|
pragma Assert (S."="(Str, "Oh! Hello, world! donkey>"));
|
||||||
|
|
||||||
|
|
||||||
|
S.Append (Str, "donkeyX");
|
||||||
|
print_string_info (Str, "Str");
|
||||||
|
pragma Assert (S.Get_Length(Str) = 33);
|
||||||
|
pragma Assert (S.Get_First_Index(Str) = 1);
|
||||||
|
pragma Assert (S.Get_Last_Index(Str) = 33);
|
||||||
|
pragma Assert (S."="(Str, "Oh! Hello, world! donkey>donkeyX"));
|
||||||
|
|
||||||
|
S.Append (Str, "ABCDE");
|
||||||
|
print_string_info (Str, "Str");
|
||||||
|
pragma Assert (S.Get_Length(Str) = 38);
|
||||||
|
pragma Assert (S.Get_First_Index(Str) = 1);
|
||||||
|
pragma Assert (S.Get_Last_Index(Str) = 38);
|
||||||
|
pragma Assert (S."="(Str, "Oh! Hello, world! donkey>donkeyXABCDE"));
|
||||||
|
|
||||||
Str2 := Str;
|
Str2 := Str;
|
||||||
S.Append (Str2, "EXTRA");
|
S.Append (Str2, "EXTRA");
|
||||||
S.Append (Str2, " THIS IS FANTASTIC ELASTIC STRING WRITTEN FOR H3");
|
print_string_info (Str2, "Str2");
|
||||||
|
pragma Assert (S.Get_Length(Str2) = 43);
|
||||||
|
pragma Assert (S.Get_First_Index(Str2) = 1);
|
||||||
|
pragma Assert (S.Get_Last_Index(Str2) = 43);
|
||||||
|
pragma Assert (S."="(Str2, "Oh! Hello, world! donkey>donkeyXABCDEEXTRA"));
|
||||||
|
pragma Assert (S.Get_Length(Str) = 38);
|
||||||
|
pragma Assert (S.Get_First_Index(Str) = 1);
|
||||||
|
pragma Assert (S.Get_Last_Index(Str) = 38);
|
||||||
|
pragma Assert (S."="(Str, "Oh! Hello, world! donkey>donkeyXABCDE"));
|
||||||
|
|
||||||
|
S.Append (Str2, " THIS IS FANTASTIC ELASTIC STRING WRITTEN FOR H3");
|
||||||
|
print_string_info (Str2, "Str2");
|
||||||
|
pragma Assert (S.Get_Length(Str2) = 91);
|
||||||
|
pragma Assert (S.Get_First_Index(Str2) = 1);
|
||||||
|
pragma Assert (S.Get_Last_Index(Str2) = 91);
|
||||||
|
pragma Assert (S."="(Str2, "Oh! Hello, world! donkey>donkeyXABCDEEXTRA THIS IS FANTASTIC ELASTIC STRING WRITTEN FOR H3"));
|
||||||
|
pragma Assert (S.Get_Length(Str) = 38);
|
||||||
|
pragma Assert (S.Get_First_Index(Str) = 1);
|
||||||
|
pragma Assert (S.Get_Last_Index(Str) = 38);
|
||||||
|
pragma Assert (S."="(Str, "Oh! Hello, world! donkey>donkeyXABCDE"));
|
||||||
|
|
||||||
|
S.Replace (Str2, 1, 1, 'Q');
|
||||||
|
print_string_info (Str2, "Str2");
|
||||||
|
pragma Assert (S.Get_Length(Str2) = 91);
|
||||||
|
pragma Assert (S.Get_First_Index(Str2) = 1);
|
||||||
|
pragma Assert (S.Get_Last_Index(Str2) = 91);
|
||||||
|
pragma Assert (S."="(Str2, "Qh! Hello, world! donkey>donkeyXABCDEEXTRA THIS IS FANTASTIC ELASTIC STRING WRITTEN FOR H3"));
|
||||||
|
|
||||||
S.Replace (Str2, 1, 'Q');
|
|
||||||
S.Insert (Str2, 1, 'B');
|
S.Insert (Str2, 1, 'B');
|
||||||
S.Insert (Str2, 1, 'A');
|
print_string_info (Str2, "Str2");
|
||||||
|
pragma Assert (S.Get_Length(Str2) = 92);
|
||||||
|
pragma Assert (S.Get_First_Index(Str2) = 1);
|
||||||
|
pragma Assert (S.Get_Last_Index(Str2) = 92);
|
||||||
|
pragma Assert (S."="(Str2, "BQh! Hello, world! donkey>donkeyXABCDEEXTRA THIS IS FANTASTIC ELASTIC STRING WRITTEN FOR H3"));
|
||||||
|
|
||||||
|
S.Insert (Str2, 1, 'A', 3);
|
||||||
|
print_string_info (Str2, "Str2");
|
||||||
|
pragma Assert (S.Get_Length(Str2) = 95);
|
||||||
|
pragma Assert (S.Get_First_Index(Str2) = 1);
|
||||||
|
pragma Assert (S.Get_Last_Index(Str2) = 95);
|
||||||
|
pragma Assert (S."="(Str2, "AAABQh! Hello, world! donkey>donkeyXABCDEEXTRA THIS IS FANTASTIC ELASTIC STRING WRITTEN FOR H3"));
|
||||||
|
|
||||||
|
S.Insert (Str2, 3, 'C', 2);
|
||||||
|
print_string_info (Str2, "Str2");
|
||||||
|
pragma Assert (S.Get_Length(Str2) = 97);
|
||||||
|
pragma Assert (S.Get_First_Index(Str2) = 1);
|
||||||
|
pragma Assert (S.Get_Last_Index(Str2) = 97);
|
||||||
|
pragma Assert (S."="(Str2, "AACCABQh! Hello, world! donkey>donkeyXABCDEEXTRA THIS IS FANTASTIC ELASTIC STRING WRITTEN FOR H3"));
|
||||||
|
|
||||||
|
S.Insert (Str2, 5, "");
|
||||||
|
print_string_info (Str2, "Str2");
|
||||||
|
pragma Assert (S.Get_Length(Str2) = 97);
|
||||||
|
pragma Assert (S.Get_First_Index(Str2) = 1);
|
||||||
|
pragma Assert (S.Get_Last_Index(Str2) = 97);
|
||||||
|
pragma Assert (S."="(Str2, "AACCABQh! Hello, world! donkey>donkeyXABCDEEXTRA THIS IS FANTASTIC ELASTIC STRING WRITTEN FOR H3"));
|
||||||
|
|
||||||
|
S.Insert (Str2, S.Get_Last_Index(Str2) + 1, "");
|
||||||
|
print_string_info (Str2, "Str2");
|
||||||
|
pragma Assert (S.Get_Length(Str2) = 97);
|
||||||
|
pragma Assert (S.Get_First_Index(Str2) = 1);
|
||||||
|
pragma Assert (S.Get_Last_Index(Str2) = 97);
|
||||||
|
pragma Assert (S."="(Str2, "AACCABQh! Hello, world! donkey>donkeyXABCDEEXTRA THIS IS FANTASTIC ELASTIC STRING WRITTEN FOR H3"));
|
||||||
|
|
||||||
|
S.Insert (Str2, S.Get_Last_Index(Str2) + 1, " => ABCDEF");
|
||||||
|
print_string_info (Str2, "Str2");
|
||||||
|
pragma Assert (S.Get_Length(Str2) = 107);
|
||||||
|
pragma Assert (S.Get_First_Index(Str2) = 1);
|
||||||
|
pragma Assert (S.Get_Last_Index(Str2) = 107);
|
||||||
|
pragma Assert (S."="(Str2, "AACCABQh! Hello, world! donkey>donkeyXABCDEEXTRA THIS IS FANTASTIC ELASTIC STRING WRITTEN FOR H3 => ABCDEF"));
|
||||||
--S.Replace (Str2, 10000, 'Q'); -- constraint error
|
--S.Replace (Str2, 10000, 'Q'); -- constraint error
|
||||||
|
|
||||||
|
S.Prepend (Str2, '>', 3);
|
||||||
|
print_string_info (Str2, "Str2");
|
||||||
|
pragma Assert (S.Get_Length(Str2) = 110);
|
||||||
|
pragma Assert (S.Get_First_Index(Str2) = 1);
|
||||||
|
pragma Assert (S.Get_Last_Index(Str2) = 110);
|
||||||
|
pragma Assert (S."="(Str2, ">>>AACCABQh! Hello, world! donkey>donkeyXABCDEEXTRA THIS IS FANTASTIC ELASTIC STRING WRITTEN FOR H3 => ABCDEF"));
|
||||||
|
|
||||||
|
S.Delete (Str2, 1, 3);
|
||||||
|
print_string_info (Str2, "Str2");
|
||||||
|
pragma Assert (S.Get_Length(Str2) = 107);
|
||||||
|
pragma Assert (S.Get_First_Index(Str2) = 1);
|
||||||
|
pragma Assert (S.Get_Last_Index(Str2) = 107);
|
||||||
|
pragma Assert (S."="(Str2, "AACCABQh! Hello, world! donkey>donkeyXABCDEEXTRA THIS IS FANTASTIC ELASTIC STRING WRITTEN FOR H3 => ABCDEF"));
|
||||||
|
|
||||||
|
S.Delete (Str2, S.Get_Last_Index(Str2) - 9, S.Get_Last_Index(Str2));
|
||||||
|
print_string_info (Str2, "Str2");
|
||||||
|
pragma Assert (S.Get_Length(Str2) = 97);
|
||||||
|
pragma Assert (S.Get_First_Index(Str2) = 1);
|
||||||
|
pragma Assert (S.Get_Last_Index(Str2) = 97);
|
||||||
|
pragma Assert (S."="(Str2, "AACCABQh! Hello, world! donkey>donkeyXABCDEEXTRA THIS IS FANTASTIC ELASTIC STRING WRITTEN FOR H3"));
|
||||||
|
|
||||||
|
S.Delete (Str2, 5, 9);
|
||||||
|
print_string_info (Str2, "Str2");
|
||||||
|
pragma Assert (S.Get_Length(Str2) = 92);
|
||||||
|
pragma Assert (S.Get_First_Index(Str2) = 1);
|
||||||
|
pragma Assert (S.Get_Last_Index(Str2) = 92);
|
||||||
|
pragma Assert (S."="(Str2, "AACC Hello, world! donkey>donkeyXABCDEEXTRA THIS IS FANTASTIC ELASTIC STRING WRITTEN FOR H3"));
|
||||||
|
|
||||||
|
S.Replace (Str2, 1, 5, "");
|
||||||
|
print_string_info (Str2, "Str2");
|
||||||
|
pragma Assert (S.Get_Length(Str2) = 87);
|
||||||
|
pragma Assert (S.Get_First_Index(Str2) = 1);
|
||||||
|
pragma Assert (S.Get_Last_Index(Str2) = 87);
|
||||||
|
pragma Assert (S."="(Str2, "Hello, world! donkey>donkeyXABCDEEXTRA THIS IS FANTASTIC ELASTIC STRING WRITTEN FOR H3"));
|
||||||
|
|
||||||
|
S.Replace (Str2, 8, 12, "cougar");
|
||||||
|
print_string_info (Str2, "Str2");
|
||||||
|
pragma Assert (S.Get_Length(Str2) = 88);
|
||||||
|
pragma Assert (S.Get_First_Index(Str2) = 1);
|
||||||
|
pragma Assert (S.Get_Last_Index(Str2) = 88);
|
||||||
|
pragma Assert (S."="(Str2, "Hello, cougar! donkey>donkeyXABCDEEXTRA THIS IS FANTASTIC ELASTIC STRING WRITTEN FOR H3"));
|
||||||
|
|
||||||
|
S.Replace (Str2, S.Get_Last_Index(Str2) - 1, S.Get_Last_Index(Str2) + 100, "HH");
|
||||||
|
print_string_info (Str2, "Str2");
|
||||||
|
pragma Assert (S.Get_Length(Str2) = 88);
|
||||||
|
pragma Assert (S.Get_First_Index(Str2) = 1);
|
||||||
|
pragma Assert (S.Get_Last_Index(Str2) = 88);
|
||||||
|
pragma Assert (S."="(Str2, "Hello, cougar! donkey>donkeyXABCDEEXTRA THIS IS FANTASTIC ELASTIC STRING WRITTEN FOR HH"));
|
||||||
|
|
||||||
|
S.Replace (Str2, 8, 13, "bee");
|
||||||
|
print_string_info (Str2, "Str2");
|
||||||
|
pragma Assert (S.Get_Length(Str2) = 85);
|
||||||
|
pragma Assert (S.Get_First_Index(Str2) = 1);
|
||||||
|
pragma Assert (S.Get_Last_Index(Str2) = 85);
|
||||||
|
pragma Assert (S."="(Str2, "Hello, bee! donkey>donkeyXABCDEEXTRA THIS IS FANTASTIC ELASTIC STRING WRITTEN FOR HH"));
|
||||||
|
|
||||||
declare
|
declare
|
||||||
arr: constant S.Thin_Character_Array_Pointer := S.Get_Slot_Pointer(Str);
|
arr: constant S.Thin_Character_Array_Pointer := S.Get_Slot_Pointer(Str);
|
||||||
arr2: constant S.Thin_Character_Array_Pointer := S.Get_Slot_Pointer(Str2);
|
arr2: constant S.Thin_Character_Array_Pointer := S.Get_Slot_Pointer(Str2);
|
||||||
use type H3.System_Word;
|
use type H3.System_Word;
|
||||||
begin
|
begin
|
||||||
Ada.Assertions.Assert (S.Get_Length(Str) = 25, "invalid string length");
|
print_string_info (Str, "Str");
|
||||||
Ada.Assertions.Assert (S.Get_Length(Str2) = 78, "invalid string length");
|
|
||||||
|
|
||||||
len := S.Get_Length(Str);
|
|
||||||
capa := S.Get_Capacity(Str);
|
|
||||||
first := S.Get_First_Index(Str);
|
|
||||||
last := S.Get_Last_Index(Str);
|
|
||||||
Ada.Text_IO.Put_Line ("STR length=>" & len'Img & " Capacity=>" & capa'Img & " First=>" & first'img & " Last=>" & last'img);
|
|
||||||
|
|
||||||
Ada.Wide_Text_IO.Put ("STR(By-Pointer) [");
|
Ada.Wide_Text_IO.Put ("STR(By-Pointer) [");
|
||||||
for i in S.Get_First_Index(Str) .. S.Get_Last_Index(Str) + 1 loop -- this must loop to the terminating null.
|
for i in S.Get_First_Index(Str) .. S.Get_Last_Index(Str) + 1 loop -- this must loop to the terminating null.
|
||||||
@ -185,11 +355,7 @@ begin
|
|||||||
end loop;
|
end loop;
|
||||||
Ada.Wide_Text_IO.Put_Line ("]");
|
Ada.Wide_Text_IO.Put_Line ("]");
|
||||||
|
|
||||||
len := S.Get_Length(Str2);
|
print_string_info (Str2, "Str2");
|
||||||
capa := S.Get_Capacity(Str2);
|
|
||||||
first := S.Get_First_Index(Str2);
|
|
||||||
last := S.Get_Last_Index(Str2);
|
|
||||||
Ada.Text_IO.Put_Line ("STR2 length=>" & len'Img & " Capacity=>" & capa'Img & " First=>" & first'img & " Last=>" & last'img);
|
|
||||||
|
|
||||||
Ada.Wide_Text_IO.Put ("Str2(By-Pointer) ["); -- this must loop to the terminating null.
|
Ada.Wide_Text_IO.Put ("Str2(By-Pointer) ["); -- this must loop to the terminating null.
|
||||||
for i in S.Get_First_Index(Str2) .. S.Get_Last_Index(Str2) + 1 loop
|
for i in S.Get_First_Index(Str2) .. S.Get_Last_Index(Str2) + 1 loop
|
||||||
|
Loading…
Reference in New Issue
Block a user