comp.lang.ada
 help / color / mirror / Atom feed
From: David Trudgett <wpower@zeta.org.au.nospamplease>
Subject: Re: String filtering
Date: Tue, 27 Sep 2005 19:13:17 +1000
Date: 2005-09-27T19:13:17+10:00	[thread overview]
Message-ID: <m3mzly6fgy.fsf@rr.trudgett> (raw)
In-Reply-To: m2irwnez9b.fsf@hugin.crs4.it

Jacob Sparre Andersen <sparre@nbi.dk> writes:

> David Trudgett wrote:
>
>> I've been puzzling for a little bit over a good way to filter out
>> unwanted characters from a string. In particular, I have an
>> unbounded string and want to filter out of it all characters not in
>> 'a'..'z', 'A'..'Z', '0'..'9'. So far I've only thought of tedious
>> ways to do it. Is there an easy way to do it using the string
>> handling facilities in Ada? I think I almost got there with the idea
>> of using Maps.Character_Set, and so on, but I haven't quite pieced
>> it together yet.
>
> I would probably simply iterate over the elements in the string and
> copy those which a call to "function Is_In (Element : in Character;
> Set : in Character_Set) return Boolean;" indicate to the target
> string.
>
> You could use "function Count (Source : in Unbounded_String; Set : in
> Maps.Character_Set) return Natural;" to preallocate the target string,
> if you're afraid appending to an unbounded string is too slow for your
> purpose.

OK, thanks for those hints. I've come up with the following, which
seems to do the job:

    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);

    function Strip_Non_Alphanumeric
      (Str : in Unbounded_String) return Unbounded_String
    is
       New_Str : Unbounded_String
         := To_Unbounded_String(Count(Str, Alphanumeric_Set));
    begin
       New_Str := To_Unbounded_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?


Cheers,

David



-- 

David Trudgett
http://www.zeta.org.au/~wpower/

Every war, even the most humanely conducted, with all its ordinary
consequences, the destruction of harvests, robberies, the license and
debauchery, and the murder with the justifications of its necessity
and justice, the exaltation and glorification of military exploits,
the worship of the flag, the patriotic sentiments, the feigned
solicitude for the wounded, and so on, does more in one year to
pervert men's minds than thousands of robberies, murders, and arsons
perpetrated during hundreds of years by individual men under the
influence of passion.

    -- Leo Tolstoy, "The Kingdom of God is Within You"




  reply	other threads:[~2005-09-27  9:13 UTC|newest]

Thread overview: 71+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-09-27  6:27 String filtering David Trudgett
2005-09-27  7:38 ` Jacob Sparre Andersen
2005-09-27  9:13   ` David Trudgett [this message]
2005-09-27  9:49     ` Dmitry A. Kazakov
2005-09-27 11:01       ` Martin Dowie
2005-09-27 11:12         ` Martin Dowie
2005-09-27 12:54           ` Dmitry A. Kazakov
2005-09-27 13:42             ` Martin Dowie
2005-09-27 14:24               ` Dmitry A. Kazakov
2005-09-28  0:06                 ` David Trudgett
2005-09-28  8:15                   ` Dmitry A. Kazakov
2005-09-28 10:39                     ` David Trudgett
2005-09-28 20:55                       ` Simon Wright
2005-09-28 21:53                         ` Martin Dowie
2005-09-28  9:08                   ` Jacob Sparre Andersen
2005-09-28  9:54                     ` David Trudgett
2005-09-29 14:05                       ` Georg Bauhaus
2005-10-01 19:02                         ` tmoran
2005-10-02  6:38                           ` David Trudgett
2005-10-02 14:11                             ` Martin Dowie
2005-10-02 22:40                               ` David Trudgett
2005-10-03  5:56                                 ` Martin Dowie
2005-10-03 10:33                           ` Georg Bauhaus
2005-09-28 18:21                   ` Jeffrey R. Carter
2005-09-28 21:00                   ` Simon Wright
2005-09-27 11:22         ` David Trudgett
2005-09-27 11:15       ` David Trudgett
2005-09-27 13:21         ` Dmitry A. Kazakov
2005-09-27 13:43           ` Martin Dowie
2005-09-28  0:51           ` David Trudgett
2005-09-28 12:02             ` Dmitry A. Kazakov
2005-09-28 13:25             ` Marc A. Criley
2005-09-29 22:42           ` Randy Brukardt
2005-09-30 17:54             ` Robert A Duff
2005-10-02  6:57               ` Steve Whalen
2005-10-02 14:14                 ` Martin Dowie
2005-10-03  1:21                 ` Robert A Duff
2005-10-03  7:44                   ` Jacob Sparre Andersen
2005-10-03  8:56                     ` Dmitry A. Kazakov
2005-10-03  9:25                       ` Jean-Pierre Rosen
2005-10-03 20:17                         ` Ada Notation Jeffrey R. Carter
2005-10-03 20:41                           ` Georg Bauhaus
2005-10-05 17:16                             ` Andre
2005-10-05 18:23                               ` Ludovic Brenta
2005-10-05 18:24                               ` Jeffrey R. Carter
2005-10-04 15:13                           ` brian.b.mcguinness
2005-10-04 17:00                     ` String filtering Robert A Duff
2005-10-05  8:19                       ` Jean-Pierre Rosen
2005-10-05 11:25                         ` Robert A Duff
2005-10-04 19:47                     ` Björn Persson
2005-10-05 14:14                       ` Dmitry A. Kazakov
2005-10-03 10:06                   ` Steve Whalen
2005-10-03 17:43                   ` tmoran
2005-10-03 17:59                     ` Robert A Duff
2005-10-05 23:04                       ` Randy Brukardt
2005-09-27 13:52         ` Jacob Sparre Andersen
2005-09-28  1:01           ` David Trudgett
2005-09-28  1:50             ` David Trudgett
2005-09-27 14:08         ` Georg Bauhaus
2005-09-27 14:09         ` Marc A. Criley
2005-09-28  1:09           ` David Trudgett
2005-09-28 21:09           ` Simon Wright
2005-09-27 17:59         ` tmoran
2005-09-28  1:20           ` David Trudgett
2005-09-27 17:47     ` Jeffrey R. Carter
2005-09-28  1:29       ` David Trudgett
2005-09-28 18:32         ` Jeffrey R. Carter
2005-09-27  7:41 ` tmoran
2005-09-27  9:17   ` David Trudgett
2005-09-28  1:54 ` Steve
2005-09-28  2:20   ` David Trudgett
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox