RE: Question about STRINGs...
"Phil Malin" <[email protected]> Wed, 1 Jun 2005 09:55:15 +1000
| Newsgroups | gmane.comp.lang.eiffel.smalleiffel |
|---|---|
| Message-ID | <[email protected]> |
Howdy. Thanks a lot for the info José - I'll give it a try. -----Original Message----- From: José Bollo [mailto:[email protected]] Sent: Tue 5/31/2005 6:20 PM To: [email protected] Cc: Subject: Re: Question about STRINGs... Le mardi 31 Mai 2005 08:46, Phil Malin a écrit : > Hi there. > > I've been writing some network code (using my own network library I > wrote ages ago) and have come across a bit of an annoyance (more than > likely brought about by my own lack of knowledge). In my code I > currently use an array of characters as a read buffer rather than a > STRING because when I originally wrote it I wasn't sure how STRINGs > would handle arbitrary CHARACTERs (like nulls, etc.). From looking at > the STRING code it seems as though it'd be ok because it uses a > separately defined 'count' to state the size rather than a trailing '\0' > as in C. Is this correct? > > If this is the case, what would be the best way of copying arbitrary > data in a C array into a STRING (since a lot of the time the user will > be wanting the result as a STRING). The only routines in STRING are > 'from_external' and 'from_external_copy', which seem to copy until a > '\0' is hit (which won't be of help to me here). > > The easy approach is to simply create a STRING with a certain capacity > and then loop through the buffer of read data appending each character > at a time (which is pretty inefficient). That approach allow you to avoid copies. statically 1. reserve the capacity you want: string.make(max) 2. inherit STRING_HANDLER dynamically 3. resize the string to the wanted max buffer read: string.resize(max) 4. read using pointer: string.storage.to_external 5. resize the string on what you read: string.resize(readen) remarks: CAUTION you are at LOW level the effect of resize is to give a FULLY useable STRING object but if it is more usefull for you to not resize then do it because it can avoid some unusefull operations. > Is there any possibility to add a creation routine to STRING such as > > copy_external_sequence(ptr : POINTER;size : INTEGER) > > > or something so one can copy some arbitrary data pointed to by 'ptr' in > an efficient way (by having it use bcopy/memcpy/etc.)? > > Thanks in advance for any pointers (pardon the pun ;-) ). > > Cheers. > > Phil.