Permalink
Browse files

Add charFromName and charFromFullName

  • Loading branch information...
1 parent 1ae1751 commit 057cfc80a6c769e3c7a7fc2696114df1ee58c0a1 @bos committed Nov 2, 2010
Showing with 52 additions and 4 deletions.
  1. +42 −3 Data/Text/ICU/Char.hsc
  2. +6 −0 cbits/text_icu.c
  3. +3 −0 include/hs_text_icu.h
  4. +1 −1 text-icu.cabal
View
@@ -70,6 +70,8 @@ module Data.Text.ICU.Char
, blockCode
, charFullName
, charName
+ , charFromFullName
+ , charFromName
, combiningClass
, direction
, property
@@ -86,13 +88,14 @@ module Data.Text.ICU.Char
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)
@@ -861,10 +864,42 @@ charName = charName' (#const U_UNICODE_CHAR_NAME)
-- | 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
@@ -917,6 +952,10 @@ foreign import ccall unsafe "hs_text_icu.h __hs_u_charName" u_charName
:: 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
View
@@ -350,6 +350,12 @@ int32_t __hs_u_charName(UChar32 code, UCharNameChoice nameChoice,
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)
{
View
@@ -51,6 +51,9 @@ int32_t __hs_u_charDigitValue(UChar32 c);
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);
View
@@ -1,5 +1,5 @@
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

0 comments on commit 057cfc8

Please sign in to comment.