aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorH. Peter Anvin <hpa@smyrno.hos.anvin.org>2005-12-04 17:04:34 -0800
committerH. Peter Anvin <hpa@smyrno.hos.anvin.org>2005-12-04 17:04:34 -0800
commit26995d091d2c6082ebd386bdc021742ab75589b7 (patch)
treeabae822a45a4eb34672da6e3c0ac7ecab8255083
parent3f0ea61d471da2d5d25f839657ba79401df596f7 (diff)
downloadlibucd-26995d091d2c6082ebd386bdc021742ab75589b7.tar.gz
name-to-ucs mapping function, first attempt
-rw-r--r--Makefile9
-rw-r--r--compiler.h16
-rw-r--r--libucd_int.h8
-rw-r--r--nametoucs.c42
4 files changed, 70 insertions, 5 deletions
diff --git a/Makefile b/Makefile
index 605e788..0d61fe5 100644
--- a/Makefile
+++ b/Makefile
@@ -47,7 +47,7 @@ CVT_FILES = gen/jamo.c gen/nameslist.tab gen/nametoucs.keys gen/nametoucs.tab \
LIBSRCS = proparray.c gen/nametoucs_hash.c gen/ucstoname_hash.c \
gen/jamo.c gen/nameslist.c gen/nameslist_dict.c \
- gen/ucstoname_tab.c gen/nametoucs_tab.c
+ gen/ucstoname_tab.c gen/nametoucs_tab.c nametoucs.c
LIBOBJS = $(patsubst %.c,%.o,$(LIBSRCS))
SO_OBJS = $(patsubst %.c,%.lo,$(LIBSRCS))
@@ -128,8 +128,8 @@ endif
proparray.o: proparray.c ucd.h libucd_int.h gen/proparray.c
proparray.lo: proparray.c ucd.h libucd_int.h gen/proparray.c
-mk_ucstoname_tab.ho: gen/ucstoname_hash.h
-mk_nametoucs_tab.ho: gen/nametoucs_hash.h
+mk_ucstoname_tab.ho: mk_ucstoname_tab.c gen/ucstoname_hash.h
+mk_nametoucs_tab.ho: mk_nametoucs_tab.c gen/nametoucs_hash.h
gen/ucstoname_tab.o: gen/ucstoname_tab.c libucd_int.h
gen/ucstoname_tab.lo: gen/ucstoname_tab.c libucd_int.h
@@ -139,3 +139,6 @@ gen/nametoucs_tab.lo: gen/nametoucs_tab.c libucd_int.h
gen/nameslist_dict.o: gen/nameslist_dict.c
gen/nameslist_dict.lo: gen/nameslist_dict.c
+
+nametoucs.o: nametoucs.c libucd_int.h gen/nametoucs_hash.h
+nametoucs.lo: nametoucs.c libucd_int.h gen/nametoucs_hash.h
diff --git a/compiler.h b/compiler.h
new file mode 100644
index 0000000..a975a83
--- /dev/null
+++ b/compiler.h
@@ -0,0 +1,16 @@
+#ifndef LIBUCD_COMPILER_H
+#define LIBUCD_COMPILER_H
+
+#if defined(__GNUC__)
+
+#define ALIGNED(x) __attribute__((aligned(x)))
+#define noreturn void __attribute__((noreturn))
+
+#else
+
+#define ALIGNED(x)
+#define noreturn void
+
+#endif
+
+#endif /* LIBUCD_COMPILER_H */
diff --git a/libucd_int.h b/libucd_int.h
index 58df27a..675ab79 100644
--- a/libucd_int.h
+++ b/libucd_int.h
@@ -10,6 +10,7 @@
#include <stdlib.h>
#include "ucd.h"
#include "int24.h"
+#include "compiler.h"
extern const char _libucd_hangul_jamo_l[][4];
extern const char _libucd_hangul_jamo_v[][4];
@@ -39,16 +40,19 @@ struct _libucd_property_array {
unsigned numeric_type :2;
unsigned bidi_class :5;
unsigned /* unused */ :1;
-};
+} ALIGNED(32);
+extern const struct _libucd_property_array _libucd_property_array[];
struct _libucd_ucstoname_tab {
int24 ucs;
uint24 names_offset;
uint16_t proparray_offset;
-};
+} ALIGNED(8);
+extern const struct _libucd_ucstoname_tab _libucd_ucstoname_tab[];
struct _libucd_nametoucs_tab {
int24 ucs;
};
+extern const struct _libucd_nametoucs_tab _libucd_nametoucs_tab[];
#endif
diff --git a/nametoucs.c b/nametoucs.c
new file mode 100644
index 0000000..7a54600
--- /dev/null
+++ b/nametoucs.c
@@ -0,0 +1,42 @@
+#include <stdlib.h>
+#include <string.h>
+#include <stdlib.h>
+#include <inttypes.h>
+#include "libucd_int.h"
+#include "gen/nametoucs_hash.h"
+
+static uint32_t prehash(const char *str)
+{
+ uint32_t hash = PHASHSALT;
+ const uint8_t *p = (const uint8_t *)str;
+
+ while ( *p )
+ hash = (hash ^ *p++) + ((hash << 27)+(hash >> 5));
+
+ return hash;
+}
+
+/* This returns a candidate UCS for a given name. */
+int32_t _libucd_ucs_to_name(const char *name)
+{
+ int32_t ucs;
+ uint32_t hash;
+
+ if ( !strncmp(name, "CJK UNIFIED IDEOGRAPH-", 22) ) {
+ char *q;
+
+ ucs = strtol(name+22, &q, 16);
+ if ( *q || q < name+26 || q > name+27 )
+ return -1;
+ } else {
+ hash = _libucd_nametoucs_hash(prehash(name));
+ if ( hash > PHASHNKEYS )
+ return -1;
+ ucs = getint24(_libucd_nametoucs_tab[hash].ucs);
+ }
+
+ return ucs;
+}
+
+
+