/src/wrappers/glib/library/utilities/g_pattern.e

http://github.com/tybor/Liberty · Specman e · 185 lines · 79 code · 35 blank · 71 comment · 2 complexity · 43464157f7b637a8386d0af09e806d96 MD5 · raw file

  1. indexing
  2. description: "Glob-style pattern matching."
  3. copyright: "[
  4. Copyright (C) 2007 Paolo Redaelli, Glib developers
  5. This library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public License
  7. as published by the Free Software Foundation; either version 2.1 of
  8. the License, or (at your option) any later version.
  9. This library is distributed in the hope that it will be useful, but
  10. WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with this library; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  16. 02110-1301 USA
  17. ]"
  18. class G_PATTERN
  19. -- Glob-style pattern matching - matches strings against patterns
  20. -- containing '*' (wildcard) and '?' (joker).
  21. -- G_PATTERN match a string against a pattern containing '*' and
  22. -- '?' wildcards with similar semantics as the standard glob()
  23. -- function: '*' matches an arbitrary, possibly empty, string, '?'
  24. -- matches an arbitrary character.
  25. -- Note that in contrast to glob(), the '/' character can be
  26. -- matched by the wildcards, there are no '[...]' character ranges
  27. -- and '*' and '?' can not be escaped to include them literally in
  28. -- a pattern.
  29. -- When multiple strings must be matched against the same pattern,
  30. -- it is better to compile the pattern to a GPatternSpec using
  31. -- `make' and use `match_string' instead of `match_simple'. This
  32. -- avoids the overhead of repeated pattern compilation.
  33. inherit
  34. C_STRUCT redefine is_equal, free end
  35. EIFFEL_OWNED redefine free end
  36. creation make
  37. creation {WRAPPER, WRAPPER_HANDLER} from_external_pointer
  38. feature {} -- Creation
  39. make (a_pattern: STRING) is
  40. -- Compiles `a_pattern' into a G_PATTERN.
  41. -- TODO: `a_pattern' should be a zero-terminated UTF-8
  42. -- encoded string.
  43. require pattern_not_void: a_pattern /= Void
  44. do
  45. from_external_pointer(g_pattern_spec_new(a_pattern.to_external))
  46. end
  47. feature
  48. is_equal (another: like Current): BOOLEAN is
  49. -- Compares Current and `another' patterns. Will they match
  50. -- the same set of strings?
  51. do
  52. Result:=(g_pattern_spec_equal(Current.handle,
  53. another.handle)).to_boolean
  54. end
  55. match (a_string: STRING): BOOLEAN is
  56. -- Matches a string against a compiled pattern.
  57. -- TODO: a_string should be an UTF8 string.
  58. -- TODO: provide a variant of the call the require the
  59. -- reversed string, since there is an algorithm requiring the
  60. -- reverse of the string than in some cases is (much) more
  61. -- efficient. See the comment in feature code for further
  62. -- informations.
  63. require string_not_void: a_string/=Void
  64. do
  65. -- Passing the correct length of the string given is
  66. -- mandatory. The reversed string can be omitted by passing
  67. -- NULL, this is more efficient if the reversed version of
  68. -- the string to be matched is not at hand, as
  69. -- g_pattern_match() will only construct it if the compiled
  70. -- pattern requires reverse matches.
  71. -- Note that, if the user code will (possibly) match a string
  72. -- against a multitude of patterns containing wildcards,
  73. -- chances are high that some patterns will require a
  74. -- reversed string. In this case, it's more efficient to
  75. -- provide the reversed string to avoid multiple
  76. -- constructions thereof in the various calls to
  77. -- g_pattern_match().
  78. -- Note also that the reverse of a UTF-8 encoded string can
  79. -- in general not be obtained by g_strreverse(). This works
  80. -- only if the string doesn't contain any multibyte
  81. -- characters. Glib offers the g_utf_strreverse() function to
  82. -- reverse UTF-8 encoded strings.
  83. -- pspec : a GPatternSpec.
  84. -- string_length : the length of string.
  85. -- string : the UTF-8 encoded string to match.
  86. -- string_reversed : the reverse of string or NULL.
  87. Result:=(g_pattern_match(handle, a_string.count, a_string.to_external,
  88. default_pointer)).to_boolean
  89. end
  90. match_string (a_string: STRING): BOOLEAN is
  91. -- Matches `a_string' against Current compiled pattern. If
  92. -- the string is to be matched against more than one pattern,
  93. -- consider using `match' instead while supplying the
  94. -- reversed string.
  95. -- TODO: `a_string' should be an UTF-8 encoded string to
  96. -- match.
  97. require string_not_void: a_string/=Void
  98. do
  99. Result:=g_pattern_match_string(handle,a_string.to_external).to_boolean
  100. end
  101. match_simple (a_pattern, a_string: STRING): BOOLEAN is
  102. -- Matches `a_string' against `a_pattern' given as a
  103. -- string. If this function is to be called in a loop, it's
  104. -- more efficient to compile the pattern once with `make'
  105. -- and call `match_string' repetively.
  106. -- TODO: `a_pattern': the UTF-8 encoded pattern.
  107. -- TODO: `a_string': the UTF-8 encoded string to match.
  108. require
  109. pattern_not_void: a_pattern/=Void
  110. string_not_void: a_string/=Void
  111. do
  112. Result:=g_pattern_match_simple(a_pattern.to_external, a_string.to_external).to_boolean
  113. end
  114. feature {} -- External calls
  115. g_pattern_spec_new (a_pattern: POINTER): POINTER is
  116. -- GPatternSpec* g_pattern_spec_new (const gchar *pattern);
  117. external "C use <glib.h>"
  118. end
  119. free (a_pattern: POINTER) is
  120. -- void g_pattern_spec_free (GPatternSpec *pspec);
  121. external "C use <glib.h>"
  122. alias "g_pattern_spec_free"
  123. end
  124. g_pattern_spec_equal (a_pspec1,a_pspec2: POINTER): INTEGER is
  125. -- gboolean g_pattern_spec_equal (GPatternSpec *pspec1,
  126. -- GPatternSpec *pspec2);
  127. external "C use <glib.h>"
  128. end
  129. g_pattern_match (a_pspec: POINTER; a_string_length: INTEGER; a_string, a_string_reversed: POINTER): INTEGER is
  130. -- gboolean g_pattern_match (GPatternSpec *pspec, guint
  131. -- string_length, const gchar *string, const gchar
  132. -- *string_reversed);
  133. external "C use <glib.h>"
  134. end
  135. g_pattern_match_string (a_pspec, a_string: POINTER): INTEGER is
  136. -- gboolean g_pattern_match_string (GPatternSpec *pspec,
  137. -- const gchar *string);
  138. external "C use <glib.h>"
  139. end
  140. g_pattern_match_simple (a_pattern,a_string: POINTER): INTEGER is
  141. -- gboolean g_pattern_match_simple (const gchar *pattern,
  142. -- const gchar *string);
  143. external "C use <glib.h>"
  144. end
  145. feature -- size
  146. struct_size: INTEGER is
  147. external "C inline use <glib.h>"
  148. alias "sizeof(GPatternSpec)"
  149. end
  150. end -- class class G_PATTERN