PageRenderTime 21ms CodeModel.GetById 17ms app.highlight 1ms RepoModel.GetById 2ms app.codeStats 0ms

/src/lib/string/internal/fillable_string.e

http://github.com/tybor/Liberty
Specman e | 119 lines | 97 code | 12 blank | 10 comment | 4 complexity | f6e6dac9905acf4da1d58a9fd3b09869 MD5 | raw file
  1class FILLABLE_STRING
  2	-- A string of text made of fixed text and placeholdes 
  3	obsolete "[
  4	That was a tentative implementation. 
  5	The idea was to save the explicit allocation required by STRING_PRINTER which is used like 
  6		my_string_printer.put_message
  7		("@(1) is really a nice @(2)",
  8		<<"Eiffel", "programming language">>)
  9
 10		Too bad that the idea behind FILLABLE_STRING is to scan the message
 11		during creation of Current creating a list of abstract strings
 12		(`pieces') holding the fixed pieces and the variable one.
 13
 14		The variable ones would FILLABLE_ARGUMENTs, a heir of ABSTRACT_STRING
 15		implemented with a REFERENCE[LAZY_STRING]. The indexes arguments would
 16		be stored into array `placeholders_indexes'.
 17
 18		So in the end I would save an explicit array allocation requiring a
 19		linked list and an array. Smart, isn't it? 
 20
 21		Actually current implementation of arg, creating a new rope each time
 22		arg is invoked requires N allocation of a small object.
 23		]"
 24
 25inherit ABSTRACT_STRING
 26
 27creation make
 28
 29feature 
 30	make (a_string: ABSTRACT_STRING; an_index: INTEGER; a_value: ABSTRACT_STRING) is
 31	do
 32		create pieces.make
 33		create placeholders.make
 34		initialize_with(a_string)
 35		-- the following is plainly wrong but you got the idea
 36		-- if placeholders.has(an_index) then
 37		-- 	placeholders.put(an_index,a_value)
 38		-- end
 39	end
 40
 41	arg (an_index: INTEGER; a_value: ABSTRACT_STRING): like Current is
 42		do
 43			placeholdes.put(an_index,a_value)
 44			not_yet_implemented
 45			-- je tutto sbagliato, ye tutto da rifare.
 46		end
 47
 48feature  {} -- Implementation
 49	initialize_with (a_string: ABSTRACT_STRING) is
 50		-- Initialize Current from `a_string'
 51	require a_string/=Void
 52	local 
 53		ch: CHARACTER -- or better like first or item
 54		state: INTEGER
 55		in_progress: STRING
 56		placeholder: FILLABLE_STRING_PLACEHOLDER
 57		i, backtrack_i, index: INTEGER
 58	do
 59		-- TODO: evaluate an eventual reimplementation using
 60		-- iterators. This implementation is taken from
 61		-- MESSAGE_FORMATTER
 62		create in_progress.make_empty
 63		from i:=a_string.lower; state:=normal_state
 64		until i>a_string.upper
 65		loop
 66			from 
 67			until i>a_string.upper
 68			loop
 69				ch := a_string.item(i)
 70				inspect state
 71				when print_state then 
 72					in_progress.append_character(ch)
 73					state := normal_state
 74				when normal_state then 
 75					if ch.is_equal(delimiter) then
 76						backtrack_i := i - 1
 77						state := after_delimeter_state
 78					else in_progress.append_character(ch)
 79					end
 80				when after_delimeter_state then 
 81					if ch.is_equal(delimiter) then 
 82						in_progress.append_character(ch)
 83						state := normal_state
 84					elseif ch.is_equal(opening_brace) then
 85						index := 0
 86						state := after_opening_brace_state
 87					else
 88						i := backtrack_i
 89						state := print_state 
 90					end
 91				when after_opening_brace_state then
 92					if ch.is_decimal_digit then
 93						index := 10*index + ch.decimal_value
 94					elseif ch.is_equal(closing_brace) then
 95						pieces.add_last(in_progress)
 96						create in_progress.make_empty
 97						create placeholder
 98						placeholders.put(index,placeholder)
 99						pieces.add_last(placeholder)
100						state := normal_state
101					else
102						i := backtrack_i
103						state := print_state
104					end
105				end
106				i:=i+1
107			end
108		end
109
110feature {} -- Implementation
111	pieces: LINKED_LIST[ABSTRACT_STRING]
112	placeholders_indexes: ARRAY[INTEGER]
113
114	print_state, normal_state, after_delimeter_state, after_opening_brace_state: INTEGER is unique
115	
116	delimiter: CHARACTER is '#'
117	opening_brace: CHARACTER is '('
118	closing_brace: CHARACTER is ')'
119end