โŒ About FreshRSS

Normal view

There are new articles available, click to refresh the page.
Before yesterdayNews from the Ada programming language world

Unbounded string `ENCODING_ERROR : bad input at Item`

Why is the following code failing ?

I have picked Characters.Latin_1.Reserved_128 on purpose.

-- File 'print_non_graphic_character.adb'
with Ada.Characters.Latin_1;
with Ada.Text_IO;
with Ada.Strings.Unbounded;

procedure Print_Non_Graphic_Character is
   Text_String : constant String := "Non Graphic Character: " & Ada.Characters.Latin_1.Reserved_128;
   Text_Unbounded_String : constant Ada.Strings.Unbounded.Unbounded_String :=
      Ada.Strings.Unbounded.To_Unbounded_String (Text_String);
begin
   Ada.Text_IO.Put_Line (Text_String);
   Ada.Text_IO.Put_Line (Ada.Strings.Unbounded.To_String (Text_Unbounded_String));
   Ada.Text_IO.Put_Line (Text_Unbounded_String'Image);
end Print_Non_Graphic_Character;

Note that it also fails with:

Ada.Text_IO.Put_Line (Ada.Strings.Unbounded.Unbounded_String'Image (Text_Unbounded_String));
-- File 'print_non_graphic_character.gpr'
project Print_Non_Graphic_Character is
   for Main use ("print_non_graphic_character.adb");
   for Object_Dir use ".objs";

   package Compiler is
      -- "-Og" -- Optimize for debug
      -- "-g" -- Generate debug info
      for Default_Switches ("Ada") use ("-g", "-gnat2020", "-Og");
   end Compiler;

   package Binder is
      for Switches ("Ada") use ("-Es"); --  Symbolic traceback
   end Binder;
end Print_Non_Graphic_Character;

It gives me with GNAT 12.2.0

Non Graphic Character: ๏ฟฝ
Non Graphic Character: ๏ฟฝ

raised ADA.STRINGS.UTF_ENCODING.ENCODING_ERROR : bad input at Item (25)
[./non_graphic_character/.objs/print_non_graphic_character]
0x40e8fb Ada.Strings.Utf_Encoding.Raise_Encoding_Error at a-stuten.adb:126
0x40f38b Ada.Strings.Utf_Encoding.Strings.Decode at a-suenst.adb:163
0x4049a8 Print_Non_Graphic_Character at print_non_graphic_character.adb:13
0x404efa Main at b__print_non_graphic_character.adb:279
[/lib/x86_64-linux-gnu/libc.so.6]
0x7f5e62118d08
[./non_graphic_character/.objs/print_non_graphic_character]
0x404658 _start at ???
0xfffffffffffffffe

It is annoying because, when I want to print a record which is commposed with Unbounded_String, I cannot use the 'Image attribute because of the above error and have to print each field and use To_String for Unbounded_String (which is annoying to have when I couple that with generic functions/packages).

String from integer8

I'm using a library in ada which contains many types :

type Int8 is range -8 ** 7 .. 2 ** 7 - 1;
subtype T_Name_String is Int8;
type T_Name_String_Fixed20 is array (range 1..20) of T_Name_String ;

And there is a record with :

type The_Record is record
   name : T_Name_String_Fixed20;
   -- and others
end record;

I can't change that, I don't know why there are using Int8 for ada strings but I'm not able to initiate the field name. I've try :

-- First try:
MyRecord.name = "hello               ";
-- Error : expected type T_Name_String_Fixed20 found a string type

-- Second try
Ada.Strings.Fixed.Move(Target => MyRecord.name;
                       Source => "hello               "

Bug in GNAT Get(FRom => SomeString, Item => SomeInteger, Last => Last)?

Get(TheFile, IntValue); works great with strings formatted like 16#12# to read hex values. Shouldn't Get from a string function the same way?

I tried this, but passing 16#12# only yields 16. Pure hex, eg F8 results in an exception

with Ada.Text_IO;         use Ada.Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;

procedure hex is
   IntValue  : Integer;
   Last : Positive;
begin
   Put("Enter a hexadecimal string: ");
   Get(HexString);

   -- Convert the hexadecimal string to an integer
   Get(From => HexString, Item => IntValue, Last => Last);

   Put ("The integer value is: ");
   Put (IntValue, Width => 0);
   New_Line;
end hex;

Ada: subprogram that ignores initial blank spaces

I'm interested in finding out if there is a way to create a "Get" subprogram for strings that works like "Get" for Integers or Float. As I understand the Ada get procedure for Integers ignores any type in of blank spaces before the integer and only collects the integer value.

Let's say we want to collect a string of five characters that should be stored in the variable "S" but the user type in 10 blank spaces and then the string so it would look something like this: Put in a string of 5 characters: buses I would like to create a "Get" that ignore these blank spaces and only gives my program the string value of 5 characters.

This is my main program.

S : String (1 .. 5);
begin
Put("Put in a string of 5 characters: ");
Get(S);
Put_Line(S);

I've read something about a function End_Of_Line. I understand that i need to create some kind of subprogram that collects the string and skips all initial blank spaces but I have not come up with a working solution.

Update: I tried to create my own get but got a little bit stuck. The get procedure should read the next character if the previous is ' ' but if it reads "the correct" string, how should the code look like?

       procedure Get(Item : in out String) is
      
      Ch : Character;
   begin
      
      loop
     Get(Ch);
     if Ch = ' ' and (not End_Of_Line) then
        Get(Ch);
     else 
       exit;
     end if;
      end loop;
      
   end Get;
โŒ
โŒ