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-Thread: 103376,8f802583e5c84fa X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news3.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!news1.optus.net.au!optus!newsfeeder.syd.optusnet.com.au!news.optusnet.com.au!newsfeed.pacific.net.au!nasal.pacific.net.au!not-for-mail Newsgroups: comp.lang.ada Subject: Re: String filtering From: David Trudgett Organization: Very little? References: <1j92wa9843ylq.16j89wuqatbaj$.dlg@40tude.net> <1b54lwg8s1gk8.1t3jp1cmc2x32$.dlg@40tude.net> Message-ID: User-Agent: Gnus/5.1006 (Gnus v5.10.6) Emacs/21.4 (gnu/linux) Cancel-Lock: sha1:vnAgNmqJ2TMZVHLHAQ6LMWIbkzo= MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Date: Wed, 28 Sep 2005 10:51:35 +1000 NNTP-Posting-Host: 61.8.35.118 X-Complaints-To: news@pacific.net.au X-Trace: nasal.pacific.net.au 1127868975 61.8.35.118 (Wed, 28 Sep 2005 10:56:15 EST) NNTP-Posting-Date: Wed, 28 Sep 2005 10:56:15 EST Xref: g2news1.google.com comp.lang.ada:5212 Date: 2005-09-28T10:51:35+10:00 List-Id: "Dmitry A. Kazakov" writes: > On Tue, 27 Sep 2005 21:15:15 +1000, David Trudgett wrote: > >> "Dmitry A. Kazakov" writes: >> >> OK, now I have: >> >> Alpha_Num_Space_Set : constant Character_Set >> := Alphanumeric_Set or To_Set(' '); >> >> since I realised I also need space. > > And HT? And VT? LF, CR,... (:-)) ;-) Uh, nope. >>> >>>> begin >>>> New_Str := To_Unbounded_String(""); >>> >>> No need for that, it is initially an empty string. >> >> I at first thought so myself, until I discovered that New_Str was >> uninitialised, as it says in the ARM. Hence, I added that line. > > It is, with Null_Unbounded_String. Why would I say, "it says in the ARM" if it doesn't? ;-) But someone else has already pointed out the bit to you. (I also said that I *discovered* that it was uninitialised. You may, however, have interpreted that to mean that I discovered it in the ARM, which was not the case.) > >>> Firstly it is not clear why characters need to be filtered out. Or >>> better to say, how did it happen, that you get garbage in a string? >> >> I am sanitising data received over a socket, which may be of any >> length. Hence my use of unbounded_string, and my desire to strip out >> non-alphanumeric characters. > > But sockets normally work either as a stream or with a > Storage_Element's array. Thus you don't have Unbounded_String, you > make it later. I get it as a string from the adasockets (0.1.6) library Get_Line function. Perhaps this is an old version, I don't know. I noticed a couple of people in this group have contributed to it (such as Pascal Obry), so they might be able to say. However, you are right that I *could* use string instead, by changing the way I do things. But I'm happy with unbounded strings for the time being and for my present purposes. (I'm just learning Ada, you know! :-)) > Do a String instead. > >>> Either, you need a character *stream* filtering, >> >> Possibly, but I'm not using a socket stream interface at the current >> time. The socket library I'm using right now doesn't do streams. > > Anyway, you have some protocol, and non-alpha characters seem to > violate it. So, what your filter does, is inventing some meaning out > of meaningless rubbish. Usually it is rather a bad idea, see PL/1 > and HTML. Errors should be reported as early as possible. Your points are theoretically valid, but... :-) I'm working on a toy program, Dmitry, and being forgiving with protocol is one thing that I do in toy programs. :-) If the remote player (it's a battleship game, oh, the violence, the violence ;-)) wants to send ANSI escape sequences in his name, well he can do so, but they're not going to mess up my ANSI terminal screen! ;-) I could, of course, do better than just screening out non-alphanumeric characters, but I'm lazy :-) and... it's a toy program (did I mention that :-)). I've also designed the whole thing in easily maintained modules, so added robustness is a simple add-on in future. > >>> long before you get a string token out of it, or, more realistically >>> an error message (exception), should have happened, for example if >>> you take some text from a GUI widget. A GUI is a future enhancement! :-) (But one that will be easy to add, by design.) >>> >>> Secondly, unbounded strings are rarely needed. >> >> For some definition of 'rarely', I suppose. :-) I'm sure some people >> must use them all the time, so it wouldn't be rare for them. > > You must use it only under certain conditions. Which are: mutability and > "sufficiently" unknown in advance length. In your case they aren't > satisfied, so you don't have to, if you don't want to... (:-)) But I want to, Dmitry, I want to! :-) > >> Ada does make it a pain to use unbounded_strings, so it can seem like >> a virtue to avoid them, but other languages use them by default, with >> no ill-effects to show for it ;-) > > It is a different story. Unbounded_String is a nasty kludge. But > that does not mean that if they were designed properly, they would > be more needed! (:-)) I'm almost afraid to ask... :-) What is it about Unbounded_String that makes it a kludge, in your opinion? Is there something unecessarily inefficient (space/time) about the way they are specified in the Ada95 standard? Or is it that existing implementations of it are a kludge? >> Yes, well, functions work that way in Ada (fortunately, or >> unfortunately, I don't know). I could have made it a procedure with an >> "in out" parameter, but I like functional programming better. >> Unfortunately, I haven't been able to do proper functional style >> programming in Ada so far, having been thwarted by strong typing and >> lack of "out" parameters in functions. > > Well, out parameters in functions are much desired by almost > anybody, except the ARG members. (:-)) But that won't help. Try > access parameters instead and you will see. Alright... that sounds like an invitation to dance in a mine field. ;-) > The problem is that an out parameter cannot "return" constraints as > the proper result can. [I suppose I can guess what you mean by this.] It's true that out parameters don't cure all ills. > function Op (...) return String is > Result_Length : Natural; > begin > -- evaluate Result_Length > declare > Result : String (1..Result_Length); > begin > -- Fill Result > return Result; > end; > end Op; > > Such operations can always be used as: > > declare > X : String renames Op (...); > begin > -- Using X > end; That's handy, isn't it? Very nice. I must remember to experiment with it. David -- David Trudgett http://www.zeta.org.au/~wpower/ On another level there is a principle laid down, much in line with common sense and with the original American ideal, that governments should never do what small bodies can accomplish: unions, credit unions, cooperatives, St. Vincent de Paul Societies. Peter Maurin's anarchism was on one level based on this principle of subsidiarity, and on a higher level on that scene at the Last Supper where Christ washed the feet of His Apostles. He came to serve, to show the new Way, the way of the powerless. In the face of Empire, the Way of Love. -- Dorothy Day, The Catholic Worker, May 1972. (Dorothy Day Library on the Web at http://www.catholicworker.org/dorothyday/)