chiark / gitweb /
utf32_word_split() and utf8_word_split() splits a string into words
[disorder] / lib / words.c
index 2e4001dad28fba8a1a53f068c71bba58b06cecf1..89174cd8e5048ff9047dd1c652fcc8de760d1810 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * This file is part of DisOrder
- * Copyright (C) 2004 Richard Kettlewell
+ * Copyright (C) 2004, 2007 Richard Kettlewell
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
 #include "table.h"
 #include "words.h"
 #include "utf8.h"
+#include "log.h"
+#include "charset.h"
 
-#include "casefold.h"
-#include "unicodegc.h"
+#include "unidata.h"
+#include "unicode.h"
 
 const char *casefold(const char *ptr) {
-  struct dynstr d;
-  int l, r, m;
-  uint32_t c;
-  const struct cm *t;
-  const char *start, *s = ptr;
-
-  dynstr_init(&d);
-  while(*s) {
-    start = s;
-    PARSE_UTF8(s, c, return ptr);
-    /* seek the folded equivalent */
-    t = cm[c & CM_MASK];
-    l = 0;
-    r = cmn[c & CM_MASK] - 1;
-    while(l <= r && c != t[m = (l + r) / 2].ch)
-      if(c < t[m].ch)
-       r = m - 1;
-      else
-       l = m + 1;
-    if(l <= r)
-      dynstr_append_string(&d, t[m].tr);
-    else
-      dynstr_append_bytes(&d, start, s - start);
-  }
-  dynstr_terminate(&d);
-  return d.vec;
-}
-
-static enum unicode_gc_cat cat(uint32_t c) {
-  int l, r, m;
-
-  l = 0;
-  r = sizeof gcs / sizeof *gcs;
-  while(l <= r) {
-    m = (l + r) / 2;
-    if(c < gcs[m].l)
-      r = m - 1;
-    else if(c > gcs[m].h)
-      l = m + 1;
-    else
-      return gcs[m].cat;
-  }
-  return unicode_gc_none;
+  return utf8_casefold_compat(ptr, strlen(ptr), 0);
 }
 
-/* XXX this is a bit kludgy */
-
 char **words(const char *s, int *nvecp) {
-  struct vector v;
-  struct dynstr d;
-  const char *start;
-  uint32_t c;
-  int in_word = 0;
-
-  vector_init(&v);
-  while(*s) {
-    start = s;
-    PARSE_UTF8(s, c, return 0);
-    /* special cases first */
-    switch(c) {
-    case '/':
-    case '.':
-    case '+':
-    case '&':
-    case ':':
-    case '_':
-    case '-':
-      goto separator;
-    }
-    /* do the rest on category */
-    switch(cat(c)) {
-    case unicode_gc_Ll:
-    case unicode_gc_Lm:
-    case unicode_gc_Lo:
-    case unicode_gc_Lt:
-    case unicode_gc_Lu:
-    case unicode_gc_Nd:
-    case unicode_gc_Nl:
-    case unicode_gc_No:
-    case unicode_gc_Sc:
-    case unicode_gc_Sk:
-    case unicode_gc_Sm:
-    case unicode_gc_So:
-      /* letters, digits and symbols are considered to be part of
-       * words */
-      if(!in_word) {
-       dynstr_init(&d);
-       in_word = 1;
-      }
-      dynstr_append_bytes(&d, start, s - start);
-      break;
-
-    case unicode_gc_Cc:
-    case unicode_gc_Cf:
-    case unicode_gc_Co:
-    case unicode_gc_Cs:
-    case unicode_gc_Zl:
-    case unicode_gc_Zp:
-    case unicode_gc_Zs:
-    case unicode_gc_Pe:
-    case unicode_gc_Ps:
-    separator:
-      if(in_word) {
-       dynstr_terminate(&d);
-       vector_append(&v, d.vec);
-       in_word = 0;
-      }
-      break;
-
-    case unicode_gc_Mc:
-    case unicode_gc_Me:
-    case unicode_gc_Mn:
-    case unicode_gc_Pc:
-    case unicode_gc_Pd:
-    case unicode_gc_Pf:
-    case unicode_gc_Pi:
-    case unicode_gc_Po:
-    case unicode_gc_none:
-      /* control and punctuation is completely ignored */
-      break;
+  size_t nv;
+  char **v;
 
-    }
-  }
-  if(in_word) {
-    /* pick up the final word */
-    dynstr_terminate(&d);
-    vector_append(&v, d.vec);
-  }
-  vector_terminate(&v);
-  if(nvecp)
-    *nvecp = v.nvec;
-  return v.vec;
+  v = utf8_word_split(s, strlen(s), &nv);
+  *nvecp = nv;
+  return v;
 }
 
 /*
@@ -174,4 +54,3 @@ c-basic-offset:2
 comment-column:40
 End:
 */
-/* arch-tag:0ea1f1700f14cd031b7f1fbbcca765fa */