From: "Robert I. Eachus" <rieachus@attbi.com>
Subject: Re: conversion
Date: Fri, 27 Jun 2003 13:25:40 GMT
Date: 2003-06-27T13:25:40+00:00 [thread overview]
Message-ID: <3EFC45BE.5030904@attbi.com> (raw)
In-Reply-To: mailman.18.1056709818.8204.comp.lang.ada@ada.eu.org
Andrew wrote:
> I am experimenting with Ada as the primary language in our process. It
> seems that the use of String and Unbounded_String require converting
> from one to the other in order to read a line from a file and then
> trim it or slice it or tokenize it. For now, regardless of the
> performance of these conversions it is rather inconvenient to design
> for a language that does not have a "universal" string type.
>
> For instance: a variable of type char * can be used as char * or as
> char [] and char [] can be used as char *. A fixed string in Ada (to me)
> is like declaring a char [], you must specify a size at compile time. An
> unbounded_string in Ada is like char *, it can take on a different size
> when it appears on the LHS of an assignment operator. The catch is that
> unbounded_string can not be used in context of string. This posses some
> design inconveniences and requires converting back and forth from string to
> unbounded_string multiple times.
>
> I defined a string pointer type so that I could dynamically create strings
> that are the fixed string type but I find that 'that' only defers the need
> to convert from fixed string to unbounded_string to different points
in the
> design or that the conversion is not eliminated. I am now thinking
that for
> my company we could develop a "library" that has a "universal" string
type.
> If we don't base it on the defined string in package standard and can use
> streams to read from files then we can define our own string type. I
> think...
>
> I'm not real sure whether to extend on the functionality of ada.text_io or
> to create new functionality from the stream package. Any recommendations?
> Has anyone done something like this?
>
> Andrew
Hmmm. Where to begin. You are thinking in C about Ada concepts. That
is confusing you because the Ada mappings are different. In Ada, the
TYPE String is unconstrained. Objects must be constrained, so if you
need to put a String somewhere, you have to provide a constraint,
explicitly or implicitly. If you do:
Foo: String := Bar;
The object Foo is constrained to the length of the String returned by
the call to Bar (if Bar is a function), or to have the same length as
Bar, if Bar is an object.
In C, you can have objects of type char [], but you can't have values of
char []. So parameters of char [] are implicitly converted to char *.
In Ada, you almost never need an access to String type. You can create
one, or use the one defined in Ada.Strings.Unbounded, but it just
muddies up the waters.
So what is Ada.Strings.Unbounded.Unbounded_String? Conceptually all you
need to know is that it is a (constrained) container type, so you can
have objects of type Ada.Strings.Unbounded_String. Putting strings into
a container should be less expensive than malloc and free in C. (Or I
guess free and malloc in this case.) Using the String VALUE that is in
an Unbounded_String container is no harder than using any other value.
So in Ada, 99% of the time when you need to use a (lower case) string
type, you use String, and done. If for some reason you need a string
object that can vary within bounds (like PL/I char * varying) or a
string object whose bounds can vary radically, you create an instance of
Ada.Strings.Bounded_String, or use Ada.Strings.Unbounded_String.
Ada.Strings.Unbounded_String has no counterpart that I am aware of
outside garbage collected languages. It does automatic string
allocation and storage management at a very low cost.
next prev parent reply other threads:[~2003-06-27 13:25 UTC|newest]
Thread overview: 44+ messages / expand[flat|nested] mbox.gz Atom feed top
2003-06-27 10:51 conversion Andrew
2003-06-27 12:22 ` conversion Dmitry A. Kazakov
2003-06-27 12:37 ` conversion Stephen Leake
2003-06-27 14:26 ` conversion Bill Findlay
2003-06-27 17:04 ` conversion Georg Bauhaus
2003-07-04 0:21 ` conversion Dave Thompson
2003-06-27 13:25 ` Robert I. Eachus [this message]
2003-06-27 18:42 ` conversion tmoran
2003-06-27 14:49 ` conversion Matthew Heaney
2003-06-27 17:10 ` conversion Georg Bauhaus
2003-06-27 17:13 ` conversion Alexander Kopilovitch
2003-06-27 17:34 ` conversion Preben Randhol
2003-06-27 22:10 ` conversion Alexander Kopilovitch
2003-06-28 9:46 ` conversion Preben Randhol
2003-06-27 22:13 ` conversion Robert I. Eachus
2003-06-30 8:52 ` conversion Dmitry A. Kazakov
2003-07-03 7:03 ` conversion Robert I. Eachus
2003-07-09 7:42 ` conversion Dmitry A. Kazakov
2003-07-09 17:04 ` conversion Robert I. Eachus
2003-07-10 10:19 ` conversion Dmitry A. Kazakov
2003-07-11 1:56 ` conversion Alexander Kopilovitch
2003-07-05 2:40 ` conversion Alexander Kopilovitch
2003-07-05 6:33 ` conversion Georg Bauhaus
2003-07-05 17:06 ` conversion Alexander Kopilovitch
2003-07-06 3:53 ` conversion Robert I. Eachus
2003-07-06 5:13 ` conversion Jeffrey Carter
2003-07-06 12:45 ` conversion Chad R. Meiners
2003-07-07 1:09 ` conversion Alexander Kopilovitch
2003-07-06 20:04 ` conversion Georg Bauhaus
2003-07-07 14:55 ` conversion Stephen Leake
2003-07-07 21:36 ` conversion Alexander Kopilovitch
[not found] <002701c33e22$8e9deaf0$0201a8c0@win>
2003-06-29 20:15 ` conversion David C. Hoos, Sr.
-- strict thread matches above, loose matches on Subject: below --
2003-06-29 9:41 conversion Andrew
2003-07-04 10:42 ` conversion Janeit
2003-06-28 8:46 conversion Andrew
2003-06-28 9:49 ` conversion Preben Randhol
2003-06-30 14:08 ` conversion Stephen Leake
2003-06-27 17:37 conversion Andrew
2003-06-27 17:32 ` conversion Stephen Leake
2003-06-28 2:55 ` conversion Jeffrey Carter
1998-07-22 0:00 conversion Rick
1998-07-22 0:00 ` conversion Richard Toy
1998-07-22 0:00 ` conversion Corey Ashford
1998-07-22 0:00 ` conversion Corey Ashford
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox