Re: String filtering



"Dmitry A. Kazakov" <mailbox@xxxxxxxxxxxxxxxxx> 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.


>
> Right, though my point was that one should use either
>
> New_Str : String (1...Count (...));
>
> or
>
> New_Str : Unbounded_String; -- "Uninitialized"


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;
Dest_Char : Natural := 0;
begin
if Dest_Size > 0 then
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));
Dest_Char : Natural := 0;
begin
if New_Str'Last > 0 then
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;
else
New_Str := "";
end if;
return New_Str;
end Strip_Non_Alphanumeric;



In the unbounded version, I decided to use replace_element instead of
append (with its assignment to "", which might perhaps unallocate
memory, depending on implementation??, thus potentially undoing the
purpose of the preallocation). This version might gain a bit in
efficiency, but code-wise, it is a bit more complex.

The string version also seems to work under testing.

Profiling would show no difference in performance between the two for
my current purposes, but in a different situation, involving large
amounts of data, for instance, the fixed string version would no doubt
out-perform speed-wise. Space-wise, the unbounded strings would
probably win out in many situations.



>
> So my comment about empty string concerned the latter case.
>
> If To_Unbounded_String (Count) is ever used then of course
> Replace_Element should be in place of assignment + Append.

This is what I have done. Have I mucked up anything else while doing so?


> Because, assignment might reclaim the memory allocated by
> To_Unbounded_String (Count).

I assume this is left up to the compiler implementation?


David


--

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

We come here upon what, in a large proportion of cases, forms the
source of the grossest errors of mankind. Men on a lower level of
understanding, when brought into contact with phenomena of a higher
order, instead of making efforts to understand them, to raise
themselves up to the point of view from which they must look at the
subject, judge it from their lower standpoint, and the less they
understand what they are talking about, the more confidently and
unhesitatingly they pass judgment on it.

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

.



Relevant Pages

  • Problem mit XML Serialisierung
    ... public sealed partial class Assignment { ... XmlSerializer serializer = new XmlSerializer); ... private string david; ... private Listdatabases; ...
    (microsoft.public.de.german.entwickler.dotnet.csharp)
  • Re: OpenForm vs. Form menu double click.
    ... was a string and the data type of the where variable matched the syntax... ... In my situatiation the SSN on the input is text as well as the SSN field in ... A command button to add an assignment ... Private Sub Form_Open ...
    (microsoft.public.access.formscoding)
  • Re: Java or C++?
    ... Plus there are several toolkits and existing libraries available for Python. ... Take a string object for example. ... a copy constructor or assignment operator can be implemented when using C++. ... And if you want more control you can implement the default and copy constructors, destructor, and assignment operator, and tell them to do what you want. ...
    (comp.lang.python)
  • Re: Is a function a relation?
    ... update to being shortcuts for assignment. ... make it sound like they can suddenly spring into existence. ... That is a *selection* of a relation value. ... Consider the analogy of a textual string. ...
    (comp.databases.theory)
  • Re: Ada Interfaces and the Liskov Substitution Principle
    ... referer as well as sharing semantics. ... objects from the class should or not have assignment or any other operation ... string is a value type. ... Let's move to the internals. ...
    (comp.lang.ada)