From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.5-pre1 (2020-06-20) on ip-172-31-74-118.ec2.internal X-Spam-Level: X-Spam-Status: No, score=-1.9 required=3.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.5-pre1 Date: 26 Aug 93 14:09:09 GMT From: psinntp!psinntp!jupiter.fuentez.com!hildjj@uunet.uu.net (Joe Hildebrand) Subject: Re: How to assign STRINGs? Message-ID: List-Id: >> On 25 Aug 93 21:42:26 GMT, alex@cs.umd.edu (Alex Blakemore) said: In article <25gmc2$3cf@neomimsy.cs.umd.edu> alex@cs.umd.edu (Alex Blakemore) wr ites: Alex> In article <1993Aug25.121754.20413@hellgate.utah.edu> Alex> matwood%peruvian.cs.utah.edu@cs.utah.edu (Mark Atwood) Alex> writes: >> how DO i assign the results of a function returning an >> unconstrained string to a constrained string? Alex> in Ada83, you have two good choices (and one sort of kludgy Alex> one) Alex> a. initialize a constant string with the function result Alex> cmdline : constant string := dos.get_parms; Alex> b. pass the function result as an in parameter to another Alex> subprogram text_io.put_line (dos.get_parms); Alex> or [worst of all] Alex> c. get the bounds first, declare the variable, call the Alex> function Alex> declare cmdline : string (dos.first_parm_number Alex> .. dos.last_parm_number); begin cmdline := dos.get_parms; Alex> end; Alex> -- this is brittle, esp if you ever add concurrency We created a simple function that combines b. and c. NOTE: I don't claim this is pretty, but it seems to work. It needs some cleaning up, since this is one one the first functions we wrote when we were learning Ada. In particular, Out_Str should only be an out parameter. PAR_STR_Length returns the number of "useful" characters in a string by subtracting the number of trailing spaces and ascii.nul's. procedure PAR_STR_copy( Out_str : in out string; In_Str : in string; Pad_Char : in character := ' ' ) is In_str_len : integer := 0; Out_str_len : integer := 0; Low_Char : integer := 0; Num_Chars : integer := 0; begin -- Get the length of the string to be copied In_Str_Len := PAR_STR_length( In_Str ); -- Obtain the size of the target string Out_Str_Len := Out_Str'last - Out_Str'first + 1; -- Find the first character position in the In_Str to be copied. -- This is in case the In_Str is passed in as a string slice. Low_Char := In_Str'first; -- Only copy In_Str_Len number of characters Num_Chars := In_Str_Len; -- Make sure we only copy the number of characters the target -- string can hold. if ( Num_Chars > Out_Str_Len ) then Num_Chars := Out_Str_Len; end if; -- Copy the string. Out_Str( Out_Str'first..(Out_Str'first+Num_Chars-1)) := In_Str(Low_Char..(Low_Char+Num_Chars-1)); -- Pad out the remaining cells with the pad character. Pad_LOOP: for i in (Out_Str'first+Num_Chars)..Out_Str'last loop Out_Str(i) := Pad_Char; end loop Pad_LOOP; end PAR_STR_copy; -- ---------- Joe Hildebrand Fuentez Systems Concepts hildjj@fuentez.com 11781 Lee-Jackson Hwy, Suite 700 Software Engineer Fairfax, VA 22033 Phone: (703)273-1447 Fax: (703)273-2972 Standard disclaimers apply