!standard A.4.3(56.2/2) 07-06-17 AI05-0056-1/02
!standard A.4.3(58.5/2)
!class binding interpretation 07-05-23
!status ARG Approved 8-0-2 07-06-02
!status work item 07-05-23
!status received 07-05-10
!priority Low
!difficulty Easy
!qualifier Error
!subject Wrong result for Index functions
!summary
All versions of Index return 0 when searching a null string; they do not
raise Index_Error.
!question
Even though we're careful to not call it an equivalence, the equivalence given in
A.4.3(58-58.3/2) causes an incompatibility with Ada 95. Consider:
S : String (1..0);
I : Natural := Index (S, "foo");
In Ada 95 the function Index would return 0, indicating that the pattern
was not found. Fine.
In Ada 2005 the above call is defined to be Index (S, "foo", From => 1).
Unfortunately From is not in S'Range and this call raises Index_Error.
Was this incompatibility intended? (No.)
!recommendation
(See Summary.)
!wording
In A.4.3(56.2/2), add "If Source is the null string, Index returns 0; otherwise,"
before "If From is not in...".
In A.4.3(58.5/2), add "If Source is the null string, Index returns 0; otherwise,"
before "If From is not in...".
!discussion
The problem is that calling Index ("", ...) should always return 0, irrespective
of the presence of From or its value. Note that the same problem occurs in
the wording for the set version of Index.
We considered making the various versions of Index different in this respect, but
that seems inconsistent.
The language does not define when the Pattern_Error check is made. (That's because
many common searching implementations require a non-empty pattern) That means that
the result for a call like Index ("", "") could be 0 or could raise Pattern_Error.
Similarly, in the call Index ("", "", From => 2), the language does not define
whether Pattern_Error or Index_Error is raised.
!corrigendum A.4.3(56.2/2)
@drepl
Each Index function searches, starting from From, for a slice of Source, with
length Pattern'Length, that matches Pattern with respect to Mapping; the parameter
Going indicates the direction of the lookup. If From is not in Source'Range,
then Index_Error is propagated. If Going = Forward, then Index returns the
smallest index I which is greater than or equal to From such that the slice of
Source starting at I matches Pattern. If Going = Backward, then Index returns
the largest index I such that the slice of Source starting at I matches Pattern
and has an upper bound less than or equal to From. If there is no such slice,
then 0 is returned. If Pattern is the null string, then Pattern_Error is propagated.
@dby
Each Index function searches, starting from From, for a slice of Source, with
length Pattern'Length, that matches Pattern with respect to Mapping; the parameter
Going indicates the direction of the lookup. If Source is the null string, Index
returns 0; otherwise, if From is not in Source'Range,
then Index_Error is propagated. If Going = Forward, then Index returns the
smallest index I which is greater than or equal to From such that the slice of
Source starting at I matches Pattern. If Going = Backward, then Index returns
the largest index I such that the slice of Source starting at I matches Pattern
and has an upper bound less than or equal to From. If there is no such slice,
then 0 is returned. If Pattern is the null string, then Pattern_Error is propagated.
!ACATS Test
!appendix
****************************************************************