Re: String filtering



"Dmitry A. Kazakov" <mailbox@xxxxxxxxxxxxxxxxx> writes:

> On Tue, 27 Sep 2005 21:15:15 +1000, David Trudgett wrote:
>
>> "Dmitry A. Kazakov" <mailbox@xxxxxxxxxxxxxxxxx> writes:
>>
>> OK, now I have:
>>
>> Alpha_Num_Space_Set : constant Character_Set
>> := Alphanumeric_Set or To_Set(' ');
>>
>> since I realised I also need space.
>
> And HT? And VT? LF, CR,... (:-))

;-) Uh, nope.


>>>
>>>> begin
>>>> New_Str := To_Unbounded_String("");
>>>
>>> No need for that, it is initially an empty string.
>>
>> I at first thought so myself, until I discovered that New_Str was
>> uninitialised, as it says in the ARM. Hence, I added that line.
>
> It is, with Null_Unbounded_String.

Why would I say, "it says in the ARM" if it doesn't? ;-) But someone
else has already pointed out the bit to you. (I also said that I
*discovered* that it was uninitialised. You may, however, have
interpreted that to mean that I discovered it in the ARM, which was
not the case.)


>
>>> Firstly it is not clear why characters need to be filtered out. Or
>>> better to say, how did it happen, that you get garbage in a string?
>>
>> I am sanitising data received over a socket, which may be of any
>> length. Hence my use of unbounded_string, and my desire to strip out
>> non-alphanumeric characters.
>
> But sockets normally work either as a stream or with a
> Storage_Element's array. Thus you don't have Unbounded_String, you
> make it later.

I get it as a string from the adasockets (0.1.6) library Get_Line
function. Perhaps this is an old version, I don't know. I noticed a
couple of people in this group have contributed to it (such as Pascal
Obry), so they might be able to say.

However, you are right that I *could* use string instead, by changing
the way I do things. But I'm happy with unbounded strings for the time
being and for my present purposes. (I'm just learning Ada, you know! :-))



> Do a String instead.
>
>>> Either, you need a character *stream* filtering,
>>
>> Possibly, but I'm not using a socket stream interface at the current
>> time. The socket library I'm using right now doesn't do streams.
>
> Anyway, you have some protocol, and non-alpha characters seem to
> violate it. So, what your filter does, is inventing some meaning out
> of meaningless rubbish. Usually it is rather a bad idea, see PL/1
> and HTML. Errors should be reported as early as possible.

Your points are theoretically valid, but... :-) I'm working on a toy
program, Dmitry, and being forgiving with protocol is one thing that I
do in toy programs. :-) If the remote player (it's a battleship game,
oh, the violence, the violence ;-)) wants to send ANSI escape
sequences in his name, well he can do so, but they're not going to
mess up my ANSI terminal screen! ;-) I could, of course, do better
than just screening out non-alphanumeric characters, but I'm lazy :-)
and... it's a toy program (did I mention that :-)).

I've also designed the whole thing in easily maintained modules, so
added robustness is a simple add-on in future.


>
>>> long before you get a string token out of it, or, more realistically
>>> an error message (exception), should have happened, for example if
>>> you take some text from a GUI widget.

A GUI is a future enhancement! :-) (But one that will be easy to add,
by design.)



>>>
>>> Secondly, unbounded strings are rarely needed.
>>
>> For some definition of 'rarely', I suppose. :-) I'm sure some people
>> must use them all the time, so it wouldn't be rare for them.
>
> You must use it only under certain conditions. Which are: mutability and
> "sufficiently" unknown in advance length. In your case they aren't
> satisfied, so you don't have to, if you don't want to... (:-))

But I want to, Dmitry, I want to! :-)


>
>> Ada does make it a pain to use unbounded_strings, so it can seem like
>> a virtue to avoid them, but other languages use them by default, with
>> no ill-effects to show for it ;-)
>
> It is a different story. Unbounded_String is a nasty kludge. But
> that does not mean that if they were designed properly, they would
> be more needed! (:-))

I'm almost afraid to ask... :-) What is it about Unbounded_String that
makes it a kludge, in your opinion? Is there something unecessarily
inefficient (space/time) about the way they are specified in the Ada95
standard? Or is it that existing implementations of it are a kludge?



>> Yes, well, functions work that way in Ada (fortunately, or
>> unfortunately, I don't know). I could have made it a procedure with an
>> "in out" parameter, but I like functional programming better.
>> Unfortunately, I haven't been able to do proper functional style
>> programming in Ada so far, having been thwarted by strong typing and
>> lack of "out" parameters in functions.
>
> Well, out parameters in functions are much desired by almost
> anybody, except the ARG members. (:-)) But that won't help. Try
> access parameters instead and you will see.

Alright... that sounds like an invitation to dance in a mine field. ;-)


> The problem is that an out parameter cannot "return" constraints as
> the proper result can.

[I suppose I can guess what you mean by this.] It's true that out
parameters don't cure all ills.


> function Op (...) return String is
> Result_Length : Natural;
> begin
> -- evaluate Result_Length
> declare
> Result : String (1..Result_Length);
> begin
> -- Fill Result
> return Result;
> end;
> end Op;
>
> Such operations can always be used as:
>
> declare
> X : String renames Op (...);
> begin
> -- Using X
> end;

That's handy, isn't it? Very nice. I must remember to experiment with
it.

David




--

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

On another level there is a principle laid down, much in line with
common sense and with the original American ideal, that governments
should never do what small bodies can accomplish: unions, credit
unions, cooperatives, St. Vincent de Paul Societies. Peter Maurin's
anarchism was on one level based on this principle of subsidiarity,
and on a higher level on that scene at the Last Supper where Christ
washed the feet of His Apostles. He came to serve, to show the new
Way, the way of the powerless. In the face of Empire, the Way of
Love.

-- Dorothy Day, The Catholic Worker, May 1972.

(Dorothy Day Library on the Web at
http://www.catholicworker.org/dorothyday/)
.



Relevant Pages

  • Re: TCP/IP Sockets with GNAT.Sockets
    ... It's not the sort of things i expected to write with Ada... ... > match the one for a String? ... > -- open your socket here, ... exchange messages with, say, a POP3 server? ...
    (comp.lang.ada)
  • GNAT.Sockets writing data to nowhere...
    ... I can connect to localhost:25 and conduct an SMTP session using ... Server_User: in String; ... -- Write Data to Socket. ... 22630 smtp_client RET setsockopt 0 ...
    (comp.lang.ada)
  • Re: Parallel processing questions
    ... instead of from ActiveX exe. ... Did you mean that while the server socket is sending ... > RealHostName As String ... >Private Type SMTPRemoteServer ...
    (microsoft.public.vb.general.discussion)
  • Winsock Control and Responding to Error Situations
    ... I have an application that impliments the winsock control in a control array ... So when I close the socket I call a function CloseSocket. ... Public Sub CloseSocket ... Dim strTag As String ...
    (microsoft.public.vb.controls.internet)
  • Re: Issue with multiple threads and System.Net.Sockets.Socket
    ... first off your app has a 200 loop count, I am pritty sure you can only throw ... private thd as system.threading.thread ... private eip as string ... Are you closing the socket out? ...
    (microsoft.public.dotnet.languages.vb)