Changeset 28ee32b for text.c


Ignore:
Timestamp:
Dec 24, 2007, 2:53:11 AM (13 years ago)
Author:
Alejandro R. Sedeño <asedeno@mit.edu>
Branches:
master, barnowl_perlaim, debian, release-1.4, release-1.5, release-1.6, release-1.7, release-1.8, release-1.9
Children:
762512d
Parents:
5bc0f68
Message:
UTF-8 - first pass

unicode changes:
* remove downstr() from text.c, replace on site with calls to g_utf8_strdown.
  In place downcasing is not a good idea, so the downstr() contract is unfulfillable.

* make owl_text_truncate_cols() and owl_fmtext_truncate_cols() understand character width.
  This may need more work. Some code duplication - see if we can refactor.

* stristr() rewritten to yse g_utf_casefold() instead of downstr(), and restructured to have a single return.

* only_whitespace() rewritten for unicode.

glib changes:
* rewrite owl_sprintf() in terms of g_strdup_vprintf()

WARNING: THIS IS NOT SAFE YET. Network data is not yet sanitized. Non
UTF-8 inputs may do horrible things to you. This phase is just
working on rendering.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • text.c

    r34509d5 r28ee32b  
    5050void owl_text_truncate_cols(char *out, char *in, int acol, int bcol)
    5151{
    52   char *ptr1, *ptr2, *tmpbuff, *last;
    53   int len;
    54 
     52  char *ptr_s, *ptr_e, *ptr_c, *tmpbuff, *last;
     53  int col, cnt;
     54 
    5555  tmpbuff=owl_malloc(strlen(in)+20);
    5656
    5757  strcpy(tmpbuff, "");
    5858  last=in+strlen(in)-1;
    59   ptr1=in;
    60   while (ptr1<last) {
    61     ptr2=strchr(ptr1, '\n');
    62     if (!ptr2) {
     59  ptr_s=in;
     60  while (ptr_s<last) {
     61    ptr_e=strchr(ptr_s, '\n');
     62    if (!ptr_e) {
    6363      /* but this shouldn't happen if we end in a \n */
    6464      break;
    6565    }
    6666   
    67     if (ptr2==ptr1) {
     67    if (ptr_e==ptr_s) {
    6868      strcat(tmpbuff, "\n");
    69       ptr1++;
    70       continue;
    71     }
    72 
     69      ptr_s++;
     70      continue;
     71    }
     72
     73    col = 0;
     74    cnt = 0;
     75    ptr_c = ptr_s;
     76    while(col < bcol && ptr_c < ptr_e) {
     77      gunichar c = g_utf8_get_char(ptr_c);
     78      if (g_unichar_iswide(c)) {
     79        if (col + 2 > bcol) break;
     80        else col += 2;
     81      }
     82      else if (g_unichar_type(c) == G_UNICODE_NON_SPACING_MARK) ; /*do nothing*/
     83      /* We may need more special cases here... unicode spacing is hard. */
     84      else {
     85        if (col + 1 > bcol) break;
     86        else ++col;
     87      }
     88      ptr_c = g_utf8_next_char(ptr_c);
     89      if (col >= acol) ++cnt;
     90      if (col <= acol) ptr_s = ptr_c;
     91    }
     92    strncat(tmpbuff, ptr_s, ptr_c - ptr_s - 1);
     93    strcat(tmpbuff, "\n");
     94    ptr_s = ptr_e + 1;
     95#if 0
    7396    /* we need to check that we won't run over here */
    74     if ( (ptr2-ptr1) < (bcol-acol) ) {
    75       len=ptr2-(ptr1+acol);
     97    if ( (ptr_e-ptr_s) < (bcol-acol) ) {
     98      len=ptr_e-(ptr_s+acol);
    7699    } else {
    77100      len=bcol-acol;
    78101    }
    79     if ((ptr1+len)>=last) {
    80       len-=last-(ptr1+len);
    81     }
    82 
    83     strncat(tmpbuff, ptr1+acol, len);
     102    if ((ptr_s+len)>=last) {
     103      len-=last-(ptr_s+len);
     104    }
     105
     106    strncat(tmpbuff, ptr_s+acol, len);
    84107    strcat(tmpbuff, "\n");
    85108
    86     ptr1=ptr2+1;
     109    ptr_s=ptr_e+1;
     110#endif
    87111  }
    88112  strcpy(out, tmpbuff);
     
    275299char *stristr(char *a, char *b)
    276300{
    277   char *x, *y, *ret;
    278 
    279   if ((x=owl_strdup(a))==NULL) return(NULL);
    280   if ((y=owl_strdup(b))==NULL) return(NULL);
    281   downstr(x);
    282   downstr(y);
    283   ret=strstr(x, y);
    284   if (ret==NULL) {
    285     owl_free(x);
    286     owl_free(y);
    287     return(NULL);
    288   }
    289   ret=ret-x+a;
    290   owl_free(x);
    291   owl_free(y);
     301  char *x, *y;
     302  char *ret = NULL;
     303  if ((x = g_utf8_casefold(a, -1)) != NULL) {
     304    if ((y = g_utf8_casefold(b, -1)) != NULL) {
     305      ret = strstr(x, y);
     306      if (ret != NULL) {
     307        ret = ret - x + a;
     308      }
     309      g_free(y);
     310    }
     311    g_free(x);
     312  }
    292313  return(ret);
    293314}
     
    296317int only_whitespace(char *s)
    297318{
    298   int i;
    299   for (i=0; s[i]; i++) {
    300     if (!isspace((int) s[i])) return(0);
     319  if (g_utf8_validate(s,-1,NULL)) {
     320    char *p;
     321    for(p = s; p[0]; p=g_utf8_next_char(p)) {
     322      if (!g_unichar_isspace(g_utf8_get_char(p))) return 0;
     323    }
     324  }
     325  else {
     326    int i;
     327    for (i=0; s[i]; i++) {
     328      if (!isspace((int) s[i])) return(0);
     329    }
    301330  }
    302331  return(1);
Note: See TracChangeset for help on using the changeset viewer.