From f5311ecb693659bf65280ac890f9635b36910797 Mon Sep 17 00:00:00 2001 From: hyung-hwan Date: Sun, 3 Oct 2021 15:38:31 +0000 Subject: [PATCH] implemented some functions for Elastic_String --- lib2/h3-strings.adb | 195 +++++++++++++++++++----------- lib2/h3-strings.ads | 23 +++- lib2/hello.adb | 280 +++++++++++++++++++++++++++++++++++--------- 3 files changed, 365 insertions(+), 133 deletions(-) diff --git a/lib2/h3-strings.adb b/lib2/h3-strings.adb index d7456cb..48538a8 100644 --- a/lib2/h3-strings.adb +++ b/lib2/h3-strings.adb @@ -1,10 +1,10 @@ with Ada.Unchecked_Deallocation; -with ada.text_io; - package body H3.Strings is BUFFER_ALIGN: constant := 16; + type Shift_Direction is (SHIFT_LEFT, SHIFT_RIGHT); + function To_Character_Array (Str: in Elastic_String) return Character_Array is begin return Str.Buffer.Slot(Str.Buffer.Slot'First .. Str.Buffer.Last); @@ -128,7 +128,7 @@ package body H3.Strings is end Prepare_Buffer; -- 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; First, Last: System_Size; Hard_Capa: System_Size; @@ -136,28 +136,6 @@ package body H3.Strings is First := Get_First_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 Req_Hard_Capa < Get_Hard_Capacity(Str) then Hard_Capa := Get_Hard_Capacity(Str); @@ -175,27 +153,38 @@ package body H3.Strings is goto COPY_OVER; elsif Shift_Pos > 0 then Tmp := Str; - goto COPY_OVER; + goto COPY_OVER_WITH_SHIFT; + else + -- no shift, no change in the buffer + null; end if; end if; return; <> - if Shift_Pos > 0 then - -- it is an internal function. perform no sanity check. - -- if Shift_Pos or Shift_Size is beyond the allocated capacity, - -- it will end up in an exception. - 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.Last := Last + Shift_Size; - else + 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; + <> + -- it is an internal function. perform no sanity check. + -- if Shift_Pos or Shift_Size is beyond the allocated capacity, + -- 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(Shift_Pos + Shift_Size .. Last + Shift_Size + 1) := Tmp.Buffer.Slot(Shift_Pos .. Last + 1); + Str.Buffer.Last := Last + Shift_Size; end if; end Prepare_Buffer; @@ -211,51 +200,117 @@ package body H3.Strings is Str.Buffer := Empty_Buffer'Access; 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 begin - if V'Length > 0 then - Prepare_Buffer (Str, H3.Align(Get_Length(Str) + V'Length + 1, BUFFER_ALIGN)); - Str.Buffer.Slot(Str.Buffer.Last + 1 .. Str.Buffer.Last + V'Length) := V; - Str.Buffer.Last := Str.Buffer.Last + V'Length; - Str.Buffer.Slot(Str.Buffer.Last + 1) := Null_Character; + Insert (Str, Get_Last_Index(Str) + 1, V); + end Append; + + procedure Prepend (Str: in out Elastic_String; V: in Character_Type; Repeat: in System_Size := 1) is + 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; + + Repl_Len := Act_To_Pos - From_Pos + 1; + if Repeat < Repl_Len then + Prepare_Buffer (Str, Get_Hard_Capacity(Str), Act_To_Pos, Repl_Len - Repeat, SHIFT_LEFT); + Act_To_Pos := From_Pos + Repeat - 1; + elsif Repeat > Repl_Len then + Prepare_Buffer (Str, Get_Hard_Capacity(Str), From_Pos, Repeat - Repl_Len, SHIFT_RIGHT); + Act_To_Pos := From_Pos + Repeat - 1; + else + Prepare_Buffer (Str); + end if; + Str.Buffer.Slot(From_Pos .. Act_To_Pos) := (others => V); end if; - end Append; + end Replace; - procedure Append (Str: in out Elastic_String; V: in Character_Type) is - Tmp: Character_Array(1 .. 1) := (1 => V); + 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 - Append (Str, Tmp); - end Append; + 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; - procedure Delete (Str: in out Elastic_String; Pos: in System_Index; Length: in System_Size) is + 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 Pos <= Str.Buffer.Last then - Prepare_Buffer (Str); - else - raise Standard.Constraint_Error; + 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 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 - if Pos <= Str.Buffer.Last then - ada.text_io.put_line ( "INSERT "); - Prepare_Buffer (Str, H3.Align(Get_Length(Str) + 1, BUFFER_ALIGN), Pos, 1); - Str.Buffer.Slot(Pos) := New_Char; - end if; - end Insert; + 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)); + end "="; - 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 - if Pos <= Str.Buffer.Last then - Prepare_Buffer (Str); - Str.Buffer.Slot(Pos) := New_Char; - else - -- raise Index_Error; - raise Standard.Constraint_Error; - end if; - end Replace; + return Str.Buffer.Slot(Get_First_Index(Str) .. Get_Last_Index(Str)) = Str2; + end "="; -- --------------------------------------------------------------------- -- Controlled Management diff --git a/lib2/h3-strings.ads b/lib2/h3-strings.ads index a198f3e..7fa60db 100644 --- a/lib2/h3-strings.ads +++ b/lib2/h3-strings.ads @@ -3,7 +3,7 @@ with Ada.Finalization; generic --type Character_Type is private; type Character_Type is (<>); - Null_Character: Character_Type; + Terminator: Character_Type; package H3.Strings is type Elastic_String is private; @@ -42,11 +42,22 @@ package H3.Strings is procedure Purge (Str: in out Elastic_String); procedure Clear (Str: in out Elastic_String); - procedure Append (Str: in out Elastic_String; V: in Character_Array); - 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_Type; Repeat: in System_Size := 1); + 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 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_Type; Repeat: in System_Size := 1); + 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 type Buffer_Record(Capa: System_Size) is limited record @@ -58,7 +69,7 @@ private type Buffer_Pointer is access all Buffer_Record; --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 Buffer: Buffer_Pointer := Empty_Buffer'Access; diff --git a/lib2/hello.adb b/lib2/hello.adb index c9f1f68..39e2e8f 100644 --- a/lib2/hello.adb +++ b/lib2/hello.adb @@ -49,6 +49,22 @@ procedure hello is 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 declare TTT: H3.System_Size := H3.System_Size'Last; @@ -97,87 +113,241 @@ begin TP.Deallocate (x); LP.Deallocate (y); - --GNAT.Debug_Pools.Print_Info_Stdout(P2); - --GNAT.Debug_Pools.Dump_Stdout(P2, 100); + --GNAT.Debug_Pools.Print_Info_Stdout(P2); + --GNAT.Debug_Pools.Dump_Stdout(P2, 100); declare str: S.Elastic_String; str2: S.Elastic_String; - 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_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); + begin + print_string_info (Str, "Str"); + pragma Assert (S.Get_Length(Str) = 0); + pragma Assert (S.Get_First_Index(Str) = 1); + pragma Assert (S.Get_Last_Index(Str) = 0); + + 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, ""); - 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, "donkey"); - 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); - + 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, ' ', 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.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 - 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 - Ada.Wide_Text_IO.Put ("["); + Ada.Wide_Text_IO.Put ("STR[1] => ["); for i in arr'Range loop Ada.Wide_Text_IO.Put (arr(i)); 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_Line ("]"); + Ada.Wide_Text_IO.Put_Line ("]"); end; - - -- unsafe way to access the internal buffer. - S.Append (Str, 'X'); - S.Append(Str, "donkeyX"); - S.Append(Str, "ABCDE"); + + pragma Assert (S."="(Str, "Oh! Hello, world! donkey")); + + S.Append (Str, '>'); + 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; S.Append (Str2, "EXTRA"); + 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, 'Q'); + 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.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.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 arr: constant S.Thin_Character_Array_Pointer := S.Get_Slot_Pointer(Str); arr2: constant S.Thin_Character_Array_Pointer := S.Get_Slot_Pointer(Str2); use type H3.System_Word; begin - Ada.Assertions.Assert (S.Get_Length(Str) = 25, "invalid string length"); - 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); + print_string_info (Str, "Str"); 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. @@ -185,11 +355,7 @@ begin end loop; Ada.Wide_Text_IO.Put_Line ("]"); - len := S.Get_Length(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); + print_string_info (Str2, "Str2"); 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