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