home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / pascal / library / dos / strings / faststr / strings.doc < prev    next >
Encoding:
Text File  |  1989-12-18  |  19.9 KB  |  432 lines

  1.                      Turbo Pascal Rexx STRINGS Unit
  2.                 Version 1.21 for Turbo Pascal 4.0, 5.0 & 5.5
  3.  
  4.       STRINGS.TPU is a Turbo Pascal unit containing 30 string functions
  5.       implemented in assembler.  These routines are fast (check out the
  6.       benchmark program) and powerful.  IBM mainframe users will notice
  7.       that this package is substantially equivalent to the string
  8.       routines available in IBM's Rexx language.
  9.  
  10.       Three versions are included:  STR40.TPU, STR50.TPU and STR55.TPU
  11.       for TPas versions 4.0, 5.0 and 5.5, respectively.  Be sure to
  12.       rename the appropriate file to STRINGS.TPU before usage.
  13.  
  14.                         Function Descriptions
  15.                         ---------------------
  16.  
  17. function left(str:string; width:byte; pad:char):string;
  18.          Returns STR left justified in a field of width WIDTH, padded out
  19.          with PAD characters.
  20.          Ex: left('hello',10,'=') returns 'hello====='
  21.              left('hello there',10,'=') returns 'hello ther'
  22.  
  23. function right(str:string; width:byte; pad:char):string;
  24.          Returns STR right justified in a field of width WIDTH, padded out
  25.          with PAD characters.
  26.          Ex: right('hello',10,'>') returns '>>>>>hello'
  27.              right('hello there',10,'>') returns 'ello there'
  28.  
  29. function center(str:string; width:byte; pad:char):string;
  30.          Returns STR centered in a string of length WIDTH, padded out
  31.          with PAD chars, or truncated on the right to fit within WIDTH.
  32.          Ex: center('ABC',8,' ') returns '  ABC   '
  33.              center(' ABC ',8,'-') returns '- ABC --'
  34.              center('ABCDE',3,'-') returns 'ABC'
  35.          This function is useful for centering text headings.
  36.  
  37. function strip(str:string; opt,ch:char):string;
  38.          Strips leading and/or trailing CH characters from STR.
  39.          Setting OPT to L, T or B causes leading, trailing, or both
  40.          leading and trailing characters to be stripped.
  41.          Ex: strip('   abcdef  ','L',' ') returns 'abcdef  '
  42.              strip('   abcdef  ','t',' ') returns '   abcdef'
  43.              strip('   abcdef  ','b',' ') returns 'abcdef'
  44.              strip('++ abcdef +','B','+') returns ' abcdef '
  45.  
  46. function lastpos(findstr,instr:string; start:byte):byte;
  47.          Returns the position of the last occurrance of FINDSTR in INSTR,
  48.          searching backwards from the character position START.  If START
  49.          is 0, the search begins at the end of INSTR.  Returns 0 if the
  50.          string is not found.
  51.          Ex: lastpos('he','he was the best',15) returns 9.
  52.              lastpos('he','he was the best',6) returns 1.
  53.              lastpos('he','he was the best',0) returns 9.
  54.              lastpos('he','he was the best',1) returns 0.
  55.  
  56. function firstpos(findstr,instr:string; start:byte):byte;
  57.          This function was included for completeness.  It works exactly
  58.          the same way as Turbo's built in POS function, except for the
  59.          presence of the START option.  It is equivalent to:
  60.          start-1+pos(findstr,copy(instr,start,length(instr)-start+1));
  61.          except for being more efficient.
  62.          Ex: firstpos('he','he was the best',15) returns 0.
  63.              firstpos('he','he was the best',6) returns 9.
  64.              firstpos('he','he was the best',0) returns 1.
  65.              firstpos('he','he was the best',1) returns 1.
  66.          While being similar to TP's pos function, firstpos is up to 10
  67.          times faster, depending on the version of TP and the specific
  68.          strings involved.  About twice as fast as the Boyer-Moore
  69.          routine presented in Dr. Dobbs Journal, July 1989.
  70.  
  71. function copies(str:string; count:byte):string;
  72.          Returns COUNT copies of STR concatenated together.
  73.          If the length of n(<=count) copies of STR would exceed 255,
  74.          n-1 copies are returned.
  75.          Ex:  copies('----+',4) returns '----+----+----+----+'
  76.  
  77. function ovrstr(new,str:string; pos:byte; pad:char):string;
  78.          Returns the string STR, overlayed by the string NEW starting
  79.          at character position POS, padding out STR with PAD characters
  80.          if necessary.
  81.          Ex: ovrstr('aygu','Ronald Reagan',9,'+') returns 'Ronald Raygun'
  82.              ovrstr('abc','xyz',6,'+') returns 'xyz++abc'
  83.          Note this function used to be named 'overlay.'  It was changed
  84.          to avoid conflict with TPas's overlay unit.
  85.  
  86. function instr(new,str:string; pos:byte; pad:char):string;
  87.          Returns the string STR after insertion of the string NEW,
  88.          starting at character position POS, padding out STR with PAD
  89.          characters if necessary.
  90.          Ex: instr('abcdef','123456',4,'+') returns '123abcdef456'
  91.              instr('abcdef','123456',10,'+') returns '123456+++abcdef'
  92.          While having similar function to TPas's INSERT procedure, the
  93.          timing benchmark in BENCHMRK.PAS indicates instr is about 30%
  94.          faster.
  95.  
  96. function delstr(str:string; pos,len:byte):string;
  97.          Returns the string STR after deletion of LEN characters,
  98.          starting at character position POS.
  99.          Ex: delstr('abcdefgh',4,2) returns 'abcfgh'
  100.              delstr('abcdefgh',4,20) returns 'abc'
  101.          While having similar function to TPas's DELETE procedure, the
  102.          timing benchmark in BENCHMRK.PAS indicates delstr is about 30%
  103.          faster.
  104.  
  105. function substr(str:string; pos,len:byte):string;
  106.          Returns a substring from STR, starting at position POS
  107.          and continuing for LEN characters, or until the end of STR.
  108.          Ex: substr('1234567890',4,2) returns '45'
  109.              substr('1234567890',4,20) returns '4567890'
  110.          This function is identical to TPas's COPY function, and it
  111.          executes at the same speed.  So why did I include it?  Who
  112.          knows?  I guess I like typing substr better than typing copy...
  113.  
  114. function uppercase(str:string):string;
  115.          Folds the argument STR to uppercase.
  116.          Ex: uppercase('abcdef123') returns 'ABCDEF123'
  117.  
  118. function lowercase(str:string):string;
  119.          Folds the argument STR to lowercase.
  120.          Ex: lowercase('ABCDEF123') returns 'abcdef123'
  121.  
  122. function words(str:string):byte;
  123.          Returns the number of (blank delimited) words in the string STR.
  124.          Ex: words('two four six      eight') returns 4.
  125.  
  126. function werd(str:string; n:byte):string;
  127.          Returns the N'th (blank delimited) word from the string STR.
  128.          The strange spelling is to avoid conflict with Tpas's WORD type.
  129.          Ex: werd('two     air is humin',3) returns 'is'
  130.              werd('two     air is humin',5) returns ''
  131.  
  132. function subword(str:string; n,count:byte):string;
  133.          Returns COUNT words from STR, starting at the N'th word.
  134.          Embedded blanks are preserved.
  135.          Ex: subword('one two three  four',2,3) returns 'two three  four'
  136.              subword('one two three  four',2,0) returns ''
  137.              subword('one two  three ',2,3) returns 'two  three'
  138.              subword('one two  three ',4,2) returns ''
  139.  
  140. function delword(str:string; n,count:byte):string;
  141.          Returns STR with COUNT words deleted, starting at word N.
  142.          Preceeding blanks are preserved.
  143.          Ex: delword('here   we go again',2,2) returns 'here   again'
  144.  
  145. function pos2word(str:string; pos:byte):byte;
  146.          Returns the number of the word in STR pointed to by POS.  If
  147.          POS points to a blank, the number of the following word is
  148.          returned.  If POS points after the last word or end of STR,
  149.          or POS is 0, then 0 is returned.
  150.          Ex: pos2word('abc def ghi ',4) returns 2.
  151.              pos2word('abc def ghi ',6) returns 2.
  152.              pos2word('abc def ghi ',11) returns 3.
  153.              pos2word('abc def ghi ',12) returns 0.
  154.              pos2word('abc def ghi ',0) returns 0.
  155.  
  156. function word2pos(str:string; wrd:byte):byte;
  157.          Returns the position in STR of the first character in the WRD'th
  158.          word.  If WRD>WORDS(STR) or WRD=0, returns 0.
  159.          Ex: word2pos('  abcd e  fghi jk',2) returns 8.
  160.          Note that this function is equivalent to Rexx's WORDINDEX
  161.          function.
  162.  
  163. function space(str:string; spc:byte):string;
  164.          Returns STR with each word separated by SPC blanks.
  165.          Ex: space('  here  we   go again  ',0) returns 'herewegoagain'
  166.              space('  here  we   go again  ',1) returns 'here we go again'
  167.          If the length of the output string would exceed 255, the string
  168.          is truncated at the end of the last whole word which fits.
  169.  
  170. function justify(str:string; len:byte):string;
  171.          Distributes blanks between words in STR so that length(STR)=LEN.
  172.          Ex: justify(' a b cd ef ',10)='a b  cd ef'
  173.          The length of STR should be <= than LEN.
  174.          This runs about eight times faster than the previous version,
  175.          which was written in pascal.
  176.          See usage notes below for important information on usage.
  177.  
  178. function translate(str,intable,outable:string):string;
  179.          Returns STR after translation via the map INTABLE->OUTABLE.
  180.          In other words, each occurrance in STR of the i'th character
  181.          in INTABLE is replaced by the i'th character in OUTABLE.
  182.          Ex: translate('ABC BDE',' BCF','XYZ ') returns 'AYZXYDE'
  183.          INTABLE and OUTABLE should be of the same length.
  184.  
  185. function verify(str,ref:string; opt:char; start:byte):byte;
  186.          Returns the position of the first character in STR (after START)
  187.          which matches/doesn't match a character in REF.  Setting OPT to
  188.          'M' or 'N' returns matching or non-matching character positions,
  189.          respectively.
  190.          Ex: verify('abcd1ef','0123456789','M',0) returns 5.
  191.              verify('123a125','0123456789','n',0) returns 4.
  192.  
  193. function compare(s1,s2:string):byte;
  194.          Compares S1 to S2 and returns the position of the first
  195.          characters which don't match, or 0 if all characters match.
  196.          Ex: compare('hello','hello there') returns 6.
  197.              compare('hello','hexlo') returns 3.
  198.              compare('hello','hello') returns 0.
  199.              compare('','') returns 0.
  200.  
  201. function xrange(c1,c2:char):string;
  202.          Returns a string containing all characters from C1 to C2
  203.          inclusive.  Note the ordering of C1 & C2 can now be reversed.
  204.          Ex: xrange('a','h') returns 'abcdefgh'
  205.              xrange('h','a') returns 'hgfedcba'
  206.  
  207. function reverse(str:string):string;
  208.          Returns contents of STR in reverse order.
  209.          Ex: reverse('hello there') returns 'ereht olleh'
  210.  
  211. function abbrev(str,abbr:string; len:byte):boolean;
  212.          Returns true if ABBR is an 'acceptable' abbreviation for STR.
  213.          The criterion is:
  214.             length(ABBR)>=LEN and ABBR=left(STR,length(ABBR),' ')
  215.          LEN should be set <= length(STR).
  216.          Ex: abbrev('DELETE','DEL',3)=true
  217.              abbrev('DELETE','DELY',3)=false
  218.              abbrev('DELETE','DELET',3)=true
  219.              abbrev('DELETE','DELETEX',3)=false
  220.  
  221. function d2x(i:word):xstr;
  222.          (XSTR is defined in the TPU as STRING[4])
  223.          Returns a four byte string equal to the hex representation of I.
  224.          Ex: d2x(255) returns '00FF'
  225.  
  226. function x2d(x:xstr):word;
  227.          Returns the numeric value represented by the xstr X.  Upper
  228.          and lower case A-F are valid on input.  No checking is done for
  229.          the validity of the characters in X, so garbage input gives
  230.          garbage output.  If the validity of X is in doubt, use the
  231.          VERIFY function first:
  232.          validx:=(verify(x,'0123456789ABCDEFabcdef','N')=0);
  233.          Ex: x2d('7F') returns 127.
  234.  
  235. |function scanmem(fstr:string; bufptr:pointer; count:word):word;
  236. |
  237. |        Finds a string in memory.  FSTR is the string to find, BUFPTR
  238. |        specifies where to start and COUNT is the maximum number
  239. |        of bytes to scan.  Returns the 'distance' in bytes between
  240. |        BUFPTR and the string.  Returns $FFFF if the string is not
  241. |        found.  If ofs(bufptr^)+count>$10000, the scan stops at the
  242. |        end of the segment.  A simple example of its usage is in
  243. |        SCANFOR.PAS.
  244.  
  245.                                  Usage Notes
  246.                                  -----------
  247.  
  248.       Note that Rexx's FIND and WORDLENGTH functions can be readily
  249.       synthesized using functions in this package:
  250.  
  251.       {find returns the number of the word in str1 where str2 starts}
  252.       find(str1,str2) ::= pos2word(str1,firstpos(str2,str1,1))
  253.  
  254.       {wordlength returns the length of a word in str}
  255.       wordlength(str,n) ::= length(word(str,n))
  256.  
  257.       In releases prior to 1.2, justify did a space(str,1) upon entry.
  258.       However, it turns out that if a space() needs to be done, the only
  259.       sensible place to do it is BEFORE the call to justify (think about
  260.       it) so the implicit call is actually redundant, so I got rid of
  261.       it.  This also allows the added flexibility of inserting an extra
  262.       space after periods (at the end of sentences) before calling
  263.       justify, if you're picky about such things.  Justify DOES do a
  264.       strip(str,'B',' ') upon entry.
  265.  
  266. |     An easy way to gain direct access to the command line parameters is:
  267. |     var parm:^string;
  268. |      ....
  269. |     parm:=ptr(prefixseg,$80);
  270. |     This is useful when you don't want the parameters treated as a
  271. |     series of words (if embedded blanks are important, for instance).
  272.  
  273. Why the lack of VAR string parameters? (in case you were wondering)
  274.  
  275.       I initially wrote this unit to use VAR string formal parameters in
  276.       the interest of speed, but I've since discovered that when calling
  277.       external assembler routines with value string formal parameters,
  278.       when the actual parameter is a string variable (as opposed to a
  279.       string expression), TP passes a pointer to the ACTUAL variable,
  280.       not a copy of the variable.  While this behavior isn't consistent
  281.       with the pascal standard, assembler isn't pascal, and it does give
  282.       the programmer the best of both worlds:  fast execution and low
  283.       stack overhead when using string variables as parameters, and the
  284.       flexibility of value parameters.  At any rate, string variable
  285.       function parameters are NEVER modified.
  286.  
  287. Differences between releases:
  288.  
  289. Release 1.05:
  290.  
  291.       Converted ABBREV and DELWORD to asm.  Included turbo compiler
  292.       directives {$N-,E-,D-,L-,B-,I-,R-,S+,V-} for optimization.  Added
  293.       PAD parameter to OVERLAY.  Added CHGSTR.  Fixed bug in FIRSTPOS.
  294.       Changed behavior of SUBWORD when COUNT=0, to match Rexx's SUBWORD
  295.       function.
  296.  
  297. Release 1.1:
  298.  
  299.       Compiled for Tpas 5.0.  Modified to cooperate with Tpas's dead
  300.       code elimination feature.  Added X2D & D2X.  Changed JUSTIFY to
  301.       use integer arithmetic for added speed.  Changed name of function
  302.       WORD to WERD to avoid conflict with Tpas's new WORD type.  Added
  303.       type XSTR for use with X2D & D2X.
  304.  
  305. Release 1.11:
  306.  
  307.       Tweaked LEFT, RIGHT, SPACE and COMPARE for optimization.
  308.  
  309. Release 1.2:
  310.  
  311.       Added SUBSTR, INSTR & DELSTR.  Converted JUSTIFY to asm.  Got rid
  312.       of CHGSTR (it didn't seem as useful as I'd first thought, it's not
  313.       part of standard Rexx, and it was out of place as a pascal routine
  314.       among asm routines).  Changed truncation behavior in SPACE when
  315.       output string exceeds 255 chars.  Changed XRANGE to allow reverse
  316.       ordering of C1 and C2, to produce reversed output.  Included
  317.       benchmark and test suite programs.  Included TPU versions for TP
  318.       4.0, 5.0 and 5.5.
  319.  
  320. Release 1.21:
  321.  
  322.       Added SCANMEM.  Tweaked nearly every routine for optimization,
  323.       mostly using suggestions supplied by Dave Kirschbaum
  324.       (kirsch@arsocomvax.socom.mil) (Thanks, Dave!)  Renamed OVERLAY to
  325.       avoid conflict with TP's overlay unit.  Added PREEN.PAS and
  326.       SCANFOR.PAS demos.
  327.  
  328. Examples:
  329.  
  330.       Capitalize the first char in each word in a string S:
  331.       for i:=1 to words(S);
  332.           j:=word2pos(S,i);
  333.           S[j]:=upcase(S[j]);
  334.       end;
  335.  
  336.       Find the lowest-ordered alphabetic character in a string STR
  337.       of uppercase characters:
  338.       lochar:=chr(ord('A')+verify(xrange('A','Z'),STR,'M',0)-1);
  339.  
  340.       Find the highest-ordered alphabetic character in a string STR
  341.       of uppercase characters:
  342.       hichar:=
  343.          chr(ord('Z')-verify(xrange('Z','A'),STR,'M',0)+1);
  344.  
  345.       Replace non-alphabetic chars with blanks in a string S:
  346.       S:=translate(S,xrange('!','9')+xrange(':','@')+
  347.              xrange('[','`')+xrange('{',''),left('',33,' '));
  348.       Or:
  349.       S:=translate(S,translate(xrange('!',''),xrange('A','Z')+xrange('a','z'),
  350.              left('',52,' ')),left('',ord('')-ord('!')+1,' '));
  351.  
  352.       Generate a sorted string S consisting of chars between '0' and 'z',
  353.       none of which occur in an alphanumeric string STR:
  354.       S:=space(translate(xrange('0','z'),STR,left('',length(STR),' ')),0);
  355.  
  356.       Generate a sorted string S, each of who's characters occurs at least
  357.       once in an alphanumeric string STR:
  358.       S:=space(translate(xrange('0','z'),translate(xrange('0','z'),
  359.              STR,left('',length(STR),' ')),left('',75,' ')),0);
  360.  
  361.       Permute the characters in a 'MM/DD/YY' date string to allow for
  362.       easy date comparison:
  363.       translate('78312645','12345678','12/25/88') returns '88/12/25'.
  364.  
  365. |     A simple text formatting example is in TXTFMT.PAS.  A somewhat
  366. |     painful but useful way to treat a large buffer as a sequence of
  367. |     strings is shown in PREEN.PAS.
  368.  
  369.  
  370.                              The Fine Print
  371.                              --------------
  372.  
  373.       STRINGS.TPU is copyright 1989 by Richard Winkel.  The ASM & OBJ
  374.       files are available from me for $10 if you send me a formatted
  375.       360K disk in a stamped self addressed mailer.  If you want to
  376.       avoid the hassle, add $3 and I'll buy the floppy, mailer and
  377.       postage.
  378.  
  379. Note: Purchase of the ASM & OBJ files confers an unlimited license to
  380.       incorporate these routines into your own programs for any purpose,
  381.       commercial or otherwise.
  382.  
  383. Note: You don't need an assembler to recompile the TPU, as long as
  384.       you have the OBJ files and a Turbo Pascal compiler.
  385.  
  386. Send cash, check or money order to:
  387.       Richard Winkel
  388.       6051 W. Benedict Rd.
  389.       Harrisburg, MO.   65256
  390.  
  391. Internet address:
  392.       MATHRICH@UMCVMB.BITNET or
  393.       MATHRICH@UMCVMB.MISSOURI.EDU or
  394.       MATHRICH%UMCVMB@CUNYVM.CUNY.EDU
  395.  
  396. ------------------------------- cut here ---------------------------
  397.  
  398.                   Calling Syntax Guide to STRINGS.TPU
  399.  
  400. type xstr=string[4];
  401.  
  402. function abbrev(str,abbr:string; len:byte):boolean;
  403. function center(s:string; width:byte; pad:char):string;
  404. function compare(s1,s2:string):byte;
  405. function copies(str:string; count:byte):string;
  406. function d2x(i:word):xstr;
  407. function delstr(str:string; pos,len:byte):string;
  408. function delword(str:string; n,len:byte):string;
  409. function firstpos(findstr,instr:string; start:byte):byte;
  410. function instr(new,str:string; pos:byte; pad:char):string;
  411. function justify(str:string; len:byte):string;
  412. function lastpos(findstr,instr:string; start:byte):byte;
  413. function left(str:string; width:byte; pad:char):string;
  414. function lowercase(s:string):string;
  415. function ovrstr(new,str:string; pos:byte; pad:char):string;
  416. function pos2word(s:string; pos:byte):byte;
  417. function reverse(str:string):string;
  418. function right(str:string; width:byte; pad:char):string;
  419. function scanmem(fstr:string; bufptr:pointer; count:word):word;
  420. function space(str:string; spc:byte):string;
  421. function strip(s:string; opt,ch:char):string; {opt='L', 'T' or 'B'}
  422. function substr(str:string; pos,len:byte):string;
  423. function subword(str:string; num,count:byte):string;
  424. function translate(str,intable,outable:string):string;
  425. function uppercase(s:string):string;
  426. function verify(str,ref:string; opt:char; start:byte):byte; {opt='M' or 'N'}
  427. function werd(s:string;c:byte):string;
  428. function word2pos(s:string; wrd:byte):byte;
  429. function words(s:string):byte;
  430. function x2d(str:xstr):word;
  431. function xrange(c1,c2:char):string;
  432.