comp.lang.ada
 help / color / mirror / Atom feed
* problemn with string'last
@ 2023-05-05  9:51 Daniel Gaudry
  2023-05-05 10:02 ` Dmitry A. Kazakov
  2023-05-05 11:17 ` Jeffrey R.Carter
  0 siblings, 2 replies; 10+ messages in thread
From: Daniel Gaudry @ 2023-05-05  9:51 UTC (permalink / raw)


hi,
here is a code that i simplified

with Ada.Text_Io;
procedure Help
   is
   Double_Quote : constant String(1 .. 1) := (others => '"' );
   Data         : constant String         := "  PLUS_STRING    : CONSTANT STRING(1..3) :=(1 =>'" & Double_Quote & "' , 2 =>'+' , 3 =>'" & Double_Quote & ");";
   begin
 
   Ada.Text_Io.Put_Line("     05   10   15   20   25   30   35   40   45   50   55   65   70   75   80   85   90");
   Ada.Text_Io.Put_Line(" ....|....!....|....!....|....!....|....!....|....!....|....!....|....!....|....!....|....!");
   Ada.Text_Io.Put_Line("»" & Data & "« " & Data'Last'Img);
 
end Help;


but the data'last seems not to match the string size

Any help is welcome
best regards

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: problemn with string'last
  2023-05-05  9:51 problemn with string'last Daniel Gaudry
@ 2023-05-05 10:02 ` Dmitry A. Kazakov
  2023-05-05 11:17 ` Jeffrey R.Carter
  1 sibling, 0 replies; 10+ messages in thread
From: Dmitry A. Kazakov @ 2023-05-05 10:02 UTC (permalink / raw)


On 2023-05-05 11:51, Daniel Gaudry wrote:

> here is a code that i simplified
> 
> with Ada.Text_Io;
> procedure Help
>     is
>     Double_Quote : constant String(1 .. 1) := (others => '"' );
>     Data         : constant String         := "  PLUS_STRING    : CONSTANT STRING(1..3) :=(1 =>'" & Double_Quote & "' , 2 =>'+' , 3 =>'" & Double_Quote & ");";
>     begin
>   
>     Ada.Text_Io.Put_Line("     05   10   15   20   25   30   35   40   45   50   55   65   70   75   80   85   90");
>     Ada.Text_Io.Put_Line(" ....|....!....|....!....|....!....|....!....|....!....|....!....|....!....|....!....|....!");
>     Ada.Text_Io.Put_Line("»" & Data & "« " & Data'Last'Img);
>   
> end Help;
> 
> but the data'last seems not to match the string size

It matches. You missed the "60" tick in your scale. (:-))

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: problemn with string'last
  2023-05-05  9:51 problemn with string'last Daniel Gaudry
  2023-05-05 10:02 ` Dmitry A. Kazakov
@ 2023-05-05 11:17 ` Jeffrey R.Carter
  2023-05-05 12:08   ` Dmitry A. Kazakov
  2023-05-05 16:28   ` Manuel Gomez
  1 sibling, 2 replies; 10+ messages in thread
From: Jeffrey R.Carter @ 2023-05-05 11:17 UTC (permalink / raw)


On 2023-05-05 11:51, Daniel Gaudry wrote:
>   
>     Ada.Text_Io.Put_Line("     05   10   15   20   25   30   35   40   45   50   55   65   70   75   80   85   90");
>     Ada.Text_Io.Put_Line(" ....|....!....|....!....|....!....|....!....|....!....|....!....|....!....|....!....|....!");
>     Ada.Text_Io.Put_Line("»" & Data & "« " & Data'Last'Img);

As Kazakov pointed out, your scale is incorrect.

However, it's important to remember that 'Last is not necessarily the same as 
'Length because 'First is not necessarily one. This is especially important in 
subprograms that take String parameters, as they are often passed slices.

Some questions:

>    Double_Quote : constant String(1 .. 1) := (others => '"' );

ARM 2.1(15/3) 
(http://www.ada-auth.org/standards/aarm12_w_tc1/html/AA-2-1.html#I1199) defines 
the name of '"' as "quotation mark". Why do you refer to a single quotation mark 
as Double_Quote?

>    Data         : constant String         := "  PLUS_STRING    : CONSTANT STRING(1..3) :=(1 =>'" & Double_Quote & "' , 2 =>'+' , 3 =>'" & Double_Quote & ");";

Wouldn't it be simpler and clearer to write this as

Data : constant String := "  PLUS_STRING : CONSTANT STRING := " & '"' & '+' & '"';

? Note also that you seem to be missing an apostrophe (''') before the right 
parenthesis if you intend Data to be a valid Ada declaration.

-- 
Jeff Carter
"All citizens will be required to change their underwear
every half hour. Underwear will be worn on the outside,
so we can check."
Bananas
29

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: problemn with string'last
  2023-05-05 11:17 ` Jeffrey R.Carter
@ 2023-05-05 12:08   ` Dmitry A. Kazakov
  2023-05-05 16:28   ` Manuel Gomez
  1 sibling, 0 replies; 10+ messages in thread
From: Dmitry A. Kazakov @ 2023-05-05 12:08 UTC (permalink / raw)


On 2023-05-05 13:17, Jeffrey R.Carter wrote:

> However, it's important to remember that 'Last is not necessarily the 
> same as 'Length because 'First is not necessarily one. This is 
> especially important in subprograms that take String parameters, as they 
> are often passed slices.

Non-sliding indices is one of the most useful Ada features. You can pass 
indices around as-is, which is extremely helpful when processing strings.

You will also find that actually 'Length and 'Last have different types, 
which is quite useful as well.

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: problemn with string'last
  2023-05-05 11:17 ` Jeffrey R.Carter
  2023-05-05 12:08   ` Dmitry A. Kazakov
@ 2023-05-05 16:28   ` Manuel Gomez
  2023-05-05 16:33     ` Simon Wright
  1 sibling, 1 reply; 10+ messages in thread
From: Manuel Gomez @ 2023-05-05 16:28 UTC (permalink / raw)


El 5/5/23 a las 13:17, Jeffrey R.Carter escribió:
> On 2023-05-05 11:51, Daniel Gaudry wrote:
>>    Data         : constant String         := "  PLUS_STRING    : 
>> CONSTANT STRING(1..3) :=(1 =>'" & Double_Quote & "' , 2 =>'+' , 3 =>'" 
>> & Double_Quote & ");";
> 
> Wouldn't it be simpler and clearer to write this as
> 
> Data : constant String := "  PLUS_STRING : CONSTANT STRING := " & '"' & 
> '+' & '"';
> 
> ? Note also that you seem to be missing an apostrophe (''') before the 
> right parenthesis if you intend Data to be a valid Ada declaration.
> 

Or like this (doubling the quotes, which is the way to _escape_ the 
double quote inside Ada strings):

    Data         : constant String         := "  PLUS_STRING    : 
CONSTANT STRING(1..3) :=(1 =>'""' , 2 =>'+' , 3 =>'""');";

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: problemn with string'last
  2023-05-05 16:28   ` Manuel Gomez
@ 2023-05-05 16:33     ` Simon Wright
  2023-05-05 18:18       ` Manuel Gomez
  0 siblings, 1 reply; 10+ messages in thread
From: Simon Wright @ 2023-05-05 16:33 UTC (permalink / raw)


Manuel Gomez <mgrojo@gmail.com> writes:

> Or like this (doubling the quotes, which is the way to _escape_ the
> double quote inside Ada strings):
>
>    Data         : constant String         := "  PLUS_STRING    :
>    CONSTANT STRING(1..3) :=(1 =>'""' , 2 =>'+' , 3 =>'""');";

'"' is a single quote character.
"""" is a string containing one quote character.

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: problemn with string'last
  2023-05-05 16:33     ` Simon Wright
@ 2023-05-05 18:18       ` Manuel Gomez
  2023-05-05 19:52         ` J-P. Rosen
  2023-05-05 20:44         ` Simon Wright
  0 siblings, 2 replies; 10+ messages in thread
From: Manuel Gomez @ 2023-05-05 18:18 UTC (permalink / raw)


El 5/5/23 a las 18:33, Simon Wright escribió:
> Manuel Gomez <mgrojo@gmail.com> writes:
> 
>> Or like this (doubling the quotes, which is the way to _escape_ the
>> double quote inside Ada strings):
>>
>>     Data         : constant String         := "  PLUS_STRING    :
>>     CONSTANT STRING(1..3) :=(1 =>'""' , 2 =>'+' , 3 =>'""');";
> 
> '"' is a single quote character.
> """" is a string containing one quote character.

Yes, and "'""'" is a string containing a quote as an Ada character (when 
displayed).

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: problemn with string'last
  2023-05-05 18:18       ` Manuel Gomez
@ 2023-05-05 19:52         ` J-P. Rosen
  2023-05-05 20:44         ` Simon Wright
  1 sibling, 0 replies; 10+ messages in thread
From: J-P. Rosen @ 2023-05-05 19:52 UTC (permalink / raw)


Le 05/05/2023 à 20:18, Manuel Gomez a écrit :
> Yes, and "'""'" is a string containing a quote as an Ada character (when 
> displayed).

Even more fun (I use this for people who write a naive Ada parser):
    subtype C is Character;
    V : String := C'(')')'Img;

-- 
J-P. Rosen
Adalog
2 rue du Docteur Lombard, 92441 Issy-les-Moulineaux CEDEX
https://www.adalog.fr https://www.adacontrol.fr

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: problemn with string'last
  2023-05-05 18:18       ` Manuel Gomez
  2023-05-05 19:52         ` J-P. Rosen
@ 2023-05-05 20:44         ` Simon Wright
  2023-05-05 23:27           ` Jeffrey R.Carter
  1 sibling, 1 reply; 10+ messages in thread
From: Simon Wright @ 2023-05-05 20:44 UTC (permalink / raw)


Manuel Gomez <mgrojo@gmail.com> writes:

> El 5/5/23 a las 18:33, Simon Wright escribió:
>> Manuel Gomez <mgrojo@gmail.com> writes:
>> 
>>> Or like this (doubling the quotes, which is the way to _escape_ the
>>> double quote inside Ada strings):
>>>
>>>     Data         : constant String         := "  PLUS_STRING    :
>>>     CONSTANT STRING(1..3) :=(1 =>'""' , 2 =>'+' , 3 =>'""');";
>> '"' is a single quote character.
>> """" is a string containing one quote character.
>
> Yes, and "'""'" is a string containing a quote as an Ada character
> (when displayed).

Oh, I see. Ugh.

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: problemn with string'last
  2023-05-05 20:44         ` Simon Wright
@ 2023-05-05 23:27           ` Jeffrey R.Carter
  0 siblings, 0 replies; 10+ messages in thread
From: Jeffrey R.Carter @ 2023-05-05 23:27 UTC (permalink / raw)


On 2023-05-05 22:44, Simon Wright wrote:
> Manuel Gomez <mgrojo@gmail.com> writes:
> 
>> El 5/5/23 a las 18:33, Simon Wright escribió:
>>> Manuel Gomez <mgrojo@gmail.com> writes:
>>>
>>>>      Data         : constant String         := "  PLUS_STRING    :
>>>>      CONSTANT STRING(1..3) :=(1 =>'""' , 2 =>'+' , 3 =>'""');";
>>> '"' is a single quote character.
>>> """" is a string containing one quote character.
>>
>> Yes, and "'""'" is a string containing a quote as an Ada character
>> (when displayed).
> 
> Oh, I see. Ugh.

As you have discovered, using doubled quotation marks within a String literal 
can be significantly less clear than concatenating the quotation-mark Character 
with the rest of the String.

-- 
Jeff Carter
"All citizens will be required to change their underwear
every half hour. Underwear will be worn on the outside,
so we can check."
Bananas
29

^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2023-05-05 23:27 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-05-05  9:51 problemn with string'last Daniel Gaudry
2023-05-05 10:02 ` Dmitry A. Kazakov
2023-05-05 11:17 ` Jeffrey R.Carter
2023-05-05 12:08   ` Dmitry A. Kazakov
2023-05-05 16:28   ` Manuel Gomez
2023-05-05 16:33     ` Simon Wright
2023-05-05 18:18       ` Manuel Gomez
2023-05-05 19:52         ` J-P. Rosen
2023-05-05 20:44         ` Simon Wright
2023-05-05 23:27           ` Jeffrey R.Carter

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox