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

/src/lib/string/lazy_string.e

http://github.com/tybor/Liberty
Specman e | 202 lines | 142 code | 33 blank | 27 comment | 3 complexity | 717d9d2d9dd07ced624b0cd4dc2cf651 MD5 | raw file
  1-- This file is part of a Liberty Eiffel library.
  2-- See the full copyright at the end.
  3--
  4class LAZY_STRING
  5   -- A string made with the output of an agent function given at creation time.
  6
  7   -- The agent function will be run only when actually required, for example
  8   -- accessing the elements of the string or iterating over it; the result
  9   -- of the agent function will be cached for further usage.
 10inherit
 11   ABSTRACT_STRING
 12      redefine
 13         immutable, copy_slice_to_native, new_iterator, lazy_out, prefix "&"
 14      end
 15
 16create {ANY}
 17   make
 18
 19feature {ANY} -- redefinitions
 20   fill_tagged_out_memory
 21      do
 22         tagged_out_memory.append(once "[lazy outter: ")
 23         tagged_out_memory.append(outter.out)
 24         tagged_out_memory.append(once "]")
 25      end
 26
 27   immutable: BOOLEAN True
 28
 29   new_iterator: ITERATOR[CHARACTER]
 30      do
 31         Result := lazy_out_.new_iterator
 32      end
 33
 34   lazy_out, prefix "&": ABSTRACT_STRING
 35      do
 36         Result := Current
 37      end
 38
 39feature {ANY} -- from ABSTRACT_STRING
 40   copy (other: like Current)
 41      do
 42         check False end
 43      end
 44
 45   item (i: INTEGER): CHARACTER
 46      do
 47         Result := lazy_out_.item(i)
 48      end
 49
 50   is_equal, same_as (other: ABSTRACT_STRING): BOOLEAN
 51      do
 52         Result := lazy_out_.is_equal(other)
 53      end
 54
 55   occurrences (c: CHARACTER): INTEGER
 56      do
 57         Result := lazy_out_.occurrences(c)
 58      end
 59
 60   infix "&" (other: ABSTRACT_STRING): ABSTRACT_STRING
 61      do
 62         create {ROPE} Result.from_strings(Current, other)
 63      end
 64
 65   first: CHARACTER
 66      do
 67         Result := lazy_out_.first
 68      end
 69
 70   last: CHARACTER
 71      do
 72         Result := lazy_out_.last
 73      end
 74
 75   substring (start_index, end_index: INTEGER): like Current
 76      do
 77         create Result.make(agent substring_out(Current, start_index, end_index))
 78      end
 79
 80   intern: FIXED_STRING
 81      do
 82         Result := lazy_out_.intern
 83      end
 84
 85   to_external: POINTER
 86      do
 87         Result := lazy_out_.to_external
 88      end
 89
 90feature {ANY} -- from SEARCHABLE
 91   has (x: like item): BOOLEAN
 92      do
 93         Result := lazy_out_.has(x)
 94      end
 95
 96   fast_has (x: like item): BOOLEAN
 97      do
 98         Result := lazy_out_.fast_has(x)
 99      end
100
101   index_of (element: like item; start_index: INTEGER): INTEGER
102      do
103         Result := lazy_out_.index_of(element, start_index)
104      end
105
106   reverse_index_of (element: like item; start_index: INTEGER): INTEGER
107      do
108         Result := lazy_out_.reverse_index_of(element, start_index)
109      end
110
111   fast_index_of (element: like item; start_index: INTEGER): INTEGER
112      do
113         Result := lazy_out_.fast_index_of(element, start_index)
114      end
115
116   fast_reverse_index_of (element: like item; start_index: INTEGER): INTEGER
117      do
118         Result := lazy_out_.fast_reverse_index_of(element, start_index)
119      end
120
121feature {ANY} -- from HOARD
122   count: INTEGER
123      do
124         Result := lazy_out_.count
125      end
126
127feature {ANY} -- from HASHABLE
128   hash_code: INTEGER
129      do
130         Result := lazy_out_.hash_code
131      end
132
133feature {RECYCLING_POOL}
134   recycle
135      do
136      end
137
138feature {STRING_HANDLER}
139   copy_slice_to_native (start_index, end_index: INTEGER; target: NATIVE_ARRAY[CHARACTER]; target_offset: INTEGER)
140      do
141         lazy_out_.copy_slice_to_native(start_index, end_index, target, target_offset)
142      end
143
144feature {}
145   make (a_outter: like outter)
146      require
147         a_outter /= Void
148      do
149         outter := a_outter
150      ensure
151         outter = a_outter
152      end
153
154   substring_out (a_lazy_string: like Current; start_index, end_index: INTEGER): ABSTRACT_STRING
155      do
156         Result := a_lazy_string.lazy_out_.substring(start_index, end_index)
157      end
158
159   outter: FUNCTION[TUPLE, ABSTRACT_STRING]
160
161   lazy_out_memory: WEAK_REFERENCE[ABSTRACT_STRING]
162
163feature {LAZY_STRING}
164   lazy_out_: ABSTRACT_STRING
165      do
166         if lazy_out_memory /= Void then
167            Result := lazy_out_memory.item
168         end
169         if Result = Void then
170            Result := outter.item([])
171         end
172         if lazy_out_memory = Void then
173            create lazy_out_memory.set_item(Result)
174         else
175            lazy_out_memory.set_item(Result)
176         end
177      end
178
179invariant
180   immutable
181
182end -- class LAZY_STRING
183--
184-- Copyright (C) 2009-2017: by all the people cited in the AUTHORS file.
185--
186-- Permission is hereby granted, free of charge, to any person obtaining a copy
187-- of this software and associated documentation files (the "Software"), to deal
188-- in the Software without restriction, including without limitation the rights
189-- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
190-- copies of the Software, and to permit persons to whom the Software is
191-- furnished to do so, subject to the following conditions:
192--
193-- The above copyright notice and this permission notice shall be included in
194-- all copies or substantial portions of the Software.
195--
196-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
197-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
198-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
199-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
200-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
201-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
202-- THE SOFTWARE.