home *** CD-ROM | disk | FTP | other *** search
/ Programmer's ROM - The Computer Language Library / programmersrom.iso / ada / misc / strcomp.doc < prev    next >
Encoding:
Text File  |  1988-05-03  |  7.1 KB  |  166 lines

  1. The intention of this posting is not to provide a facility, but 
  2. rather to demonstrate a technique to do string comparisons 
  3. in a more sophisticated way than simply using ASCII values.
  4.  
  5. Comments, questions etc are very welcome to:
  6. Erland Sommarskog       
  7. ENEA Data, Stockholm    
  8. sommar@enea.UUCP        
  9.  
  10. The posting contains seven files that can be divided into three
  11. groups:
  12. I:   strcompS.a and strcompB.a
  13.      The core of the posting. They contain a package for string 
  14.      comparisons. It has a character-transscription table to be
  15.      loaded by the user and comparison operators for trans-
  16.      scripted string. The exported routines are described below. 
  17.      StrcompS is the specification, whereas strcompB contains
  18.      the package body.
  19. II:  latin1.a and natascii.a
  20.      They declare names for characters, to be used, for example,
  21.      when defining a collating sequence for the package above.
  22.      Latin1 declares names for the ISO standard 8859/1. Natascii
  23.      declares names for national replacements of the ordinary 
  24.      ASCII set.
  25. III: define.a, comline.a and main.a
  26.      An demonstration application that uses the string-comparison
  27.      package. Define.a loads the character collating sequence.
  28.        Comline.a reads the command line. Note that this file is
  29.      bound to Verdix Ada for Unix and must be rewritten for another
  30.      system.
  31.        Main.a is the main program. It reads lines from standard 
  32.      input or a named file and writes the sorted lines to standard
  33.      output when end-of-file is detected. 
  34.        You find a description of the options last in this file.
  35.        
  36. You should compile the files in the order: latin1, natascii,
  37. strcompS, strcompB, define, comline, main.
  38.  
  39. Four-dimensional sorting
  40. ------------------------
  41.        
  42. The string-comparison package compares strings at four levels:
  43. 1) Alphabetic
  44. 2) Accents
  45. 3) Non-letters
  46. 4) Difference in case 
  47. What is an alphabetic etc is up to the user. He may define "$" 
  48. being a letter with "(" as its lowercase variant if he likes. 
  49.  
  50. One level is only regarded if the level above have no difference.
  51. As an example I take 
  52.       T^ete-`a-t^ete
  53. (I assume a "normal" loading of the character table here.)
  54.   For the first level we use TETEATETE, thus we remove the accents
  55. and the hyphens. On the next we re-insert the accents so we get
  56.       T^ETE`AT^ETE
  57. On level three we only take the hyphens in regard. When comparing
  58. non-letters the package uses the simple ASCII values. The earlier
  59. a character comes, the lower is the sort value. Thus, "trans-scription"
  60. will precede "transscrip-tion". (Actually, as the implementation 
  61. is done, the position is more important than the ASCII value.)
  62.   On the last level we use 
  63.     T^ete`at^ete
  64. thus, the original writing with the hyphens removed. Note that the
  65. user can specify case to be insigificant.
  66.   (This isn't a description on how the package is implemented, just 
  67. a way of illustrating the result. In practice it's done a little
  68. more effective.)
  69.  
  70. When defining accented variants it is possible to let a character
  71. be a variant of a string, in this way the AE ligature can be sorted
  72. as "AE". The opposite is not possible, and what worse is, a string
  73. can't have an alphabetic value. Thus the package is not able to sort
  74. languages as Spanish (CH and LL) correctly.
  75.  
  76. The number characters are handled in a special way if you define them 
  77. as alphabetics. A sequence of figures will read as one number and sort 
  78. after all other alphabetics. (Even if they were defined as the first 
  79. characters.) So you will get
  80.    File1   File2   File10   File11
  81. instead of the usual
  82.    File1   File10  File11   File2
  83.   If you like to sort them as they are read, this is also possible.
  84. E.g. load "0" as a variant of "zero".
  85.  
  86. The package contains the following routines:
  87.  
  88. Load Operations
  89. ---------------
  90. PROCEDURE Load_alphabetic(ch : IN character);
  91. Loads ch as the next alphabetic character. The order of loading
  92. determines the sorting values.
  93.  
  94. PROCEDURE Load_variant(ch       : IN character;  
  95.                        Equ_ch   : IN character;
  96.                        Equ_kind : IN Equivalence_kind);
  97. TYPE Equivalence_kind IS (Exact, Case_diff, Accented);   
  98. PROCEDURE Load_variant(ch      : IN character;  
  99.                        Equ_str : IN string);  
  100. Load_variant loads ch as a variant of Equ_ch or Equ_str. The interpretation
  101. of Equ_kind is:
  102. Exact: Exactly the same. There is no difference. What you use when you
  103.        don't want case to be significant.
  104. Case_diff: Load ch as a lowercase variant of Equ_ch. There will be
  105.            difference at level 4.
  106. Accented:  Load ch as variant of Equ_ch at level 2.
  107. The latter version of Load_variant always loads ch at level 2.
  108.  
  109. For simplify loading, the package also provides routines for loading
  110. a character and its ASCII lowercase equivalent simultaneously:
  111. PROCEDURE Set_case_significance(Flag : boolean);
  112. PROCEDURE Alpha_both_cases(ch : IN character);  
  113. PROCEDURE Variant_both_cases(ch     : IN character;
  114.                              Equ_ch : IN character);
  115. PROCEDURE Variant_both_cases(ch      : IN character;       
  116.                              Equ_str : IN string);
  117. With Set_case_significant you determine whether case should be
  118. significant when loading the pairs. Variant_both_cases loads ch
  119. at level 2.
  120.  
  121. The loading operations raise Already_defined if an attempt is
  122. made to load a character twice. If Equ_ch or part of Equ_str is
  123. undefined, this gives the exception Undefined_equivalent.
  124.  
  125. Transscription operations
  126. -------------------------
  127. These routines translates a string to the internal coding. 
  128. TYPE Transscripted_string(Max_length : natural) IS PRIVATE;
  129. PROCEDURE Transscribe(ch        : IN character;
  130.                       Trans_str : OUT Transscripted_string);
  131. PROCEDURE Transscribe(Str       : IN string;
  132.                       Trans_str : OUT Transscripted_string);
  133. If the transscription is too long, the routines will raise
  134. Transscription_error.
  135.                       
  136. Comparison operators:
  137. ---------------------
  138. FUNCTION "<=" (Left, Right : Transscripted_string) RETURN boolean;
  139. FUNCTION "<"  (Left, Right : Transscripted_string) RETURN boolean;
  140. FUNCTION ">=" (Left, Right : Transscripted_string) RETURN boolean;
  141. FUNCTION ">"  (Left, Right : Transscripted_string) RETURN boolean;
  142.  
  143. I have only included operations for comparing transscripted 
  144. strings. Of course there could be a set for uncoded strings too.
  145.  
  146. Other function
  147. --------------
  148. FUNCTION Is_letter(ch : character) RETURN boolean;
  149.  
  150. The demonstration program
  151. -------------------------
  152. The program takes the options:
  153. -8  Use ISO/Latin-1. If not present, use 7-bit ASCII with national
  154.     replacements.
  155. -e  Case is significant. When omitted, case is not significant.
  156. -LX Selects language. X should be one of the following:
  157.     s or S: Swedish. (Default)
  158.     d or D: Danish
  159.     g:      German1: "A, "O and "U sorts as A, O and U.
  160.     G:      German2: "A, "O and "U sorts as AE, OE and UE.
  161.     f or F  French
  162.    
  163. In the definition routine I load space as the first alphabetic
  164. letter. This gives the result that "Smith, Tony" will sort
  165. before "Smithson, Alan".
  166.