[Q] String>>withBlanksTrimmed
Michael Roberts
mike at mjr104.co.uk
Fri Dec 12 10:41:18 UTC 2003
Hi Lex,
thanks for your response.
>
> Seems odd -- can you describe a situation where a *text* string in
> Squeak would end up with a null character? I am imagining you have
> bumped into one since you are asking!
>
Yes, I was thinking it was probably a bit odd too since the null character is a C thing really.
The first example is rather a silly one but in my newbie mode I stumbled over it.
String ofSize: 4 -> ' '
String gets this from Collection. Should there be a trap in there to stop you doing that? I guess people just don't do that... but I did having discovered ByteArray ofSize:.
Here is a better example.
| socket result |
socket := Socket newTCP.
socket connectToHostNamed:'mike' port: 554.
socket sendData: RTSPAnnounce example2.
result := ByteArray ofSize:1000.
socket receiveDataInto: result.
result asString inspect
I am playing with Squeak to test some RTP/RTSP stuff. (Squeak is SO amazing for this!). I obviously get the nulls because I have created an arbitrary sized array and then converted that into a string. That is not a particularly smart thing to do but I'd only discovered the methods about 5mins before. (I couldn't contain my excitement! Playing with protocols in < 10 loc just rocks.)
After I posted the question, and reading more classes, I converted the last bit to
result := ByteArray ofSize: 1000.
socket receiveDataInto: result.
(result select: [:c | c ~= 0]) asString
I guess I could have used reject: as well. Anyway, so if I want to grab an RTSP Announce I can do a little something like
| socket result |
socket _ Socket newTCP.
socket listenOn: 554.
socket
waitForConnectionFor: 10
ifTimedOut: [self error: 'Socket timed out'].
result _ ByteArray ofSize: 1000.
socket receiveDataInto: result.
(result select: [:c | c ~= 0]) asString
Ideally, I would like to use SocketStream and I have been playing around with this. I haven't yet figured out its blocking/timeout nature and how to interact with that.
>
> Overall, my inclination would be to leave withBlanksTrimmed alone, but
> perhaps to add a new method that does what you want. Nulls don't seem
> like blanks; they don't even seem like valid characters at all. But
> it's hard to say; it's hard to think about a character that shouldn't
> even be there to begin with.
>
Yes I should have given you more information... I guess the actual question should have been "should I be seeing nulls in my String" and you have helped me answer that.
> By the way, realize that you can do things like translate null's into
> spaces, if you want. See String>>translateWith: .
cool, I'll look at that. That was one of the reasons for asking - thanks!
Cheers
Mike
More information about the Squeak-dev
mailing list
|