[comp.lang.pascal] String Lengths

CDCKAB%EMUVM1.BITNET@cunyvm.cuny.edu ( Karl Brendel) (06/19/91)

In article <campbell.677302720@cutmcvax>, campbell@cutmcvax.cs.curtin.edu.au
  (Trevor George Campbell CC361) wrote:

>>stone@cunixb.cc.columbia.edu (Glenn Stone) writes:
>
>[...deleted...]
>
>>               delete(t,length(t),1);                       ^----here
>
>[...deleted...]
>
>One further point that may speed up the program (a minute amount) would be
>to replace the delete(t,...) above with:   t[0] := CHR(ORD(t[0]) - 1) as turbo
>just stores the length of a STRING in position 0. This is a useful hint if you
>need to use the LENGTH of a string a lot (just look at position 0 of the String
>BUT unfortunately this is only in a character form (not a byte or integer)

In the last few releases (since TP4? TP5?), TPas has produced the
same code for assignments using the Length() approach as for those
accessing the [0] byte directly. From 5.5 (courtesy of TD 2.01):

PROGRAM.9:  b := Length(s);
  cs:0008A03E00         mov    al,[PROGRAM.S]
  cs:000B A23E01         mov    [PROGRAM.C],al
PROGRAM.10:  c := s[0];
  cs:000E A03E00         mov    al,[PROGRAM.S]
  cs:0011 A23E01         mov    [PROGRAM.C],al

In the example cited here, a better approach is to Dec the [0] byte
directly (IFF the byte is _known_ not to be 0!). Again:

PROGRAM.12:  s[0] := Chr(Ord(s[0])-1);
  cs:0014 A03E00         mov    al,[003E]
  cs:0017 30E4           xor    ah,ah
  cs:0019 48             dec    ax
  cs:001A A23E00         mov    [003E],al
PROGRAM.13:  Dec(s[0]);
  cs:001D FE0E3E00       dec    byte ptr [003E]

Of course, both of these do "blow away" the call to the Delete
procedure in this special case, even if we ignore the Delete code
itself:

PROGRAM.15:  Delete(s,Length(s),1);
  cs:0021 BF3E00         mov    di,003E
  cs:0024 1E             push   ds
  cs:0025 57             push   di
  cs:0026 A03E00         mov    al,[003E]
  cs:0029 30E4           xor    ah,ah
  cs:002B 50             push   ax
  cs:002C B80100         mov    ax,0001
  cs:002F 50             push   ax
  cs:0030 9AB8034362     call   6243:03B8


As you almost certainly know, accessing the [0] byte is not
guaranteed to work in future versions, so all of us who do it trade
off minor speed improvements now for the risk of future maintenance
problems.

Cheers--                        --Karl

+====================================================================+
| Karl Brendel                           Centers for Disease Control |
| Internet: CDCKAB@EMUVM1.BITNET         Epidemiology Program Office |
| Bitnet: CDCKAB@EMUVM1                  Atlanta GA  30333       USA |
|                        Home of Epi Info 5.0                        |
+====================================================================+