home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS 1992 December / simtel1292_SIMTEL_1292_Walnut_Creek.iso / msdos / turbopas / conv_p18.arc / STRINGS.DOC < prev    next >
Text File  |  1989-11-21  |  18KB  |  409 lines

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