comp.lang.ada
 help / color / mirror / Atom feed
From: Stephen Leake <Stephe.Leake@nasa.gov>
Subject: Re: conversion
Date: 27 Jun 2003 08:37:55 -0400
Date: 2003-06-27T12:38:24+00:00	[thread overview]
Message-ID: <uznk3u258.fsf@nasa.gov> (raw)
In-Reply-To: mailman.18.1056709818.8204.comp.lang.ada@ada.eu.org

"Andrew" <andrew@carroll-tech.net> writes:

> 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.

The "correct" choice of what to do depends heavily on exactly what
your application is.

If you are writing a tokenizer, you should consider using OpenToken.

> 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.

Yes, it is sometimes difficult to decide which of Ada's many features
to use. It's much easier in C, where you have no choice :).

> For instance: a variable of type char * can be used as char * or as
> char [] and char [] can be used as char *. 

yes.

> A fixed string in Ada (to me) is like declaring a char [], you must
> specify a size at compile time.

Yes.

> 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.

No. The best analogy to char * is Ada.Strings.Unbounded.String_Access;
a pointer to an allocated string.

C does _not_ have a true unbounded string type, that can automatically
change size; you have to free and reallocate.

C++ provides a String class that behaves much like Ada's
Unbounded_String. 

> The catch is that unbounded_string can not be used in context of
> string. 

That's what To_String is for. You should not worry about whether that
is "efficient". When you have finished you application, if it is too
slow, you can measure it's speed, find the bottlenecks, and fix them.
I'd be very surprised if To_String is a bottleneck.

> This posses some design inconveniences and requires converting back
> and forth from string to unbounded_string multiple times.

That does sound like a problem. Perhaps you could give more details,
and we could provide better advice.

You could also measure the speed of the conversions now, to convince
yourself that it is (or is not) a problem.

> 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. 

There are many ways to avoid the need for unbounded strings,
typicallly using a local declare block. However, there are also times
when you need them, typically when storing strings in records.

How would you solve the problem in C? If you are happy with that
solution, do exactly the same thing in Ada, with the mapping char[] =>
String, char * => String_Access, malloc => new, free =>
Unchecked_Conversion.

> 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...

What features would this "universal" string type have that either
String or Unbounded_String does not have?

> I'm not real sure whether to extend on the functionality of
> ada.text_io 

What is missing from Ada.Text_IO?

> or to create new functionality from the stream package. Any
> recommendations? Has anyone done something like this?

Many people have complained about Ada strings. They do take getting
used to, especially if you are coming from a C (rather than C++)
background. But no one has proposed a better solution (to my
knowledge). Hmm, Matthew Heaney's Charles library has a new String
type, but I haven't looked at it to see why.

-- 
-- Stephe



  parent reply	other threads:[~2003-06-27 12:37 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 ` Stephen Leake [this message]
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 ` conversion Robert I. Eachus
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