[Python-checkins] CVS: python/dist/src/Modules ucnhash.c,1.4,1.5

Fredrik Lundh effbot@users.sourceforge.net
Fri, 19 Jan 2001 03:00:44 -0800


Update of /cvsroot/python/python/dist/src/Modules
In directory usw-pr-cvs1:/tmp/cvs-serv10825/Modules

Modified Files:
	ucnhash.c 
Log Message:


added "getcode" and "getname" methods to the ucnhash module (they're
probably more useful for the test code than for any applications, but
one never knows...)


Index: ucnhash.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Modules/ucnhash.c,v
retrieving revision 1.4
retrieving revision 1.5
diff -C2 -r1.4 -r1.5
*** ucnhash.c	2001/01/19 09:45:02	1.4
--- ucnhash.c	2001/01/19 11:00:42	1.5
***************
*** 23,27 ****
   */
  
- #define cKeys 10538
  #define k_cHashElements 18836
  #define k_cchMaxKey  83
--- 23,26 ----
***************
*** 112,121 ****
  }
  
- const _Py_UnicodeCharacterName *
- getValue(unsigned long iKey)
- {
-     return (_Py_UnicodeCharacterName *) &aucn[iKey];
- }
- 
  static int
  mystrnicmp(const char *s1, const char *s2, size_t count)
--- 111,114 ----
***************
*** 137,147 ****
  
  static int
! ucnhash_getname(Py_UCS4 code, char* buffer, int buflen)
  {
      return 0;
  }
  
  static int
! ucnhash_getcode(const char* name, int namelen, Py_UCS4* code)
  {
      unsigned long j;
--- 130,152 ----
  
  static int
! getname(Py_UCS4 code, char* buffer, int buflen)
  {
+     int i;
+ 
+     /* brute force search */
+     for (i = 0; i < k_cKeys; i++)
+         if (aucn[i].value == code) {
+             int len = strlen(aucn[i].pszUCN);
+             if (buflen <= len)
+                 return 0;
+             memcpy(buffer, aucn[i].pszUCN, len+1);
+             return 1;
+         }
+ 
      return 0;
  }
  
  static int
! getcode(const char* name, int namelen, Py_UCS4* code)
  {
      unsigned long j;
***************
*** 149,156 ****
      j = hash(name, namelen);
  
!     if (j > cKeys || mystrnicmp(name, getValue(j)->pszUCN, namelen) != 0)
          return 0;
  
!     *code = getValue(j)->value;
  
      return 1;
--- 154,161 ----
      j = hash(name, namelen);
  
!     if (j > k_cKeys || mystrnicmp(name, aucn[j].pszUCN, namelen) != 0)
          return 0;
  
!     *code = aucn[j].value;
  
      return 1;
***************
*** 160,170 ****
  {
      sizeof(_PyUnicode_Name_CAPI),
!     ucnhash_getname,
!     ucnhash_getcode
  };
  
  static  
  PyMethodDef ucnhash_methods[] =
  {   
      {NULL, NULL},
  };
--- 165,215 ----
  {
      sizeof(_PyUnicode_Name_CAPI),
!     getname,
!     getcode
  };
  
+ /* -------------------------------------------------------------------- */
+ /* Python bindings */
+ 
+ static PyObject *
+ ucnhash_getname(PyObject* self, PyObject* args)
+ {
+     char name[256];
+ 
+     int code;
+     if (!PyArg_ParseTuple(args, "i", &code))
+         return NULL;
+ 
+     if (!getname((Py_UCS4) code, name, sizeof(name))) {
+         PyErr_SetString(PyExc_ValueError, "undefined character code");
+         return NULL;
+     }
+ 
+     return Py_BuildValue("s", name);
+ }
+ 
+ static PyObject *
+ ucnhash_getcode(PyObject* self, PyObject* args)
+ {
+     Py_UCS4 code;
+ 
+     char* name;
+     int namelen;
+     if (!PyArg_ParseTuple(args, "s#", &name, &namelen))
+         return NULL;
+ 
+     if (!getcode(name, namelen, &code)) {
+         PyErr_SetString(PyExc_ValueError, "undefined character name");
+         return NULL;
+     }
+ 
+     return Py_BuildValue("i", code);
+ }
+ 
  static  
  PyMethodDef ucnhash_methods[] =
  {   
+     {"getname", ucnhash_getname, 1},
+     {"getcode", ucnhash_getcode, 1},
      {NULL, NULL},
  };