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