Ada slicing with strings

I have been a C ++ programmer for a long time studying Ada for fun. If any of the following problems is bad, feel free to list it. I'm trying to learn Ada's way of doing something, but old habits are hard to break (and I miss Boost!)

I am trying to upload a file containing an integer, space and character string. There might be a better way to do this, but I thought that I should load the string into a string buffer, which, as I know, will be no more than 80 characters. I declare a buffer variable in the appropriate place, as shown below:

 Line_Buffer : String(1..80);

After opening the file, I scroll through each line and break the buffer into a space character:

 while not Ada.Text_IO.End_Of_File(File_Handle) loop
   Ada.Text_IO.Get_Line(File_Handle, Item=>Line_Buffer, Last=>Last);
   -- Break line at space to get match id and entry
   for String_Index in Line_Buffer'Range loop
     if Line_Buffer(String_Index) = ' ' then
       Add_Entry(Root_Link=>Root_Node,
        ID_String=> Line_Buffer(1..String_Index-1),
        Entry_String=> Line_Buffer(String_Index+1..Last-1)
        );
     end if;
   end loop;
 end loop;

What happens in Add_Entry is not so important, but its specification is as follows:

 procedure Add_Entry(
   Root_Link : in out Link;
   ID_String : in String;
   Entry_String : in String);

, , , . , Add_Entry, Entry_String, , 1, . , Line_Buffer "14 ", , 4 10.

for Index in Entry_String'Range loop
  Ada.Text_IO.Put("Index: " & Integer'Image(Index));
  Ada.Text_IO.New_Line;  
end loop;

, , Add_Entry, , 1? , "in" , , , ?

+3
2

-, . -, , ++ Ada. , , C/++ Ada, , , , , Ada, C. :

Ada ( ) , . , (, nul), . , 1 0 .

, Ada , , . . 'first, 'last 'range . , ( - ), :

for Index in Entry_String'Range loop
  Ada.Text_IO.Put("Index offset: " & Integer'Image(Index-Entry_string'first));
  Ada.Text_IO.New_Line;  
end loop;

OK. Ada C. in . , : Ada C! , , Ada . , (, , ). , . . . , , , . . ( ++) ​​. - Ada, . .

, Ada "" . . , Ada.Text_IO.Get_Line . ( , ), Carlisle Text_IO.

+6

, GNAT, Ada.Strings.Unbounded.Text_IO.

, Ada.Strings( , ) , Index() - : )

GNAT, GNAT.Array_Split ( GNAT.String_Split), , ( ).

+3

Source: https://habr.com/ru/post/1790542/


All Articles