comp.lang.ada
 help / color / mirror / Atom feed
* Simplifying Unbounded_Stri
@ 1995-03-19 13:37 David Wheeler
  1995-03-22  5:21 ` Tucker Taft
  0 siblings, 1 reply; 2+ messages in thread
From: David Wheeler @ 1995-03-19 13:37 UTC (permalink / raw)



I'm looking for some advice on how to simplify Unbounded_String
handling. Discussion and source code follows.



As many of you know I've developed a tutorial on Ada 95, and I've
been developing an expanded version.  Since the Ada 83 "String"
type involves all sorts of convolutions when you want to vary its
length, I've decided that it'd be better to teach about
Unbounded_String first.

However, I think Unbounded_String is a little clumsy to use as-is.
For example, there isn't a Text IO package for it (as far as I can tell)!
No problem, that's easy to create.
However, there's also no way to declare Unbounded_String constants..
I have to keep using To_Unbounded_String("text").
Also, while the type Unbounded_String and the package name
Ada.Strings.Unbounded are very descriptive,
they are too darn long for names used all over the place.

I've decided that I want to create a single package that simplifies
a user's life.  I want to create a package "Ustrings"
that provides the subtype Ustring, all the operations
of Ada.Strings.Unbounded and some Text_IO types of operations.

There are only two ways I've found to do this, and I'm
not entirely happy with either:
 1. Using cut & paste, paste in all the declarations from the other
    packages and then rename each one. Yuk.  What I'd like to do
    is put a "use-like" clause in the package and have it be
    re-exported, but I don't think I can do that.. can I?
 2. Create a child package of Ada.Strings.Unbounded, add what I want,
    and then create "Ustrings" by declaring it to be a rename of
    that child.  That looks much nicer, but I'm concerned that some
    compilers may not like me creating a child of a predefined package.

Any suggestions, improvements, comments?
Code samples follow; suggestions appreciated.

--- David A. Wheeler
Net address: wheeler@ida.org

==============================================================

METHOD 1:


with Ada.Strings.Unbounded, Unbounded_String_Text_IO;
use  Ada.Strings.Unbounded, Unbounded_String_Text_IO;

package Ustrings is

 -- This package provides a simpler way to work with type
 -- Unbounded_String, since this type will be used very often.
 --
 -- This package the following simplifications:
 --  + Shortens package name to "Ustrings" and type name to "Ustring".
 --  + Combines Ada.Strings.Unbounded and Unbounded_String_Text_IO
 --    into a single package.
 --  + Creates shorter function names for To_Unbounded_String, i.e.
 --    To_Ustring(U) and U(S).  "U" is not a very readable name, but
 --    it's such a common operation that a short name seems appropriate
 --    (this function is needed every time a String constant is used).
 -- Developed by David A. Wheeler; released to the public domain.

 subtype Ustring is Unbounded_String;

 function To_Ustring(Source : String) return Unbounded_String
                                       renames To_Unbounded_String;
 function U(Source : String)          return Unbounded_String
                                       renames To_Unbounded_String;


 -- Include all declarations from packages
 --  Ada.Strings.Unbounded and Unbounded_String_Text_IO.

 -- These "use" clauses don't do the job, but creating a mass of
 -- "rename"s is awkward:

 use Ada.Strings.Unbounded;
 use Unbounded_String_Text_IO;

end Ustrings;


===========================================================
METHOD 2:


with Ada.Strings.Unbounded.Simplified;
package Ustrings renames Ada.Strings.Unbounded.Simplified;


with Unbounded_String_Text_IO, Text_IO;
use  Unbounded_String_Text_IO, Text_IO;

package Ada.Strings.Unbounded.Simplified is

 -- This package provides a simpler way to work with type
 -- Unbounded_String, since this type will be used very often.
 --
 -- This package the following simplifications:
 --  + Shortens package name to "Ustrings" and type name to "Ustring".
 --  + Combines Ada.Strings.Unbounded and Unbounded_String_Text_IO
 --    into a single package.
 --  + Creates shorter function names for To_Unbounded_String, i.e.
 --    To_Ustring(U) and U(S).  "U" is not a very readable name, but
 --    it's such a common operation that a short name seems appropriate
 --    (this function is needed every time a String constant is used).
 -- Developed by David A. Wheeler; released to the public domain.

 subtype Ustring is Unbounded_String;

 function To_Ustring(Source : String) return Unbounded_String
                                       renames To_Unbounded_String;
 function U(Source : String)          return Unbounded_String
                                       renames To_Unbounded_String;


 -- Include all declarations from packages
 --  Ada.Strings.Unbounded and Unbounded_String_Text_IO.

  procedure Get_Line(File : in File_Type; Item : out Unbounded_String)
            renames Get_Line;
  procedure Get_Line(Item : out Unbounded_String)
            renames Get_Line;

  procedure Put(File : in File_Type; Item : in Unbounded_String)
            renames Put;
  procedure Put(Item : in Unbounded_String)
            renames Put;

  procedure Put_Line(File : in File_Type; Item : in Unbounded_String)
            renames Put_Line;
  procedure Put_Line(Item : in Unbounded_String)
            renames Put_Line;

end Ada.Strings.Unbounded.Simplified;



with Ada.Strings.Unbounded, Text_IO;
use  Ada.Strings.Unbounded, Text_IO;
package Unbounded_String_Text_IO is
  procedure Get_Line(File : in File_Type; Item : out Unbounded_String);
  procedure Get_Line(Item : out Unbounded_String);

  procedure Put(File : in File_Type; Item : in Unbounded_String);
  procedure Put(Item : in Unbounded_String);

  procedure Put_Line(File : in File_Type; Item : in Unbounded_String);
  procedure Put_Line(Item : in Unbounded_String);
end Unbounded_String_Text_IO;






^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: Simplifying Unbounded_Stri
  1995-03-19 13:37 Simplifying Unbounded_Stri David Wheeler
@ 1995-03-22  5:21 ` Tucker Taft
  0 siblings, 0 replies; 2+ messages in thread
From: Tucker Taft @ 1995-03-22  5:21 UTC (permalink / raw)


David Wheeler (wheeler@ida.org) wrote:

: I'm looking for some advice on how to simplify Unbounded_String
: handling. Discussion and source code follows.
..

I would recommend the following:

   with Ada.Strings.Unbounded;
   use Ada.Strings.Unbounded;
   package Unbounded_Converters is
       function "+"(From : String) return Unbounded_String renames
          To_Unbounded_String;
       function "+"(From : Unbounded_String) return String renames
          To_String;
   end Unbounded_Converters;

And then a user of unbounded strings can:
   with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
   with Unbounded_Converters; use Unbounded_Converters;

Now they have a convenient unary operator "+" for converting back
and forth between unbounded strings and strings, meaning that there
is no particular reason to create separate routines for unbounded
string output.  You might still want a version of Get_Line which
didn't bother to go through String, so you might want to add this
to your package.

I don't see the need to define a child package, and as you mention,
implementations are allowed to limit the creation of child packages
of implementation-provided packages (though I doubt that many will
impose such limitations).

-Tucker Taft  stt@inmet.com
Intermetrics, Inc.




^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~1995-03-22  5:21 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1995-03-19 13:37 Simplifying Unbounded_Stri David Wheeler
1995-03-22  5:21 ` Tucker Taft

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox