[svn:parrot] r37851 - branches/p6strings/src/pmc

pmichaud at svn.parrot.org pmichaud at svn.parrot.org
Wed Apr 1 19:34:05 UTC 2009


Author: pmichaud
Date: Wed Apr  1 19:34:04 2009
New Revision: 37851
URL: https://trac.parrot.org/parrot/changeset/37851

Log:
[codestring]:  Add 'ord_from_name' to obtain codepoints from character names
(e.g., "LATIN CAPITAL LETTER A" and "MUSICAL FLAT SIGN").

Modified:
   branches/p6strings/src/pmc/codestring.pmc

Modified: branches/p6strings/src/pmc/codestring.pmc
==============================================================================
--- branches/p6strings/src/pmc/codestring.pmc	Wed Apr  1 19:31:38 2009	(r37850)
+++ branches/p6strings/src/pmc/codestring.pmc	Wed Apr  1 19:34:04 2009	(r37851)
@@ -32,6 +32,10 @@
 
 #include "parrot/parrot.h"
 
+#if PARROT_HAS_ICU
+#  include <unicode/uchar.h>
+#endif
+
 pmclass CodeString extends String provides string {
 
 /*
@@ -200,6 +204,33 @@
 
 /*
 
+=item C<charname_to_ord(string name)>
+
+Converts the Unicode character name given by C<name> to its
+codepoint value.  Returns -1 if an error occurs in conversion.
+
+*/
+
+
+  METHOD charname_to_ord(STRING *name) {
+#if PARROT_HAS_ICU
+    UChar32    codepoint;
+    UErrorCode err       = U_ZERO_ERROR;
+    char       *cstr     = Parrot_str_to_cstring(INTERP, name);
+    codepoint = u_charFromName(U_UNICODE_CHAR_NAME, cstr, &err);
+    Parrot_str_free_cstring(cstr);
+    if (U_SUCCESS(err)) {
+        RETURN(INTVAL codepoint);
+    }
+    RETURN(INTVAL -1);
+#else
+    Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_LIBRARY_ERROR,
+        "no ICU lib loaded");
+#endif
+  }
+
+/*
+
 =item C<key( string name1 [, string name2, ...] )>
 
 Construct a PIR key using the strings passed as arguments.


More information about the parrot-commits mailing list