home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 3 / AACD03.BIN / AACD / Programming / sofa / archive / SmallEiffel.lha / SmallEiffel / contrib / lib / string_formatter.e < prev   
Text File  |  1999-06-05  |  3KB  |  120 lines

  1. -- This file is  free  software, which  comes  along  with  SmallEiffel. This
  2. -- software  is  distributed  in the hope that it will be useful, but WITHOUT 
  3. -- ANY  WARRANTY;  without  even  the  implied warranty of MERCHANTABILITY or
  4. -- FITNESS  FOR A PARTICULAR PURPOSE. You can modify it as you want, provided
  5. -- this header is kept unaltered, and a notification of the changes is added.
  6. -- You  are  allowed  to  redistribute  it and sell it, alone or as a part of 
  7. -- another product.
  8. --
  9. -- This file is copyrighted and maintained by P.M. Hegt
  10. -- E-mail: p_m_hegt@hotmail.com
  11. --
  12.  
  13. indexing
  14.  
  15.     description: "String formatter."
  16.  
  17.     author: "P.M. Hegt (mailto: p_m_hegt@hotmail.com)"
  18.  
  19.     copyright: "Freeware. No warranty."
  20.  
  21.     note: "This is a simple string formatter because there is only a limited number of arguments possible."
  22.  
  23.     how_to_use: "You can use facility inheritance or a client relation. %
  24.         % In case of facility inheritance, inherit STRING_FORMATTER %
  25.         % and call function `format_string': %
  26.         %     my_feature is %
  27.         %         local %
  28.         %             s: STRING %
  29.         %         do %
  30.         %             s := format_string ("1 = '~1', 2 = '~2', 1 = '~1'." , << "A", "B">>) %
  31.         %             print (s); io.put_new_line %
  32.         %         end %
  33.         %  %
  34.         % produces the following output: %
  35.         %      1 = 'A', 2 = 'B', 1 = 'A'. %
  36.         %  %
  37.         % In case of a client relation, create an instance first %
  38.         % and call function `format_string': %
  39.         %     my_feature is %
  40.         %         local %
  41.         %             sf: STRING_FORMATTER
  42.         %             s: STRING %
  43.         %         do %
  44.         %             !! sf
  45.         %             s := sf.format_string ("1 = '~1', 2 = '~2', 1 = '~1'." , << "A", "B">>) %
  46.         %             print (s); io.put_new_line %
  47.         %         end %
  48.         % produces identical output. %
  49.         "
  50.  
  51. class
  52.     STRING_FORMATTER
  53.  
  54. feature -- Formatting
  55.  
  56.     format_string (format: STRING; arguments: COLLECTION[STRING]): STRING is
  57.             -- Format string.
  58.             -- `Result' is a copy of `format' where substring
  59.             -- "~1" is replaced with `arguments' @ 1, etc.
  60.             -- Max 9 arguments if arguments.lower = 1.
  61.             -- Max 10 arguments if arguments.lower = 0.
  62.         require
  63.             format /= Void
  64.             arguments /= Void
  65.             arguments.lower <= 1
  66.             arguments.count <= 9
  67.         local
  68.             i: INTEGER
  69.             c: CHARACTER
  70.             j: INTEGER
  71.         do
  72.             from
  73.                 !! Result.make (50)
  74.                 i := 1
  75.             invariant
  76.                 (1 <= i) and (i <= 1 + format.count)
  77.             variant
  78.                 format.count - i
  79.             until
  80.                 not (i <= format.count)
  81.             loop
  82.                 c := format @ i
  83.                 if c = argument_indicator then
  84.                     if i < format.count then
  85.                         i := i + 1
  86.                         c := format @ i
  87.                         if c.is_digit then
  88.                             j := c.code - ('0').code
  89.                             if arguments.lower <= j and j <= arguments.upper then
  90.                                 Result.append_string (arguments @ j)
  91.                             else
  92.                                 -- Argument not present
  93.                                 Result.append_character (argument_indicator)
  94.                                 Result.append_character (c)
  95.                             end
  96.                         else
  97.                             -- Is not an argument.
  98.                             Result.append_character (argument_indicator)
  99.                             Result.append_character (c)
  100.                         end
  101.                     else
  102.                         -- c is last character so cannot be an argument.
  103.                         Result.append_character (c)
  104.                     end
  105.                 else
  106.                     Result.append_character (c)
  107.                 end
  108.  
  109.                 i := i + 1
  110.             end
  111.         ensure
  112.             Result /= Void
  113.         end
  114.  
  115. feature -- Constants
  116.  
  117.     argument_indicator: CHARACTER is '~'
  118.  
  119. end -- class STRING_FORMATTER
  120.