Commits

Bryan O'Sullivan committed 045b9f4

Add charFromName and charFromFullName

Comments (0)

Files changed (4)

Data/Text/ICU/Char.hsc

     , blockCode
     , charFullName
     , charName
+    , charFromFullName
+    , charFromName
     , combiningClass
     , direction
     , property
 import Control.Exception (throw)
 import Data.Char (chr, ord)
 import Data.Int (Int32)
-import Data.Text.ICU.Error (isFailure, u_BUFFER_OVERFLOW_ERROR)
+import Data.Text.ICU.Error (isFailure, u_BUFFER_OVERFLOW_ERROR,
+                            u_INVALID_CHAR_FOUND)
 import Data.Text.ICU.Error.Internal (UErrorCode, withError)
 import Data.Text.ICU.Internal (UBool, UChar32, asBool)
 import Data.Text.ICU.Normalize.Internal (toNCR)
 import Data.Typeable (Typeable)
 import Data.Word (Word8)
-import Foreign.C.String (CString, peekCStringLen)
+import Foreign.C.String (CString, peekCStringLen, withCString)
 import Foreign.C.Types (CInt)
 import Foreign.Marshal.Alloc (allocaBytes)
 import Foreign.Ptr (Ptr)
 -- | Return the full name of a Unicode character.
 --
 -- Compared to 'charName', this function gives each Unicode code point
--- a unique name.
+-- a unique extended name. Extended names are lowercase followed by an
+-- uppercase hexadecimal number, within angle brackets.
 charFullName :: Char -> String
 charFullName = charName' (#const U_EXTENDED_CHAR_NAME)
 
+-- | Find a Unicode character by its full name, and return its code
+-- point value.
+--
+-- The name is matched exactly and completely.
+--
+-- A Unicode 1.0 name is matched only if it differs from the modern
+-- name.  Unicode names are all uppercase.
+charFromName :: String -> Maybe Char
+charFromName = charFromName' (#const U_UNICODE_CHAR_NAME)
+
+-- | Find a Unicode character by its full or extended name, and return
+-- its code point value.
+--
+-- The name is matched exactly and completely.
+--
+-- A Unicode 1.0 name is matched only if it differs from the modern
+-- name.
+--
+-- Compared to 'charFromName', this function gives each Unicode code
+-- point a unique extended name. Extended names are lowercase followed
+-- by an uppercase hexadecimal number, within angle brackets.
+charFromFullName :: String -> Maybe Char
+charFromFullName = charFromName' (#const U_EXTENDED_CHAR_NAME)
+
+charFromName' :: UCharNameChoice -> String -> Maybe Char
+charFromName' choice name = unsafePerformIO . withCString name $ \ptr -> do
+  (err,r) <- withError $ u_charFromName choice ptr
+  return $! if err == u_INVALID_CHAR_FOUND || r == 0xffff
+            then Nothing
+            else Just $! chr (fromIntegral r)
+
 -- | Return the ISO 10646 comment for a character.
 --
 -- If a character does not have an associated comment, the empty
     :: UChar32 -> UCharNameChoice -> CString -> Int32 -> Ptr UErrorCode
     -> IO Int32
 
+foreign import ccall unsafe "hs_text_icu.h __hs_u_charFromName" u_charFromName
+    :: UCharNameChoice -> CString -> Ptr UErrorCode
+    -> IO UChar32
+
 foreign import ccall unsafe "hs_text_icu.h __hs_u_getISOComment" u_getISOComment
     :: UChar32 -> CString -> Int32 -> Ptr UErrorCode -> IO Int32
 
     return u_charName(code, nameChoice, buffer, bufferLength, pErrorCode);
 }
 
+UChar32 __hs_u_charFromName(UCharNameChoice nameChoice, const char *name,
+			    UErrorCode *pErrorCode)
+{
+    return u_charFromName(nameChoice, name, pErrorCode);
+}
+
 int32_t __hs_u_getISOComment(UChar32 c, char *dest, int32_t destCapacity,
 			     UErrorCode *pErrorCode)
 {

include/hs_text_icu.h

 int32_t __hs_u_charName(UChar32 code, UCharNameChoice nameChoice,
 			char *buffer, int32_t bufferLength,
 			UErrorCode *pErrorCode);
+UChar32 __hs_u_charFromName(UCharNameChoice nameChoice,
+			    const char *name,
+			    UErrorCode *pErrorCode);
 int32_t __hs_u_getISOComment(UChar32 c, char *dest, int32_t destCapacity,
 			     UErrorCode *pErrorCode);
 int32_t __hs_u_getIntPropertyValue(UChar32 c, UProperty which);
 name:           text-icu
-version:        0.6.2.1
+version:        0.6.3.0
 synopsis:       Bindings to the ICU library
 homepage:       http://bitbucket.org/bos/text-icu
 bug-reports:    http://bitbucket.org/bos/text-icu/issues
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.