monching@quiche.cs.mcgill.ca (Patrick WONG) (02/02/90)
I am having problems in figuring out what is the proper way to copy
a pascal sting in think C. The only way I know is converting the pascal
sting to a C string then doing a standard c strcpy and then convert the
c strings back to pascal strings. This pretty nasty seeing that in
pascal we just have to use a simple assignment with an atomic operator
(eg. thisString := thatString).
If anyone has a better way please let me know through e-mail. Thanks.
--
patrick wong | McGill University | \\//
monching@quiche.cs.mcgill.ca | School of Computer Science | ||
| Montreal, Quebec | --
d88-jwa@nada.kth.se (Jon Watte) (02/02/90)
In article <2083@quiche.cs.mcgill.ca> monching@quiche.cs.mcgill.ca (Patrick WONG) writes: >I am having problems in figuring out what is the proper way to copy >a pascal sting in think C. The only way I know is converting the pascal Str255 p1, p2; memmove(p2, p1, *p1 + 1); /* copies pascal string p1 to p2 */ Memmove is a) available in the ANSI library b) available as source file (Thank GOD for source files for the libraries !) c) not hard to implement by yourself. h+ -- --- Stay alert ! - Trust no one ! - Keep your laser handy ! --- h+@nada.kth.se == h+@proxxi.se == Jon Watte longer .sig available on request
oster@dewey.soe.berkeley.edu (David Phillip Oster) (02/04/90)
In article <2826@draken.nada.kth.se> d88-jwa@nada.kth.se (Jon W{tte) writes: >memmove(p2, p1, *p1 + 1); /* copies pascal string p1 to p2 */ ^ This is NOT the right way to get the length of a pascal string. I've has subtle bugs happen on long strings. Many people say, but pascal strings are unsigned chars, so the zeroth one is already in the range 0..255, isn't that right? Well, it is, until you try to calculate with them. For exmaple, suppose you do the following: StringPtr p = (StringPtr) ""; now, what is *p - 1? The answer: 0xFFFF. If you say (long) (*p - 1), you get 0x0000FFFFL because of C's promotion rules for expressions involving unsigned operands. The right way to take the length of a pascal string is: #define Length(s) ((short) (unsigned char) (s)[0]) The second cast forces the result to be a signed integer in the range 0..255. Now, (long) (Length(p) - 1) is -1L, as it should be. Write it right, and you only have to write it once. > The mac is a detour in the inevitable march of mediocre computers. > drs@bnlux0.bnl.gov (David R. Stampf) --- David Phillip Oster -master of the ad hoc odd hack. Arpa: oster@dewey.soe.berkeley.edu Uucp: {uwvax,decvax}!ucbvax!oster%dewey.soe.berkeley.edu
d88-jwa@nada.kth.se (Jon Watte) (02/05/90)
In article <34105@ucbvax.BERKELEY.EDU> oster@dewey.soe.berkeley.edu.UUCP (David Phillip Oster) writes: >In article <2826@draken.nada.kth.se> d88-jwa@nada.kth.se (Jon W{tte) writes: >>memmove(p2, p1, *p1 + 1); /* copies pascal string p1 to p2 */ > ^ This is NOT the right way to get the length of a pascal This is, of course, the right and _efficient_ way to do _this_ operation. It doesn'n fail, given two legal pascal strings. I suppose I could even prove it, strongly. Of course, the rest of the article is right about _that_ special case, but that doesn't happen in this case. h+ -- --- Stay alert ! - Trust no one ! - Keep your laser handy ! --- h+@nada.kth.se == h+@proxxi.se == Jon Watte longer .sig available on request
dwb@archer.apple.com (David W. Berry) (02/07/90)
In article <2834@draken.nada.kth.se> d88-jwa@nada.kth.se (Jon W{tte) writes: >In article <34105@ucbvax.BERKELEY.EDU> oster@dewey.soe.berkeley.edu.UUCP (David Phillip Oster) writes: >>In article <2826@draken.nada.kth.se> d88-jwa@nada.kth.se (Jon W{tte) writes: >>>memmove(p2, p1, *p1 + 1); /* copies pascal string p1 to p2 */ >> ^ This is NOT the right way to get the length of a pascal > >This is, of course, the right and _efficient_ way to do _this_ operation. >It doesn'n fail, given two legal pascal strings. I suppose I could even >prove it, strongly. > >Of course, the rest of the article is right about _that_ special case, >but that doesn't happen in this case. Actually, it's not such a special case. A legal pascal string may have up to 255 characters. Any more than 127 is going to cause a sign extension problem. Remember, there defined using char, not unsigned char. All of a sudden it's a much rarer problem...
pratt@boulder.Colorado.EDU (Jonathan Pratt) (02/08/90)
In article <6548@internal.Apple.COM> dwb@archer.apple.com (David W. Berry) writes: > Actually, it's not such a special case. A legal pascal string >may have up to 255 characters. Any more than 127 is going to cause a >sign extension problem. Remember, there defined using char, not unsigned >char. All of a sudden it's a much rarer problem... No, in THINK C you have typedef unsigned char Str255[256]; so there isn't sign extension. Jonathan /* Jonathan Pratt Internet: pratt@boulder.colorado.edu * * Campus Box 525 uucp: ..!{ncar|nbires}!boulder!pratt * * University of Colorado * * Boulder, CO 80309 Phone: (303) 492-4293 */
tim@hoptoad.uucp (Tim Maroney) (02/08/90)
In article <6548@internal.Apple.COM> dwb@archer.apple.com (David W. Berry) writes: >> Actually, it's not such a special case. A legal pascal string >>may have up to 255 characters. Any more than 127 is going to cause a >>sign extension problem. Remember, there defined using char, not unsigned >>char. In article <16666@boulder.Colorado.EDU> pratt@boulder.Colorado.EDU (Jonathan Pratt) writes: >No, in THINK C you have > >typedef unsigned char Str255[256]; > >so there isn't sign extension. The same in MPW C 3.0. Perhaps our colleagues are thinking of older versions? -- Tim Maroney, Mac Software Consultant, sun!hoptoad!tim, tim@toad.com Feminism that refuses to use the word "patriarchy" is kin to abolitionism that refuses to use the word "slavery".