PageRenderTime 37ms CodeModel.GetById 30ms app.highlight 2ms RepoModel.GetById 2ms app.codeStats 0ms

/src/wrappers/glib/partially-implemented/g_string.e

http://github.com/tybor/Liberty
Specman e | 235 lines | 163 code | 33 blank | 39 comment | 8 complexity | 49c00e9e103ec462b6a3f3c73a36fc33 MD5 | raw file
  1indexing
  2	copyright: "(C) 2005,2010 Paolo Redaelli "
  3	license: "LGPL v2 or later"
  4	date: "$Date:$"
  5	revision: "$REvision:$"
  6
  7class G_STRING
  8	-- Automatically resized string provided by Glib.
  9
 10	-- Currently it uses the implementation provided by ABSTRACT_STRING
 11	-- whenever possible. TODO: use underlying GLib's implementations when it
 12	-- may improve performances
 13
 14	-- Note: this class is seldom used in other libraries.
 15inherit
 16	ABSTRACT_STRING
 17		redefine
 18			lower
 19		end
 20	C_STRUCT undefine out_in_tagged_out_memory end 
 21	DISPOSABLE undefine out_in_tagged_out_memory end 
 22
 23insert
 24	GSTRING_EXTERNALS undefine out_in_tagged_out_memory end 
 25	GSTRING_STRUCT undefine out_in_tagged_out_memory end 
 26	GLIB_STRING_UTILITY_FUNCTIONS undefine out_in_tagged_out_memory end 
 27
 28	
 29creation {ANY}
 30	make, copy, make_empty, from_string
 31	
 32feature {ANY}
 33	count: INTEGER is
 34		do
 35			Result := gstring_struct_get_len(handle)
 36		end
 37		
 38	capacity: INTEGER is
 39		do
 40			Result := gstring_struct_get_allocated_len(handle)
 41		end
 42		
 43	lower: INTEGER is 0
 44
 45	upper: INTEGER is
 46		do
 47			Result := count-1
 48		end
 49
 50feature {ANY} -- Creation / Modification:
 51	make (needed_capacity: INTEGER) is
 52			-- Initialize the string to have at least `needed_capacity'
 53			-- characters of storage.
 54		do
 55			handle := g_string_sized_new (needed_capacity)
 56		ensure
 57			capacity >= needed_capacity
 58		end
 59
 60	make_empty is
 61			-- Create an empty string.
 62		do
 63			make(0)
 64      ensure
 65         empty: count = 0
 66	 end
 67
 68	from_string (a_string: ABSTRACT_STRING) is
 69			-- Create a new G_STRING from `a_string'
 70		require a_string_not_void: a_string/=Void
 71		do
 72			handle := g_string_new (a_string.to_external)
 73		end
 74	
 75feature {ANY} 
 76	item (i: INTEGER): CHARACTER is
 77			-- Character at position `i'.
 78		require
 79			valid_index: i.in_range (lower, upper)
 80		do
 81			Result := array.item(i)
 82		end
 83
 84	hash_code: INTEGER is
 85		do
 86			Result := g_string_hash(handle).hash_code
 87		end
 88
 89	is_equal (other: ABSTRACT_STRING): BOOLEAN is
 90			-- Do both strings have the same character sequence?
 91			--
 92			-- See also `same_as'.
 93		local os: G_STRING; ci,oi: ITERATOR[CHARACTER]
 94		do
 95			os ?= other
 96			if os/=Void then
 97				Result := g_string_equal(handle, other.handle).to_boolean
 98			else -- different types
 99				Result:= count=other.count 
100				from
101					ci:=new_iterator; ci.start
102					oi:=other.new_iterator; oi.start
103				until not Result or else ci.is_off
104				loop
105					Result := (ci.item = oi.item)
106					ci.next; oi.next
107				end
108				check ci.is_off implies oi.is_off end
109			end
110		end
111
112	index_of (c: CHARACTER; start_index: INTEGER): INTEGER is
113		local i: INTEGER; found: BOOLEAN
114		do
115			from i:=start_index until not found or else i<=upper
116			loop
117				if item(i)=c then found := True; Result :=i
118				else i:=i+1
119				end
120			end
121		end
122
123	-- has_suffix (s: ABSTRACT_STRING): BOOLEAN is
124	-- 		-- True if suffix of `Current' is `s'.
125	-- 	do
126	-- 		if {NATIVELY_STORED_STRING} ?:= s then 
127	-- 			Result:=(g_str_has_suffix(handle, s.to_external)).to_boolean
128	-- 		else 
129	-- 			Result := Precursor(s)
130	-- 		end
131	-- 	end
132
133	-- has_prefix (p: ABSTRACT_STRING): BOOLEAN is
134	-- 		-- True if prefix of `Current' is `p'.
135	-- 	do
136	-- 		if {NATIVELY_STORED_STRING} ?:= s then 
137	-- 			Result:=(g_str_has_prefix(handle, s.to_external)).to_boolean
138	-- 		else 
139	-- 			Result := Precursor(s)
140	-- 		end
141	-- 	end
142
143feature {ANY} -- Modification:
144	resize (new_count: NATURAL_32) is 
145local
146		do
147			if new_count < count then g_string_truncate (handle, new_count)
148			else handle := g_string_set_size (handle, new_count)
149			end
150		end
151
152	clear is
153		do
154			handle := g_string_set_size (handle, 0.to_natural_32)
155		end
156
157	copy (other: like Current) is
158			-- Copy `other' onto Current.
159		do
160			handle := g_string_new_len (gstring_struct_get_str(other.handle), gstring_struct_get_len(other.handle))
161		end
162
163	fill_with (c: CHARACTER) is
164		local i:INTEGER
165		do
166			from i:=lower until i<upper loop put(c,i); i:=i+1 end
167		end
168
169	append (s: STRING) is
170		do
171			handle := g_string_append (handle, s.to_external)
172		end
173
174	prepend (other: STRING) is
175		do
176			handle:=g_string_prepend(handle, other.to_external)
177		end
178
179	put (c: CHARACTER; i: INTEGER) is
180			-- Put `c' at index `i' (start counting with 1).
181		require valid_index: i.in_range (1, count)
182		do
183			array.put(c, i-1)
184		end
185   
186   add_last (c: CHARACTER) is
187         -- append `c'
188		local
189         oc: INTEGER
190		do
191         oc := count
192         resize ((count + 1).to_natural_32)
193		 array.put(c, oc)         
194      ensure
195         increased: count = old count + 1
196         value_set: item (count) = c
197      end
198   
199feature -- Conversion to STRING
200	to_string: STRING is
201			-- A string holding a copy of Current
202		do
203			create Result.from_external_copy (c_string)
204		end
205
206	as_string: FIXED_STRING is
207			-- A string holding the very same content of Current
208			-- (i.e. using the same memory area.)
209		do
210			create Result.from_external (c_string)
211		end
212	
213feature  -- Disposing
214	dispose is
215		local
216         p: POINTER
217		do
218			p := g_string_free (handle, 1)
219			handle := default_pointer
220		end
221
222feature {G_STRING}
223	c_string: POINTER is
224		-- Shortcut for gstring_struct_get_str(handle)
225	do
226		Result := gstring_struct_get_str(handle)
227	end
228
229	array: NATIVE_ARRAY[CHARACTER] is
230		-- Direct access to content
231	do
232		Result := Result.from_pointer(c_string)
233	end
234end
235