[comp.lang.pascal] Long strings in TP

stone@cunixb.cc.columbia.edu (Glenn Stone) (02/16/91)

Pardon me if this is a common question, but:

What's the best way to implement a long string variable, say 600 chars?

-------------------------------------------------------------------------
  Glenn Davis Stone                BITNET stone@cunixc
  Columbia University            INTERNET stone@cunixb.cc.columbia.edu
-------------------------------------------------------------------------

ajayshah@almaak.usc.edu (Ajay Shah) (02/16/91)

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.


-- 
_______________________________________________________________________________
Ajay Shah, (213)734-3930, ajayshah@usc.edu
                              The more things change, the more they stay insane.
_______________________________________________________________________________

dmurdoch@watstat.waterloo.edu (Duncan Murdoch) (02/16/91)

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?

That depends on what you want to do with it.  If you don't need to
know the length, and it doesn't change much, and you don't use nulls in
the strings, probably C-style null terminated strings are best.  Turbo
and Object Professional from TurboPower provide units to handle these, but
they aren't hard to write yourself.

If you want to do a lot of manipulation on the strings, a record with a
length word at the start would probably be best, something like

 type 
  longstring = 
  record
   length : word;
   chars  : array[1..600] of char;
  end;

Again you'll have to write (or find) routines to do all the usual operations
on these, since the TP library doesn't have them.

If you allocate these in the heap, you can allocate just as much as you need,
and easily deallocate them, using GetMem and FreeMem.

Hope this helps.

Duncan Murdoch
dmurdoch@watstat.waterloo.edu

nmouawad@watmath.waterloo.edu (Naji Mouawad) (02/16/91)

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"  |

ts@uwasa.fi (Timo Salmi) (02/16/91)

In article <1991Feb15.162333.24408@cunixf.cc.columbia.edu> stone@cunixb.cc.columbia.edu (Glenn Stone) writes:
>
>What's the best way to implement a long string variable, say 600 chars?

Besides the obvious
  type longString = record
                      length : word; 
                      ch     : array [1..600] of char;
                    end;
see O'Brien, Turbo Pascal, the Complete Reference for lots of
longstring procedures.

...................................................................
Prof. Timo Salmi        
Moderating at garbo.uwasa.fi anonymous ftp archives 128.214.12.37
School of Business Studies, University of Vaasa, SF-65101, Finland
Internet: ts@chyde.uwasa.fi Funet: gado::salmi Bitnet: salmi@finfun

stone@cunixb.cc.columbia.edu (Glenn Stone) (02/17/91)

In article <1991Feb15.205214.28291@maytag.waterloo.edu> dmurdoch@watstat.waterloo.edu (Duncan Murdoch) writes:
>In article <1991Feb15.162333.24408@cunixf.cc.columbia.edu> stone@cunixb.cc.columbia.edu (Glenn Stone) writes:
>>What's the best way to implement a long string variable, say 600 chars?
>
>That depends on what you want to do with it.  If you don't need to

I should have explained this at the outset.  I want to be able to tack 
other strings onto it either with CONCAT or STRING := STRING + NEWSTRING,
and I have to be able to write it to a text file.  So the ARRAY OF CHAR
solutions you and others suggested won't work.

What I have done so far is define an overflow string, which gives me
510 characters total, but I really need 600 chars, and having two
overflow strings and all the attendant overhead seems sort of messy.
But if there's no way to avoid using multiple strings, I guess I
can put them in a queue and keep things fairly neat.

>know the length, and it doesn't change much, and you don't use nulls in
>the strings, probably C-style null terminated strings are best.  Turbo
                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Could you explain?  No hablo C.

Thanks.

-------------------------------------------------------------------------
  Glenn Davis Stone                BITNET stone@cunixc
  Columbia University            INTERNET stone@cunixb.cc.columbia.edu
-------------------------------------------------------------------------