Appendix D Text_package package The text_package package is used by first year students for variable length string manipulation. It is loosely based on that provided in the LRM. Equality can be tested by simply using the standard "=" operator because padding is maintained as spaces in the unused portion of the record. Ada95 allows the overriding of the "=" operator for all types, and allows the operands to be of different types, so such padding would not be required. with text_io; package text_package is max_chars :constant integer := 256; subtype length_range is integer range 0..max_chars; subtype index_range is length_range range 1..length_range'last; type text is record value :string(index_range); length :length_range:=0; end record; ------------------------------------------------------------------- ------------- -- read a whole line of characters, throw away anything that doesn't fit. procedure get_line( item :in out text); procedure get_line(file :in out text_io.file_type; item :in out text ); ------------------------------------------------------------------- ------------- -- put the characters in the_text to the screen. do not include a new_line at -- the end procedure put(item :in text); procedure put(file :in text_io.file_type; item :in text); ------------------------------------------------------------------- ------------- -- Put the characters in the_text to the screen. Do include a new_line at the -- end. procedure put_line(item :in text); procedure put_line(file :in text_io.file_type; item :in text); ------------------------------------------------------------------- ------------- -- get the string inside the text item. function to_string(item :in text) return string; ------------------------------------------------------------------- ------------- -- return the length of the string function length(item :in text) return length_range; ------------------------------------------------------------------- ------------- -- return true if the string is empty function empty(item :in text) return boolean; ------------------------------------------------------------------- ------------- -- set the value of "the_text" to "a_string". Disregard any characters that -- don't fit into the record function to_text(item :in string) return text; ------------------------------------------------------------------- ------------- -- append a character onto the end of the text -- disregard anything that doesn't fit in procedure append( item :in out text; add_on :in character); ------------------------------------------------------------------- ------------- -- append a string onto the end of the text -- disregard anything that doesn't fit in procedure append( item :in out text; add_on :in string); ------------------------------------------------------------------- ------------- -- append the two text items together -- disregard anything that doesn't fit in procedure append( item :in out text; add_on :in text); ------------------------------------------------------------------- ------------- -- returns the first n characters of the string, the string is padded out with -- spaces if item is not long enough -- function first_n_chars(item :in text; n :in natural) return string; end text_package;