!standard 04.02 (11) 97-11-14 AI95-00076/01 !class confirmation 95-07-27 !status ARG Approved 11-0-0 97-11-14 !status work item 95-07-27 !status received 95-07-27 !priority Low !difficulty Easy !subject String literal constraint ramification is correct. !summary 95-07-27 The ramification in 4.2(11.a) is correct. !question 95-07-27 Is the ramification in 4.2(11.a) is correct? !response 95-07-27 > 4.2 (11) states; > > For the evaluation of a string_literal of type T, a check is made that > the value of each character of the string_literal belongs to the > component subtype of T. ... The exception > Constraint_Error is raised if either of these checks fails. > > (11.a) RAMIFICATION: The checks on the characters need not involve more > than two checks altogether, since one need only check the characters of > the string with the lowest and highest position numbers against the range > of the component subtype. > > I don't believe the ramification (or I don't understand it) because > > package t138 is > type Roman_Digit is ('C', 'D', 'I', 'L', 'M', 'V', 'X'); > type Roman_Date is array(positive range <>) of Roman_Digit; > x : Roman_Date := "CWX"; > end; This is illegal, since 'W' is not a character literal of the type. For this legality check, you definitely must look at every character of the string. However, this ramification is talking about the run-time check required, when the component subtype of a string type is a constrained subtype of some character type. For example: subtype Some_Digits is Roman_Digit range F(1) .. G(2); type Some_Date is array(Positive range <>) of Some_Digits; y : Some_Date := "MMVIII"; Even though we don't statically know the values returned by F(1) and G(2), we don't have to make 6 run-time checks to make sure all characters of the string are within Some_Digits'Range. Instead, we take (at compile time) the max and min position numbers of characters in the string -- V has the max position number (Roman_Digit'Pos('V') = 5), and I has the min position number (Roman_Digit'Pos('I') = 2), and all we need to do at run-time is see whether Some_Digits'Last >= 'V' and Some_Digits'First <= 'I'. !appendix 95-07-27 !section 4.2(11) !subject String literal constraint ramification seems bogus. !reference RM95-4.2(11.a) !from Michael K. Smith 95-07-14 !reference as: 95-5223.a Michael K. Smith 95-7-14>> !discussion 4.2 (11) states; For the evaluation of a string_literal of type T, a check is made that the value of each character of the string_literal belongs to the component subtype of T. ... The exception Constraint_Error is raised if either of these checks fails. (11.a) RAMIFICATION: The checks on the characters need not involve more than two checks altogether, since one need only check the characters of the string with the lowest and highest position numbers against the range of the component subtype. I don't believe the ramification (or I don't understand it) because package t138 is type Roman_Digit is ('C', 'D', 'I', 'L', 'M', 'V', 'X'); type Roman_Date is array(positive range <>) of Roman_Digit; x : Roman_Date := "CWX"; end; Michael K. Smith Computational Logic Inc. [http://www.cli.com/] Austin, TX Email: mksmith@cli.com **************************************************************** !section 4.2(11) !subject String literal constraint ramification seems bogus. !reference RM95-4.2(11.a) !reference 95-5223.a Michael K. Smith 95-07-14 !from Tucker Taft 95-07-14 !reference as: 95-5224.a Tucker Taft 95-7-14>> !discussion > 4.2 (11) states; > > For the evaluation of a string_literal of type T, a check is made that > the value of each character of the string_literal belongs to the > component subtype of T. ... The exception > Constraint_Error is raised if either of these checks fails. > > (11.a) RAMIFICATION: The checks on the characters need not involve more > than two checks altogether, since one need only check the characters of > the string with the lowest and highest position numbers against the range > of the component subtype. > > I don't believe the ramification (or I don't understand it) because > > package t138 is > type Roman_Digit is ('C', 'D', 'I', 'L', 'M', 'V', 'X'); > type Roman_Date is array(positive range <>) of Roman_Digit; > x : Roman_Date := "CWX"; > end; This is illegal, since 'W' is not a character literal of the type. For this legality check, you definitely must look at every character of the string. However, this ramification is talking about the run-time check required, when the component subtype of a string type is a constrained subtype of some character type. For example: subtype Some_Digits is Roman_Digit range F(1) .. G(2); type Some_Date is array(Positive range <>) of Some_Digit; y : Some_Date := "MMVIII"; Even though we don't statically know the values returned by F(1) and G(2), we don't have to make 6 run-time checks to make sure all characters of the string are within Some_Digits'Range. Instead, we take the max and min position numbers of characters in the string -- V has the max position number (Roman_Digit'Pos('V') = 5), and I has the min position number (Roman_Digit'Pos('I') = 2), and all we need to do at run-time is see whether Some_Digits'Last >= 'V' and Some_Digits'First <= 'I'. > Michael K. Smith > Computational Logic Inc. [http://www.cli.com/] > Austin, TX -Tuck ****************************************************************