Que dis-tu du code ci-dessous?
Une question à mon tour: pourquoi le choix 32, 64, 128 (puissances de deux) puis 255 (=2^8 - 1) et pourquoi +2 pour la longueur des trois premiers tableaux et +1 pour le dernier?
----8<-----------8<-----------8<-----------8<-----------8<-----------8<-------
with Interfaces.C;
procedure genconv is
generic
type Item_Type is private;
terminator : Item_Type;
type Index_Type is mod <>;
array_length : Index_Type;
type Array_Type is array (Index_Type range <>) of Item_Type;
with function To_Item_Type (c : Character) return Item_Type;
package TW_Factory is
subtype Tailored_Array is Array_Type (Index_Type'(1) .. array_length);
function To_TW_STR (message : String) return Tailored_Array;
end TW_Factory;
package body TW_Factory is
function To_TW_STR (message : String) return Tailored_Array is
begin
pragma Assert (message'Length = array_length); -- Or adjust terminator position ?
return Result : Tailored_Array with Relaxed_Initialization do
for J in message'Range loop
Result (Index_Type (J - message'First + 1)) := To_Item_Type (message (J));
end loop;
Result (Index_Type (message'Length + 1)) := terminator;
end return;
end To_TW_STR;
end TW_Factory;
use Interfaces.C;
type char_array is array (size_t range <>) of aliased char;
package TW_128 is new TW_Factory (char, nul, size_t, 128, char_array, to_C);
x : TW_128.Tailored_Array;
begin
x := TW_128.To_TW_STR ("Hey!");
end;