static GRegex * create_regex ( const char *input, int case_sensitive ) { #define R( s ) g_regex_new ( s, G_REGEX_OPTIMIZE | ( ( case_sensitive ) ? 0 : G_REGEX_CASELESS ), 0, NULL ) GRegex * retv = NULL; if ( config.glob ) { gchar *r = glob_to_regex ( input ); retv = R ( r ); g_free ( r ); } else if ( config.regex ) { retv = R ( input ); if ( retv == NULL ) { gchar *r = g_regex_escape_string ( input, -1 ); retv = R ( r ); g_free ( r ); } } else{ // TODO; regex should be default? gchar *r = g_regex_escape_string ( input, -1 ); retv = R ( r ); g_free ( r ); } return retv; }
/* Replace all occurences of match_string with replacement_string inside start_string.*/ char* string_helper_gsub (const char* start_string, const char* match_string, const char* replacement_string) { GError * inner_error; GRegex* grx; char* result; char* _tmp5; g_return_val_if_fail (start_string != NULL, NULL); g_return_val_if_fail (match_string != NULL, NULL); g_return_val_if_fail (replacement_string != NULL, NULL); inner_error = NULL; grx = NULL; result = NULL; { GRegex* _tmp1; char* _tmp0; char* _tmp2; char* _tmp3; _tmp1 = NULL; _tmp0 = NULL; grx = (_tmp1 = g_regex_new (_tmp0 = g_regex_escape_string (match_string, -1), 0, 0, &inner_error), (grx == NULL) ? NULL : (grx = (g_regex_unref (grx), NULL)), _tmp1); if (inner_error != NULL) { if (inner_error->domain == G_REGEX_ERROR) { goto __catch4_g_regex_error; } goto __finally4; } _tmp0 = (g_free (_tmp0), NULL); _tmp2 = g_regex_replace_literal (grx, start_string, strlen (start_string), 0, replacement_string, 0, &inner_error); if (inner_error != NULL) { if (inner_error->domain == G_REGEX_ERROR) { goto __catch4_g_regex_error; } goto __finally4; } _tmp3 = NULL; result = (_tmp3 = _tmp2, result = (g_free (result), NULL), _tmp3); } goto __finally4; __catch4_g_regex_error: { GError * e; e = inner_error; inner_error = NULL; { char* _tmp4; _tmp4 = NULL; return (_tmp4 = g_strdup (""), (e == NULL) ? NULL : (e = (g_error_free (e), NULL)), (grx == NULL) ? NULL : (grx = (g_regex_unref (grx), NULL)), result = (g_free (result), NULL), _tmp4); } } __finally4: if (inner_error != NULL) { (grx == NULL) ? NULL : (grx = (g_regex_unref (grx), NULL)); result = (g_free (result), NULL); g_critical ("file %s: line %d: uncaught error: %s", __FILE__, __LINE__, inner_error->message); g_clear_error (&inner_error); return NULL; } _tmp5 = NULL; return (_tmp5 = result, (grx == NULL) ? NULL : (grx = (g_regex_unref (grx), NULL)), _tmp5); }
/* Replace all occurences of match_string with replacement_string inside start_string.*/ char* string_helper_gsub (const char* start_string, const char* match_string, const char* replacement_string) { GError * _inner_error_; GRegex* grx; char* _result_; char* _tmp5_; g_return_val_if_fail (start_string != NULL, NULL); g_return_val_if_fail (match_string != NULL, NULL); g_return_val_if_fail (replacement_string != NULL, NULL); _inner_error_ = NULL; grx = NULL; _result_ = NULL; { GRegex* _tmp1_; char* _tmp0_; char* _tmp2_; char* _tmp3_; _tmp1_ = NULL; _tmp0_ = NULL; grx = (_tmp1_ = g_regex_new (_tmp0_ = g_regex_escape_string (match_string, -1), 0, 0, &_inner_error_), (grx == NULL) ? NULL : (grx = (g_regex_unref (grx), NULL)), _tmp1_); if (_inner_error_ != NULL) { if (_inner_error_->domain == G_REGEX_ERROR) { goto __catch4_g_regex_error; } goto __finally4; } _tmp0_ = (g_free (_tmp0_), NULL); _tmp2_ = g_regex_replace_literal (grx, start_string, strlen (start_string), 0, replacement_string, 0, &_inner_error_); if (_inner_error_ != NULL) { if (_inner_error_->domain == G_REGEX_ERROR) { goto __catch4_g_regex_error; } goto __finally4; } _tmp3_ = NULL; _result_ = (_tmp3_ = _tmp2_, _result_ = (g_free (_result_), NULL), _tmp3_); } goto __finally4; __catch4_g_regex_error: { GError * e; e = _inner_error_; _inner_error_ = NULL; { char* _tmp4_; _tmp4_ = NULL; return (_tmp4_ = g_strdup (""), (e == NULL) ? NULL : (e = (g_error_free (e), NULL)), (grx == NULL) ? NULL : (grx = (g_regex_unref (grx), NULL)), _result_ = (g_free (_result_), NULL), _tmp4_); } } __finally4: if (_inner_error_ != NULL) { (grx == NULL) ? NULL : (grx = (g_regex_unref (grx), NULL)); _result_ = (g_free (_result_), NULL); g_critical ("file %s: line %d: uncaught error: %s", __FILE__, __LINE__, _inner_error_->message); g_clear_error (&_inner_error_); return NULL; } _tmp5_ = NULL; return (_tmp5_ = _result_, (grx == NULL) ? NULL : (grx = (g_regex_unref (grx), NULL)), _tmp5_); }
static gpointer pk_backend_pattern_regex (PkBackend *backend, const gchar *needle, GError **error) { _cleanup_free_ gchar *pattern = NULL; g_return_val_if_fail (needle != NULL, NULL); pattern = g_regex_escape_string (needle, -1); return g_regex_new (pattern, G_REGEX_CASELESS, 0, error); }
static void update_regex (IdeTerminalSearch *self) { const char *search_text; gboolean caseless; g_autofree gchar *pattern = NULL; g_autoptr(GError) error = NULL; g_assert (IDE_IS_TERMINAL_SEARCH (self)); search_text = gtk_entry_get_text (GTK_ENTRY (self->search_entry)); caseless = !gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (self->match_case_checkbutton)); if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (self->regex_checkbutton))) pattern = g_strdup (search_text); else pattern = g_regex_escape_string (search_text, -1); if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (self->entire_word_checkbutton))) { char *new_pattern; new_pattern = g_strdup_printf ("\\b%s\\b", pattern); g_free (pattern); pattern = new_pattern; } if (self->regex_caseless == caseless && g_strcmp0 (self->regex_pattern, pattern) == 0) return; if (self->regex) { vte_regex_unref (self->regex); } g_clear_pointer (&self->regex_pattern, g_free); if (search_text[0] != '\0') { guint32 compile_flags; compile_flags = PCRE2_UTF | PCRE2_NO_UTF_CHECK | PCRE2_MULTILINE; if (caseless) compile_flags |= PCRE2_CASELESS; self->regex = vte_regex_new_for_search (pattern, -1, compile_flags, &error); if (self->regex != NULL) self->regex_pattern = g_steal_pointer (&pattern); } else self->regex = NULL; update_sensitivity (self); g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_REGEX]); }
static GString *gstr_replace_all(GString *text, const gchar* old_str, const gchar *new_str) { gchar *escaped_s1 = g_regex_escape_string (old_str, -1); GRegex *regex = g_regex_new (escaped_s1, 0, 0, NULL); gchar *new_string = g_regex_replace_literal(regex, text->str, -1, 0, new_str, 0, NULL); g_regex_unref(regex); g_free(escaped_s1); g_string_assign(text, new_string); g_free(new_string); return text; }
static const GRegex * re_url (void) { static GRegex *url_ret = NULL; GString *grist_gstr; char *grist; int i; if (url_ret) return url_ret; grist_gstr = g_string_new (NULL); for (i = 0; uri[i].scheme; i++) { if (i) g_string_append (grist_gstr, "|"); g_string_append (grist_gstr, "("); g_string_append_printf (grist_gstr, "%s:", uri[i].scheme); if (uri[i].flags & URI_AUTHORITY) g_string_append (grist_gstr, "//"); if (uri[i].flags & URI_USERINFO) g_string_append (grist_gstr, USERINFO); else if (uri[i].flags & URI_OPT_USERINFO) g_string_append (grist_gstr, USERINFO "?"); if (uri[i].flags & URI_AUTHORITY) g_string_append (grist_gstr, HOST_URL_OPT_TLD OPT_PORT); if (uri[i].flags & URI_PATH) { char *sep_escaped; sep_escaped = g_regex_escape_string (uri[i].path_sep, strlen(uri[i].path_sep)); g_string_append_printf(grist_gstr, "(" "%s" PATH ")?", sep_escaped); g_free(sep_escaped); } g_string_append(grist_gstr, ")"); } grist = g_string_free (grist_gstr, FALSE); url_ret = make_re (grist); g_free (grist); return url_ret; }
/* Finds the big file in the deployment to figure out its checksum, so * it can then remove it from client's repository. */ static void delete_big_file_object_from_client_repo (EtcData *data) { const gchar *bigfile_path = eos_test_client_get_big_file_path (); const gchar *paths[] = { bigfile_path, NULL }; g_autoptr(GFile) client_repo = NULL; OstreeLsFlags flags = OSTREE_LS_CHECKSUM; g_auto(CmdResult) listed = CMD_RESULT_CLEARED; g_autoptr(GError) error = NULL; g_autofree gchar *escaped_bigfile_path = NULL; g_autofree gchar *regex = NULL; g_autoptr(GRegex) gregex = NULL; gboolean matched; g_autoptr(GMatchInfo) match_info = NULL; g_autofree gchar *checksum = NULL; g_autofree gchar *object = NULL; g_assert_nonnull (data); g_assert_nonnull (data->client); client_repo = eos_test_client_get_repo (data->client); ostree_ls (client_repo, flags, default_ref, paths, &listed, &error); g_assert_no_error (error); g_assert_true (cmd_result_ensure_ok_verbose (&listed)); escaped_bigfile_path = g_regex_escape_string (bigfile_path, -1); regex = g_strdup_printf ("\\s+([0-9a-zA-Z]{64})\\s+%s", escaped_bigfile_path); gregex = g_regex_new (regex, /* compile flags: */ 0, /* match flags: */ 0, &error); g_assert_no_error (error); matched = g_regex_match (gregex, listed.standard_output, /* match flags: */ 0, &match_info); g_assert_true (matched); checksum = g_match_info_fetch (match_info, /* match group: */ 1); object = g_strdup_printf ("%s.file", checksum); etc_delete_object (client_repo, object); }
static char* string_replace (const char* self, const char* old, const char* replacement) { GError * _inner_error_; g_return_val_if_fail (self != NULL, NULL); g_return_val_if_fail (old != NULL, NULL); g_return_val_if_fail (replacement != NULL, NULL); _inner_error_ = NULL; { char* _tmp0_; GRegex* _tmp1_; GRegex* regex; char* _tmp2_; char* _tmp3_; _tmp0_ = NULL; _tmp1_ = NULL; regex = (_tmp1_ = g_regex_new (_tmp0_ = g_regex_escape_string (old, -1), 0, 0, &_inner_error_), _tmp0_ = (g_free (_tmp0_), NULL), _tmp1_); if (_inner_error_ != NULL) { if (_inner_error_->domain == G_REGEX_ERROR) { goto __catch4_g_regex_error; } goto __finally4; } _tmp2_ = g_regex_replace_literal (regex, self, (glong) (-1), 0, replacement, 0, &_inner_error_); if (_inner_error_ != NULL) { (regex == NULL) ? NULL : (regex = (g_regex_unref (regex), NULL)); if (_inner_error_->domain == G_REGEX_ERROR) { goto __catch4_g_regex_error; } goto __finally4; } _tmp3_ = NULL; return (_tmp3_ = _tmp2_, (regex == NULL) ? NULL : (regex = (g_regex_unref (regex), NULL)), _tmp3_); } goto __finally4; __catch4_g_regex_error: { GError * e; e = _inner_error_; _inner_error_ = NULL; { g_assert_not_reached (); (e == NULL) ? NULL : (e = (g_error_free (e), NULL)); } } __finally4: if (_inner_error_ != NULL) { g_critical ("file %s: line %d: uncaught error: %s", __FILE__, __LINE__, _inner_error_->message); g_clear_error (&_inner_error_); return NULL; } }
static GString *sanitize_contents(GumboNode* node, GPtrArray *inlines_ary) { GString *contents = g_string_new(NULL); GString *tagname = get_tag_name(node); gchar *key = g_strjoin(NULL, "|", tagname->str, "|", NULL); g_string_free(tagname, TRUE); // Since we include pipes (|) we have to escape the regex string gchar *key_pattern = g_regex_escape_string(key, -1); g_free(key); gboolean no_entity_substitution = g_regex_match_simple(key_pattern, no_entity_sub, G_REGEX_CASELESS, 0); g_free(key_pattern); // build up result for each child, recursively if need be GumboVector* children = &node->v.element.children; guint i; for (i = 0; i < children->length; ++i) { GumboNode* child = (GumboNode*) (children->data[i]); if (child->type == GUMBO_NODE_TEXT) { if (no_entity_substitution) { g_string_append(contents, child->v.text.text); } else { GString *subd = gstr_substitute_xml_entities_into_text(child->v.text.text); g_string_append(contents, subd->str); g_string_free(subd, TRUE); } } else if (child->type == GUMBO_NODE_ELEMENT || child->type == GUMBO_NODE_TEMPLATE) { GString *child_ser = sanitize(child, inlines_ary); g_string_append(contents, child_ser->str); g_string_free(child_ser, TRUE); } else if (child->type == GUMBO_NODE_WHITESPACE) { // keep all whitespace to keep as close to original as possible g_string_append(contents, child->v.text.text); } else if (child->type != GUMBO_NODE_COMMENT) { // Does this actually exist: (child->type == GUMBO_NODE_CDATA) fprintf(stderr, "unknown element of type: %d\n", child->type); } } return contents; }
GRegex * terminal_search_dialog_get_regex (GtkWidget *dialog) { TerminalSearchDialogPrivate *priv; GRegexCompileFlags compile_flags; const char *text, *pattern; g_return_val_if_fail (GTK_IS_DIALOG (dialog), NULL); priv = TERMINAL_SEARCH_DIALOG_GET_PRIVATE (dialog); g_return_val_if_fail (priv, NULL); pattern = text = terminal_search_dialog_get_search_text (dialog); compile_flags = G_REGEX_OPTIMIZE; if (!GET_FLAG (match_case_checkbutton)) compile_flags |= G_REGEX_CASELESS; if (GET_FLAG (regex_checkbutton)) compile_flags |= G_REGEX_MULTILINE; else pattern = g_regex_escape_string (text, -1); if (GET_FLAG (entire_word_checkbutton)) { const char *old_pattern = pattern; pattern = g_strdup_printf ("\\b%s\\b", pattern); if (old_pattern != text) g_free ((char *) old_pattern); } if (!priv->regex || priv->regex_compile_flags != compile_flags || g_strcmp0 (pattern, g_regex_get_pattern (priv->regex)) != 0) { priv->regex_compile_flags = compile_flags; if (priv->regex) g_regex_unref (priv->regex); /* TODO Error handling */ priv->regex = g_regex_new (pattern, compile_flags, 0, NULL); } if (pattern != text) g_free ((char *) pattern); return priv->regex; }
static gchar *glob_to_regex ( const char *input ) { gchar *r = g_regex_escape_string ( input, -1 ); size_t str_l = strlen ( r ); for ( size_t i = 0; i < str_l; i++ ) { if ( r[i] == '\\' ) { if ( r[i + 1] == '*' ) { r[i] = '.'; } else if ( r[i + 1] == '?' ) { r[i + 1] = 'S'; } i++; } } return r; }
static gboolean replace_start_regex (const GMatchInfo *match_info, GString *expanded_regex, gpointer user_data) { gchar *num_string, *subst, *subst_escaped, *escapes; gint num; struct RegexResolveData *data = user_data; escapes = g_match_info_fetch (match_info, 1); num_string = g_match_info_fetch (match_info, 2); num = _gtk_source_string_to_int (num_string); if (num < 0) { subst = g_match_info_fetch_named (data->start_regex->u.regex.match, num_string); } else { subst = g_match_info_fetch (data->start_regex->u.regex.match, num); } if (subst != NULL) { subst_escaped = g_regex_escape_string (subst, -1); } else { g_warning ("Invalid group: %s", num_string); subst_escaped = g_strdup (""); } g_string_append (expanded_regex, escapes); g_string_append (expanded_regex, subst_escaped); g_free (escapes); g_free (num_string); g_free (subst); g_free (subst_escaped); return FALSE; }
gchar * cut_utils_create_regex_pattern (const gchar *string) { gchar *pattern; if (!string) { pattern = g_strdup(".*"); } else if (strlen(string) > 1 && g_str_has_prefix(string, "/") && g_str_has_suffix(string, "/")) { pattern = g_strndup(string + 1, strlen(string) - 2); } else { gchar *escaped_string; escaped_string = g_regex_escape_string(string, -1); pattern = g_strdup_printf("^%s$", escaped_string); g_free(escaped_string); } return pattern; }
static char* string_replace (const char* self, const char* old, const char* replacement) { #line 163 "rygel-http-item-uri.c" char* result = NULL; GError * _inner_error_; #line 1052 "glib-2.0.vapi" g_return_val_if_fail (self != NULL, NULL); #line 1052 "glib-2.0.vapi" g_return_val_if_fail (old != NULL, NULL); #line 1052 "glib-2.0.vapi" g_return_val_if_fail (replacement != NULL, NULL); #line 172 "rygel-http-item-uri.c" _inner_error_ = NULL; { char* _tmp0_; GRegex* _tmp1_; GRegex* regex; char* _tmp2_; #line 1054 "glib-2.0.vapi" regex = (_tmp1_ = g_regex_new (_tmp0_ = g_regex_escape_string (old, -1), 0, 0, &_inner_error_), _g_free0 (_tmp0_), _tmp1_); #line 181 "rygel-http-item-uri.c" if (_inner_error_ != NULL) { if (_inner_error_->domain == G_REGEX_ERROR) { goto __catch2_g_regex_error; } g_critical ("file %s: line %d: unexpected error: %s (%s, %d)", __FILE__, __LINE__, _inner_error_->message, g_quark_to_string (_inner_error_->domain), _inner_error_->code); g_clear_error (&_inner_error_); return NULL; } #line 1055 "glib-2.0.vapi" _tmp2_ = g_regex_replace_literal (regex, self, (gssize) (-1), 0, replacement, 0, &_inner_error_); #line 192 "rygel-http-item-uri.c" if (_inner_error_ != NULL) { _g_regex_unref0 (regex); if (_inner_error_->domain == G_REGEX_ERROR) { goto __catch2_g_regex_error; } _g_regex_unref0 (regex); g_critical ("file %s: line %d: unexpected error: %s (%s, %d)", __FILE__, __LINE__, _inner_error_->message, g_quark_to_string (_inner_error_->domain), _inner_error_->code); g_clear_error (&_inner_error_); return NULL; } result = _tmp2_; _g_regex_unref0 (regex); #line 1055 "glib-2.0.vapi" return result; #line 207 "rygel-http-item-uri.c" } goto __finally2; __catch2_g_regex_error: { GError * e; e = _inner_error_; _inner_error_ = NULL; { #line 1057 "glib-2.0.vapi" g_assert_not_reached (); #line 218 "rygel-http-item-uri.c" _g_error_free0 (e); } } __finally2: if (_inner_error_ != NULL) { g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error_->message, g_quark_to_string (_inner_error_->domain), _inner_error_->code); g_clear_error (&_inner_error_); return NULL; } }
GeeArrayList* string_helper_occurrences (const char* s, const char* t) { GError * _inner_error_; GRegex* grx; GeeArrayList* poss; GMatchInfo* m; GeeArrayList* _tmp7_; g_return_val_if_fail (s != NULL, NULL); g_return_val_if_fail (t != NULL, NULL); _inner_error_ = NULL; grx = NULL; poss = gee_array_list_new (G_TYPE_INT, NULL, NULL, g_direct_equal); m = NULL; { GRegex* _tmp1_; char* _tmp0_; GMatchInfo* _tmp4_; gboolean _tmp3_; GMatchInfo* _tmp2_; _tmp1_ = NULL; _tmp0_ = NULL; grx = (_tmp1_ = g_regex_new (_tmp0_ = g_regex_escape_string (t, -1), 0, 0, &_inner_error_), (grx == NULL) ? NULL : (grx = (g_regex_unref (grx), NULL)), _tmp1_); if (_inner_error_ != NULL) { if (_inner_error_->domain == G_REGEX_ERROR) { goto __catch5_g_regex_error; } goto __finally5; } _tmp0_ = (g_free (_tmp0_), NULL); _tmp4_ = NULL; _tmp2_ = NULL; if ((_tmp3_ = g_regex_match (grx, s, 0, &_tmp2_), m = (_tmp4_ = _tmp2_, (m == NULL) ? NULL : (m = (g_match_info_free (m), NULL)), _tmp4_), _tmp3_)) { gint spos; gint epos; spos = 0; epos = 0; g_match_info_fetch_pos (m, 0, &spos, &epos); gee_collection_add ((GeeCollection*) poss, GINT_TO_POINTER (spos)); while (TRUE) { gboolean _tmp5_; gint mspos; gint mepos; _tmp5_ = g_match_info_next (m, &_inner_error_); if (_inner_error_ != NULL) { if (_inner_error_->domain == G_REGEX_ERROR) { goto __catch5_g_regex_error; } goto __finally5; } if (!_tmp5_) { break; } mspos = 0; mepos = 0; g_match_info_fetch_pos (m, 0, &mspos, &mepos); gee_collection_add ((GeeCollection*) poss, GINT_TO_POINTER (mspos)); } } } goto __finally5; __catch5_g_regex_error: { GError * e; e = _inner_error_; _inner_error_ = NULL; { GeeArrayList* _tmp6_; _tmp6_ = NULL; return (_tmp6_ = gee_array_list_new (G_TYPE_INT, NULL, NULL, g_direct_equal), (e == NULL) ? NULL : (e = (g_error_free (e), NULL)), (grx == NULL) ? NULL : (grx = (g_regex_unref (grx), NULL)), (poss == NULL) ? NULL : (poss = (g_object_unref (poss), NULL)), (m == NULL) ? NULL : (m = (g_match_info_free (m), NULL)), _tmp6_); } } __finally5: if (_inner_error_ != NULL) { (grx == NULL) ? NULL : (grx = (g_regex_unref (grx), NULL)); (poss == NULL) ? NULL : (poss = (g_object_unref (poss), NULL)); (m == NULL) ? NULL : (m = (g_match_info_free (m), NULL)); g_critical ("file %s: line %d: uncaught error: %s", __FILE__, __LINE__, _inner_error_->message); g_clear_error (&_inner_error_); return NULL; } _tmp7_ = NULL; return (_tmp7_ = poss, (grx == NULL) ? NULL : (grx = (g_regex_unref (grx), NULL)), (m == NULL) ? NULL : (m = (g_match_info_free (m), NULL)), _tmp7_); }
static GString *build_attributes(GumboNode* node, GumboAttribute *at, gboolean no_entities, GPtrArray *inlines_ary) { gchar *key = g_strjoin(NULL, "|", at->name, "|", NULL); gchar *key_pattern = g_regex_escape_string(key, -1); g_free(key); gboolean is_permitted_attribute = g_regex_match_simple(key_pattern, permitted_attributes, G_REGEX_CASELESS, 0); gboolean is_protocol_attribute = g_regex_match_simple(key_pattern, protocol_attributes, G_REGEX_CASELESS, 0); gchar *cid_content_id = NULL; g_free(key_pattern); if (!is_permitted_attribute) return g_string_new(NULL); GString *attr_value = g_string_new(at->value); gstr_strip(attr_value); if (is_protocol_attribute) { gchar **protocol_parts = g_regex_split_simple(protocol_separators_regex, attr_value->str, G_REGEX_CASELESS, 0); guint pparts_length = 0; while (protocol_parts[pparts_length]) pparts_length++; gboolean is_permitted_protocol = FALSE; if (pparts_length) { static gchar* protocol_join_str = ":"; gchar* new_joined = g_strjoinv(protocol_join_str, protocol_parts); g_string_assign(attr_value, new_joined); g_free(new_joined); gchar *attr_protocol = g_strjoin(NULL, "|", protocol_parts[0], "|", NULL); gchar *attr_prot_pattern = g_regex_escape_string(attr_protocol, -1); g_free(attr_protocol); is_permitted_protocol = g_regex_match_simple(attr_prot_pattern, permitted_protocols, G_REGEX_CASELESS, 0); g_free(attr_prot_pattern); if (is_permitted_protocol && !g_ascii_strcasecmp(protocol_parts[0], "cid")) cid_content_id = g_strdup(protocol_parts[1]); } g_strfreev(protocol_parts); if (!is_permitted_protocol) { g_string_free(attr_value, TRUE); return g_string_new(NULL); } } gboolean cid_replaced = FALSE; if (cid_content_id) { if (inlines_ary && inlines_ary->len) { guint i; for (i = 0; i < inlines_ary->len; i++) { CollectedPart *inline_body = g_ptr_array_index(inlines_ary, i); if (inline_body->content_id && !g_ascii_strcasecmp(inline_body->content_id, cid_content_id)) { if (inline_body->content->len < MAX_CID_SIZE) { gchar *base64_data = g_base64_encode((const guchar *) inline_body->content->data, inline_body->content->len); gchar *new_attr_value = g_strjoin(NULL, "data:", inline_body->content_type, ";base64,", base64_data, NULL); g_string_assign(attr_value, new_attr_value); g_free(base64_data); g_free(new_attr_value); cid_replaced = TRUE; } } } } // `cid` is not a valid URI schema, so if it was not replaced by the inline content, // we replace it with a 1x1 image which should hide it. If there is content and we missed // it due to the wrong contentId given, it will be avaialable as a downloadable attachment. if (!cid_replaced) g_string_assign(attr_value, MIN_DATA_URI_IMAGE); g_free(cid_content_id); } GString *atts = g_string_new(" "); if (node->type == GUMBO_NODE_ELEMENT) if (((node->v.element.tag == GUMBO_TAG_IMG) && !g_ascii_strcasecmp(at->name, "src")) || (!g_ascii_strcasecmp(at->name, "style") && g_regex_match_simple("url", attr_value->str, G_REGEX_CASELESS, 0))) g_string_append(atts, "data-proxy-"); g_string_append(atts, at->name); // how do we want to handle attributes with empty values // <input type="checkbox" checked /> or <input type="checkbox" checked="" /> gchar quote = at->original_value.data[0]; if (attr_value->len || (quote == '"') || (quote == '\'')) { gchar *qs = ""; if (quote == '\'') qs = "'"; if (quote == '"') qs = "\""; g_string_append(atts, "="); g_string_append(atts, qs); if (no_entities) { g_string_append(atts, attr_value->str); } else { GString *subd = gstr_substitute_xml_entities_into_attributes(quote, attr_value->str); g_string_append(atts, subd->str); g_string_free(subd, TRUE); } g_string_append(atts, qs); } g_string_free(attr_value, TRUE); return atts; }
static char* string_replace (const char* self, const char* old, const char* replacement) { #line 139 "ease-import-oca-service.c" char* result = NULL; GError * _inner_error_ = NULL; #line 1157 "glib-2.0.vapi" g_return_val_if_fail (self != NULL, NULL); #line 1157 "glib-2.0.vapi" g_return_val_if_fail (old != NULL, NULL); #line 1157 "glib-2.0.vapi" g_return_val_if_fail (replacement != NULL, NULL); #line 148 "ease-import-oca-service.c" { char* _tmp0_; GRegex* _tmp1_; GRegex* regex; char* _tmp2_; #line 1159 "glib-2.0.vapi" regex = (_tmp1_ = g_regex_new (_tmp0_ = g_regex_escape_string (old, -1), 0, 0, &_inner_error_), _g_free0 (_tmp0_), _tmp1_); #line 156 "ease-import-oca-service.c" if (_inner_error_ != NULL) { if (_inner_error_->domain == G_REGEX_ERROR) { goto __catch8_g_regex_error; } g_critical ("file %s: line %d: unexpected error: %s (%s, %d)", __FILE__, __LINE__, _inner_error_->message, g_quark_to_string (_inner_error_->domain), _inner_error_->code); g_clear_error (&_inner_error_); return NULL; } #line 1160 "glib-2.0.vapi" _tmp2_ = g_regex_replace_literal (regex, self, (gssize) (-1), 0, replacement, 0, &_inner_error_); #line 167 "ease-import-oca-service.c" if (_inner_error_ != NULL) { _g_regex_unref0 (regex); if (_inner_error_->domain == G_REGEX_ERROR) { goto __catch8_g_regex_error; } _g_regex_unref0 (regex); g_critical ("file %s: line %d: unexpected error: %s (%s, %d)", __FILE__, __LINE__, _inner_error_->message, g_quark_to_string (_inner_error_->domain), _inner_error_->code); g_clear_error (&_inner_error_); return NULL; } result = _tmp2_; _g_regex_unref0 (regex); #line 1160 "glib-2.0.vapi" return result; #line 182 "ease-import-oca-service.c" } goto __finally8; __catch8_g_regex_error: { GError * e; e = _inner_error_; _inner_error_ = NULL; { #line 1162 "glib-2.0.vapi" g_assert_not_reached (); #line 193 "ease-import-oca-service.c" _g_error_free0 (e); } } __finally8: if (_inner_error_ != NULL) { g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error_->message, g_quark_to_string (_inner_error_->domain), _inner_error_->code); g_clear_error (&_inner_error_); return NULL; } }
static GString *sanitize(GumboNode* node, GPtrArray* inlines_ary) { // special case the document node if (node->type == GUMBO_NODE_DOCUMENT) { GString *results = g_string_new("<!DOCTYPE html>\n"); GString *node_ser = sanitize_contents(node, inlines_ary); g_string_append(results, node_ser->str); g_string_free(node_ser, TRUE); return results; } GString *tagname = get_tag_name(node); gchar *key = g_strjoin(NULL, "|", tagname->str, "|", NULL); gchar *key_pattern = g_regex_escape_string(key, -1); g_free(key); gboolean need_special_handling = g_regex_match_simple(key_pattern, special_handling, G_REGEX_CASELESS, 0); gboolean is_empty_tag = g_regex_match_simple(key_pattern, empty_tags, G_REGEX_CASELESS, 0); gboolean no_entity_substitution = g_regex_match_simple(key_pattern, no_entity_sub, G_REGEX_CASELESS, 0); gboolean tag_permitted = g_regex_match_simple(key_pattern, permitted_tags, G_REGEX_CASELESS, 0); g_free(key_pattern); if (!need_special_handling && !tag_permitted) { g_string_free(tagname, TRUE); return g_string_new(NULL); } GString *close = g_string_new(NULL); GString *closeTag = g_string_new(NULL); GString *atts = g_string_new(NULL); const GumboVector *attribs = &node->v.element.attributes; guint i; for (i = 0; i < attribs->length; ++i) { GumboAttribute* at = (GumboAttribute*)(attribs->data[i]); GString *attsstr = build_attributes(node, at, no_entity_substitution, inlines_ary); g_string_append(atts, attsstr->str); g_string_free(attsstr, TRUE); } if (is_empty_tag) { g_string_append_c(close, '/'); } else { g_string_append_printf(closeTag, "</%s>", tagname->str); } GString *contents = sanitize_contents(node, inlines_ary); if (need_special_handling) { gstr_strip(contents); g_string_append_c(contents, '\n'); } GString *results = g_string_new(NULL); g_string_append_printf(results, "<%s%s%s>", tagname->str, atts->str, close->str); g_string_free(atts, TRUE); g_string_free(tagname, TRUE); if (need_special_handling) g_string_append_c(results, '\n'); g_string_append(results, contents->str); g_string_free(contents, TRUE); g_string_append(results, closeTag->str); if (need_special_handling) g_string_append_c(results, '\n'); g_string_free(close, TRUE); g_string_free(closeTag, TRUE); return results; }
static void ide_editor_frame_actions_find (GSimpleAction *action, GVariant *variant, gpointer user_data) { IdeEditorFrame *self = user_data; GtkTextBuffer *buffer; GtkDirectionType search_direction; g_assert (IDE_IS_EDITOR_FRAME (self)); gtk_widget_set_visible (GTK_WIDGET (self->replace_entry), FALSE); gtk_widget_set_visible (GTK_WIDGET (self->replace_button), FALSE); gtk_widget_set_visible (GTK_WIDGET (self->replace_all_button), FALSE); search_direction = (GtkDirectionType) g_variant_get_int32 (variant); ide_source_view_set_search_direction (self->source_view, search_direction); buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (self->source_view)); /* * If the buffer currently has a selection, we prime the search entry with the * selected text. If not, we use our previous search text in the case that it was * cleared by the IdeSourceView internal state. */ if (gtk_text_buffer_get_has_selection (buffer)) { GtkTextIter start_sel; GtkTextIter end_sel; g_autofree gchar *selected_text = NULL; g_autofree gchar *escaped_selected_text = NULL; GtkSourceSearchContext *search_context; GtkSourceSearchSettings *search_settings; gtk_text_buffer_get_selection_bounds (buffer, &start_sel, &end_sel); selected_text = gtk_text_buffer_get_text (buffer, &start_sel, &end_sel, FALSE); search_context = ide_source_view_get_search_context (self->source_view); search_settings = gtk_source_search_context_get_settings (search_context); if (gtk_source_search_settings_get_regex_enabled (search_settings)) escaped_selected_text = g_regex_escape_string (selected_text, -1); else escaped_selected_text = gtk_source_utils_escape_search_text (selected_text); gtk_entry_set_text (GTK_ENTRY (self->search_entry), escaped_selected_text); } else { GtkSourceSearchContext *search_context; GtkSourceSearchSettings *search_settings; const gchar *search_text; search_context = ide_source_view_get_search_context (self->source_view); search_settings = gtk_source_search_context_get_settings (search_context); search_text = gtk_source_search_settings_get_search_text (search_settings); if ((search_text != NULL) && (search_text [0] != '\0')) gtk_entry_set_text (GTK_ENTRY (self->search_entry), search_text); else if (self->previous_search_string != NULL) gtk_entry_set_text (GTK_ENTRY (self->search_entry), self->previous_search_string); } gtk_revealer_set_reveal_child (self->search_revealer, TRUE); gtk_widget_grab_focus (GTK_WIDGET (self->search_entry)); }
static gchar* string_replace (const gchar* self, const gchar* old, const gchar* replacement) { gchar* result = NULL; GError * _inner_error_ = NULL; g_return_val_if_fail (self != NULL, NULL); g_return_val_if_fail (old != NULL, NULL); g_return_val_if_fail (replacement != NULL, NULL); { GRegex* regex = NULL; const gchar* _tmp0_ = NULL; gchar* _tmp1_ = NULL; gchar* _tmp2_ = NULL; GRegex* _tmp3_ = NULL; GRegex* _tmp4_ = NULL; gchar* _tmp5_ = NULL; GRegex* _tmp6_ = NULL; const gchar* _tmp7_ = NULL; gchar* _tmp8_ = NULL; gchar* _tmp9_ = NULL; _tmp0_ = old; _tmp1_ = g_regex_escape_string (_tmp0_, -1); _tmp2_ = _tmp1_; _tmp3_ = g_regex_new (_tmp2_, 0, 0, &_inner_error_); _tmp4_ = _tmp3_; _g_free0 (_tmp2_); regex = _tmp4_; if (_inner_error_ != NULL) { if (_inner_error_->domain == G_REGEX_ERROR) { goto __catch2_g_regex_error; } g_critical ("file %s: line %d: unexpected error: %s (%s, %d)", __FILE__, __LINE__, _inner_error_->message, g_quark_to_string (_inner_error_->domain), _inner_error_->code); g_clear_error (&_inner_error_); return NULL; } _tmp6_ = regex; _tmp7_ = replacement; _tmp8_ = g_regex_replace_literal (_tmp6_, self, (gssize) (-1), 0, _tmp7_, 0, &_inner_error_); _tmp5_ = _tmp8_; if (_inner_error_ != NULL) { _g_regex_unref0 (regex); if (_inner_error_->domain == G_REGEX_ERROR) { goto __catch2_g_regex_error; } _g_regex_unref0 (regex); g_critical ("file %s: line %d: unexpected error: %s (%s, %d)", __FILE__, __LINE__, _inner_error_->message, g_quark_to_string (_inner_error_->domain), _inner_error_->code); g_clear_error (&_inner_error_); return NULL; } _tmp9_ = _tmp5_; _tmp5_ = NULL; result = _tmp9_; _g_free0 (_tmp5_); _g_regex_unref0 (regex); return result; } goto __finally2; __catch2_g_regex_error: { GError* e = NULL; e = _inner_error_; _inner_error_ = NULL; g_assert_not_reached (); _g_error_free0 (e); } __finally2: if (_inner_error_ != NULL) { g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error_->message, g_quark_to_string (_inner_error_->domain), _inner_error_->code); g_clear_error (&_inner_error_); return NULL; } }
static void gb_source_view_update_search (GbSourceView *view) { GbSourceViewPrivate *priv; GRegexCompileFlags flags = 0; GtkTextBuffer *buffer; GtkTextIter begin; GtkTextIter end; GMatchInfo *match_info = NULL; GtkTextTag *search_tag; gboolean has_matches = FALSE; GRegex *regex = NULL; gchar *text; gchar *escaped; g_assert(GB_IS_SOURCE_VIEW(view)); priv = view->priv; buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(view)); gtk_text_buffer_get_bounds(buffer, &begin, &end); search_tag = gb_source_view_ref_search_tag(view); gtk_text_buffer_remove_tag(buffer, search_tag, &begin, &end); if (g_str_empty0(priv->search_text) && !priv->search_regex) { goto cleanup; } if (priv->search_text) { if (!priv->search_case_sensitive) { flags = G_REGEX_CASELESS; } escaped = g_regex_escape_string(priv->search_text, -1); regex = g_regex_new(escaped, flags, 0, NULL); g_free(escaped); } else if (priv->search_regex) { regex = g_regex_ref(priv->search_regex); } if (regex) { text = gtk_text_buffer_get_text(buffer, &begin, &end, TRUE); if (g_regex_match(regex, text, 0, &match_info)) { guint count; guint i; gint begin_pos; gint end_pos; do { count = g_match_info_get_match_count(match_info); for (i = 0; i < count; i++) { if (g_match_info_fetch_pos(match_info, i, &begin_pos, &end_pos)) { gtk_text_buffer_get_iter_at_offset(buffer, &begin, begin_pos); gtk_text_buffer_get_iter_at_offset(buffer, &end, end_pos); gtk_text_buffer_apply_tag(buffer, search_tag, &begin, &end); has_matches = TRUE; } } } while (g_match_info_next(match_info, NULL)); } g_match_info_free(match_info); g_regex_unref(regex); g_free(text); } cleanup: if (priv->has_matches != has_matches) { priv->has_matches = has_matches; g_object_notify_by_pspec(G_OBJECT(view), gParamSpecs[PROP_HAS_MATCHES]); } gtk_widget_queue_draw(GTK_WIDGET(view)); g_object_unref(search_tag); }
static VALUE rg_s_escape_string(G_GNUC_UNUSED VALUE self, VALUE string) { return CSTR2RVAL(g_regex_escape_string(RVAL2CSTR(string), RSTRING_LEN(string))); }
gchar *rclib_lyric_search_lyric(const gchar *uri, const gchar *title, const gchar *artist) { RCLibLyricPrivate *priv; GRegex *regex; GString *key; GDir *dir = NULL; gchar *fdir = NULL, *rname = NULL; gchar *path, *tmp1, *tmp2; const gchar *fname_foreach; gchar *result = NULL; gboolean flag = FALSE; if(uri!=NULL) { path = g_filename_from_uri(uri, NULL, NULL); if(path!=NULL) { fdir = g_path_get_dirname(path); rname = rclib_tag_get_name_from_fpath(path); g_free(path); } } key = g_string_new("^("); if(rname!=NULL) { tmp1 = g_regex_escape_string(rname, -1); g_free(rname); g_string_append(key, tmp1); g_free(tmp1); flag = TRUE; } if(artist!=NULL && title!=NULL && strlen(artist)>0 && strlen(title)>0) { if(flag) g_string_append_c(key, '|'); tmp1 = g_regex_escape_string(title, -1); tmp2 = g_regex_escape_string(artist, -1); g_string_append_printf(key, "%s - %s|%s - %s", tmp1, tmp2, tmp2, tmp1); g_free(tmp1); g_free(tmp2); flag = TRUE; } if(title!=NULL && strlen(title)>0) { if(flag) g_string_append_c(key, '|'); tmp1 = g_regex_escape_string(title, -1); g_string_append(key, tmp1); g_free(tmp1); flag = TRUE; } if(!flag) { g_string_free(key, TRUE); return NULL; } g_string_append(key, ")\\.([Ll][Rr][Cc])$"); regex = g_regex_new(key->str, 0, 0, NULL); g_string_free(key, TRUE); if(regex==NULL) { g_free(fdir); return NULL; } if(fdir!=NULL) dir = g_dir_open(fdir, 0, NULL); if(dir!=NULL) { while((fname_foreach=g_dir_read_name(dir))!=NULL) { if(g_regex_match(regex, fname_foreach, 0, NULL)) { result = g_build_filename(fdir, fname_foreach, NULL); break; } } g_dir_close(dir); g_free(fdir); if(result!=NULL) { g_regex_unref(regex); return result; } } if(lyric_instance==NULL) { g_regex_unref(regex); return NULL; } priv = RCLIB_LYRIC(lyric_instance)->priv; if(priv==NULL || priv->search_dir==NULL) { g_regex_unref(regex); return NULL; } dir = g_dir_open(priv->search_dir, 0, NULL); if(dir!=NULL) { while((fname_foreach=g_dir_read_name(dir))!=NULL) { if(g_regex_match(regex, fname_foreach, 0, NULL)) { result = g_build_filename(priv->search_dir, fname_foreach, NULL); break; } } g_dir_close(dir); } g_regex_unref(regex); return result; }
void init_mock_requester (GrlNetWc *self) { g_atomic_int_inc (&refcount); if (refcount > 1) { return; } char *config_filename = g_strdup (g_getenv (GRL_NET_MOCKED_VAR)); enable_mocking = FALSE; int i; if (config_filename == NULL) { return; } /* Read configuration file. */ GError *error = NULL; config = g_key_file_new (); GRL_DEBUG ("Loading mock responses from \"%s\"", config_filename); g_key_file_load_from_file (config, config_filename, G_KEY_FILE_NONE, &error); int version = 0; if (error) { GRL_WARNING ("Failed to load mock file \"%s\": %s", config_filename, error->message); g_clear_error (&error); } else { /* Check if we managed to load a file */ version = g_key_file_get_integer (config, "default", "version", &error); if (error || version < GRL_NET_MOCK_VERSION) { GRL_WARNING ("Unsupported mock version."); g_clear_error (&error); } else { enable_mocking = TRUE; } } if (!enable_mocking) { g_free (config_filename); g_clear_pointer (&config, g_key_file_unref); return; } char **parameter_names = g_key_file_get_string_list (config, "default", "ignored-parameters", NULL, NULL); /* Build regular expressions for ignored query parameters. */ if (parameter_names) { GString *pattern = g_string_new ("(?:^|\\&)"); if (parameter_names[0] && strcmp(parameter_names[0], "*") == 0) { g_string_append (pattern, "[^=&]+"); } else { g_string_append (pattern, "(?:"); for (i = 0; parameter_names[i]; ++i) { if (i) g_string_append (pattern, "|"); char *escaped = g_regex_escape_string (parameter_names[i], -1); g_string_append (pattern, escaped); g_free (escaped); } g_string_append (pattern, ")(?:=[^&]*)?"); } ignored_parameters = g_regex_new (pattern->str, G_REGEX_OPTIMIZE, 0, &error); if (error) { GRL_WARNING ("Failed to compile regular expression " "for ignored query parameters: %s", error->message); g_clear_error (&error); } g_strfreev (parameter_names); g_string_free (pattern, TRUE); } /* Find base path for mock data. */ GFile *file = g_file_new_for_commandline_arg (config_filename); GFile *parent = g_file_get_parent (file); base_path = g_file_get_path (parent); g_object_unref (parent); g_object_unref (file); g_free (config_filename); }
gchar *rc_tag_search_album_file(const gchar *dirname, const RCMusicMetaData *mmd) { gchar *result = NULL; GRegex *realname_regex = NULL; GRegex *artist_regex = NULL; GRegex *title_regex = NULL; GRegex *album_regex = NULL; gchar *path = NULL; gchar *realname = NULL; gchar *realname_pattern = NULL; gchar *tmp, *string; GDir *gdir; const gchar *fname_foreach = NULL; const gchar *match_result = NULL; guint level = 0; if(dirname==NULL || mmd==NULL) return NULL; gdir = g_dir_open(dirname, 0, NULL); if(gdir==NULL) return NULL; if(mmd->uri!=NULL) path = g_filename_from_uri(mmd->uri, NULL, NULL); if(path!=NULL) { realname = rc_tag_get_name_from_fpath(path); g_free(path); } if(realname!=NULL) { tmp = g_regex_escape_string(realname, -1); realname_pattern = g_strdup_printf("^(%s)\\.(BMP|JPG|JPEG|PNG)$", tmp); g_free(tmp); realname_regex = g_regex_new(realname_pattern, G_REGEX_CASELESS, 0, NULL); g_free(realname); g_free(realname_pattern); } if(mmd->title!=NULL && strlen(mmd->title)>0) { string = g_regex_escape_string(mmd->title, -1); tmp = g_strdup_printf("^(%s)\\.(BMP|JPG|JPEG|PNG)$", string); g_free(string); title_regex = g_regex_new(tmp, G_REGEX_CASELESS, 0, NULL); g_free(tmp); } if(mmd->artist!=NULL && strlen(mmd->artist)>0) { string = g_regex_escape_string(mmd->artist, -1); tmp = g_strdup_printf("^(%s)\\.(BMP|JPG|JPEG|PNG)$", string); g_free(string); artist_regex = g_regex_new(tmp, G_REGEX_CASELESS, 0, NULL); g_free(tmp); } if(mmd->album!=NULL && strlen(mmd->album)>0) { string = g_regex_escape_string(mmd->album, -1); tmp = g_strdup_printf("^(%s)\\.(BMP|JPG|JPEG|PNG)$", string); g_free(string); album_regex = g_regex_new(tmp, G_REGEX_CASELESS, 0, NULL); g_free(tmp); } while((fname_foreach=g_dir_read_name(gdir))!=NULL) { if(realname_regex!=NULL) { if(g_regex_match(realname_regex, fname_foreach, 0, NULL)) { match_result = fname_foreach; level = 4; break; } } if(title_regex!=NULL) { if(g_regex_match(title_regex, fname_foreach, 0, NULL) && level<3) { match_result = fname_foreach; level = 3; } } if(album_regex!=NULL) { if(g_regex_match(album_regex, fname_foreach, 0, NULL) && level<2) { match_result = fname_foreach; level = 2; } } if(artist_regex!=NULL) { if(g_regex_match(artist_regex, fname_foreach, 0, NULL) && level<1) { match_result = fname_foreach; level = 1; } } } if(realname_regex!=NULL) g_regex_unref(realname_regex); if(artist_regex!=NULL) g_regex_unref(artist_regex); if(title_regex!=NULL) g_regex_unref(title_regex); if(album_regex!=NULL) g_regex_unref(album_regex); if(match_result!=NULL) result = g_build_filename(dirname, match_result, NULL); g_dir_close(gdir); return result; }
gchar *build_remote_exec_cmd(TrgClient * tc, GtkTreeModel * model, GList * selection, const gchar * input) { TrgPrefs *prefs = trg_client_get_prefs(tc); JsonObject *session = trg_client_get_session(tc); JsonObject *profile = trg_prefs_get_connection(prefs); gchar *work; GRegex *regex, *replacerx; GMatchInfo *match_info; gchar *whole, *wholeEscaped, *id, *tmp, *valuestr, *repeater; JsonNode *replacement; if (!profile) return NULL; work = g_strdup(input); regex = g_regex_new("%{([A-Za-z\\-]+)}(?:\\[(.*)\\])?", 0, 0, NULL); g_regex_match_full(regex, input, -1, 0, 0, &match_info, NULL); if (match_info) { while (g_match_info_matches(match_info)) { whole = g_match_info_fetch(match_info, 0); wholeEscaped = g_regex_escape_string(whole, -1); id = g_match_info_fetch(match_info, 1); repeater = g_match_info_fetch(match_info, 2); replacerx = g_regex_new(wholeEscaped, 0, 0, NULL); valuestr = NULL; if (profile && json_object_has_member(profile, id)) { replacement = json_object_get_member(profile, id); if (JSON_NODE_HOLDS_VALUE(replacement)) valuestr = dump_json_value(replacement); } else if (session && json_object_has_member(session, id)) { replacement = json_object_get_member(session, id); if (JSON_NODE_HOLDS_VALUE(replacement)) valuestr = dump_json_value(replacement); } else { GString *gs = g_string_new(""); GList *li; GtkTreeIter iter; JsonObject *json; gchar *piece; for (li = selection; li; li = g_list_next(li)) { piece = NULL; gtk_tree_model_get_iter(model, &iter, (GtkTreePath *) li->data); gtk_tree_model_get(model, &iter, TORRENT_COLUMN_JSON, &json, -1); if (json_object_has_member(json, id)) { replacement = json_object_get_member(json, id); if (JSON_NODE_HOLDS_VALUE(replacement)) { piece = dump_json_value(replacement); } } if (!piece) { if (!g_strcmp0(id, "full-dir")) { piece = torrent_get_full_dir(json); } else if (!g_strcmp0(id, "full-path")) { piece = torrent_get_full_path(json); } } if (piece) { g_string_append(gs, piece); g_free(piece); } if (!repeater) break; if (piece && li != g_list_last(selection)) g_string_append(gs, repeater); } if (gs->len > 0) valuestr = g_string_free(gs, FALSE); else g_string_free(gs, TRUE); } if (valuestr) { tmp = g_regex_replace(replacerx, work, -1, 0, valuestr, 0, NULL); g_free(work); work = tmp; g_free(valuestr); } g_regex_unref(replacerx); g_free(whole); g_free(repeater); g_free(wholeEscaped); g_free(id); g_match_info_next(match_info, NULL); } g_match_info_free(match_info); } g_regex_unref(regex); return work; }
static void update_search_terms (EphyCompletionModel *model, const char *text) { const char *current; const char *ptr; char *tmp; char *term; GRegex *term_regex; GRegex *quote_regex; gint count; gboolean inside_quotes = FALSE; EphyCompletionModelPrivate *priv = model->priv; if (priv->search_terms) { free_search_terms (priv->search_terms); priv->search_terms = NULL; } quote_regex = g_regex_new ("\"", G_REGEX_OPTIMIZE, G_REGEX_MATCH_NOTEMPTY, NULL); /* * This code loops through the string using pointer arythmetics. * Although the string we are handling may contain UTF-8 chars * this works because only ASCII chars affect what is actually * copied from the string as a search term. */ for (count = 0, current = ptr = text; ptr[0] != '\0'; ptr++, count++) { /* * If we found a double quote character; we will * consume bytes up until the next quote, or * end of line; */ if (ptr[0] == '"') inside_quotes = !inside_quotes; /* * If we found a space, and we are not looking for a * closing double quote, or if the next char is the * end of the string, append what we have already as * a search term. */ if (((ptr[0] == ' ') && (!inside_quotes)) || ptr[1] == '\0') { /* * We special-case the end of the line because * we would otherwise not copy the last character * of the search string, since the for loop will * stop before that. */ if (ptr[1] == '\0') count++; /* * remove quotes, and quote any regex-sensitive * characters */ tmp = g_regex_escape_string (current, count); term = g_regex_replace (quote_regex, tmp, -1, 0, "", G_REGEX_MATCH_NOTEMPTY, NULL); g_strstrip (term); g_free (tmp); /* we don't want empty search terms */ if (term[0] != '\0') { term_regex = g_regex_new (term, G_REGEX_CASELESS | G_REGEX_OPTIMIZE, G_REGEX_MATCH_NOTEMPTY, NULL); priv->search_terms = g_slist_append (priv->search_terms, term_regex); } g_free (term); /* count will be incremented by the for loop */ count = -1; current = ptr + 1; } } g_regex_unref (quote_regex); }