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