Dec 232017
String functions in Turbo Pascal 4.0.
File STRINGS.ZIP from The Programmer’s Corner in
Category Pascal Source Code
String functions in Turbo Pascal 4.0.
File Name File Size Zip Size Zip Type
STRINGS.DOC 13122 3952 deflated
STRINGS.TPU 4560 2458 deflated
TXTFMT.PAS 1089 485 deflated

Download File STRINGS.ZIP Here

Contents of the STRINGS.DOC file

Turbo Pascal Rexx STRINGS Unit
(released 7/23/88)

STRINGS.TPU is a Turbo Pascal unit containing 24 string related
functions mostly implemented in assembler. These routines are robust,
idiot proof, highly optimized and utilize the 80x86 string-oriented
opcodes whenever possible. IBM mainframe hackers will notice that this
package, together with Turbo's built in string functions, is
substantially equivalent to the string routines available in IBM's Rexx
language. (Rexx, in case you haven't heard, is the slickest piece of
software to come out of IBM in many years)

This software is being offered under a shareware agreement. The typical
user is asked to pay $7. The fine print can be found elsewhere in this

Function Descriptions

All functions are implemented in assembler, unless otherwise noted.

function uppercase(str:string):string;
Folds the argument STR to uppercase.
Ex: uppercase('abcdef123') returns 'ABCDEF123'

function lowercase(str:string):string;
Folds the argument STR to lowercase.
Ex: lowercase('ABCDEF123') returns 'abcdef123'

function strip(str:string; opt,ch:char):string;
Strips leading and/or trailing CH characters from STR.
Setting OPT to L, T or B causes leading, trailing, or both
leading and trailing characters to be stripped.
Ex: strip(' abcdef ','L',' ') returns 'abcdef '
strip(' abcdef ','t',' ') returns ' abcdef'
strip(' abcdef ','b',' ') returns 'abcdef'
strip('++ abcdef +','B','+') returns ' abcdef '

function words(str:string):byte;
Returns the number of (blank delimited) words in the string STR.
Ex: words('two four six eight') returns 4.

function word(str:string; n:byte):string;
Returns the N'th (blank delimited) word from the string STR.
Ex: word('two air is humin',3) returns 'is'
word('two air is humin',5) returns ''

function subword(str:string; n,count:byte):string;
Returns COUNT words from STR, starting at the N'th word.
Embedded blanks are preserved. If COUNT is 0, all words from
the N'th word to the end of STR are returned.
Ex: subword('one two three four',2,3) returns 'two three four'
subword('one two three four',2,0) returns 'two three four'
subword('one two three ',2,3) returns 'two three'
subword('one two three ',4,2) returns ''

function lastpos(findstr,instr:string; start:byte):byte;
Returns the position of the last occurrance of FINDSTR in INSTR,
searching backwards from the character position START. If START
is 0, the search begins at the end of INSTR. Returns 0 if the
string is not found.
Ex: lastpos('he','he was the best',15) returns 9.
lastpos('he','he was the best',6) returns 1.
lastpos('he','he was the best',0) returns 9.
lastpos('he','he was the best',1) returns 0.

function firstpos(findstr,instr:string; start:byte):byte;
This function was included for completeness. It works exactly
the same way as Turbo's built in POS function, except for the
presence of the START option. It is equivalent to:
except for being more efficient.
Ex: firstpos('he','he was the best',15) returns 0.
firstpos('he','he was the best',6) returns 9.
firstpos('he','he was the best',0) returns 1.
firstpos('he','he was the best',1) returns 1.

function left(str:string; width:byte; pad:char):string;
Returns STR left justified in a field of width WIDTH, padded out
with PAD characters.
Ex: left('hello',10,'=') returns 'hello====='
left('hello there',10,'=') returns 'hello ther'

function right(str:string; width:byte; pad:char):string;
Returns STR right justified in a field of width WIDTH, padded out
with PAD characters.
Ex: right('hello',10,'>') returns '>>>>>hello'
right('hello there',10,'>') returns 'ello there'

function copies(str:string; count:byte):string;
Returns COUNT copies of STR concatenated together.
Ex: copies('----+',4) returns '----+----+----+----+'
If the length of n copies of STR would exceed 255, n-1 copies
are returned.

function center(str:string; width:byte; pad:char):string;
Returns STR centered in a string of length WIDTH, padded out
with PAD chars.
Ex: center('ABC',8,' ') returns ' ABC '
center(' ABC ',8,'-') returns '- ABC --'
center('ABCDE',3,'-') returns 'ABC'

function overlay(new,str:string; pos:byte):string;
Returns the string STR, overlayed by the string NEW, starting
at character position POS. If length(new)+pos>255, new is
truncated to protect the stack.
Ex: overlay('aygu','Ronald Reagan',9) returns 'Ronald Raygun'
overlay('abc','xyz',5) returns 'xyz abc'

function translate(str,intable,outable:string):string;
Returns STR after translation via the map INTABLE->OUTABLE.
In other words, each occurrance in STR of the i'th character
in INTABLE is replaced by the i'th character in OUTABLE.
Ex: translate('ABC BDE',' BCF','XYZ ') returns 'AYZXYDE'
INTABLE and OUTABLE should be of the same length.

function compare(s1,s2:string):byte;
Compares S1 to S2 and returns the position of the first
characters which don't match, or 0 if all characters match.
Ex: compare('hello','hello there') returns 6.
compare('hello','hexlo') returns 3.
compare('hello','hello') returns 0.

function pos2word(str:string; pos:byte):byte;
Returns the number of the word in STR pointed to by POS. If
POS points to a blank, the number of the following word is
returned. If POS points after the last word or end of STR,
or POS is 0, then 0 is returned.
Ex: pos2word('abc def ghi ',4) returns 2.
pos2word('abc def ghi ',6) returns 2.
pos2word('abc def ghi ',11) returns 3.
pos2word('abc def ghi ',12) returns 0.
pos2word('abc def ghi ',0) returns 0.

function word2pos(str:string; wrd:byte):byte;
Returns the position in STR of the first character in the WRD'th
word. If WRD>WORDS(STR) or WRD=0, returns 0.
Ex: word2pos(' abcd e fghi jk',2) returns 8.
Note that this function is equivalent to Rexx's WORDINDEX

function space(str:string; spc:byte):string;
Returns STR with each word separated by SPC blanks. String
bounds checking protects the stack.
Ex: space(' here we go again ',0) returns 'herewegoagain'
space(' here we go again ',1) returns 'here we go again'

function verify(str,ref:string; opt:char; start:byte):byte;
Returns the position of the first character in STR (after START)
which matches/doesn't match a character in REF. Setting OPT to
'M' or 'N' returns matching or non-matching character positions,
Ex: verify('abcd1ef','0123456789','M',0) returns 5.
verify('123a125','0123456789','n',0) returns 4.

function xrange(c1,c2:char):string;
Returns a string containing all characters from C1 to C2
inclusive. xrange(#0,#255) returns xrange(#0,#254) to protect
the stack.
Ex: xrange('a','h') returns 'abcdefgh'

function reverse(str:string):string;
Returns contents of STR in reverse order.
Ex: reverse(xrange('a','h')) returns 'hgfedcba'

The following functions are implemented in Pascal and use one or more
of the previous routines.

function abbrev(str,abbr,len):boolean;
Returns true if ABBR is an 'acceptable' abbreviation for STR.
The criterion is:
length(ABBR)>=LEN and ABBR=left(STR,length(ABBR),' ')
Normally, LEN should be set <=length(STR).
Ex: abbrev('DELETE','DEL',3)=true

function delword(str:string; n,len:byte):string;
Returns STR with LEN words deleted, starting at word N.
Ex: delword('here we go again',2,2) returns 'here again'

function justify(str:string; len:byte):string;
Inserts blanks between words in STR so that length(STR)=LEN.
If length(space(STR,1))>=LEN, justify just returns space(STR,1).
Ex: justify(' a b cd ef ',10)='a b cd ef'

Usage notes:

Note that Rexx's FIND and WORDLENGTH functions can be readily
synthesized using functions in this package:

{find returns the number of the word in str1 where str2 starts}
find(str1,str2) ::= pos2word(str1,firstpos(str2,str1,0))

{wordlength returns the length of a word in str}
wordlength(str,n) ::= length(word(str,n))


Capitalize the first char in each word in a string S:
for i:=1 to words(S);

Find the lowest-ordered alphabetic character in a string STR
of uppercase characters:

Find the highest-ordered alphabetic character in a string STR
of uppercase characters:

Replace non-alphabetic chars with blanks in a string S:
xrange('[','`')+xrange('{',''),left('',33,' '));
left('',52,' ')),left('',ord('')-ord('!')+1,' '));

Generate a string S consisting of chars between '0' and 'z', none of
which occur in an alphanumeric string STR:
S:=space(translate(xrange('0','z'),STR,left('',length(STR),' ')),0);

Generate a string S, each of who's characters occurs at least
once in an alphanumeric string STR:
STR,left('',length(STR),' ')),left('',75,' ')),0);

A simple text formatting example is in txtfmt.pas.

The Fine Print

STRINGS.TPU is copyright 1988 by Richard Winkel. You are encouraged
to try it out and share it with others, but if you like it enough to
use it, you should express your appreciation by helping defray the
cost of its development. I won't belabor the point .. if you're honest,
you know what I'm talking about.

The payment schedule is:

$15 for ASM and PAS source. This package should be readily
portable to other languages.
$0 for inclusion of STRINGS.TPU in a public domain program.
$30 for inclusion of STRINGS.TPU in another shareware program.
Other types of commercial applications are negotiable .. send
inquiries to the address below.

Send cash, check or money order to:

Richard Winkel
Route 1, box 193
Harrisburg, MO. 65256

Include a 360K formatted diskette in a self-addressed, stamped mailer
if you wish to purchase the source.

------------------------------- cut here ---------------------------

Calling Syntax Guide to STRINGS.TPU

function abbrev(str,abbr:string; len:byte):boolean;
function center(s:string; width:byte; pad:char):string;
function compare(s1,s2:string):byte;
function copies(str:string; count:byte):string;
function delword(str:string; n,len:byte):string;
function firstpos(findstr, instr:string; start:byte):byte;
function justify(str:string; len:byte):string;
function lastpos(findstr,instr:string; start:byte):byte;
function left(str:string; width:byte; pad:char):string;
function lowercase(s:string):string;
function overlay(new,str:string; pos:byte):string;
function pos2word(s:string; pos:byte):byte;
function reverse(str:string):string;
function right(str:string; width:byte; pad:char):string;
function space(str:string; spc:byte):string;
function strip(s:string; opt,ch:char):string; {opt='L', 'T', or 'B'}
function subword(str:string; n,count:byte):string;
function translate(str,intable,outable:string):string;
function uppercase(s:string):string;
function verify(str,ref:string; opt:char; start:byte):byte; {opt='N' or 'M'}
function word(s:string; n:byte):string;
function word2pos(s:string; wrd:byte):byte;
function words(s:string):byte;
function xrange(c1,c2:char):string;

 December 23, 2017  Add comments

Leave a Reply