File: string_formatter.e

package info (click to toggle)
smarteiffel 1.1-11
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 12,288 kB
  • ctags: 40,785
  • sloc: ansic: 35,791; lisp: 4,036; sh: 1,783; java: 895; ruby: 613; python: 209; makefile: 115; csh: 78; cpp: 50
file content (119 lines) | stat: -rw-r--r-- 3,315 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
-- This file is  free  software, which  comes  along  with  SmartEiffel. This
-- software  is  distributed  in the hope that it will be useful, but WITHOUT 
-- ANY  WARRANTY;  without  even  the  implied warranty of MERCHANTABILITY or
-- FITNESS  FOR A PARTICULAR PURPOSE. You can modify it as you want, provided
-- this header is kept unaltered, and a notification of the changes is added.
-- You  are  allowed  to  redistribute  it and sell it, alone or as a part of 
-- another product.
--
-- This file is copyrighted and maintained by P.M. Hegt
-- E-mail: p_m_hegt@hotmail.com
--
 
indexing

	description: "String formatter."

	author: "P.M. Hegt (mailto: p_m_hegt@hotmail.com)"

	copyright: "Freeware. No warranty."

	note: "This is a simple string formatter because there is only a limited number of arguments possible."

	how_to_use: "You can use facility inheritance or a client relation. %
		% In case of facility inheritance, inherit STRING_FORMATTER %
		% and call function `format_string': %
		%     my_feature is %
		%         local %
		%             s: STRING %
		%         do %
		%             s := format_string ("1 = '~1', 2 = '~2', 1 = '~1'." , << "A", "B">>) %
		%             print (s); io.put_new_line %
		%         end %
		%  %
		% produces the following output: %
		%      1 = 'A', 2 = 'B', 1 = 'A'. %
		%  %
		% In case of a client relation, create an instance first %
		% and call function `format_string': %
		%     my_feature is %
		%         local %
		%             sf: STRING_FORMATTER
		%             s: STRING %
		%         do %
		%             !! sf
		%             s := sf.format_string ("1 = '~1', 2 = '~2', 1 = '~1'." , << "A", "B">>) %
		%             print (s); io.put_new_line %
		%         end %
		% produces identical output. %
		"

class
	STRING_FORMATTER

feature -- Formatting

	format_string (format: STRING; arguments: COLLECTION[STRING]): STRING is
			-- Format string.
			-- `Result' is a copy of `format' where substring
			-- "~1" is replaced with `arguments' @ 1, etc.
			-- Max 9 arguments if arguments.lower = 1.
			-- Max 10 arguments if arguments.lower = 0.
		require
			format /= Void
			arguments /= Void
			arguments.lower <= 1
			arguments.count <= 9
		local
			i: INTEGER
			c: CHARACTER
			j: INTEGER
		do
			from
				!! Result.make (50)
				i := 1
			invariant
				(1 <= i) and (i <= 1 + format.count)
			variant
				format.count - i
			until
				not (i <= format.count)
			loop
				c := format @ i
				if c = argument_indicator then
					if i < format.count then
						i := i + 1
						c := format @ i
						if c.is_digit then
							j := c.code - ('0').code
							if arguments.lower <= j and j <= arguments.upper then
								Result.append_string (arguments @ j)
							else
								-- Argument not present
								Result.append_character (argument_indicator)
								Result.append_character (c)
							end
						else
							-- Is not an argument.
							Result.append_character (argument_indicator)
							Result.append_character (c)
						end
					else
						-- c is last character so cannot be an argument.
						Result.append_character (c)
					end
				else
					Result.append_character (c)
				end

				i := i + 1
			end
		ensure
			Result /= Void
		end

feature -- Constants

	argument_indicator: CHARACTER is '~'

end -- class STRING_FORMATTER