/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

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