2000-06-27 16:30:16 +00:00
|
|
|
/*
|
|
|
|
*******************************************************************************
|
2001-03-21 20:31:13 +00:00
|
|
|
* Copyright (C) 1997-2001, International Business Machines
|
2000-06-27 16:30:16 +00:00
|
|
|
* Corporation and others. All Rights Reserved.
|
|
|
|
*******************************************************************************
|
|
|
|
* Date Name Description
|
|
|
|
* 06/21/00 aliu Creation.
|
|
|
|
*******************************************************************************
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "unicode/utrans.h"
|
|
|
|
#include "unicode/putil.h"
|
|
|
|
#include "unicode/rbt.h"
|
|
|
|
#include "unicode/rep.h"
|
|
|
|
#include "unicode/translit.h"
|
|
|
|
#include "unicode/unifilt.h"
|
|
|
|
#include "unicode/uniset.h"
|
|
|
|
#include "unicode/ustring.h"
|
|
|
|
#include "cpputils.h"
|
|
|
|
|
|
|
|
// Following macro is to be followed by <return value>';' or just ';'
|
|
|
|
#define utrans_ENTRY(s) if ((s)==NULL || U_FAILURE(*(s))) return
|
|
|
|
|
|
|
|
/********************************************************************
|
|
|
|
* Replaceable-UReplaceableCallbacks glue
|
|
|
|
********************************************************************/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Make a UReplaceable + UReplaceableCallbacks into a Replaceable object.
|
|
|
|
*/
|
2001-10-08 23:26:58 +00:00
|
|
|
U_NAMESPACE_BEGIN
|
2000-06-27 16:30:16 +00:00
|
|
|
class ReplaceableGlue : public Replaceable {
|
|
|
|
|
|
|
|
UChar *buf;
|
|
|
|
int32_t bufLen;
|
|
|
|
UReplaceable *rep;
|
|
|
|
UReplaceableCallbacks *func;
|
|
|
|
|
|
|
|
enum { BUF_PAD = 8 };
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
2000-09-30 01:57:38 +00:00
|
|
|
ReplaceableGlue(UReplaceable *replaceable,
|
|
|
|
UReplaceableCallbacks *funcCallback);
|
2000-06-27 16:30:16 +00:00
|
|
|
|
|
|
|
virtual ~ReplaceableGlue();
|
|
|
|
|
2002-03-12 01:32:42 +00:00
|
|
|
virtual void handleReplaceBetween(int32_t start,
|
|
|
|
int32_t limit,
|
2000-06-27 16:30:16 +00:00
|
|
|
const UnicodeString& text);
|
|
|
|
|
2002-03-12 01:32:42 +00:00
|
|
|
virtual void extractBetween(int32_t start,
|
|
|
|
int32_t limit,
|
2002-02-26 17:50:59 +00:00
|
|
|
UnicodeString& target) const;
|
|
|
|
|
2000-06-27 16:30:16 +00:00
|
|
|
virtual void copy(int32_t start, int32_t limit, int32_t dest);
|
2001-02-14 18:02:44 +00:00
|
|
|
|
|
|
|
protected:
|
|
|
|
|
2001-07-02 20:58:14 +00:00
|
|
|
virtual int32_t getLength() const;
|
|
|
|
|
2002-03-12 01:32:42 +00:00
|
|
|
virtual UChar getCharAt(int32_t offset) const;
|
2001-02-14 18:02:44 +00:00
|
|
|
|
2002-03-12 01:32:42 +00:00
|
|
|
virtual UChar32 getChar32At(int32_t offset) const;
|
2000-06-27 16:30:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2000-09-30 01:57:38 +00:00
|
|
|
ReplaceableGlue::ReplaceableGlue(UReplaceable *replaceable,
|
2001-02-14 18:02:44 +00:00
|
|
|
UReplaceableCallbacks *funcCallback)
|
2001-07-02 20:58:14 +00:00
|
|
|
: Replaceable()
|
2001-02-14 18:02:44 +00:00
|
|
|
{
|
2000-09-30 01:57:38 +00:00
|
|
|
this->rep = replaceable;
|
|
|
|
this->func = funcCallback;
|
2000-06-27 16:30:16 +00:00
|
|
|
buf = 0;
|
|
|
|
bufLen = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
ReplaceableGlue::~ReplaceableGlue() {
|
2002-02-28 01:42:40 +00:00
|
|
|
uprv_free(buf);
|
2000-06-27 16:30:16 +00:00
|
|
|
}
|
|
|
|
|
2001-07-02 20:58:14 +00:00
|
|
|
int32_t ReplaceableGlue::getLength() const {
|
|
|
|
return (*func->length)(rep);
|
|
|
|
}
|
|
|
|
|
2002-03-12 01:32:42 +00:00
|
|
|
UChar ReplaceableGlue::getCharAt(int32_t offset) const {
|
2000-06-27 16:30:16 +00:00
|
|
|
return (*func->charAt)(rep, offset);
|
|
|
|
}
|
|
|
|
|
2002-03-12 01:32:42 +00:00
|
|
|
UChar32 ReplaceableGlue::getChar32At(int32_t offset) const {
|
2000-06-27 16:30:16 +00:00
|
|
|
return (*func->char32At)(rep, offset);
|
|
|
|
}
|
|
|
|
|
2002-03-12 01:32:42 +00:00
|
|
|
void ReplaceableGlue::handleReplaceBetween(int32_t start,
|
|
|
|
int32_t limit,
|
2000-06-27 16:30:16 +00:00
|
|
|
const UnicodeString& text) {
|
|
|
|
int32_t len = text.length();
|
|
|
|
if (buf == 0 || bufLen < len) {
|
2002-02-28 01:42:40 +00:00
|
|
|
uprv_free(buf);
|
2000-06-27 16:30:16 +00:00
|
|
|
bufLen = len + BUF_PAD;
|
2002-02-28 01:42:40 +00:00
|
|
|
buf = (UChar*) uprv_malloc(sizeof(UChar) * bufLen);
|
2000-06-27 16:30:16 +00:00
|
|
|
}
|
|
|
|
text.extract(0, len, buf);
|
|
|
|
(*func->replace)(rep, start, limit, buf, len);
|
2002-02-26 17:50:59 +00:00
|
|
|
}
|
|
|
|
|
2002-03-12 01:32:42 +00:00
|
|
|
void ReplaceableGlue::extractBetween(int32_t start,
|
|
|
|
int32_t limit,
|
2002-02-26 17:50:59 +00:00
|
|
|
UnicodeString& target) const {
|
|
|
|
(*func->extract)(rep, start, limit, target.getBuffer(limit-start));
|
|
|
|
target.releaseBuffer(limit-start);
|
2000-06-27 16:30:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ReplaceableGlue::copy(int32_t start, int32_t limit, int32_t dest) {
|
|
|
|
(*func->copy)(rep, start, limit, dest);
|
|
|
|
}
|
2001-10-08 23:26:58 +00:00
|
|
|
U_NAMESPACE_END
|
2000-06-27 16:30:16 +00:00
|
|
|
/********************************************************************
|
|
|
|
* General API
|
|
|
|
********************************************************************/
|
2001-10-08 23:26:58 +00:00
|
|
|
U_NAMESPACE_USE
|
2001-08-31 03:23:39 +00:00
|
|
|
#if 0
|
2000-06-27 16:30:16 +00:00
|
|
|
|
|
|
|
U_CAPI UTransliterator*
|
|
|
|
utrans_open(const char* id,
|
|
|
|
UTransDirection dir,
|
2001-08-31 03:23:39 +00:00
|
|
|
UParseError* parseError,
|
2000-06-27 16:30:16 +00:00
|
|
|
UErrorCode* status) {
|
|
|
|
|
|
|
|
utrans_ENTRY(status) NULL;
|
|
|
|
|
|
|
|
if (id == NULL) {
|
|
|
|
*status = U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
UnicodeString ID(id, ""); // use invariant converter
|
|
|
|
Transliterator *trans = NULL;
|
|
|
|
|
2001-08-31 03:23:39 +00:00
|
|
|
trans = Transliterator::createInstance(ID, dir, *parseError, *status);
|
2000-06-27 16:30:16 +00:00
|
|
|
|
|
|
|
if (trans == NULL) {
|
|
|
|
*status = U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
}
|
|
|
|
return (UTransliterator*) trans;
|
|
|
|
}
|
|
|
|
|
|
|
|
U_CAPI UTransliterator*
|
|
|
|
utrans_openRules(const char* id,
|
|
|
|
const UChar* rules,
|
|
|
|
int32_t rulesLength, /* -1 if null-terminated */
|
|
|
|
UTransDirection dir,
|
|
|
|
UParseError* parseErr, /* may be NULL */
|
|
|
|
UErrorCode* status) {
|
|
|
|
|
|
|
|
utrans_ENTRY(status) NULL;
|
|
|
|
|
|
|
|
if (id == NULL || rules == NULL) {
|
|
|
|
*status = U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
UnicodeString ID(id, ""); // use invariant converter
|
|
|
|
UnicodeString ruleStr(rulesLength < 0,
|
|
|
|
rules,
|
|
|
|
rulesLength); // r-o alias
|
|
|
|
|
|
|
|
RuleBasedTransliterator *trans = NULL;
|
|
|
|
|
|
|
|
// Use if() to avoid construction of ParseError object on stack
|
|
|
|
// unless it is called for by user.
|
|
|
|
if (parseErr != NULL) {
|
2000-06-27 19:00:38 +00:00
|
|
|
trans = new RuleBasedTransliterator(ID, ruleStr, dir,
|
|
|
|
NULL, *parseErr, *status);
|
2000-06-27 16:30:16 +00:00
|
|
|
} else {
|
2000-06-27 19:00:38 +00:00
|
|
|
trans = new RuleBasedTransliterator(ID, ruleStr, dir,
|
|
|
|
NULL, *status);
|
2000-06-27 16:30:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (trans == NULL) {
|
|
|
|
*status = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
} else if (U_FAILURE(*status)) {
|
|
|
|
delete trans;
|
|
|
|
trans = NULL;
|
|
|
|
}
|
|
|
|
return (UTransliterator*) trans;
|
|
|
|
}
|
2001-08-31 03:23:39 +00:00
|
|
|
#endif
|
|
|
|
|
2001-11-21 01:08:55 +00:00
|
|
|
U_CAPI UTransliterator* U_EXPORT2
|
2001-08-31 03:23:39 +00:00
|
|
|
utrans_open(const char* id,
|
|
|
|
UTransDirection dir,
|
|
|
|
const UChar* rules, /* may be Null */
|
|
|
|
int32_t rulesLength, /* -1 if null-terminated */
|
|
|
|
UParseError* parseError, /* may be Null */
|
|
|
|
UErrorCode* status) {
|
|
|
|
|
|
|
|
utrans_ENTRY(status) NULL;
|
|
|
|
|
|
|
|
if (id == NULL) {
|
|
|
|
*status = U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
UParseError temp;
|
|
|
|
|
|
|
|
if(parseError == NULL){
|
|
|
|
parseError = &temp;
|
|
|
|
}
|
|
|
|
|
|
|
|
UnicodeString ID(id, ""); // use invariant converter
|
|
|
|
|
|
|
|
if(rules==NULL){
|
|
|
|
|
|
|
|
Transliterator *trans = NULL;
|
|
|
|
|
|
|
|
trans = Transliterator::createInstance(ID, dir, *parseError, *status);
|
|
|
|
|
|
|
|
if(U_FAILURE(*status)){
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return (UTransliterator*) trans;
|
|
|
|
}else{
|
|
|
|
UnicodeString ruleStr(rulesLength < 0,
|
|
|
|
rules,
|
|
|
|
rulesLength); // r-o alias
|
|
|
|
|
|
|
|
RuleBasedTransliterator *trans = NULL;
|
|
|
|
trans = new RuleBasedTransliterator(ID, ruleStr, dir,
|
|
|
|
NULL, *parseError, *status);
|
|
|
|
if (trans == NULL) {
|
|
|
|
*status = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
} else if (U_FAILURE(*status)) {
|
|
|
|
delete trans;
|
|
|
|
trans = NULL;
|
|
|
|
}
|
|
|
|
return (UTransliterator*) trans;
|
|
|
|
}
|
|
|
|
}
|
2000-06-27 16:30:16 +00:00
|
|
|
|
2001-11-21 01:08:55 +00:00
|
|
|
U_CAPI UTransliterator* U_EXPORT2
|
2000-06-27 16:30:16 +00:00
|
|
|
utrans_openInverse(const UTransliterator* trans,
|
|
|
|
UErrorCode* status) {
|
|
|
|
|
|
|
|
utrans_ENTRY(status) NULL;
|
|
|
|
|
|
|
|
UTransliterator* result =
|
2001-08-31 03:23:39 +00:00
|
|
|
(UTransliterator*) ((Transliterator*) trans)->createInverse(*status);
|
2000-06-27 16:30:16 +00:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2001-11-21 01:08:55 +00:00
|
|
|
U_CAPI UTransliterator* U_EXPORT2
|
2000-06-27 16:30:16 +00:00
|
|
|
utrans_clone(const UTransliterator* trans,
|
|
|
|
UErrorCode* status) {
|
|
|
|
|
|
|
|
utrans_ENTRY(status) NULL;
|
|
|
|
|
|
|
|
if (trans == NULL) {
|
|
|
|
*status = U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
Transliterator *t = ((Transliterator*) trans)->clone();
|
|
|
|
if (t == NULL) {
|
|
|
|
*status = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
}
|
|
|
|
return (UTransliterator*) t;
|
|
|
|
}
|
|
|
|
|
2001-11-21 01:08:55 +00:00
|
|
|
U_CAPI void U_EXPORT2
|
2000-06-27 16:30:16 +00:00
|
|
|
utrans_close(UTransliterator* trans) {
|
|
|
|
delete (Transliterator*) trans;
|
|
|
|
}
|
|
|
|
|
2001-11-21 01:08:55 +00:00
|
|
|
U_CAPI int32_t U_EXPORT2
|
2000-06-27 16:30:16 +00:00
|
|
|
utrans_getID(const UTransliterator* trans,
|
|
|
|
char* buf,
|
|
|
|
int32_t bufCapacity) {
|
2001-10-04 01:05:57 +00:00
|
|
|
return ((Transliterator*) trans)->getID().extract(0, 0x7fffffff, buf, bufCapacity, "");
|
2000-06-27 16:30:16 +00:00
|
|
|
}
|
|
|
|
|
2001-11-21 01:08:55 +00:00
|
|
|
U_CAPI void U_EXPORT2
|
2000-06-27 16:30:16 +00:00
|
|
|
utrans_register(UTransliterator* adoptedTrans,
|
|
|
|
UErrorCode* status) {
|
|
|
|
utrans_ENTRY(status);
|
2001-08-15 19:06:40 +00:00
|
|
|
// status currently ignored; may remove later
|
|
|
|
Transliterator::registerInstance((Transliterator*) adoptedTrans);
|
2000-06-27 16:30:16 +00:00
|
|
|
}
|
|
|
|
|
2001-11-21 01:08:55 +00:00
|
|
|
U_CAPI void U_EXPORT2
|
2000-06-27 16:30:16 +00:00
|
|
|
utrans_unregister(const char* id) {
|
|
|
|
UnicodeString ID(id, ""); // use invariant converter
|
|
|
|
Transliterator::unregister(ID);
|
|
|
|
}
|
|
|
|
|
2001-11-21 01:08:55 +00:00
|
|
|
U_CAPI void U_EXPORT2
|
2000-06-27 16:30:16 +00:00
|
|
|
utrans_setFilter(UTransliterator* trans,
|
|
|
|
const UChar* filterPattern,
|
|
|
|
int32_t filterPatternLen,
|
|
|
|
UErrorCode* status) {
|
|
|
|
|
|
|
|
utrans_ENTRY(status);
|
|
|
|
UnicodeFilter* filter = NULL;
|
2000-06-28 23:30:26 +00:00
|
|
|
if (filterPattern != NULL && *filterPattern != 0) {
|
2000-06-27 16:30:16 +00:00
|
|
|
// Create read only alias of filterPattern:
|
|
|
|
UnicodeString pat(filterPatternLen < 0, filterPattern, filterPatternLen);
|
|
|
|
filter = new UnicodeSet(pat, *status);
|
|
|
|
if (U_FAILURE(*status)) {
|
|
|
|
delete filter;
|
|
|
|
filter = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
((Transliterator*) trans)->adoptFilter(filter);
|
|
|
|
}
|
|
|
|
|
2001-11-21 01:08:55 +00:00
|
|
|
U_CAPI int32_t U_EXPORT2
|
2000-06-27 16:30:16 +00:00
|
|
|
utrans_countAvailableIDs(void) {
|
|
|
|
return Transliterator::countAvailableIDs();
|
|
|
|
}
|
|
|
|
|
2001-11-21 01:08:55 +00:00
|
|
|
U_CAPI int32_t U_EXPORT2
|
2000-06-27 16:30:16 +00:00
|
|
|
utrans_getAvailableID(int32_t index,
|
|
|
|
char* buf, // may be NULL
|
|
|
|
int32_t bufCapacity) {
|
2001-10-04 01:05:57 +00:00
|
|
|
return Transliterator::getAvailableID(index).extract(0, 0x7fffffff, buf, bufCapacity, "");
|
2000-06-27 16:30:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/********************************************************************
|
|
|
|
* Transliteration API
|
|
|
|
********************************************************************/
|
|
|
|
|
2001-11-21 01:08:55 +00:00
|
|
|
U_CAPI void U_EXPORT2
|
2000-06-27 16:30:16 +00:00
|
|
|
utrans_trans(const UTransliterator* trans,
|
|
|
|
UReplaceable* rep,
|
|
|
|
UReplaceableCallbacks* repFunc,
|
|
|
|
int32_t start,
|
|
|
|
int32_t* limit,
|
|
|
|
UErrorCode* status) {
|
|
|
|
|
|
|
|
utrans_ENTRY(status);
|
|
|
|
|
|
|
|
if (trans == 0 || rep == 0 || repFunc == 0 || limit == 0) {
|
|
|
|
*status = U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ReplaceableGlue r(rep, repFunc);
|
|
|
|
|
|
|
|
*limit = ((Transliterator*) trans)->transliterate(r, start, *limit);
|
|
|
|
}
|
|
|
|
|
2001-11-21 01:08:55 +00:00
|
|
|
U_CAPI void U_EXPORT2
|
2000-06-27 16:30:16 +00:00
|
|
|
utrans_transIncremental(const UTransliterator* trans,
|
|
|
|
UReplaceable* rep,
|
|
|
|
UReplaceableCallbacks* repFunc,
|
|
|
|
UTransPosition* pos,
|
|
|
|
UErrorCode* status) {
|
|
|
|
|
|
|
|
utrans_ENTRY(status);
|
|
|
|
|
|
|
|
if (trans == 0 || rep == 0 || repFunc == 0 || pos == 0) {
|
|
|
|
*status = U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ReplaceableGlue r(rep, repFunc);
|
|
|
|
|
2000-06-27 19:00:38 +00:00
|
|
|
((Transliterator*) trans)->transliterate(r, *pos, *status);
|
2000-06-27 16:30:16 +00:00
|
|
|
}
|
|
|
|
|
2001-11-21 01:08:55 +00:00
|
|
|
U_CAPI void U_EXPORT2
|
2000-06-27 16:30:16 +00:00
|
|
|
utrans_transUChars(const UTransliterator* trans,
|
|
|
|
UChar* text,
|
|
|
|
int32_t* textLength,
|
|
|
|
int32_t textCapacity,
|
|
|
|
int32_t start,
|
|
|
|
int32_t* limit,
|
|
|
|
UErrorCode* status) {
|
|
|
|
|
|
|
|
utrans_ENTRY(status);
|
|
|
|
|
|
|
|
if (trans == 0 || text == 0 || limit == 0) {
|
|
|
|
*status = U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t textLen = (textLength == NULL || *textLength < 0)
|
|
|
|
? u_strlen(text) : *textLength;
|
|
|
|
// writeable alias: for this ct, len CANNOT be -1 (why?)
|
|
|
|
UnicodeString str(text, textLen, textCapacity);
|
|
|
|
|
|
|
|
*limit = ((Transliterator*) trans)->transliterate(str, start, *limit);
|
|
|
|
|
|
|
|
// Copy the string buffer back to text (only if necessary)
|
|
|
|
// and fill in *neededCapacity (if neededCapacity != NULL).
|
2001-09-05 23:39:38 +00:00
|
|
|
textLen = str.extract(text, textCapacity, *status);
|
2001-03-17 23:36:26 +00:00
|
|
|
if(textLength != NULL) {
|
|
|
|
*textLength = textLen;
|
|
|
|
}
|
2000-06-27 16:30:16 +00:00
|
|
|
}
|
|
|
|
|
2001-11-21 01:08:55 +00:00
|
|
|
U_CAPI void U_EXPORT2
|
2000-06-27 16:30:16 +00:00
|
|
|
utrans_transIncrementalUChars(const UTransliterator* trans,
|
|
|
|
UChar* text,
|
|
|
|
int32_t* textLength,
|
|
|
|
int32_t textCapacity,
|
|
|
|
UTransPosition* pos,
|
|
|
|
UErrorCode* status) {
|
|
|
|
|
|
|
|
utrans_ENTRY(status);
|
|
|
|
|
|
|
|
if (trans == 0 || text == 0 || pos == 0) {
|
|
|
|
*status = U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t textLen = (textLength == NULL || *textLength < 0)
|
|
|
|
? u_strlen(text) : *textLength;
|
|
|
|
// writeable alias: for this ct, len CANNOT be -1 (why?)
|
|
|
|
UnicodeString str(text, textLen, textCapacity);
|
|
|
|
|
2000-06-27 19:00:38 +00:00
|
|
|
((Transliterator*) trans)->transliterate(str, *pos, *status);
|
2000-06-27 16:30:16 +00:00
|
|
|
|
|
|
|
// Copy the string buffer back to text (only if necessary)
|
|
|
|
// and fill in *neededCapacity (if neededCapacity != NULL).
|
2001-09-05 23:39:38 +00:00
|
|
|
textLen = str.extract(text, textCapacity, *status);
|
2001-03-17 23:36:26 +00:00
|
|
|
if(textLength != NULL) {
|
|
|
|
*textLength = textLen;
|
|
|
|
}
|
2000-06-27 16:30:16 +00:00
|
|
|
}
|