Initialiser une chaine de taille contrainte.
Pascal <[email protected]>
| Newsgroups | gmane.comp.lang.ada.france |
|---|---|
| Message-ID | <[email protected]> |
Bonjour,
Voici une question somme toute basique voire pour "débutant" mais les réponses trouvées ne me satisfont pas :
comment initialiser simplement une chaine de caractère contrainte en taille avec une valeur chaine (càd "toto") ?
Je pourrais utiliser les chaines non contraintes, les très utiles Unbounded_String ou le faire en 2 temps : déclaration et affectation avec Ada.Strings.Fixed.Move.
Quel est le problème ?
Prenons le type suivant :
type TTabChaine is array (1 .. 2) of String (1 .. 13);
Initialisons une constante de ce type avec des valeurs chaines de longueurs variables et terminées par Ascii.NUL :
Je dois écrire :
NomsOK : constant TTabChaine :=
(('F', '1', '2', '.', 't', 'x', 't', ASCII.NUL, others => ' '),
('g', 'n', 'a', 't', '.', 'e', 'x', 'e', ASCII.NUL, others => ' '));
Ce qui n'est, accordons le, vraiment pas lisible.
J'aimerais écrire :
NomsKO : constant TTabChaine :=(
("F12.txt", ASCII.NUL, others => ' '),
("gnat.exe", ASCII.NUL, others => ' '));
Il me semblait que les 2 notations (agrégat de caractères et chaine) étaient équivalentes.
Il y a des solutions de contournement comme écrire une fonction qui renvoie des chaines de longueur déterminées.
Pourquoi n'est-il pas possible d'écrire directement l'initialisation comme cela apparait faisable ?
Merci, Pascal.
http://blady.pagesperso-orange.fr
Texte complet de l'exemple :
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Strings.Fixed; use Ada.Strings.Fixed;
procedure Hello is
type TTabChaine is array (1 .. 2) of String (1 .. 13);
NomsOK : constant TTabChaine :=
(('F', '1', '2', '.', 't', 'x', 't', ASCII.NUL, others => ' '),
('g', 'n', 'a', 't', '.', 'e', 'x', 'e', ASCII.NUL, others => ' '));
-- NomsKO : constant TTabChaine :=(
-- ("F12.txt", ASCII.NUL, others => ' '),
-- ("gnat.exe", ASCII.NUL, others => ' '));
begin
if NomsOK (1) (1 .. Index (NomsOK (1), (1 => ASCII.NUL)) - 1) =
('F', '1', '2', '.', 't', 'x', 't')
then
Put_Line ("Trouvé !");
else
Put_Line ("Perdu !");
end if;
if NomsOK (2) (1 .. Index (NomsOK (2), (1 => ASCII.NUL)) - 1) =
"gnat.exe"
then
Put_Line ("Trouvé !");
else
Put_Line ("Perdu !");
end if;
if String'('g', 'n', 'a', 't', '.', 'e', 'x', 'e') = "gnat.exe"
then
Put_Line ("Trouvé !");
else
Put_Line ("Perdu !");
end if;
end Hello;
_______________________________________________
Site WWW de l'association Ada-France: http://www.ada-france.org/
[email protected]
http://www.ada-france.org/mailman/listinfo/ada-france