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  * Conversion parameters:
26  * inFile  = 
27  * outPack = glib
28  * outFile = MatchInfo
29  * strct   = GMatchInfo
30  * realStrct=
31  * ctorStrct=
32  * clss    = MatchInfo
33  * interf  = 
34  * class Code: Yes
35  * interface Code: No
36  * template for:
37  * extend  = 
38  * implements:
39  * prefixes:
40  * 	- g_match_info_
41  * omit structs:
42  * omit prefixes:
43  * omit code:
44  * omit signals:
45  * imports:
46  * 	- glib.Str
47  * 	- glib.ErrorG
48  * 	- glib.GException
49  * 	- glib.Regex
50  * 	- gtkc.Loader
51  * 	- gtkc.paths
52  * structWrap:
53  * 	- GMatchInfo* -> MatchInfo
54  * 	- GRegex* -> Regex
55  * module aliases:
56  * local aliases:
57  * overrides:
58  */
59 
60 module glib.MatchInfo;
61 
62 public  import gtkc.glibtypes;
63 
64 private import gtkc.glib;
65 private import glib.ConstructionException;
66 
67 private import glib.Str;
68 private import glib.ErrorG;
69 private import glib.GException;
70 private import glib.Regex;
71 private import gtkc.Loader;
72 private import gtkc.paths;
73 
74 
75 
76 /**
77  * The g_regex_*() functions implement regular
78  * expression pattern matching using syntax and semantics similar to
79  * Perl regular expression.
80  *
81  * Some functions accept a start_position argument, setting it differs
82  * from just passing over a shortened string and setting G_REGEX_MATCH_NOTBOL
83  * in the case of a pattern that begins with any kind of lookbehind assertion.
84  * For example, consider the pattern "\Biss\B" which finds occurrences of "iss"
85  * in the middle of words. ("\B" matches only if the current position in the
86  * subject is not a word boundary.) When applied to the string "Mississipi"
87  * from the fourth byte, namely "issipi", it does not match, because "\B" is
88  * always false at the start of the subject, which is deemed to be a word
89  * boundary. However, if the entire string is passed , but with
90  * start_position set to 4, it finds the second occurrence of "iss" because
91  * it is able to look behind the starting point to discover that it is
92  * preceded by a letter.
93  *
94  * Note that, unless you set the G_REGEX_RAW flag, all the strings passed
95  * to these functions must be encoded in UTF-8. The lengths and the positions
96  * inside the strings are in bytes and not in characters, so, for instance,
97  * "\xc3\xa0" (i.e. "à") is two bytes long but it is treated as a
98  * single character. If you set G_REGEX_RAW the strings can be non-valid
99  * UTF-8 strings and a byte is treated as a character, so "\xc3\xa0" is two
100  * bytes and two characters long.
101  *
102  * When matching a pattern, "\n" matches only against a "\n" character in
103  * the string, and "\r" matches only a "\r" character. To match any newline
104  * sequence use "\R". This particular group matches either the two-character
105  * sequence CR + LF ("\r\n"), or one of the single characters LF (linefeed,
106  * U+000A, "\n"), VT vertical tab, U+000B, "\v"), FF (formfeed, U+000C, "\f"),
107  * CR (carriage return, U+000D, "\r"), NEL (next line, U+0085), LS (line
108  * separator, U+2028), or PS (paragraph separator, U+2029).
109  *
110  * The behaviour of the dot, circumflex, and dollar metacharacters are
111  * affected by newline characters, the default is to recognize any newline
112  * character (the same characters recognized by "\R"). This can be changed
113  * with G_REGEX_NEWLINE_CR, G_REGEX_NEWLINE_LF and G_REGEX_NEWLINE_CRLF
114  * compile options, and with G_REGEX_MATCH_NEWLINE_ANY,
115  * G_REGEX_MATCH_NEWLINE_CR, G_REGEX_MATCH_NEWLINE_LF and
116  * G_REGEX_MATCH_NEWLINE_CRLF match options. These settings are also
117  * relevant when compiling a pattern if G_REGEX_EXTENDED is set, and an
118  * unescaped "#" outside a character class is encountered. This indicates
119  * a comment that lasts until after the next newline.
120  *
121  * When setting the G_REGEX_JAVASCRIPT_COMPAT flag, pattern syntax and pattern
122  * matching is changed to be compatible with the way that regular expressions
123  * work in JavaScript. More precisely, a lonely ']' character in the pattern
124  * is a syntax error; the '\x' escape only allows 0 to 2 hexadecimal digits, and
125  * you must use the '\u' escape sequence with 4 hex digits to specify a unicode
126  * codepoint instead of '\x' or 'x{....}'. If '\x' or '\u' are not followed by
127  * the specified number of hex digits, they match 'x' and 'u' literally; also
128  * '\U' always matches 'U' instead of being an error in the pattern. Finally,
129  * pattern matching is modified so that back references to an unset subpattern
130  * group produces a match with the empty string instead of an error. See
131  * man:pcreapi(3) for more information.
132  *
133  * Creating and manipulating the same GRegex structure from different
134  * threads is not a problem as GRegex does not modify its internal
135  * state between creation and destruction, on the other hand GMatchInfo
136  * is not threadsafe.
137  *
138  * The regular expressions low-level functionalities are obtained through
139  * the excellent PCRE library
140  * written by Philip Hazel.
141  */
142 public class MatchInfo
143 {
144 	
145 	/** the main Gtk struct */
146 	protected GMatchInfo* gMatchInfo;
147 	
148 	
149 	/** Get the main Gtk struct */
150 	public GMatchInfo* getMatchInfoStruct()
151 	{
152 		return gMatchInfo;
153 	}
154 	
155 	
156 	/** the main Gtk struct as a void* */
157 	protected void* getStruct()
158 	{
159 		return cast(void*)gMatchInfo;
160 	}
161 	
162 	/**
163 	 * Sets our main struct and passes it to the parent class
164 	 */
165 	public this (GMatchInfo* gMatchInfo)
166 	{
167 		this.gMatchInfo = gMatchInfo;
168 	}
169 	
170 	~this ()
171 	{
172 		if ( Linker.isLoaded(LIBRARY.GLIB) && gMatchInfo !is null )
173 		{
174 			g_match_info_free(gMatchInfo);
175 		}
176 	}
177 	
178 	/**
179 	 */
180 	
181 	/**
182 	 * Returns GRegex object used in match_info. It belongs to Glib
183 	 * and must not be freed. Use g_regex_ref() if you need to keep it
184 	 * after you free match_info object.
185 	 * Since 2.14
186 	 * Returns: GRegex object used in match_info
187 	 */
188 	public Regex getRegex()
189 	{
190 		// GRegex * g_match_info_get_regex (const GMatchInfo *match_info);
191 		auto p = g_match_info_get_regex(gMatchInfo);
192 		
193 		if(p is null)
194 		{
195 			return null;
196 		}
197 		
198 		return new Regex(cast(GRegex*) p);
199 	}
200 	
201 	/**
202 	 * Returns the string searched with match_info. This is the
203 	 * string passed to g_regex_match() or g_regex_replace() so
204 	 * you may not free it before calling this function.
205 	 * Since 2.14
206 	 * Returns: the string searched with match_info
207 	 */
208 	public string getString()
209 	{
210 		// const gchar * g_match_info_get_string (const GMatchInfo *match_info);
211 		return Str.toString(g_match_info_get_string(gMatchInfo));
212 	}
213 	
214 	/**
215 	 * Increases reference count of match_info by 1.
216 	 * Since 2.30
217 	 * Returns: match_info
218 	 */
219 	public MatchInfo doref()
220 	{
221 		// GMatchInfo * g_match_info_ref (GMatchInfo *match_info);
222 		auto p = g_match_info_ref(gMatchInfo);
223 		
224 		if(p is null)
225 		{
226 			return null;
227 		}
228 		
229 		return new MatchInfo(cast(GMatchInfo*) p);
230 	}
231 	
232 	/**
233 	 * Decreases reference count of match_info by 1. When reference count drops
234 	 * to zero, it frees all the memory associated with the match_info structure.
235 	 * Since 2.30
236 	 */
237 	public void unref()
238 	{
239 		// void g_match_info_unref (GMatchInfo *match_info);
240 		g_match_info_unref(gMatchInfo);
241 	}
242 	
243 	/**
244 	 * If match_info is not NULL, calls g_match_info_unref(); otherwise does
245 	 * nothing.
246 	 * Since 2.14
247 	 */
248 	public void free()
249 	{
250 		// void g_match_info_free (GMatchInfo *match_info);
251 		g_match_info_free(gMatchInfo);
252 	}
253 	
254 	/**
255 	 * Returns whether the previous match operation succeeded.
256 	 * Since 2.14
257 	 * Returns: TRUE if the previous match operation succeeded, FALSE otherwise
258 	 */
259 	public int matches()
260 	{
261 		// gboolean g_match_info_matches (const GMatchInfo *match_info);
262 		return g_match_info_matches(gMatchInfo);
263 	}
264 	
265 	/**
266 	 * Scans for the next match using the same parameters of the previous
267 	 * call to g_regex_match_full() or g_regex_match() that returned
268 	 * match_info.
269 	 * The match is done on the string passed to the match function, so you
270 	 * cannot free it before calling this function.
271 	 * Since 2.14
272 	 * Returns: TRUE is the string matched, FALSE otherwise
273 	 * Throws: GException on failure.
274 	 */
275 	public int next()
276 	{
277 		// gboolean g_match_info_next (GMatchInfo *match_info,  GError **error);
278 		GError* err = null;
279 		
280 		auto p = g_match_info_next(gMatchInfo, &err);
281 		
282 		if (err !is null)
283 		{
284 			throw new GException( new ErrorG(err) );
285 		}
286 		
287 		return p;
288 	}
289 	
290 	/**
291 	 * Retrieves the number of matched substrings (including substring 0,
292 	 * that is the whole matched text), so 1 is returned if the pattern
293 	 * has no substrings in it and 0 is returned if the match failed.
294 	 * If the last match was obtained using the DFA algorithm, that is
295 	 * using g_regex_match_all() or g_regex_match_all_full(), the retrieved
296 	 * count is not that of the number of capturing parentheses but that of
297 	 * the number of matched substrings.
298 	 * Since 2.14
299 	 * Returns: Number of matched substrings, or -1 if an error occurred
300 	 */
301 	public int getMatchCount()
302 	{
303 		// gint g_match_info_get_match_count (const GMatchInfo *match_info);
304 		return g_match_info_get_match_count(gMatchInfo);
305 	}
306 	
307 	/**
308 	 * Usually if the string passed to g_regex_match*() matches as far as
309 	 * it goes, but is too short to match the entire pattern, FALSE is
310 	 * returned. There are circumstances where it might be helpful to
311 	 * distinguish this case from other cases in which there is no match.
312 	 * Consider, for example, an application where a human is required to
313 	 * type in data for a field with specific formatting requirements. An
314 	 * example might be a date in the form ddmmmyy, defined by the pattern
315 	 * "^\d?\d(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)\d\d$".
316 	 * If the application sees the user’s keystrokes one by one, and can
317 	 * check that what has been typed so far is potentially valid, it is
318 	 * able to raise an error as soon as a mistake is made.
319 	 * GRegex supports the concept of partial matching by means of the
320 	 * G_REGEX_MATCH_PARTIAL_SOFT and G_REGEX_MATCH_PARTIAL_HARD flags.
321 	 * When they are used, the return code for
322 	 * g_regex_match() or g_regex_match_full() is, as usual, TRUE
323 	 * for a complete match, FALSE otherwise. But, when these functions
324 	 * return FALSE, you can check if the match was partial calling
325 	 * g_match_info_is_partial_match().
326 	 * The difference between G_REGEX_MATCH_PARTIAL_SOFT and
327 	 * G_REGEX_MATCH_PARTIAL_HARD is that when a partial match is encountered
328 	 * with G_REGEX_MATCH_PARTIAL_SOFT, matching continues to search for a
329 	 * possible complete match, while with G_REGEX_MATCH_PARTIAL_HARD matching
330 	 * stops at the partial match.
331 	 * When both G_REGEX_MATCH_PARTIAL_SOFT and G_REGEX_MATCH_PARTIAL_HARD
332 	 * are set, the latter takes precedence.
333 	 * There were formerly some restrictions on the pattern for partial matching.
334 	 * The restrictions no longer apply.
335 	 * See man:pcrepartial for more information on partial matching.
336 	 * Since 2.14
337 	 * Returns: TRUE if the match was partial, FALSE otherwise
338 	 */
339 	public int isPartialMatch()
340 	{
341 		// gboolean g_match_info_is_partial_match (const GMatchInfo *match_info);
342 		return g_match_info_is_partial_match(gMatchInfo);
343 	}
344 	
345 	/**
346 	 * Returns a new string containing the text in string_to_expand with
347 	 * references and escape sequences expanded. References refer to the last
348 	 * match done with string against regex and have the same syntax used by
349 	 * g_regex_replace().
350 	 * The string_to_expand must be UTF-8 encoded even if G_REGEX_RAW was
351 	 * passed to g_regex_new().
352 	 * The backreferences are extracted from the string passed to the match
353 	 * function, so you cannot call this function after freeing the string.
354 	 * match_info may be NULL in which case string_to_expand must not
355 	 * contain references. For instance "foo\n" does not refer to an actual
356 	 * pattern and '\n' merely will be replaced with \n character,
357 	 * while to expand "\0" (whole match) one needs the result of a match.
358 	 * Use g_regex_check_replacement() to find out whether string_to_expand
359 	 * contains references.
360 	 * Since 2.14
361 	 * Params:
362 	 * stringToExpand = the string to expand
363 	 * Returns: the expanded string, or NULL if an error occurred. [allow-none]
364 	 * Throws: GException on failure.
365 	 */
366 	public string expandReferences(string stringToExpand)
367 	{
368 		// gchar * g_match_info_expand_references (const GMatchInfo *match_info,  const gchar *string_to_expand,  GError **error);
369 		GError* err = null;
370 		
371 		auto p = g_match_info_expand_references(gMatchInfo, Str.toStringz(stringToExpand), &err);
372 		
373 		if (err !is null)
374 		{
375 			throw new GException( new ErrorG(err) );
376 		}
377 		
378 		return Str.toString(p);
379 	}
380 	
381 	/**
382 	 * Retrieves the text matching the match_num'th capturing
383 	 * parentheses. 0 is the full text of the match, 1 is the first paren
384 	 * set, 2 the second, and so on.
385 	 * If match_num is a valid sub pattern but it didn't match anything
386 	 * (e.g. sub pattern 1, matching "b" against "(a)?b") then an empty
387 	 * string is returned.
388 	 * If the match was obtained using the DFA algorithm, that is using
389 	 * g_regex_match_all() or g_regex_match_all_full(), the retrieved
390 	 * string is not that of a set of parentheses but that of a matched
391 	 * substring. Substrings are matched in reverse order of length, so
392 	 * 0 is the longest match.
393 	 * The string is fetched from the string passed to the match function,
394 	 * so you cannot call this function after freeing the string.
395 	 * Since 2.14
396 	 * Params:
397 	 * matchNum = number of the sub expression
398 	 * Returns: The matched substring, or NULL if an error occurred. You have to free the string yourself. [allow-none]
399 	 */
400 	public string fetch(int matchNum)
401 	{
402 		// gchar * g_match_info_fetch (const GMatchInfo *match_info,  gint match_num);
403 		return Str.toString(g_match_info_fetch(gMatchInfo, matchNum));
404 	}
405 	
406 	/**
407 	 * Retrieves the position in bytes of the match_num'th capturing
408 	 * parentheses. 0 is the full text of the match, 1 is the first
409 	 * paren set, 2 the second, and so on.
410 	 * If match_num is a valid sub pattern but it didn't match anything
411 	 * (e.g. sub pattern 1, matching "b" against "(a)?b") then start_pos
412 	 * and end_pos are set to -1 and TRUE is returned.
413 	 * If the match was obtained using the DFA algorithm, that is using
414 	 * g_regex_match_all() or g_regex_match_all_full(), the retrieved
415 	 * position is not that of a set of parentheses but that of a matched
416 	 * substring. Substrings are matched in reverse order of length, so
417 	 * 0 is the longest match.
418 	 * Since 2.14
419 	 * Params:
420 	 * matchNum = number of the sub expression
421 	 * startPos = pointer to location where to store
422 	 * the start position, or NULL. [out][allow-none]
423 	 * endPos = pointer to location where to store
424 	 * the end position, or NULL. [out][allow-none]
425 	 * Returns: TRUE if the position was fetched, FALSE otherwise. If the position cannot be fetched, start_pos and end_pos are left unchanged
426 	 */
427 	public int fetchPos(int matchNum, out int startPos, out int endPos)
428 	{
429 		// gboolean g_match_info_fetch_pos (const GMatchInfo *match_info,  gint match_num,  gint *start_pos,  gint *end_pos);
430 		return g_match_info_fetch_pos(gMatchInfo, matchNum, &startPos, &endPos);
431 	}
432 	
433 	/**
434 	 * Retrieves the text matching the capturing parentheses named name.
435 	 * If name is a valid sub pattern name but it didn't match anything
436 	 * (e.g. sub pattern "X", matching "b" against "(?P<X>a)?b")
437 	 * then an empty string is returned.
438 	 * The string is fetched from the string passed to the match function,
439 	 * so you cannot call this function after freeing the string.
440 	 * Since 2.14
441 	 * Params:
442 	 * name = name of the subexpression
443 	 * Returns: The matched substring, or NULL if an error occurred. You have to free the string yourself. [allow-none]
444 	 */
445 	public string fetchNamed(string name)
446 	{
447 		// gchar * g_match_info_fetch_named (const GMatchInfo *match_info,  const gchar *name);
448 		return Str.toString(g_match_info_fetch_named(gMatchInfo, Str.toStringz(name)));
449 	}
450 	
451 	/**
452 	 * Retrieves the position in bytes of the capturing parentheses named name.
453 	 * If name is a valid sub pattern name but it didn't match anything
454 	 * (e.g. sub pattern "X", matching "b" against "(?P<X>a)?b")
455 	 * then start_pos and end_pos are set to -1 and TRUE is returned.
456 	 * Since 2.14
457 	 * Params:
458 	 * name = name of the subexpression
459 	 * startPos = pointer to location where to store
460 	 * the start position, or NULL. [out][allow-none]
461 	 * endPos = pointer to location where to store
462 	 * the end position, or NULL. [out][allow-none]
463 	 * Returns: TRUE if the position was fetched, FALSE otherwise. If the position cannot be fetched, start_pos and end_pos are left unchanged.
464 	 */
465 	public int fetchNamedPos(string name, out int startPos, out int endPos)
466 	{
467 		// gboolean g_match_info_fetch_named_pos (const GMatchInfo *match_info,  const gchar *name,  gint *start_pos,  gint *end_pos);
468 		return g_match_info_fetch_named_pos(gMatchInfo, Str.toStringz(name), &startPos, &endPos);
469 	}
470 	
471 	/**
472 	 * Bundles up pointers to each of the matching substrings from a match
473 	 * and stores them in an array of gchar pointers. The first element in
474 	 * the returned array is the match number 0, i.e. the entire matched
475 	 * text.
476 	 * If a sub pattern didn't match anything (e.g. sub pattern 1, matching
477 	 * "b" against "(a)?b") then an empty string is inserted.
478 	 * If the last match was obtained using the DFA algorithm, that is using
479 	 * g_regex_match_all() or g_regex_match_all_full(), the retrieved
480 	 * strings are not that matched by sets of parentheses but that of the
481 	 * matched substring. Substrings are matched in reverse order of length,
482 	 * so the first one is the longest match.
483 	 * The strings are fetched from the string passed to the match function,
484 	 * so you cannot call this function after freeing the string.
485 	 * Since 2.14
486 	 * Returns: a NULL-terminated array of gchar * pointers. It must be freed using g_strfreev(). If the previous match failed NULL is returned. [transfer full]
487 	 */
488 	public string[] fetchAll()
489 	{
490 		// gchar ** g_match_info_fetch_all (const GMatchInfo *match_info);
491 		return Str.toStringArray(g_match_info_fetch_all(gMatchInfo));
492 	}
493 }