1 /*
2  * This file is part of gtkD.
3  *
4  * gtkD is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License
6  * as published by the Free Software Foundation; either version 3
7  * of the License, or (at your option) any later version, with
8  * some exceptions, please read the COPYING file.
9  *
10  * gtkD is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with gtkD; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
18  */
19 
20 // generated automatically - do not change
21 // find conversion definition on APILookup.txt
22 // implement new conversion functionalities on the wrap.utils pakage
23 
24 
25 module glib.MatchInfo;
26 
27 private import glib.ErrorG;
28 private import glib.GException;
29 private import glib.Regex;
30 private import glib.Str;
31 private import glib.c.functions;
32 public  import glib.c.types;
33 private import gtkd.Loader;
34 
35 
36 /**
37  * A GMatchInfo is an opaque struct used to return information about
38  * matches.
39  */
40 public class MatchInfo
41 {
42 	/** the main Gtk struct */
43 	protected GMatchInfo* gMatchInfo;
44 	protected bool ownedRef;
45 
46 	/** Get the main Gtk struct */
47 	public GMatchInfo* getMatchInfoStruct(bool transferOwnership = false)
48 	{
49 		if (transferOwnership)
50 			ownedRef = false;
51 		return gMatchInfo;
52 	}
53 
54 	/** the main Gtk struct as a void* */
55 	protected void* getStruct()
56 	{
57 		return cast(void*)gMatchInfo;
58 	}
59 
60 	/**
61 	 * Sets our main struct and passes it to the parent class.
62 	 */
63 	public this (GMatchInfo* gMatchInfo, bool ownedRef = false)
64 	{
65 		this.gMatchInfo = gMatchInfo;
66 		this.ownedRef = ownedRef;
67 	}
68 
69 	~this ()
70 	{
71 		if ( Linker.isLoaded(LIBRARY_GLIB) && ownedRef )
72 			g_match_info_unref(gMatchInfo);
73 	}
74 
75 
76 	/**
77 	 * Returns a new string containing the text in @string_to_expand with
78 	 * references and escape sequences expanded. References refer to the last
79 	 * match done with @string against @regex and have the same syntax used by
80 	 * g_regex_replace().
81 	 *
82 	 * The @string_to_expand must be UTF-8 encoded even if #G_REGEX_RAW was
83 	 * passed to g_regex_new().
84 	 *
85 	 * The backreferences are extracted from the string passed to the match
86 	 * function, so you cannot call this function after freeing the string.
87 	 *
88 	 * @match_info may be %NULL in which case @string_to_expand must not
89 	 * contain references. For instance "foo\n" does not refer to an actual
90 	 * pattern and '\n' merely will be replaced with \n character,
91 	 * while to expand "\0" (whole match) one needs the result of a match.
92 	 * Use g_regex_check_replacement() to find out whether @string_to_expand
93 	 * contains references.
94 	 *
95 	 * Params:
96 	 *     stringToExpand = the string to expand
97 	 *
98 	 * Returns: the expanded string, or %NULL if an error occurred
99 	 *
100 	 * Since: 2.14
101 	 *
102 	 * Throws: GException on failure.
103 	 */
104 	public string expandReferences(string stringToExpand)
105 	{
106 		GError* err = null;
107 
108 		auto retStr = g_match_info_expand_references(gMatchInfo, Str.toStringz(stringToExpand), &err);
109 
110 		if (err !is null)
111 		{
112 			throw new GException( new ErrorG(err) );
113 		}
114 
115 		scope(exit) Str.freeString(retStr);
116 		return Str.toString(retStr);
117 	}
118 
119 	/**
120 	 * Retrieves the text matching the @match_num'th capturing
121 	 * parentheses. 0 is the full text of the match, 1 is the first paren
122 	 * set, 2 the second, and so on.
123 	 *
124 	 * If @match_num is a valid sub pattern but it didn't match anything
125 	 * (e.g. sub pattern 1, matching "b" against "(a)?b") then an empty
126 	 * string is returned.
127 	 *
128 	 * If the match was obtained using the DFA algorithm, that is using
129 	 * g_regex_match_all() or g_regex_match_all_full(), the retrieved
130 	 * string is not that of a set of parentheses but that of a matched
131 	 * substring. Substrings are matched in reverse order of length, so
132 	 * 0 is the longest match.
133 	 *
134 	 * The string is fetched from the string passed to the match function,
135 	 * so you cannot call this function after freeing the string.
136 	 *
137 	 * Params:
138 	 *     matchNum = number of the sub expression
139 	 *
140 	 * Returns: The matched substring, or %NULL if an error
141 	 *     occurred. You have to free the string yourself
142 	 *
143 	 * Since: 2.14
144 	 */
145 	public string fetch(int matchNum)
146 	{
147 		auto retStr = g_match_info_fetch(gMatchInfo, matchNum);
148 
149 		scope(exit) Str.freeString(retStr);
150 		return Str.toString(retStr);
151 	}
152 
153 	/**
154 	 * Bundles up pointers to each of the matching substrings from a match
155 	 * and stores them in an array of gchar pointers. The first element in
156 	 * the returned array is the match number 0, i.e. the entire matched
157 	 * text.
158 	 *
159 	 * If a sub pattern didn't match anything (e.g. sub pattern 1, matching
160 	 * "b" against "(a)?b") then an empty string is inserted.
161 	 *
162 	 * If the last match was obtained using the DFA algorithm, that is using
163 	 * g_regex_match_all() or g_regex_match_all_full(), the retrieved
164 	 * strings are not that matched by sets of parentheses but that of the
165 	 * matched substring. Substrings are matched in reverse order of length,
166 	 * so the first one is the longest match.
167 	 *
168 	 * The strings are fetched from the string passed to the match function,
169 	 * so you cannot call this function after freeing the string.
170 	 *
171 	 * Returns: a %NULL-terminated array of gchar *
172 	 *     pointers.  It must be freed using g_strfreev(). If the previous
173 	 *     match failed %NULL is returned
174 	 *
175 	 * Since: 2.14
176 	 */
177 	public string[] fetchAll()
178 	{
179 		auto retStr = g_match_info_fetch_all(gMatchInfo);
180 
181 		scope(exit) Str.freeStringArray(retStr);
182 		return Str.toStringArray(retStr);
183 	}
184 
185 	/**
186 	 * Retrieves the text matching the capturing parentheses named @name.
187 	 *
188 	 * If @name is a valid sub pattern name but it didn't match anything
189 	 * (e.g. sub pattern "X", matching "b" against "(?P<X>a)?b")
190 	 * then an empty string is returned.
191 	 *
192 	 * The string is fetched from the string passed to the match function,
193 	 * so you cannot call this function after freeing the string.
194 	 *
195 	 * Params:
196 	 *     name = name of the subexpression
197 	 *
198 	 * Returns: The matched substring, or %NULL if an error
199 	 *     occurred. You have to free the string yourself
200 	 *
201 	 * Since: 2.14
202 	 */
203 	public string fetchNamed(string name)
204 	{
205 		auto retStr = g_match_info_fetch_named(gMatchInfo, Str.toStringz(name));
206 
207 		scope(exit) Str.freeString(retStr);
208 		return Str.toString(retStr);
209 	}
210 
211 	/**
212 	 * Retrieves the position in bytes of the capturing parentheses named @name.
213 	 *
214 	 * If @name is a valid sub pattern name but it didn't match anything
215 	 * (e.g. sub pattern "X", matching "b" against "(?P<X>a)?b")
216 	 * then @start_pos and @end_pos are set to -1 and %TRUE is returned.
217 	 *
218 	 * Params:
219 	 *     name = name of the subexpression
220 	 *     startPos = pointer to location where to store
221 	 *         the start position, or %NULL
222 	 *     endPos = pointer to location where to store
223 	 *         the end position, or %NULL
224 	 *
225 	 * Returns: %TRUE if the position was fetched, %FALSE otherwise.
226 	 *     If the position cannot be fetched, @start_pos and @end_pos
227 	 *     are left unchanged.
228 	 *
229 	 * Since: 2.14
230 	 */
231 	public bool fetchNamedPos(string name, out int startPos, out int endPos)
232 	{
233 		return g_match_info_fetch_named_pos(gMatchInfo, Str.toStringz(name), &startPos, &endPos) != 0;
234 	}
235 
236 	/**
237 	 * Retrieves the position in bytes of the @match_num'th capturing
238 	 * parentheses. 0 is the full text of the match, 1 is the first
239 	 * paren set, 2 the second, and so on.
240 	 *
241 	 * If @match_num is a valid sub pattern but it didn't match anything
242 	 * (e.g. sub pattern 1, matching "b" against "(a)?b") then @start_pos
243 	 * and @end_pos are set to -1 and %TRUE is returned.
244 	 *
245 	 * If the match was obtained using the DFA algorithm, that is using
246 	 * g_regex_match_all() or g_regex_match_all_full(), the retrieved
247 	 * position is not that of a set of parentheses but that of a matched
248 	 * substring. Substrings are matched in reverse order of length, so
249 	 * 0 is the longest match.
250 	 *
251 	 * Params:
252 	 *     matchNum = number of the sub expression
253 	 *     startPos = pointer to location where to store
254 	 *         the start position, or %NULL
255 	 *     endPos = pointer to location where to store
256 	 *         the end position, or %NULL
257 	 *
258 	 * Returns: %TRUE if the position was fetched, %FALSE otherwise. If
259 	 *     the position cannot be fetched, @start_pos and @end_pos are left
260 	 *     unchanged
261 	 *
262 	 * Since: 2.14
263 	 */
264 	public bool fetchPos(int matchNum, out int startPos, out int endPos)
265 	{
266 		return g_match_info_fetch_pos(gMatchInfo, matchNum, &startPos, &endPos) != 0;
267 	}
268 
269 	/**
270 	 * If @match_info is not %NULL, calls g_match_info_unref(); otherwise does
271 	 * nothing.
272 	 *
273 	 * Since: 2.14
274 	 */
275 	public void free()
276 	{
277 		g_match_info_free(gMatchInfo);
278 		ownedRef = false;
279 	}
280 
281 	/**
282 	 * Retrieves the number of matched substrings (including substring 0,
283 	 * that is the whole matched text), so 1 is returned if the pattern
284 	 * has no substrings in it and 0 is returned if the match failed.
285 	 *
286 	 * If the last match was obtained using the DFA algorithm, that is
287 	 * using g_regex_match_all() or g_regex_match_all_full(), the retrieved
288 	 * count is not that of the number of capturing parentheses but that of
289 	 * the number of matched substrings.
290 	 *
291 	 * Returns: Number of matched substrings, or -1 if an error occurred
292 	 *
293 	 * Since: 2.14
294 	 */
295 	public int getMatchCount()
296 	{
297 		return g_match_info_get_match_count(gMatchInfo);
298 	}
299 
300 	/**
301 	 * Returns #GRegex object used in @match_info. It belongs to Glib
302 	 * and must not be freed. Use g_regex_ref() if you need to keep it
303 	 * after you free @match_info object.
304 	 *
305 	 * Returns: #GRegex object used in @match_info
306 	 *
307 	 * Since: 2.14
308 	 */
309 	public Regex getRegex()
310 	{
311 		auto __p = g_match_info_get_regex(gMatchInfo);
312 
313 		if(__p is null)
314 		{
315 			return null;
316 		}
317 
318 		return new Regex(cast(GRegex*) __p, true);
319 	}
320 
321 	/**
322 	 * Returns the string searched with @match_info. This is the
323 	 * string passed to g_regex_match() or g_regex_replace() so
324 	 * you may not free it before calling this function.
325 	 *
326 	 * Returns: the string searched with @match_info
327 	 *
328 	 * Since: 2.14
329 	 */
330 	public string getString()
331 	{
332 		return Str.toString(g_match_info_get_string(gMatchInfo));
333 	}
334 
335 	/**
336 	 * Usually if the string passed to g_regex_match*() matches as far as
337 	 * it goes, but is too short to match the entire pattern, %FALSE is
338 	 * returned. There are circumstances where it might be helpful to
339 	 * distinguish this case from other cases in which there is no match.
340 	 *
341 	 * Consider, for example, an application where a human is required to
342 	 * type in data for a field with specific formatting requirements. An
343 	 * example might be a date in the form ddmmmyy, defined by the pattern
344 	 * "^\d?\d(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)\d\d$".
345 	 * If the application sees the user’s keystrokes one by one, and can
346 	 * check that what has been typed so far is potentially valid, it is
347 	 * able to raise an error as soon as a mistake is made.
348 	 *
349 	 * GRegex supports the concept of partial matching by means of the
350 	 * #G_REGEX_MATCH_PARTIAL_SOFT and #G_REGEX_MATCH_PARTIAL_HARD flags.
351 	 * When they are used, the return code for
352 	 * g_regex_match() or g_regex_match_full() is, as usual, %TRUE
353 	 * for a complete match, %FALSE otherwise. But, when these functions
354 	 * return %FALSE, you can check if the match was partial calling
355 	 * g_match_info_is_partial_match().
356 	 *
357 	 * The difference between #G_REGEX_MATCH_PARTIAL_SOFT and
358 	 * #G_REGEX_MATCH_PARTIAL_HARD is that when a partial match is encountered
359 	 * with #G_REGEX_MATCH_PARTIAL_SOFT, matching continues to search for a
360 	 * possible complete match, while with #G_REGEX_MATCH_PARTIAL_HARD matching
361 	 * stops at the partial match.
362 	 * When both #G_REGEX_MATCH_PARTIAL_SOFT and #G_REGEX_MATCH_PARTIAL_HARD
363 	 * are set, the latter takes precedence.
364 	 *
365 	 * There were formerly some restrictions on the pattern for partial matching.
366 	 * The restrictions no longer apply.
367 	 *
368 	 * See pcrepartial(3) for more information on partial matching.
369 	 *
370 	 * Returns: %TRUE if the match was partial, %FALSE otherwise
371 	 *
372 	 * Since: 2.14
373 	 */
374 	public bool isPartialMatch()
375 	{
376 		return g_match_info_is_partial_match(gMatchInfo) != 0;
377 	}
378 
379 	/**
380 	 * Returns whether the previous match operation succeeded.
381 	 *
382 	 * Returns: %TRUE if the previous match operation succeeded,
383 	 *     %FALSE otherwise
384 	 *
385 	 * Since: 2.14
386 	 */
387 	public bool matches()
388 	{
389 		return g_match_info_matches(gMatchInfo) != 0;
390 	}
391 
392 	/**
393 	 * Scans for the next match using the same parameters of the previous
394 	 * call to g_regex_match_full() or g_regex_match() that returned
395 	 * @match_info.
396 	 *
397 	 * The match is done on the string passed to the match function, so you
398 	 * cannot free it before calling this function.
399 	 *
400 	 * Returns: %TRUE is the string matched, %FALSE otherwise
401 	 *
402 	 * Since: 2.14
403 	 *
404 	 * Throws: GException on failure.
405 	 */
406 	public bool next()
407 	{
408 		GError* err = null;
409 
410 		auto __p = g_match_info_next(gMatchInfo, &err) != 0;
411 
412 		if (err !is null)
413 		{
414 			throw new GException( new ErrorG(err) );
415 		}
416 
417 		return __p;
418 	}
419 
420 	alias doref = ref_;
421 	/**
422 	 * Increases reference count of @match_info by 1.
423 	 *
424 	 * Returns: @match_info
425 	 *
426 	 * Since: 2.30
427 	 */
428 	public MatchInfo ref_()
429 	{
430 		auto __p = g_match_info_ref(gMatchInfo);
431 
432 		if(__p is null)
433 		{
434 			return null;
435 		}
436 
437 		return new MatchInfo(cast(GMatchInfo*) __p, true);
438 	}
439 
440 	/**
441 	 * Decreases reference count of @match_info by 1. When reference count drops
442 	 * to zero, it frees all the memory associated with the match_info structure.
443 	 *
444 	 * Since: 2.30
445 	 */
446 	public void unref()
447 	{
448 		g_match_info_unref(gMatchInfo);
449 	}
450 }