home *** CD-ROM | disk | FTP | other *** search
/ ftp.mactech.com 2010 / ftp.mactech.com.tar / ftp.mactech.com / challenge / 12.03-Mar96 / sweeney.RTW.sit / ReverseTheWords.c next >
Text File  |  1996-04-24  |  23KB  |  696 lines

  1. //
  2. //        ReverseTheWords.c By John Sweeney
  3. //
  4. //        The main "feature" of this implementation
  5. //        is that I use longs to read in four char's
  6. //        at a time.  This allows me to keep most words
  7. //        in registers until they are written out to
  8. //        the reversed string.  This saves quite a few
  9. //        memory accesses!!  I only used 4 registers
  10. //        (up to 16 letters), but this could easily
  11. //        be extended to an almost absurd level -
  12. //  10 registers == 40 letters!
  13. //
  14. //        Note : No matter how good this solution
  15. //  might be, you won't see it in MacTech
  16. //  I didn't include the two special cases
  17. //  handled in the very start....so I    
  18. //  crashed and burned during testing!!!!
  19. //
  20. //  Another note : I've also included a
  21. //  file named ReverseTheWords.o.  This
  22. //        is the object file produced when
  23. //        this source file is compiled by
  24. //        Motorola's C compiler under MPW.
  25. //        The *.o file can be included in a
  26. //  CodeWarrior project file and
  27. //        linked for testing.  I saw an
  28. //        average of 20% improvement for
  29. //        the Motorola compiled version of this
  30. //  routine vs the CodeWarrior version!
  31. //
  32. // 
  33. #include <stdio.h>
  34. #include <string.h>
  35. #include <stdlib.h>
  36. #include <time.h>
  37. #include <Types.h>
  38. #include <Timer.h>
  39. #include <Memory.h>
  40.  
  41. pascal void ReverseTheWords(
  42.   char *text,            /* the words you should reverse */
  43.   const long numCharsIn  /* length of inputText in chars */
  44. );
  45.  
  46. pascal void ReverseTheWords(
  47.   char *text,            /* the words you should reverse */
  48.   const long numCharsIn  /* length of inputText in chars */
  49. ) {
  50.  
  51. static char myAlphaNum[256] = {    
  52.  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  53.  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  54.  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  55.  '0','1','2','3','4','5','6','7',
  56.  '8','9',0,0,0,0,0,0,
  57.  0,'a','b','c','d','e','f','g',
  58.  'h','i','j','k','l','m','n','o',
  59.  'p','q','r','s','t','u','v','w',
  60.  'x','y','z',0,0,0,0,0,
  61.  0,'A','B','C','D','E','F','G',
  62.  'H','I','J','K','L','M','N','O',
  63.  'P','Q','R','S','T','U','V','W',
  64.  'X','Y','Z',0,0,0,0,0,
  65.  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  66.  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  67.  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  68.  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  69.  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  70.  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  71.  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  72.  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  73. };
  74. unsigned long headWordLength,tailWordLength,textOffset,
  75.                                                         bufferOffset,offset,done;
  76. short i=0, wordCount=0;
  77. register unsigned long w1,w2,w3,w4;
  78. register char c1,c2,c3,c4,hCap,tCap;
  79. char *ubuffer,*buffer;
  80. unsigned long headBufferPos = 0,
  81.       tailBufferPos = (long)numCharsIn-1;
  82. char *headWordStart = (char *)text,
  83.      *tailWordEnd   = (char *)text+numCharsIn-1;
  84.      
  85.  
  86.         // Check for special case of less than 2 words 
  87.         // (i.e. nothing to reverse)
  88.         while (i < numCharsIn) {
  89.             c1 = *(char *)(text+i++);
  90.             if (myAlphaNum[c1]!=0) { // in a word
  91.                 wordCount++;
  92.                 if (wordCount == 2) break;
  93.                 while (i < numCharsIn) {
  94.                     c1 = *(char *)(text+i++);
  95.                     if (myAlphaNum[c1]==0) break;
  96.                 }
  97.             }
  98.         }
  99.         if (wordCount < 2) return;
  100.         
  101.         // Handle special case of 3 chars : 'a b'
  102.         if (numCharsIn == 3) {
  103.             c1 = *(char *)(text);
  104.             c2 = *(char *)(text+2);
  105.             *(char *)(text) = c2;
  106.             *(char *)(text+2) = c1;
  107.             return;
  108.         }
  109.         
  110.         
  111.         textOffset = (unsigned long)text % 8;
  112.         
  113.         ubuffer = (char *)malloc(numCharsIn+16);
  114.         
  115.         bufferOffset = (unsigned long)ubuffer % 8;
  116.         
  117.         buffer = ubuffer + 8 + textOffset - bufferOffset;
  118.     
  119.         /* Copy Trailing White Space */
  120.         done = 0;
  121.         while (!done) {
  122.             w1 = *(unsigned long *)(tailWordEnd-3);
  123.             c1  = w1 & 0xFF;
  124.             c2 = (w1 >> 8) & 0xFF;
  125.                 if (myAlphaNum[c1]==0) { // 1 space
  126.                     if (myAlphaNum[c2]==0) { // 2 spaces
  127.                         c3 = (w1 >> 16) & 0xFF;
  128.                         c4 = (w1 >> 24);
  129.                         if (myAlphaNum[c3]==0) { // 3 spaces
  130.                             if (myAlphaNum[c4]==0) { // 4 spaces
  131.                                 *(unsigned long *)(buffer+tailBufferPos) = w1;
  132.                                 tailBufferPos = tailBufferPos - 4;
  133.                                 tailWordEnd = tailWordEnd - 4;
  134.                             }
  135.                             else {// 3 spaces
  136.                                 done = 1;
  137.                                 buffer[tailBufferPos-2] = c3;
  138.                                 *(unsigned short *)(buffer+tailBufferPos-1) = (unsigned short)w1;
  139.                                 tailBufferPos = tailBufferPos - 3;
  140.                                 tailWordEnd = tailWordEnd - 3;
  141.                             }
  142.                         }
  143.                         else { // 2 spaces
  144.                             done = 1;
  145.                             *(unsigned short *)(buffer+tailBufferPos-1) = (unsigned short)w1;
  146.                             tailBufferPos = tailBufferPos - 2;
  147.                             tailWordEnd = tailWordEnd - 2;
  148.                         }
  149.                     }
  150.                     else { // 1 space
  151.                         done = 1;
  152.                         buffer[tailBufferPos] = c1;
  153.                         tailBufferPos = tailBufferPos - 1;
  154.                         tailWordEnd = tailWordEnd - 1;
  155.                     }
  156.                 }
  157.                 else { // no space
  158.                     done = 1;
  159.                 }
  160.             }
  161.     
  162.         /* Copy Leading white space */
  163.         done = 0;
  164.         while (!done) {
  165.             w1 = *(unsigned long *)(headWordStart);
  166.             c1  = (w1 >> 24);
  167.             c2 = (w1 >> 16) & 0xFF;
  168.                 if (myAlphaNum[c1]==0) { // 1 space
  169.                     if (myAlphaNum[c2]==0) { // 2 spaces
  170.                         c3 = (w1 >> 8) & 0xFF;
  171.                         c4 = w1 & 0xFF;
  172.                         if (myAlphaNum[c3]==0) { // 3 spaces
  173.                             if (myAlphaNum[c4]==0) { // 4 spaces
  174.                                 *(unsigned long *)(buffer+headBufferPos) = w1;
  175.                                 headBufferPos = headBufferPos + 4;
  176.                                 headWordStart = headWordStart + 4;
  177.                             }
  178.                             else {// 3 spaces
  179.                                 done = 1;
  180.                                 buffer[headBufferPos] = c1;
  181.                                 buffer[headBufferPos+1] = c2;
  182.                                 buffer[headBufferPos+2] = c3;
  183.                                 headBufferPos = headBufferPos + 3;
  184.                                 headWordStart = headWordStart + 3;
  185.                             }
  186.                         }
  187.                         else { // 2 spaces
  188.                             done = 1;
  189.                             buffer[headBufferPos] = c1;
  190.                             buffer[headBufferPos+1] = c2;
  191.                             headBufferPos = headBufferPos + 2;
  192.                             headWordStart = headWordStart + 2;
  193.                             }
  194.                     }
  195.                     else { // 1 space
  196.                         done = 1;
  197.                         buffer[headBufferPos] = c1;
  198.                         headBufferPos = headBufferPos + 1;
  199.                         headWordStart = headWordStart + 1;
  200.                     }
  201.                 }
  202.                 else { // no space
  203.                     done = 1;
  204.                 }
  205.         }
  206.         
  207.     while (headWordStart < tailWordEnd) {
  208.     
  209.         /* Find the leading word */
  210.  
  211.         headWordLength = 0;
  212.         w1 = * (unsigned long *)headWordStart;
  213.         c1 = w1 >> 24;
  214.         hCap = c1;
  215.         c2 = (w1>>16) &0xFF;
  216.         if (myAlphaNum[c2]){ // char #2
  217.             c3 = (w1 >> 8) & 0xFF;
  218.             c4 = w1 & 0xFF;
  219.             if (myAlphaNum[c3]) { // char #3
  220.                 if (myAlphaNum[c4]) { // char #4
  221.                     w2 = * (unsigned long *)(headWordStart+4);
  222.                     c1 =w2 >> 24;
  223.                     c2 = (w2>>16) &0xFF;
  224.                     if (myAlphaNum[c1]) { // char #5
  225.                         if (myAlphaNum[c2]) { // char #6
  226.                             c3 = (w2 >> 8) & 0xFF;
  227.                             c4 = w2 & 0xFF;
  228.                             if (myAlphaNum[c3]) { // char #7
  229.                                 if (myAlphaNum[c4]) { // char #8
  230.                                     w3 = * (unsigned long *)(headWordStart+8);
  231.                                     c1 = w3 >> 24;
  232.                                     c2 = (w3>>16) &0xFF;
  233.                                     if (myAlphaNum[c1]) { // char #9
  234.                                         if (myAlphaNum[c2]) { // char #10
  235.                                             c3 = (w3 >> 8) & 0xFF;
  236.                                             c4 = w3 & 0xFF;
  237.                                             if (myAlphaNum[c3]) { // char #11
  238.                                                 if (myAlphaNum[c4]) { // char #12
  239.                                                     w4 = * (unsigned long *)(headWordStart+12);
  240.                                                     c1 =w4 >> 24;
  241.                                                     c2 = (w4>>16) &0xFF;
  242.                                                     if (myAlphaNum[c1]) { // char #13
  243.                                                         if (myAlphaNum[c2]) { // char #14
  244.                                                             c3 = (w4 >> 8) & 0xFF;
  245.                                                             c4 = w4 & 0xFF;
  246.                                                             if (myAlphaNum[c3]) { // char #15
  247.                                                                 if (myAlphaNum[c4]) { // char #16
  248.                                                                     headWordLength = 16;
  249.                                                                     while (myAlphaNum[*(headWordStart+headWordLength)]){
  250.                                                                         headWordLength++;
  251.                                                                     }
  252.                                                                 
  253.                                                                     /* Copy Leading Word */
  254.                                                         
  255.                                                                     for (i=headWordLength-17; i>=0; i--) {
  256.                                                                         *(buffer+tailBufferPos--) = *(headWordStart+16+i);
  257.                                                                     }
  258.                                                         
  259.                                                                     *(unsigned long *)(buffer+tailBufferPos-3) = w4;
  260.                                                                     *(unsigned long *)(buffer+tailBufferPos-7) = w3;
  261.                                                                     *(unsigned long *)(buffer+tailBufferPos-11) = w2;
  262.                                                                     *(unsigned long *)(buffer+tailBufferPos-15) = w1;
  263.                                                                                                     
  264.                                                                     tailBufferPos = tailBufferPos-16;
  265.                                                                     headWordStart = headWordStart+headWordLength;
  266.                                                                 }
  267.                                                                 else { /* 15-letter word */
  268.                                                                     headWordStart = headWordStart+15;
  269.                                                                     buffer[tailBufferPos] = c3;
  270.                                                                     buffer[tailBufferPos-1] = c2;
  271.                                                                     buffer[tailBufferPos-2] = c1;
  272.                                                                     *(unsigned long *)(buffer+tailBufferPos-6) = w3;
  273.                                                                     *(unsigned long *)(buffer+tailBufferPos-10) = w2;
  274.                                                                     *(unsigned long *)(buffer+tailBufferPos-14) = w1;
  275.                                                                     tailBufferPos = tailBufferPos-15;
  276.                                                                 } 
  277.                                                             }
  278.                                                             else { /* 14-letter word */
  279.                                                                 headWordStart = headWordStart+14;
  280.                                                                 buffer[tailBufferPos] = c2;
  281.                                                                 buffer[tailBufferPos-1] = c1;
  282.                                                                 *(unsigned long *)(buffer+tailBufferPos-5) = w3;
  283.                                                                 *(unsigned long *)(buffer+tailBufferPos-9) = w2;
  284.                                                                 *(unsigned long *)(buffer+tailBufferPos-13) = w1;
  285.                                                                 tailBufferPos = tailBufferPos-14;
  286.                                                             }
  287.                                                         }
  288.                                                         else { /* 13-letter word */
  289.                                                             headWordStart = headWordStart+13;
  290.                                                             buffer[tailBufferPos] = c1;
  291.                                                             *(unsigned long *)(buffer+tailBufferPos-4) = w3;
  292.                                                             *(unsigned long *)(buffer+tailBufferPos-8) = w2;
  293.                                                             *(unsigned long *)(buffer+tailBufferPos-12) = w1;
  294.                                                             tailBufferPos = tailBufferPos-13;
  295.                                                         }
  296.                                                     }
  297.                                                     else { /* 12-letter word */
  298.                                                         headWordStart = headWordStart+12;
  299.                                                         *(unsigned long *)(buffer+tailBufferPos-3) = w3;
  300.                                                         *(unsigned long *)(buffer+tailBufferPos-7) = w2;
  301.                                                         *(unsigned long *)(buffer+tailBufferPos-11) = w1;
  302.                                                         tailBufferPos = tailBufferPos-12;
  303.                                                     }
  304.                                                 }
  305.                                                 else { /* 11-letter word */
  306.                                                     headWordStart = headWordStart+11;
  307.                                                     buffer[tailBufferPos] = c3;
  308.                                                     buffer[tailBufferPos-1] = c2;
  309.                                                     buffer[tailBufferPos-2] = c1;
  310.                                                     *(unsigned long *)(buffer+tailBufferPos-6) = w2;
  311.                                                     *(unsigned long *)(buffer+tailBufferPos-10) = w1;
  312.                                                     tailBufferPos = tailBufferPos-11;
  313.                                                 }
  314.                                             }
  315.                                             else { /* 10-letter word */
  316.                                                 headWordStart = headWordStart+10;
  317.                                                 buffer[tailBufferPos] = c2;
  318.                                                 buffer[tailBufferPos-1] = c1;
  319.                                                 *(unsigned long *)(buffer+tailBufferPos-5) = w2;
  320.                                                 *(unsigned long *)(buffer+tailBufferPos-9) = w1;
  321.                                                 tailBufferPos = tailBufferPos-10;
  322.                                             }
  323.                                         }
  324.                                         else { /* 9-letter word */
  325.                                             headWordStart = headWordStart+9;
  326.                                             buffer[tailBufferPos] = c1;
  327.                                             *(unsigned long *)(buffer+tailBufferPos-4) = w2;
  328.                                             *(unsigned long *)(buffer+tailBufferPos-8) = w1;
  329.                                             tailBufferPos = tailBufferPos-9;
  330.                                         }
  331.                                     }
  332.                                     else { /* 8-letter word */
  333.                                         headWordStart = headWordStart+8;
  334.                                         *(unsigned long *)(buffer+tailBufferPos-3) = w2;
  335.                                         *(unsigned long *)(buffer+tailBufferPos-7) = w1;
  336.                                         tailBufferPos = tailBufferPos-8;
  337.                                         
  338.                                     }
  339.                                 }
  340.                                 else { /* 7-letter word */
  341.                                     headWordStart = headWordStart+7;
  342.                                     buffer[tailBufferPos] = c3;
  343.                                     buffer[tailBufferPos-1] = c2;
  344.                                     buffer[tailBufferPos-2] = c1;
  345.                                     *(unsigned long *)(buffer+tailBufferPos-6) = w1;
  346.                                     tailBufferPos = tailBufferPos-7;
  347.                                 }
  348.                             }
  349.                             else { /* 6-letter word */
  350.                                 headWordStart = headWordStart+6;
  351.                                 buffer[tailBufferPos] = c2;
  352.                                 buffer[tailBufferPos-1] = c1;
  353.                                 *(unsigned long *)(buffer+tailBufferPos-5) = w1;
  354.                                 tailBufferPos = tailBufferPos-6;
  355.                             }
  356.                         }
  357.                         else { /* 5-letter word */
  358.                             headWordStart = headWordStart+5;
  359.                             buffer[tailBufferPos] = c1;
  360.                             *(unsigned long *)(buffer+tailBufferPos-4) = w1;
  361.                             tailBufferPos = tailBufferPos-5;
  362.                         }
  363.                     }
  364.                     else { /* 4-letter word */
  365.                         headWordStart = headWordStart+4;
  366.                         *(unsigned long *)(buffer+tailBufferPos-3) = w1;
  367.                         tailBufferPos = tailBufferPos-4;
  368.                     }
  369.                 }
  370.                 else { /* 3-letter word */
  371.                     headWordStart = headWordStart+3;
  372.                     buffer[tailBufferPos] = c3;
  373.                     buffer[tailBufferPos-1] = c2;
  374.                     buffer[tailBufferPos-2] = c1;
  375.                     tailBufferPos = tailBufferPos-3;
  376.                 }
  377.             }
  378.             else { /* 2-letter word */
  379.                 headWordStart = headWordStart+2;
  380.                 buffer[tailBufferPos] = c2;
  381.                 buffer[tailBufferPos-1] = c1;
  382.                 tailBufferPos = tailBufferPos-2;
  383.             }
  384.         }
  385.         else { /* 1-letter word */
  386.             headWordStart = headWordStart+1;
  387.             buffer[tailBufferPos] = c1;
  388.             tailBufferPos = tailBufferPos-1;
  389.         }
  390.         
  391.         /* Find the trailing word */
  392.         tailWordLength = 0;
  393.         w1 = *(unsigned long *)(tailWordEnd-3);
  394.         c1 = w1 & 0xFF;
  395.         c2 = (w1 >> 8) & 0xFF;
  396.         if (myAlphaNum[c2]){ // 2-letters
  397.             c3 = (w1>>16) &0xFF;
  398.             c4 = w1 >> 24;
  399.             if (myAlphaNum[c3]) { // 3-letters
  400.                 if (myAlphaNum[c4]) { // 4-letters
  401.                     w2 = *(unsigned long *)(tailWordEnd-7);
  402.                     c1 = w2 & 0xFF;
  403.                     c2 = (w2 >> 8) & 0xFF;
  404.                     if (myAlphaNum[c1]) { // 5-letters
  405.                         if (myAlphaNum[c2]) { // 6-letters
  406.                             c3 = (w2>>16) &0xFF;
  407.                             c4 = w2 >> 24;
  408.                             if (myAlphaNum[c3]) { // 7-letters
  409.                                 if (myAlphaNum[c4]) { // 8-letters
  410.                                     w3 = *(unsigned long *)(tailWordEnd-11);
  411.                                     c1 = w3 & 0xFF;
  412.                                     c2 = (w3 >> 8) & 0xFF;
  413.                                     if (myAlphaNum[c1]) { // 9-letters
  414.                                         if (myAlphaNum[c2]) { // 10-letters
  415.                                             c3 = (w3>>16) &0xFF;
  416.                                             c4 = w3 >> 24;
  417.                                             if (myAlphaNum[c3]) { // 11-letters
  418.                                                 if (myAlphaNum[c4]) { // 12-letters
  419.                                                     w4 = *(unsigned long *)(tailWordEnd-15);
  420.                                                     c1 = w4 & 0xFF;
  421.                                                     c2 = (w4 >> 8) & 0xFF;
  422.                                                     if (myAlphaNum[c1]) { // 13-letters
  423.                                                         if (myAlphaNum[c2]) { // 14-letters
  424.                                                             c3 = (w4>>16) &0xFF;
  425.                                                             c4 = w4 >> 24;
  426.                                                             if (myAlphaNum[c3]) { // 15-letters
  427.                                                                 if (myAlphaNum[c4]) { // 16-letters
  428.                                                                     tailWordLength = 16;
  429.                                                                     while (myAlphaNum[(char)*(tailWordEnd-tailWordLength)]){
  430.                                                                         tailWordLength++;
  431.                                                                     }
  432.                                                                     /* Copy trailing Word */
  433.                                                                     for (i=tailWordLength-17; i>=0; i--) {
  434.                                                                         buffer[headBufferPos++] = *(tailWordEnd-i);
  435.                                                                     }
  436.                                                                     tailWordEnd = tailWordEnd-tailWordLength;
  437.                                                                     *(unsigned long *)(buffer+headBufferPos) = w4;
  438.                                                                     *(unsigned long *)(buffer+headBufferPos+4) = w3;
  439.                                                                     *(unsigned long *)(buffer+headBufferPos+8) = w2;
  440.                                                                     *(unsigned long *)(buffer+headBufferPos+12) = w1;
  441.                                                                     headBufferPos = headBufferPos+16;
  442.                                                                     tCap = buffer[headBufferPos-tailWordLength];
  443.                                                                 }
  444.                                                                 else { /* 15-letter word */
  445.                                                                     tCap = c3;
  446.                                                                     buffer[headBufferPos] = c3;
  447.                                                                     *(unsigned short *)(buffer+headBufferPos+1) = (unsigned short) w4;
  448.                                                                     tailWordLength = 15;
  449.                                                                     *(unsigned long *)(buffer+headBufferPos+3) = w3;
  450.                                                                     tailWordEnd = tailWordEnd-15;
  451.                                                                     *(unsigned long *)(buffer+headBufferPos+7) = w2;
  452.                                                                     *(unsigned long *)(buffer+headBufferPos+11) = w1;
  453.                                                                     headBufferPos = headBufferPos+15;
  454.                                                                 }
  455.                                                             }
  456.                                                             else { /* 14-letter word */
  457.                                                                 tCap = c2;
  458.                                                                 *(unsigned short *)(buffer+headBufferPos) = (unsigned short) w4;
  459.                                                                 tailWordLength = 14;
  460.                                                                 *(unsigned long *)(buffer+headBufferPos+2) = w3;
  461.                                                                 tailWordEnd = tailWordEnd-14;
  462.                                                                 *(unsigned long *)(buffer+headBufferPos+6) = w2;
  463.                                                                 *(unsigned long *)(buffer+headBufferPos+10) = w1;
  464.                                                                 headBufferPos = headBufferPos+14;
  465.                                                             }
  466.                                                         }
  467.                                                         else { /* 13-letter word */
  468.                                                             tCap = c1;
  469.                                                             buffer[headBufferPos] = c1;
  470.                                                             tailWordLength = 13;
  471.                                                             *(unsigned long *)(buffer+headBufferPos+1) = w3;
  472.                                                             tailWordEnd = tailWordEnd-13;
  473.                                                             *(unsigned long *)(buffer+headBufferPos+5) = w2;
  474.                                                             *(unsigned long *)(buffer+headBufferPos+9) = w1;
  475.                                                             headBufferPos = headBufferPos+13;
  476.                                                         }
  477.                                                     }
  478.                                                     else { /* 12-letter word */
  479.                                                         tCap = c4;
  480.                                                         *(unsigned long *)(buffer+headBufferPos) = w3;
  481.                                                         tailWordLength = 12;
  482.                                                         *(unsigned long *)(buffer+headBufferPos+4) = w2;
  483.                                                         tailWordEnd = tailWordEnd-12;
  484.                                                         *(unsigned long *)(buffer+headBufferPos+8) = w1;
  485.                                                         headBufferPos = headBufferPos+12;
  486.                                                     }
  487.                                                 }
  488.                                                 else { /* 11-letter word */
  489.                                                     tCap = c3;
  490.                                                     buffer[headBufferPos] = c3;
  491.                                                     *(unsigned short *)(buffer+headBufferPos+1) = (unsigned short) w3;
  492.                                                     tailWordLength = 11;
  493.                                                     *(unsigned long *)(buffer+headBufferPos+3) = w2;
  494.                                                     tailWordEnd = tailWordEnd-11;
  495.                                                     *(unsigned long *)(buffer+headBufferPos+7) = w1;
  496.                                                     headBufferPos = headBufferPos+11;
  497.                                                 }
  498.                                             }
  499.                                             else { /* 10-letter word */
  500.                                                 tCap = c2;
  501.                                                 *(unsigned short *)(buffer+headBufferPos) = (unsigned short) w3;
  502.                                                 tailWordLength = 10;
  503.                                                 *(unsigned long *)(buffer+headBufferPos+2) = w2;
  504.                                                 tailWordEnd = tailWordEnd-10;
  505.                                                 *(unsigned long *)(buffer+headBufferPos+6) = w1;
  506.                                                 headBufferPos = headBufferPos+10;
  507.                                             }
  508.                                         }
  509.                                         else { /* 9-letter word */
  510.                                             tCap = c1;
  511.                                             buffer[headBufferPos] = c1;
  512.                                             tailWordLength = 9;
  513.                                             *(unsigned long *)(buffer+headBufferPos+1) = w2;
  514.                                             tailWordEnd = tailWordEnd-9;
  515.                                             *(unsigned long *)(buffer+headBufferPos+5) = w1;
  516.                                             headBufferPos = headBufferPos+9;
  517.                                         }
  518.                                     }
  519.                                     else { /* 8-letter word */
  520.                                         tCap = c4;
  521.                                         *(unsigned long *)(buffer+headBufferPos) = w2;
  522.                                         tailWordLength = 8;
  523.                                         *(unsigned long *)(buffer+headBufferPos+4) = w1;
  524.                                         headBufferPos = headBufferPos+8;
  525.                                         tailWordEnd = tailWordEnd-8;
  526.                                     }
  527.                                 }
  528.                                 else { /* 7-letter word */
  529.                                     tCap = c3;
  530.                                     buffer[headBufferPos] = c3;
  531.                                     *(unsigned short *)(buffer+headBufferPos+1) = (unsigned short) w2;
  532.                                     tailWordLength = 7;
  533.                                     *(unsigned long *)(buffer+headBufferPos+3) = w1;
  534.                                     headBufferPos = headBufferPos+7;
  535.                                     tailWordEnd = tailWordEnd-7;
  536.                                 }
  537.                             }
  538.                             else { /* 6-letter word */
  539.                                 tCap = c2;
  540.                                 *(unsigned short *)(buffer+headBufferPos) = (unsigned short) w2;
  541.                                 tailWordLength = 6;
  542.                                 *(unsigned long *)(buffer+headBufferPos+2) = w1;
  543.                                 headBufferPos = headBufferPos+6;
  544.                                 tailWordEnd = tailWordEnd-6;
  545.                             }
  546.                         }
  547.                         else { /* 5-letter word */
  548.                             tCap = c1;
  549.                             buffer[headBufferPos] = c1;
  550.                             tailWordLength = 5;
  551.                             *(unsigned long *)(buffer+headBufferPos+1) = w1;
  552.                             headBufferPos = headBufferPos+5;
  553.                             tailWordEnd = tailWordEnd-5;
  554.                         }
  555.                     }
  556.                     else { /* 4-letter word */
  557.                         tCap = c4;
  558.                         tailWordLength = 4;
  559.                         *(unsigned long *)(buffer+headBufferPos) = w1;
  560.                         headBufferPos = headBufferPos+4;
  561.                         tailWordEnd = tailWordEnd-4;
  562.                     }
  563.                 }
  564.                 else { /* 3-letter word */
  565.                     tCap = c3;
  566.                     buffer[headBufferPos] = c3;
  567.                     tailWordLength = 3;
  568.                     *(unsigned short *)(buffer+headBufferPos+1) = (unsigned short) w1;
  569.                     headBufferPos = headBufferPos+3;
  570.                     tailWordEnd = tailWordEnd-3;
  571.                 }
  572.             }
  573.             else { /* 2-letter word */
  574.                 tCap = c2;
  575.                 tailWordLength = 2;
  576.                 *(unsigned short *)(buffer+headBufferPos) = (unsigned short) w1;
  577.                 headBufferPos = headBufferPos+2;
  578.                 tailWordEnd = tailWordEnd-2;
  579.             }
  580.         }
  581.         else { /* 1-letter word */
  582.             tCap = c1;
  583.             tailWordLength = 1;
  584.             buffer[headBufferPos] = c1;
  585.             headBufferPos = headBufferPos+1;
  586.             tailWordEnd = tailWordEnd-1;
  587.         }
  588.  
  589.         
  590.         /* Adjust Capitalization */
  591.         
  592.         if (((tCap^hCap)&0x60)==0x20) {
  593.             buffer[headBufferPos-tailWordLength] = 
  594.                 myAlphaNum[tCap];
  595.             buffer[tailBufferPos+1] = 
  596.                 myAlphaNum[hCap];
  597.         }
  598.         
  599.     
  600.         /* Copy Trailing White Space */
  601.         done = 0;
  602.         while (!done) {
  603.             w1 = *(unsigned long *)(tailWordEnd-3);
  604.             c1  = w1 & 0xFF;
  605.             c2 = (w1 >> 8) & 0xFF;
  606.                 if (myAlphaNum[c1]==0) { // 1 space
  607.                     if (myAlphaNum[c2]==0) { // 2 spaces
  608.                         c3 = (w1 >> 16) & 0xFF;
  609.                         c4 = (w1 >> 24);
  610.                         if (myAlphaNum[c3]==0) { // 3 spaces
  611.                             if (myAlphaNum[c4]==0) { // 4 spaces
  612.                                 *(unsigned long *)(buffer+tailBufferPos) = w1;
  613.                                 tailBufferPos = tailBufferPos - 4;
  614.                                 tailWordEnd = tailWordEnd - 4;
  615.                             }
  616.                             else {// 3 spaces
  617.                                 done = 1;
  618.                                 buffer[tailBufferPos-2] = c3;
  619.                                 *(unsigned short *)(buffer+tailBufferPos-1) = (unsigned short)w1;
  620.                                 tailBufferPos = tailBufferPos - 3;
  621.                                 tailWordEnd = tailWordEnd - 3;
  622.                             }
  623.                         }
  624.                         else { // 2 spaces
  625.                             done = 1;
  626.                             *(unsigned short *)(buffer+tailBufferPos-1) = (unsigned short)w1;
  627.                             tailBufferPos = tailBufferPos - 2;
  628.                             tailWordEnd = tailWordEnd - 2;
  629.                         }
  630.                     }
  631.                     else { // 1 space
  632.                         done = 1;
  633.                         buffer[tailBufferPos] = c1;
  634.                         tailBufferPos = tailBufferPos - 1;
  635.                         tailWordEnd = tailWordEnd - 1;
  636.                     }
  637.                 }
  638.                 else { // no space
  639.                     done = 1;
  640.                 }
  641.             }
  642.  
  643.         /* Copy Leading white space */
  644.         done = 0;
  645.         while (!done) {
  646.             w1 = *(unsigned long *)(headWordStart);
  647.             c1  = (w1 >> 24);
  648.             c2 = (w1 >> 16) & 0xFF;
  649.                 if (myAlphaNum[c1]==0) { // 1 space
  650.                     if (myAlphaNum[c2]==0) { // 2 spaces
  651.                         c3 = (w1 >> 8) & 0xFF;
  652.                         c4 = w1 & 0xFF;
  653.                         if (myAlphaNum[c3]==0) { // 3 spaces
  654.                             if (myAlphaNum[c4]==0) { // 4 spaces
  655.                                 *(unsigned long *)(buffer+headBufferPos) = w1;
  656.                                 headBufferPos = headBufferPos + 4;
  657.                                 headWordStart = headWordStart + 4;
  658.                             }
  659.                             else {// 3 spaces
  660.                                 done = 1;
  661.                                 buffer[headBufferPos] = c1;
  662.                                 buffer[headBufferPos+1] = c2;
  663.                                 buffer[headBufferPos+2] = c3;
  664.                                 headBufferPos = headBufferPos + 3;
  665.                                 headWordStart = headWordStart + 3;
  666.                             }
  667.                         }
  668.                         else { // 2 spaces
  669.                             done = 1;
  670.                             buffer[headBufferPos] = c1;
  671.                             buffer[headBufferPos+1] = c2;
  672.                             headBufferPos = headBufferPos + 2;
  673.                             headWordStart = headWordStart + 2;
  674.                             }
  675.                     }
  676.                     else { // 1 space
  677.                         done = 1;
  678.                         buffer[headBufferPos] = c1;
  679.                         headBufferPos = headBufferPos + 1;
  680.                         headWordStart = headWordStart + 1;
  681.                     }
  682.                 }
  683.                 else { // no space
  684.                     done = 1;
  685.                 }
  686.         }
  687.         
  688.     }
  689.     
  690.     /* Copy buffer back to text */
  691.     
  692.     memmove((char *)text,(char*)buffer,numCharsIn);
  693.     
  694.     free(ubuffer);
  695. }
  696.