Newsgroups: comp.lang.pascal
Path: utzoo!utgpu!watserv1!watmath!nmouawad
From: nmouawad@watmath.waterloo.edu (Naji Mouawad)
Subject: Re: Long strings in TP
Message-ID: <1991Feb16.045747.9629@watmath.waterloo.edu>
Organization: University of Waterloo
References: <1991Feb15.162333.24408@cunixf.cc.columbia.edu> <30409@usc>
Date: Sat, 16 Feb 1991 04:57:47 GMT
Lines: 50

In article <30409@usc> ajayshah@almaak.usc.edu (Ajay Shah) writes:
>In article <1991Feb15.162333.24408@cunixf.cc.columbia.edu> stone@cunixb.cc.columbia.edu (Glenn Stone) writes:
>>Pardon me if this is a common question, but:
>>
>>What's the best way to implement a long string variable, say 600 chars?
>
>Option 1:
>	C-like.  Use null-terminated strings.  Flexible but you
>	lose all strong typing and Pascal compiler protection.  
>
>Option 2:
>	Pascal-like.  Use a word for the length counter.  Something
>	like
>
>	longstring = record
>	     length:word
>	     data:array[1..600] of char
>	end;
>
>	Advantage: close to Pascal.  
>	Disadvantages: relatively inflexible, could easily be
>	wasteful of memory.

Option 3.

      Type
        Pstring = ^string;
        LongString = Record
            TheString : Array[1..Max] of Pstring;
            Case boolean of
                 True : (MyOffset, MySegment : byte);
                 False: (MyLength            : Word);
        end; {LongString}

The trick is as follows: In order to access a character at position
n in LongString, you do TheString[n div 256][n mod 256].
Mylength hold the length of TheString, while MyOffset tells you
how many Pstrings are in use and MySegment tells you the length
of TheString[MyOffset].

Not an ideal the solution but it is both flexible and secure.

--Naji.
      
      
-- 
     -------------------------------------------------------------------
    | Naji Mouawad  |          nmouawad@watmath.waterloo.edu            |
    |  University   |---------------------------------------------------|
    | Of Waterloo   |   "The Stranger in us is our most familiar Self"  |
