Re: String filtering
- From: David Trudgett <wpower@xxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Tue, 27 Sep 2005 19:13:17 +1000
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"
.
- Follow-Ups:
- Re: String filtering
- From: Jeffrey R. Carter
- Re: String filtering
- References:
- String filtering
- From: David Trudgett
- String filtering
- Prev by Date: Re: GNAT Mingw32 link error?
- Next by Date: Re: String filtering
- Previous by thread: String filtering
- Next by thread: Re: String filtering
- Index(es):
Relevant Pages
|