/[eiffelstudio]/FreeELKS/trunk/library/kernel/string_general.e
ViewVC logotype

Contents of /FreeELKS/trunk/library/kernel/string_general.e

Parent Directory Parent Directory | Revision Log Revision Log


Revision 91477 - (show annotations)
Sun Jan 14 09:47:13 2007 UTC (13 years ago) by ericb
File size: 9931 byte(s)
Synchronized with ISE 6.0.65740
1 indexing
2 description: "Common ancestors to all STRING classes."
3 library: "Free implementation of ELKS library"
4 copyright: "Copyright (c) 1986-2004, Eiffel Software and others"
5 license: "Eiffel Forum License v2 (see forum.txt)"
6 date: "$Date: $"
7 revision: "$Revision: $"
8
9 deferred class
10 STRING_GENERAL
11
12 inherit
13 COMPARABLE
14
15 HASHABLE
16 undefine
17 is_equal
18 end
19
20 STRING_HANDLER
21 undefine
22 is_equal
23 end
24
25 convert
26 as_string_32: {STRING_32},
27 to_cil: {SYSTEM_STRING}
28
29 feature -- Access
30
31 code (i: INTEGER): NATURAL_32 is
32 -- Code at position `i'
33 require
34 valid_index: valid_index (i)
35 deferred
36 end
37
38 index_of_code (c: like code; start_index: INTEGER): INTEGER is
39 -- Position of first occurrence of `c' at or after `start_index';
40 -- 0 if none.
41 require
42 start_large_enough: start_index >= 1
43 start_small_enough: start_index <= count + 1
44 local
45 i, nb: INTEGER
46 do
47 nb := count
48 if start_index <= nb then
49 from
50 i := start_index
51 until
52 i > nb or else code (i) = c
53 loop
54 i := i + 1
55 end
56 if i <= nb then
57 Result := i
58 end
59 end
60 ensure
61 valid_result: Result = 0 or (start_index <= Result and Result <= count)
62 zero_if_absent: (Result = 0) = not substring (start_index, count).has_code (c)
63 found_if_present: substring (start_index, count).has_code (c) implies code (Result) = c
64 none_before: substring (start_index, count).has_code (c) implies
65 not substring (start_index, Result - 1).has_code (c)
66 end
67
68 feature -- Settings
69
70 put_code (v: like code; i: INTEGER) is
71 -- Put code `v' at position `i'.
72 require
73 valid_code: valid_code (v)
74 valid_index: valid_index (i)
75 deferred
76 ensure
77 inserted: code (i) = v
78 stable_count: count = old count
79 stable_before_i: elks_checking implies substring (1, i - 1).is_equal (old substring (1, i - 1))
80 stable_after_i: elks_checking implies substring (i + 1, count).is_equal (old substring (i + 1, count))
81 end
82
83 feature {STRING_HANDLER} -- Settings
84
85 set_count (number: INTEGER) is
86 -- Set `count' to `number' of characters.
87 require
88 valid_count: 0 <= number and number <= capacity
89 deferred
90 ensure
91 new_count: count = number
92 end
93
94 set_internal_hash_code (v: like internal_hash_code) is
95 -- Set `internal_hash_code' with `v'.
96 require
97 v_nonnegative: v >= 0
98 deferred
99 ensure
100 internal_hash_code_set: internal_hash_code = v
101 end
102
103 feature -- Status report
104
105 count: INTEGER is
106 -- Number of characters in Current
107 deferred
108 ensure
109 count_non_negative: Result >= 0
110 end
111
112 capacity: INTEGER is
113 -- Number of characters allocated in Current
114 deferred
115 ensure
116 capacity_non_negative: Result >= 0
117 end
118
119 valid_index (i: INTEGER): BOOLEAN is
120 -- Is `i' within the bounds of the string?
121 deferred
122 end
123
124 valid_code (v: like code): BOOLEAN is
125 -- Is `v' a valid code for Current?
126 deferred
127 end
128
129 is_string_8: BOOLEAN is
130 -- Is `Current' a STRING_8?
131 deferred
132 end
133
134 is_string_32: BOOLEAN is
135 -- Is `Current' a STRING_32?
136 deferred
137 end
138
139 is_valid_as_string_8: BOOLEAN is
140 -- Is `Current' convertible to STRING_8 without information loss?
141 deferred
142 end
143
144 is_empty: BOOLEAN is
145 -- Is structure empty?
146 deferred
147 end
148
149 has_code (c: like code): BOOLEAN is
150 -- Does string include `c'?
151 local
152 i, nb: INTEGER
153 do
154 nb := count
155 if nb > 0 then
156 from
157 i := 1
158 until
159 i > nb or else (code (i) = c)
160 loop
161 i := i + 1
162 end
163 Result := (i <= nb)
164 end
165 ensure then
166 false_if_empty: count = 0 implies not Result
167 true_if_first: count > 0 and then code (1) = c implies Result
168 recurse: (count > 0 and then code (1) /= c) implies
169 (Result = substring (2, count).has_code (c))
170 end
171
172 feature -- Conversion
173
174 frozen to_cil: SYSTEM_STRING is
175 -- Create an instance of SYSTEM_STRING using characters
176 -- of Current between indices `1' and `count'.
177 require
178 is_dotnet: {PLATFORM}.is_dotnet
179 do
180 Result := dotnet_convertor.from_string_to_system_string (Current)
181 ensure
182 to_cil_not_void: Result /= Void
183 end
184
185 to_string_8: STRING is
186 -- Convert `Current' as a STRING_8.
187 require
188 is_valid_as_string_8: is_valid_as_string_8
189 do
190 Result := as_string_8
191 ensure
192 as_string_8_not_void: Result /= Void
193 identity: (is_string_8 and Result = Current) or (not is_string_8 and Result /= Current)
194 end
195
196 as_string_8: STRING is
197 -- Convert `Current' as a STRING_8. If a code of `Current' is
198 -- node a valid code for a STRING_8 it is replaced with the null
199 -- character.
200 local
201 i, nb: INTEGER
202 l_code: like code
203 do
204 if is_string_8 then
205 Result ?= Current
206 else
207 nb := count
208 create Result.make (nb)
209 Result.set_count (nb)
210 from
211 i := 1
212 until
213 i > nb
214 loop
215 l_code := code (i)
216 if Result.valid_code (l_code) then
217 Result.put_code (l_code, i)
218 else
219 Result.put_code (0, i)
220 end
221 i := i + 1
222 end
223 end
224 ensure
225 as_string_8_not_void: Result /= Void
226 identity: (is_string_8 and Result = Current) or (not is_string_8 and Result /= Current)
227 end
228
229 as_string_32, to_string_32: STRING_32 is
230 -- Convert `Current' as a STRING_32.
231 local
232 i, nb: INTEGER
233 do
234 if is_string_32 then
235 Result ?= Current
236 else
237 nb := count
238 create Result.make (nb)
239 Result.set_count (nb)
240 from
241 i := 1
242 until
243 i > nb
244 loop
245 Result.put_code (code (i), i)
246 i := i + 1
247 end
248 end
249 ensure
250 as_string_32_not_void: Result /= Void
251 identity: (is_string_32 and Result = Current) or (not is_string_32 and Result /= Current)
252 end
253
254 feature -- Duplication
255
256 substring (start_index, end_index: INTEGER): like Current is
257 -- Copy of substring containing all characters at indices
258 -- between `start_index' and `end_index'
259 deferred
260 ensure
261 substring_not_void: Result /= Void
262 substring_count: Result.count = end_index - start_index + 1 or Result.count = 0
263 first_item: Result.count > 0 implies Result.code (1) = code (start_index)
264 recurse: Result.count > 0 implies
265 Result.substring (2, Result.count).is_equal (substring (start_index + 1, end_index))
266 end
267
268 feature -- Element change
269
270 append_code (c: like code) is
271 -- Append `c' at end.
272 require
273 valid_code: valid_code (c)
274 local
275 current_count: INTEGER
276 do
277 current_count := count + 1
278 if current_count > capacity then
279 resize (current_count)
280 end
281 set_count (current_count)
282 put_code (c, current_count)
283 ensure then
284 item_inserted: code (count) = c
285 new_count: count = old count + 1
286 stable_before: elks_checking implies substring (1, count - 1).is_equal (old twin)
287 end
288
289 append (s: STRING_GENERAL) is
290 -- Append a copy of `s' at end.
291 require
292 argument_not_void: s /= Void
293 compatible_strings: is_string_8 implies s.is_valid_as_string_8
294 local
295 l_count, l_s_count, l_new_size: INTEGER
296 i: INTEGER
297 do
298 l_s_count := s.count
299 if l_s_count > 0 then
300 l_count := count
301 l_new_size := l_s_count + l_count
302 if l_new_size > capacity then
303 resize (l_new_size)
304 end
305 from
306 i := 1
307 until
308 i > l_s_count
309 loop
310 append_code (s.code (i))
311 i := i + 1
312 end
313 set_count (l_new_size)
314 set_internal_hash_code (0)
315 end
316 ensure
317 new_count: count = old count + old s.count
318 appended: elks_checking implies to_string_32.is_equal (old to_string_32.twin + old s.to_string_32.twin)
319 end
320
321 feature -- Removal
322
323 remove (i: INTEGER) is
324 -- Remove `i'-th character.
325 require
326 valid_index: valid_index (i)
327 deferred
328 ensure
329 new_count: count = old count - 1
330 removed: elks_checking implies
331 to_string_32.is_equal (old substring (1, i - 1).to_string_32 + old substring (i + 1, count).to_string_32)
332 end
333
334 feature -- Resizing
335
336 resize (newsize: INTEGER) is
337 -- Rearrange string so that it can accommodate
338 -- at least `newsize' characters.
339 -- Do not lose any previously entered character.
340 require
341 new_size_non_negative: newsize >= 0
342 deferred
343 end
344
345 feature {NONE} -- Assertion helper
346
347 elks_checking: BOOLEAN is False
348 -- Are ELKS checkings verified? Must be True when changing implementation of STRING_GENERAL or descendant.
349
350 feature {NONE} -- Implementation
351
352 internal_hash_code: INTEGER is
353 --
354 deferred
355 end
356
357 string_searcher: STRING_SEARCHER is
358 -- Facilities to search string in another string.
359 once
360 create Result.make
361 ensure
362 string_searcher_not_void: Result /= Void
363 end
364
365 c_string_provider: C_STRING is
366 -- To create Eiffel strings from C string.
367 once
368 create Result.make_empty (0)
369 ensure
370 c_string_provider_not_void: Result /= Void
371 end
372
373 ctoi_convertor: STRING_TO_INTEGER_CONVERTOR is
374 -- Convertor used to convert string to integer or natural
375 once
376 create Result.make
377 Result.set_leading_separators (" ")
378 Result.set_trailing_separators (" ")
379 Result.set_leading_separators_acceptable (True)
380 Result.set_trailing_separators_acceptable (True)
381 ensure
382 ctoi_convertor_not_void: Result /= Void
383 end
384
385 ctor_convertor: STRING_TO_REAL_CONVERTOR is
386 -- Convertor used to convert string to real or double
387 once
388 create Result.make
389 Result.set_leading_separators (" ")
390 Result.set_trailing_separators (" ")
391 Result.set_leading_separators_acceptable (True)
392 Result.set_trailing_separators_acceptable (True)
393 ensure
394 ctor_convertor_not_void: Result /= Void
395 end
396
397 dotnet_convertor: SYSTEM_STRING_FACTORY is
398 -- Convertor used to convert from and to SYSTEM_STRING.
399 once
400 create Result
401 ensure
402 dotnet_convertor_not_void: Result /= Void
403 end
404
405 end

  ViewVC Help
Powered by ViewVC 1.1.23