Changeset 41c9a96


Ignore:
Timestamp:
Jul 24, 2009, 12:59:23 AM (15 years ago)
Author:
Anders Kaseorg <andersk@mit.edu>
Branches:
master, release-1.10, release-1.4, release-1.5, release-1.6, release-1.7, release-1.8, release-1.9
Children:
64c9165
Parents:
ab225e0
git-author:
Anders Kaseorg <andersk@mit.edu> (07/24/09 00:51:10)
git-committer:
Anders Kaseorg <andersk@mit.edu> (07/24/09 00:59:23)
Message:
Reimplement search in terms of owl_regex.

The current implementation of stristr has problems and there isn’t a
good replacement available.  This was its only caller, so we can get
rid of it now.

Also, this will make it possible to search with arbitrary regexes if
someone feels like coming up with a syntax.

Signed-off-by: Anders Kaseorg <andersk@mit.edu>
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • filterelement.c

    rd43edd2 r41c9a96  
    6161{
    6262  char * val = owl_filterelement_get_field(m, fe->field);
    63   return !owl_regex_compare(&(fe->re), val);
     63  return !owl_regex_compare(&(fe->re), val, NULL, NULL);
    6464}
    6565
  • fmtext.c

    r4d86e06 r41c9a96  
    285285  char attr;
    286286  short fg, bg, pair;
    287   int search_results, search_len;
    288287 
    289288  if (w==NULL) {
     
    292291  }
    293292
    294   search_results = (do_search
    295                     ? owl_fmtext_search(f, owl_global_get_search_string(&g))
    296                     : 0);
    297   search_len = (search_results
    298                 ? strlen(owl_global_get_search_string(&g))
    299                 : 0);
    300293  s = f->textbuff;
    301294  /* Set default attributes. */
     
    316309      tmp = p[0];
    317310      p[0] = '\0';
    318       if (search_results) {
     311      if (owl_global_is_search_active(&g)) {
    319312        /* Search is active, so highlight search results. */
    320         char tmp2, *ss;
    321         ss = stristr(s, owl_global_get_search_string(&g));
    322         while (ss) {
     313        char tmp2;
     314        int start, end;
     315        while (owl_regex_compare(owl_global_get_search_re(&g), s, &start, &end) == 0) {
    323316          /* Found search string, highlight it. */
    324317
    325           tmp2 = ss[0];
    326           ss[0] = '\0';
     318          tmp2 = s[start];
     319          s[start] = '\0';
    327320          waddstr(w, s);
    328           ss[0] = tmp2;
     321          s[start] = tmp2;
    329322
    330323          _owl_fmtext_wattrset(w, attr ^ OWL_FMTEXT_ATTR_REVERSE);
    331324          _owl_fmtext_wcolor_set(w, pair);
    332325         
    333           tmp2 = ss[search_len];
    334           ss[search_len] = '\0';
    335           waddstr(w, ss);
    336           ss[search_len] = tmp2;
     326          tmp2 = s[end];
     327          s[end] = '\0';
     328          waddstr(w, s + start);
     329          s[end] = tmp2;
    337330
    338331          _owl_fmtext_wattrset(w, attr);
    339332          _owl_fmtext_wcolor_set(w, pair);
    340333
    341           s = ss + search_len;
    342           ss = stristr(s, owl_global_get_search_string(&g));
     334          s += end;
    343335        }
    344336      }
     
    563555 *  insensitive search.
    564556 */
    565 int owl_fmtext_search(owl_fmtext *f, char *string)
    566 {
    567   if (stristr(f->textbuff, string)) return(1);
     557int owl_fmtext_search(owl_fmtext *f, owl_regex *re)
     558{
     559  if (owl_regex_compare(re, f->textbuff, NULL, NULL) == 0) return(1);
    568560  return(0);
    569561}
  • functions.c

    rab225e0 r41c9a96  
    29672967  /* direction is OWL_DIRECTION_DOWNWARDS or OWL_DIRECTION_UPWARDS or
    29682968   * OWL_DIRECTION_NONE */
    2969   owl_global_set_search_string(&g, string);
     2969  owl_regex re;
     2970
     2971  if (string && owl_regex_create_quoted(&re, string) == 0) {
     2972    owl_global_set_search_re(&g, &re);
     2973    owl_regex_free(&re);
     2974  } else {
     2975    owl_global_set_search_re(&g, NULL);
     2976  }
    29702977
    29712978  if (direction == OWL_DIRECTION_NONE)
     
    30203027  for (i=start; i<viewsize && i>=0;) {
    30213028    m=owl_view_get_element(v, i);
    3022     if (owl_message_search(m, owl_global_get_search_string(&g))) {
     3029    if (owl_message_search(m, owl_global_get_search_re(&g))) {
    30233030      owl_global_set_curmsg(&g, i);
    30243031      owl_function_calculate_topmsg(direction);
  • global.c

    r898eb15 r41c9a96  
    6565  owl_fmtext_init_colorpair_mgr(&(g->cpmgr));
    6666  g->debug=OWL_DEBUG;
    67   g->searchstring=NULL;
     67  owl_regex_init(&g->search_re);
    6868  g->starttime=time(NULL); /* assumes we call init only a start time */
    6969  g->lastinputtime=g->starttime;
     
    708708
    709709int owl_global_is_search_active(owl_global *g) {
    710   if (g->searchstring != NULL) return(1);
    711   return(0);
    712 }
    713 
    714 void owl_global_set_search_string(owl_global *g, char *string) {
    715   if (g->searchstring != NULL) owl_free(g->searchstring);
    716   g->searchstring = string ? owl_strdup(string) : NULL;
    717 }
    718 
    719 char *owl_global_get_search_string(owl_global *g) {
    720   if (g->searchstring==NULL) return("");
    721   return(g->searchstring);
     710  if (owl_regex_is_set(&g->search_re)) return(1);
     711  return(0);
     712}
     713
     714void owl_global_set_search_re(owl_global *g, owl_regex *re) {
     715  if (owl_regex_is_set(&g->search_re)) {
     716    owl_regex_free(&g->search_re);
     717    owl_regex_init(&g->search_re);
     718  }
     719  if (re != NULL)
     720    owl_regex_copy(re, &g->search_re);
     721}
     722
     723owl_regex *owl_global_get_search_re(owl_global *g) {
     724  return &g->search_re;
    722725}
    723726
  • message.c

    r4d86e06 r41c9a96  
    673673 * case insensitive because the functions it uses are
    674674 */
    675 int owl_message_search(owl_message *m, char *string)
     675int owl_message_search(owl_message *m, owl_regex *re)
    676676{
    677677
    678678  owl_message_format(m); /* is this necessary? */
    679679 
    680   return (owl_fmtext_search(&(m->fmtext->fmtext), string));
     680  return (owl_fmtext_search(&(m->fmtext->fmtext), re));
    681681}
    682682
  • owl.h

    r898eb15 r41c9a96  
    547547  int newmsgproc_pid;
    548548  int malloced, freed;
    549   char *searchstring;
     549  owl_regex search_re;
    550550  aim_session_t aimsess;
    551551  aim_conn_t bosconn;
  • regex.c

    rd43edd2 r41c9a96  
    4646}
    4747
    48 int owl_regex_compare(owl_regex *re, char *string)
     48int owl_regex_compare(owl_regex *re, char *string, int *start, int *end)
    4949{
    5050  int out, ret;
     51  regmatch_t match;
    5152
    5253  /* if the regex is not set we match */
     
    5556  }
    5657 
    57   ret=regexec(&(re->re), string, 0, NULL, 0);
     58  ret=regexec(&(re->re), string, 1, &match, 0);
    5859  out=ret;
    5960  if (re->negate) {
    6061    out=!out;
     62    match.rm_so = 0;
     63    match.rm_eo = strlen(string);
    6164  }
     65  if (start != NULL) *start = match.rm_so;
     66  if (end != NULL) *end = match.rm_eo;
    6267  return(out);
    6368}
     
    7681void owl_regex_copy(owl_regex *a, owl_regex *b)
    7782{
    78   b->negate=a->negate;
    79   b->string=owl_strdup(a->string);
    80   memcpy(&(b->re), &(a->re), sizeof(regex_t));
     83  owl_regex_create(b, a->string);
    8184}
    8285
Note: See TracChangeset for help on using the changeset viewer.