home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.sys.mac.programmer
- Path: sparky!uunet!mcsun!ub4b!info-sparc1.info.ucl.ac.be!NewsWatcher
- From: Meessen@slig.ucl.ac.be (Christophe Meessen)
- Subject: Re: annoying pascal strings
- Message-ID: <Meessen-140892134803@130.104.58.11>
- Followup-To: comp.sys.mac.programmer
- Sender: news@info.ucl.ac.be (News Administrator)
- Nntp-Posting-Host: 130.104.58.11
- Organization: Universite Catholique de Louvain (Belgium)
- References: <1992Aug10.132907.23439@das.harvard.edu> <zben-100892215906@zben-mac-ii.umd.edu> <1992Aug13.174942.18261@kronos.arc.nasa.gov>
- Date: Fri, 14 Aug 1992 12:21:49 GMT
- Lines: 106
-
- In article <1992Aug13.174942.18261@kronos.arc.nasa.gov>,
- joshr@kronos.arc.nasa.gov (Joshua Rabinowitz-Summer-91) wrote:
- >
- > Some common C functions: these modify the string in place.
- > ------------
- > here is header
- > char * CToPStr(char *aCStr);
- > char * PToCStr(char *aPStr);
- >
- > =
- > ------------
- > here is source
- > ------------
- >
- >
- >
- > #include "StrFuncs.h"
- > #include <string.h>
- > #include <Global.h>
- >
- >
- > char * CToPStr(char *aCStr)
- > {
- > long i, len;
- > len = strlen(aCStr);
- > for (i=len; i>0; i--)
- > aCStr[i] = aCStr[i-1];
- >
- > aCStr[0] = Min(255, len);
- > return aCStr;
- > }
- >
-
- Using pointers is faster.
-
- unsigned char * CToPStr( unsigned char *aCStr ) {
- unsigned char i, *srcPtr, *dstPtr;
-
- // scan the line until meets a '\0' or reach PStr limit
- for( i = 0, srcPtr = aCStr; (*srcPtr != '\0') && (i < 255 ); i++ )
- srcPtr++;
-
- // shift the string one char
- dstPtr = srcPtr--;
- while( srcPtr >= aCStr )
- *dstPtr-- = *srcPtr--;
-
- // store the string size
- *aCStr = i;
- }
-
- Using Apple toolbox routines it becomes:
-
- unsigned char * CToPStr( unsigned char *aCStr ) {
- unsigned char i, *dstPtr;
-
- // scan the line until meets a '\0' or reach PString limit
- for( i = 0, dstPtr = aCStr; (*dstPtr != '\0') && (i < 255 ); i++ )
- dstPtr++;
-
- // shift the string one char
- BlockMove( aCStr, aCStr+1, i );
-
- // store the string size
- *aCStr = i;
- }
-
-
- >
- > char *PToCStr(char *aPStr)
- > {
- > long i, len;
- > len = *aPStr;
- > for (i=0; i<len; i++)
- > aPStr[i] = aPStr[i+1];
- > aPStr[len] = '\0';
- > return aPStr;
- > }
-
-
- here it's simpler:
-
- unsigned char * PToCStr( char * aPStr ) {
- unsigned char i, srcPtr, dstPtr;
-
- srcPtr++ = dstPtr = aPStr;
- for( i = *aPStr; i ; i-- )
- *dstPtr++ = *srcPtr++;
- *dstPtr = "\0";
- }
-
- or using the ToolBox
-
- unsigned char * PToCStr( char * aPStr ) {
- unsigned char i, srcPtr, dstPtr;
-
- i = *aPStr;
- BlockMove( aPStr, aPstr + 1, (long)*aPStr );
- aPStr + i = i;
- }
-
-
-
- Bien cordialement,
-
- Christophe MEESSEN
-