From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=0.2 required=5.0 tests=BAYES_00,INVALID_MSGID, REPLYTO_WITHOUT_TO_CC autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,56760b0ebeac6ba1 X-Google-Attributes: gid103376,public From: Rex Reges Subject: Re: Need of help Leonid(dulman@ibm.net) Date: 1997/02/12 Message-ID: <3302387F.16BB@mds.lmco.com>#1/1 X-Deja-AN: 218474708 References: <5dhbjt$hbe@news.ibm.net.il> Content-Type: text/plain; charset=us-ascii Organization: M&DS Mime-Version: 1.0 Reply-To: rex.r.reges@lmco.com Newsgroups: comp.lang.ada X-Mailer: Mozilla 3.0 (Win95; I) Date: 1997-02-12T00:00:00+00:00 List-Id: Thomas Hoffmann wrote: ... deleted comments about types with large discriminant ranges ... > To "solve" the problem yourself, just declare a reasonable maximum length, and use a subtype > representing this length as the subtype of the discriminant: > > Max_Length : constant := 200; > > subtype Index is > Natural range 0 .. Max_Length; > > type V_String (Size : Index := 0) is > record > S : String (1 .. Size); > end record; > > GNAT 3.09 issues a warning during the compiler run. I think it is a bad idea to have a warning issued for this example. Even if warnings can be suppressed or ignored, it generally takes a written description in the comments and approval by Quality Assurance to do so. I have used the V_String in the form of a generic package with the Max_Length as the parameter. This allows the creation of necessary functions as well: catenation of V_String and V_String, catenation of String and V_String, etc. >From the V_String package I instantiate three versions: Small_Variable_Length_String Medium_Variable_Length_String Large_Variable_Length_String I would be unhappy trying to explain why the basis for most string storage causes compiler warnings. An interesting problem occurred while trying to provide the utility functions. I had the following function: function V_String_Of( Standard_String : in String ) return V_String is begin -- Test omitted for Strings that are too large to fit. return ( Size => Standard_String'Length , S => Standard_String ) ; end V_String_Of ; I liked this code because it did not require creating a temporary variable on the stack, particularly if the string was very large. However, it failed whenever a substring was passed which had a starting index other than 1: My_V_String := V_String_Of( "abcdefg"(2:3) ); -- Not sure this syntax -- is allowed but you get -- the point. The aggregation mechanism will not shift the indices, but the assignment operator will. Therefore, I was forced to rewrite the function using a temporary value on the stack: function V_String_Of( Standard_String : in String ) return V_String is Temp_String : String( 1..Standard_String'Length ) := Standard_String ; begin -- Test omitted for Strings that are too large to fit. return ( Size => Temp_String'Length , S => Temp_String ) ; end V_String_Of ; Another mechanism which causes an invisible temp variable is to use the string catenation function to shift the indices. function V_String_Of( Standard_String : in String ) return V_String is begin -- Test omitted for Strings that are too large to fit. return ( Size => Standard_String'Length , S => "" & Standard_String ) ; end V_String_Of ; Both of these have the potential of blowing out the stack for large strings, particularly in tasks with small stack allocations. -- Rex Reges or you can call me The Fixer Systems Analyst or you can call me The Lawyer Lockheed Martin, M&DS or you can call me The Doctor (610)354-5047 or you can call me Rexasaurus