Re: String filtering



Jacob Sparre Andersen <sparre@xxxxxx> 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"

.



Relevant Pages

  • Re: Aliasing or referencing assignment
    ... I thought I read in the ARM that string literals were ... > String literals are defined for all string types. ... David Trudgett ...
    (comp.lang.ada)
  • Re: Aliasing or referencing assignment
    ... David Trudgett wrote: ... would be pre-elaborated during the compilation process. ... := new String (1 .. ... Monty Python's Flying Circus ...
    (comp.lang.ada)
  • Re: String filtering
    ... >> String whose length is Length." ... > Replace_Element should be in place of assignment + Append. ... David Trudgett ... Men on a lower level of ...
    (comp.lang.ada)
  • String filtering
    ... I've been puzzling for a little bit over a good way to filter out ... unwanted characters from a string. ... David Trudgett ...
    (comp.lang.ada)
  • Re: Filtering records on a form using multiple combo boxes
    ... So, then, you will need to build the Where string from that information, ... these 3 combo boxes have command buttons to activate the filtering ... filter the field in the record list box.. ... For the Agent Combo Box, ...
    (microsoft.public.access.forms)