Re: Does gtk2 provide a case 'in'-sensitive text search via 'gtk_text_iter_..._search'?



On 02/17/2017 08:26 PM, cecashon aol com wrote:
David,

I asked a question about this on the gtk-devel-list

https://mail.gnome.org/archives/gtk-devel-list/2017-January/msg00018.html

last month. A lot I didn't know about this. For UTF-8 it is a bit complicated
even for English. For GTK's case in-sensitive search they use a casefold
function and a normalizing function. This way you can do a case in-sensitive
search on strings with ligatures and accents. If you are just sticking with
asci english chars then just converting and comparing case should work. There
is a bit more to it though for UTF-8 and unicode chars.

I worked on this a little bit so that I could understand it better. My last
attempt was

https://github.com/cecashon/OrderedSetVelociRaptor/blob/master/Misc/Csamples/search_textbuffer4.c

The GTK developers are working on GTK4. GTK3.22 should be stable for 3. GTK2
or 3 is fine with me.

Eric


Thanks Eric,

  I went though the GtkSourceView-3 code and was digesting the folding (slowly
digesting it). Yes, I'm using nothing but US ASCII set, though it is good to
incorporate (as I learn) the UTF-8 handing for other character sets.

  I finally hit on something I like. I set several search options. I have
options for forward/backward search, search from cursor, search within
selected text, search whole words only. The following is the forward
case-insensitive search. The search term is provided in 'text' and I basically
just walk an iter over the search bounds and set the start and end iters
bracketing the matching term within the buffer. Seems to work fine:

        else {                  /* search forward */
            if (app->optcase) { /* case sensitive  */
                found = gtk_text_iter_forward_search (&iter, text, 0,
                                                    &mstart, &mend, NULL);
            }
            else {  /* case insensitive */
                gunichar c;
                gchar *lctext = g_strdup (text);    /* copy search text */

                str2lower (lctext);     /* convert to lower-case */
                found = FALSE;

                for (;;) {

                    gsize len = textlen;    /* get char at iter */
                    c = g_unichar_tolower (gtk_text_iter_get_char (&iter));

                    if (c == (gunichar)lctext[0]) /* compare 1st in lctext */
                    {
                        mstart = iter;      /* set start iter to current */

                        for (gsize i = 0; i < len; i++)
                        {
                            c = g_unichar_tolower (gtk_text_iter_get_char
(&iter));

                            /* compare/advance -- order IS important */
                            if (c != (gunichar)lctext[i] ||
                                !gtk_text_iter_forward_char (&iter))
                                goto next;  /* start next search */
                        }
                        mend = iter;    /* set end iter  */
                        found = TRUE;   /* found to true */
                        break;
                    }
                    next:;  /* if at end of selecton break */
                    if (!gtk_text_iter_forward_char (&iter))
                        break;
                }
                if (lctext) g_free (lctext);
            }
        }


-- 
David C. Rankin, J.D.,P.E.


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]