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!news3.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!nx02.iad01.newshosting.com!newshosting.com!217.73.144.44.MISMATCH!ecngs!feeder.ecngs.de!newsfeed.freenet.de!npeer.de.kpn-eurorings.net!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: <1j92wa9843ylq.16j89wuqatbaj$.dlg@40tude.net> <433924a2$1_1@glkas0286.greenlnk.net> <43392732$1_1@glkas0286.greenlnk.net> <1jd30obyohnp6$.41tz3funikly.dlg@40tude.net> <43394a3e$1_1@glkas0286.greenlnk.net> Date: Wed, 28 Sep 2005 10:15:12 +0200 Message-ID: <1gdgwjo7aini9.1qzlnlpq5gsa$.dlg@40tude.net> NNTP-Posting-Date: 28 Sep 2005 10:14:44 MEST NNTP-Posting-Host: 1ff0aa0f.newsread4.arcor-online.net X-Trace: DXC=4H]=ERL]:G949B_U7Ob821:ejgIfPPld4jW\KbG]kaM8XY;eg@jLYe7enW;^6ZC`4<=9bOTW=MN> X-Complaints-To: abuse@arcor.de Xref: g2news1.google.com comp.lang.ada:5222 Date: 2005-09-28T10:14:44+02:00 List-Id: On Wed, 28 Sep 2005 10:06:44 +1000, David Trudgett wrote: > "Dmitry A. Kazakov" writes: > >> On Tue, 27 Sep 2005 14:42:02 +0100, Martin Dowie wrote: >>> 2nd sentence of ARM 95 A.4.5 (76) reads: >>> >>> "The function To_Unbounded_String(Length : in Natural) >>> returns an Unbounded_String that represents an uninitialized >>> String whose length is Length." >> >> Ah, now I see what you meant! > > Yep, that's what I meant, too. No. What Martin meant is that: X : Unbounded_String := To_Unbounded_String (Count); is filled with rubbish What I meant is that: X : Unbounded_String; is an empty string, being formally uninitialized. You can imagine it as Unbounded_Strings having a default constructor setting them empty. > I've made some revisions based on various comments, and this is what I > have at the moment (incorporating both a string and unbounded_string > version): > > Space_Char : constant Character_Range := (' ', ' '); > Lower_Chars : constant Character_Range := ('a', 'z'); > Upper_Chars : constant Character_Range := ('A', 'Z'); > Numer_Chars : constant Character_Range := ('0', '9'); > Alpha_Num_Space : constant Character_Ranges > := (Space_Char, Lower_Chars, Upper_Chars, Numer_Chars); > Alpha_Num_Space_Set : constant Character_Set > := To_Set(Alpha_Num_Space); > > > function Strip_Non_Alphanumeric > (Str : in Unbounded_String) return Unbounded_String > is > Dest_Size : Natural := Count(Str, Alpha_Num_Space_Set); > New_Str : Unbounded_String := Null_Unbounded_String; You don't need initialization here. Or you can do with Dest_Size. The parameter of To_Unbounded_String is a Natural. > Dest_Char : Natural := 0; > begin > if Dest_Size > 0 then You don't need this if. Ada's loops are safe for zero-run. > New_Str := To_Unbounded_String(Dest_Size); > for Src_Char in 1 .. Length(Str) loop > if Is_In(Element(Str, Src_Char), Alpha_Num_Space_Set) then > Dest_Char := Dest_Char + 1; > Replace_Element > (New_Str, Dest_Char, Element(Str, Src_Char)); > end if; > end loop; > end if; > return New_Str; > end Strip_Non_Alphanumeric; > > > function Strip_Non_Alphanumeric > (Str : in String) return String > is > New_Str : String(1 .. Count(Str, Alpha_Num_Space_Set)); You also can do instead: New_Str : String(1 .. Length (Str)); and save one extra scan of the string by Count. Memory is cheap and in worst case scenario you will allocate that amount anyway. > Dest_Char : Natural := 0; > begin > if New_Str'Last > 0 then No need in this if. > for Src_Char in Str'Range loop > if Is_In(Str(Src_Char), Alpha_Num_Space_Set) then > Dest_Char := Dest_Char + 1; > New_Str(Dest_Char) := Str(Src_Char); > end if; > end loop; here you do: return New_Str (1..Dest_Char); Ada strings has slices! > else > New_Str := ""; > end if; > return New_Str; > end Strip_Non_Alphanumeric; > >> Because, assignment might reclaim the memory allocated by >> To_Unbounded_String (Count). > > I assume this is left up to the compiler implementation? Yes -- Regards, Dmitry A. Kazakov http://www.dmitry-kazakov.de