From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,38c827f7e800d317 X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2003-06-27 05:40:34 PST Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!logbridge.uoregon.edu!hammer.uoregon.edu!skates!not-for-mail From: Stephen Leake Newsgroups: comp.lang.ada Subject: Re: conversion Date: 27 Jun 2003 08:37:55 -0400 Organization: NASA Goddard Space Flight Center (skates.gsfc.nasa.gov) Message-ID: References: NNTP-Posting-Host: anarres.gsfc.nasa.gov Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: skates.gsfc.nasa.gov 1056717504 12639 128.183.235.92 (27 Jun 2003 12:38:24 GMT) X-Complaints-To: usenet@news.gsfc.nasa.gov NNTP-Posting-Date: 27 Jun 2003 12:38:24 GMT User-Agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.3 Xref: archiver1.google.com comp.lang.ada:39806 Date: 2003-06-27T12:38:24+00:00 List-Id: "Andrew" 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