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 06:25:43 PST Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!cyclone.bc.net!snoopy.risq.qc.ca!chi1.webusenet.com!news.webusenet.com!cyclone1.gnilink.net!wn14feed!worldnet.att.net!204.127.198.203!attbi_feed3!attbi_feed4!attbi.com!sccrnsc01.POSTED!not-for-mail Message-ID: <3EFC45BE.5030904@attbi.com> From: "Robert I. Eachus" User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.0.2) Gecko/20021120 Netscape/7.01 X-Accept-Language: en-us, en MIME-Version: 1.0 Newsgroups: comp.lang.ada Subject: Re: conversion References: Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit NNTP-Posting-Host: 24.62.164.137 X-Complaints-To: abuse@attbi.com X-Trace: sccrnsc01 1056720340 24.62.164.137 (Fri, 27 Jun 2003 13:25:40 GMT) NNTP-Posting-Date: Fri, 27 Jun 2003 13:25:40 GMT Organization: AT&T Broadband Date: Fri, 27 Jun 2003 13:25:40 GMT Xref: archiver1.google.com comp.lang.ada:39811 Date: 2003-06-27T13:25:40+00:00 List-Id: 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.