home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / lang / pascal / 5189 < prev    next >
Encoding:
Internet Message Format  |  1992-09-04  |  2.1 KB

  1. Path: sparky!uunet!noc.near.net!ds5000!cschmidt
  2. From: cschmidt@ds5000.DAC.Northeastern.edu (Christopher Schmidt)
  3. Newsgroups: comp.lang.pascal
  4. Subject: LENGTH and POS in TP 5.5
  5. Message-ID: <7351@ds5000.DAC.Northeastern.edu>
  6. Date: 4 Sep 92 04:22:23 GMT
  7. Organization: Northeastern University, Boston MA. USA 02115
  8. Lines: 62
  9.  
  10. Here is a problem with the LENGTH function in TP 5.5 that cost me
  11. several hours of debugging.  After discovering the source of the
  12. problem, I investigated the POS function as well.
  13.  
  14. The TP 5.5 documentation says the LENGTH function returns a value
  15. of type integer.  Apparently, it actually returns a value of type
  16. byte.  If the return value were of type integer, then the
  17. following program would display the numbers 256 and -1.  Instead,
  18. the program displays the values 0 and 255.
  19.  
  20.     var
  21.         s : string;
  22.     begin
  23.         s [0] := #255;
  24.         writeln (succ (length (s)));
  25.  
  26.         s [0] := #0;
  27.         writeln (pred (length (s)));
  28.     end.
  29.  
  30. The following two workarounds produce exactly identical code.
  31.  
  32.     Workaround 1: Typecast the result of LENGTH to integer.
  33.  
  34.         succ (integer (length (s)))
  35.         pred (integer (length (s)))
  36.  
  37.     Workaround 2: Use +1 and -1 instead of SUCC and PRED.
  38.  
  39.         length (s) + 1
  40.         length (s) - 1
  41.  
  42. The TP 5.5 documentation says the POS function returns a value of
  43. type byte.  Apparently, it actually returns a value of type
  44. integer.  If the return value were of type byte, then the
  45. following program would display the numbers 0 and 256.  Instead,
  46. the program displays the values 256 and -1.
  47.  
  48.     var
  49.         s : string;
  50.     begin
  51.         fillchar (s, sizeof (s), 255);
  52.  
  53.         s [255] := 'A';
  54.         writeln (succ (pos ('A', s)));
  55.  
  56.         s [255] := ' ';
  57.         writeln (pred (pos ('A', s)));
  58.     end.
  59.  
  60. This program compiles to exactly the same code as when the POS
  61. function is typecast to integer, or when +1 and -1 are used
  62. instead of SUCC and PRED.
  63.  
  64. NOTE TO BORLAND: When you fix these and other problems, please
  65. let use know, so we can determine whether your fix invalidates
  66. our workarounds.  Thank you.
  67.  
  68. Christopher Schmidt
  69. Waltham, Massachusetts, USA
  70. cschmidt@lynx.northeastern.edu
  71.  
  72.