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=-0.3 required=5.0 tests=BAYES_00, REPLYTO_WITHOUT_TO_CC autolearn=no 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!news2.google.com!newsread.com!news-xfer.newsread.com!newspeer.monmouth.com!newsfeed.icl.net!newsfeed.fjserv.net!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsfeed.arcor.de!news.arcor.de!not-for-mail From: "Dmitry A. Kazakov" Subject: Re: String filtering Newsgroups: comp.lang.ada User-Agent: 40tude_Dialog/2.0.14.1 MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Reply-To: mailbox@dmitry-kazakov.de Organization: cbb software GmbH References: Date: Tue, 27 Sep 2005 11:49:28 +0200 Message-ID: <1j92wa9843ylq.16j89wuqatbaj$.dlg@40tude.net> NNTP-Posting-Date: 27 Sep 2005 11:49:31 MEST NNTP-Posting-Host: 5ce14b9b.newsread2.arcor-online.net X-Trace: DXC=6hVPaUfA0Ji0_l3b[L=KQcQ5U85hF6f;djW\KbG]kaMhGSi?jHD8GO`[\VU@79>Xlm[6LHn;2LCVn7enW;^6ZC`d<=9bOTW=MNn X-Complaints-To: abuse@arcor.de Xref: g2news1.google.com comp.lang.ada:5182 Date: 2005-09-27T11:49:31+02:00 List-Id: On Tue, 27 Sep 2005 19:13:17 +1000, David Trudgett wrote: > with Ada.Strings.Maps, Ada.Strings.Unbounded; > use Ada.Strings.Maps, Ada.Strings.Unbounded; > > Lower_Chars : constant Character_Range := ('a', 'z'); > Upper_Chars : constant Character_Range := ('A', 'Z'); > Numer_Chars : constant Character_Range := ('0', '9'); > Alphanumeric : constant Character_Ranges > := (Lower_Chars, Upper_Chars, Numer_Chars); > Alphanumeric_Set : constant Character_Set := To_Set(Alphanumeric); with Strings.Maps.Constants; use Strings.Maps.Constants; -- use defined there Alphanumeric_Set > function Strip_Non_Alphanumeric > (Str : in Unbounded_String) return Unbounded_String > is > New_Str : Unbounded_String > := To_Unbounded_String(Count(Str, Alphanumeric_Set)); If you do this, then use String (1..Count (...)); > begin > New_Str := To_Unbounded_String(""); No need for that, it is initially an empty string. > for Char in 1 .. Length(Str) loop > if Is_In(Element(Str, Char), Alphanumeric_Set) then > Append(New_Str, Element(Str, Char)); > end if; > end loop; > return New_Str; > end Strip_Non_Alphanumeric; > > Is something like that what y'all do in situations like this? I don't. 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? Either, you need a character *stream* filtering, 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. Secondly, unbounded strings are rarely needed. Especially in text parsing etc. It is quite uncommon to change a string content there. In your example you don't do it either. You create a new string. Also both the source and the result strings have *known* length. So you don't need unbounded strings here. Usually, after making some trivial analysis like that you'll find out that only 2% or so really need to be unbounded. -- Regards, Dmitry A. Kazakov http://www.dmitry-kazakov.de