mirror of
https://github.com/unicode-org/icu.git
synced 2025-04-07 22:44:49 +00:00
ICU-1544 add virtual extractBetween() to Replaceable and new UReplaceableCallback member 'extract'
X-SVN-Rev: 7788
This commit is contained in:
parent
21bbcb0e19
commit
e5a8f1678a
8 changed files with 77 additions and 46 deletions
|
@ -91,30 +91,18 @@ public:
|
|||
inline UChar32 char32At(UTextOffset offset) const;
|
||||
|
||||
/**
|
||||
* Copy characters from this object into the destination character
|
||||
* array. The first character to be copied is at index
|
||||
* <code>srcStart</code>; the last character to be copied is at
|
||||
* index <code>srcLimit-1</code> (thus the total number of
|
||||
* characters to be copied is <code>srcLimit-srcStart</code>). The
|
||||
* characters are copied into the subarray of <code>dst</code>
|
||||
* starting at index <code>dstStart</code> and ending at index
|
||||
* <code>dstStart + (srcLimit-srcStart) - 1</code>.
|
||||
*
|
||||
* @param srcStart the beginning index to copy, inclusive; <code>0
|
||||
* <= srcStart <= srcLimit</code>.
|
||||
* @param srcLimit the ending index to copy, exclusive;
|
||||
* <code>srcStart <= srcLimit <= length()</code>.
|
||||
* @param dst the destination array.
|
||||
* @param dstStart the start offset in the destination array.
|
||||
* @draft
|
||||
* Copy the characters in the range [<tt>start</tt>, <tt>limit</tt>)
|
||||
* into the UnicodeString <tt>target</tt>.
|
||||
* @param start offset of first character which will be copied
|
||||
* @param limit offset immediately following the last character to
|
||||
* be copied
|
||||
* @param target UnicodeString into which to copy characters.
|
||||
* @return A reference to <TT>target</TT>
|
||||
* @draft ICU 2.1
|
||||
*/
|
||||
/* THIS API IS NOT NEEDED, BUT KEPT HERE AS A COMMENT IN
|
||||
CASE OF FUTURE NEED. CURRENTLY INDIVIDUAL CHARACTER
|
||||
ACCESS SUFFICES. */
|
||||
/* virtual void extractBetween(UTextOffset srcStart,
|
||||
UTextOffset srcLimit,
|
||||
UChar* dst,
|
||||
UTextOffset dstStart = 0) const = 0; */
|
||||
virtual void extractBetween(UTextOffset start,
|
||||
UTextOffset limit,
|
||||
UnicodeString& target) const = 0;
|
||||
|
||||
/**
|
||||
* Replace a substring of this object with the given text. If the
|
||||
|
|
|
@ -1397,14 +1397,14 @@ public:
|
|||
|
||||
/**
|
||||
* Copy the characters in the range [<tt>start</tt>, <tt>limit</tt>)
|
||||
* into the UnicodeString <tt>target</tt>.
|
||||
* into the UnicodeString <tt>target</tt>. Replaceable API.
|
||||
* @param start offset of first character which will be copied
|
||||
* @param limit offset immediately following the last character to be copied
|
||||
* @param target UnicodeString into which to copy characters.
|
||||
* @return A reference to <TT>target</TT>
|
||||
* @stable
|
||||
*/
|
||||
inline void extractBetween(UTextOffset start,
|
||||
virtual void extractBetween(UTextOffset start,
|
||||
UTextOffset limit,
|
||||
UnicodeString& target) const;
|
||||
|
||||
|
@ -3528,12 +3528,6 @@ UnicodeString::extractBetween(UTextOffset start,
|
|||
UTextOffset dstStart) const
|
||||
{ doExtract(start, limit - start, dst, dstStart); }
|
||||
|
||||
inline void
|
||||
UnicodeString::extractBetween(UTextOffset start,
|
||||
UTextOffset limit,
|
||||
UnicodeString& target) const
|
||||
{ doExtract(start, limit - start, target); }
|
||||
|
||||
inline UChar
|
||||
UnicodeString::doCharAt(UTextOffset offset) const
|
||||
{
|
||||
|
|
|
@ -92,6 +92,22 @@ typedef struct _UReplaceableCallbacks {
|
|||
const UChar* text,
|
||||
int32_t textLength);
|
||||
|
||||
/**
|
||||
* Function pointer that copies the characters in the range
|
||||
* [<tt>start</tt>, <tt>limit</tt>) into the array <tt>dst</tt>.
|
||||
* @param start offset of first character which will be copied
|
||||
* into the array
|
||||
* @param limit offset immediately following the last character to
|
||||
* be copied
|
||||
* @param dst array in which to copy characters. The length of
|
||||
* <tt>dst</tt> must be at least <tt>(limit - start)</tt>.
|
||||
* @draft ICU 2.1
|
||||
*/
|
||||
void (*extract)(UReplaceable* rep,
|
||||
int32_t start,
|
||||
int32_t limit,
|
||||
UChar* dst);
|
||||
|
||||
/**
|
||||
* Function pointer that copies text between start and limit in
|
||||
* this text to another index in the text. Attributes (out of
|
||||
|
|
|
@ -1582,6 +1582,12 @@ UnicodeString::extract(char *dest, int32_t destCapacity,
|
|||
return length;
|
||||
}
|
||||
|
||||
void
|
||||
UnicodeString::extractBetween(UTextOffset start,
|
||||
UTextOffset limit,
|
||||
UnicodeString& target) const
|
||||
{ doExtract(start, limit - start, target); }
|
||||
|
||||
int32_t
|
||||
UnicodeString::doExtract(UTextOffset start, int32_t length,
|
||||
char *dest, int32_t destCapacity,
|
||||
|
|
|
@ -68,18 +68,12 @@ void LowercaseTransliterator::handleTransliterate(Replaceable& text,
|
|||
UBool isIncremental) const
|
||||
{
|
||||
int32_t textPos = offsets.start;
|
||||
int32_t loop;
|
||||
if (textPos >= offsets.limit) return;
|
||||
|
||||
// get string for context
|
||||
// TODO: add convenience method to do this, since we do it all over
|
||||
|
||||
|
||||
UnicodeString original;
|
||||
/*UChar *original = new UChar[offsets.contextLimit - offsets.contextStart+1];*/ // get whole context
|
||||
/* Extract the characters from Replaceable */
|
||||
for (loop = offsets.contextStart; loop < offsets.contextLimit; loop++) {
|
||||
original.append(text.charAt(loop));
|
||||
}
|
||||
text.extractBetween(offsets.contextStart, offsets.contextLimit, original);
|
||||
|
||||
// Walk through original string
|
||||
// If there is a case change, modify corresponding position in replaceable
|
||||
|
|
|
@ -71,18 +71,12 @@ void UppercaseTransliterator::handleTransliterate(Replaceable& text,
|
|||
UTransPosition& offsets,
|
||||
UBool isIncremental) const {
|
||||
int32_t textPos = offsets.start;
|
||||
int32_t loop = 0;
|
||||
if (textPos >= offsets.limit) return;
|
||||
|
||||
// get string for context
|
||||
// TODO: add convenience method to do this, since we do it all over
|
||||
|
||||
UnicodeString original;
|
||||
/* UChar *original = new UChar[offsets.contextLimit - offsets.contextStart+1]; */// get whole context
|
||||
/* Extract the characters from Replaceable */
|
||||
for (loop = offsets.contextStart; loop < offsets.contextLimit; loop++) {
|
||||
original.append(text.charAt(loop));
|
||||
}
|
||||
text.extractBetween(offsets.contextStart, offsets.contextLimit, original);
|
||||
|
||||
// Walk through original string
|
||||
// If there is a case change, modify corresponding position in replaceable
|
||||
|
|
|
@ -49,6 +49,10 @@ public:
|
|||
UTextOffset limit,
|
||||
const UnicodeString& text);
|
||||
|
||||
virtual void extractBetween(UTextOffset start,
|
||||
UTextOffset limit,
|
||||
UnicodeString& target) const;
|
||||
|
||||
virtual void copy(int32_t start, int32_t limit, int32_t dest);
|
||||
|
||||
protected:
|
||||
|
@ -100,6 +104,13 @@ void ReplaceableGlue::handleReplaceBetween(UTextOffset start,
|
|||
(*func->replace)(rep, start, limit, buf, len);
|
||||
}
|
||||
|
||||
void ReplaceableGlue::extractBetween(UTextOffset start,
|
||||
UTextOffset limit,
|
||||
UnicodeString& target) const {
|
||||
(*func->extract)(rep, start, limit, target.getBuffer(limit-start));
|
||||
target.releaseBuffer(limit-start);
|
||||
}
|
||||
|
||||
void ReplaceableGlue::copy(int32_t start, int32_t limit, int32_t dest) {
|
||||
(*func->copy)(rep, start, limit, dest);
|
||||
}
|
||||
|
|
|
@ -23,6 +23,7 @@ static void TestFilter(void);
|
|||
static void TestOpenInverse(void);
|
||||
static void TestClone(void);
|
||||
static void TestRegisterUnregister(void);
|
||||
static void TestExtractBetween(void);
|
||||
|
||||
static void _expectRules(const char*, const char*, const char*);
|
||||
static void _expect(const UTransliterator* trans, const char* cfrom, const char* cto);
|
||||
|
@ -38,6 +39,7 @@ addUTransTest(TestNode** root) {
|
|||
TEST(TestOpenInverse);
|
||||
TEST(TestClone);
|
||||
TEST(TestRegisterUnregister);
|
||||
TEST(TestExtractBetween);
|
||||
}
|
||||
|
||||
/*------------------------------------------------------------------
|
||||
|
@ -106,11 +108,19 @@ static void Xcopy(UReplaceable* rep, int32_t start, int32_t limit, int32_t dest)
|
|||
x->text = newText;
|
||||
}
|
||||
|
||||
/* UReplaceableCallbacks callback */
|
||||
static void Xextract(UReplaceable* rep, int32_t start, int32_t limit, UChar* dst) {
|
||||
XReplaceable* x = (XReplaceable*)rep;
|
||||
int32_t len = limit - start;
|
||||
u_strncpy(dst, x->text, len);
|
||||
}
|
||||
|
||||
static void InitXReplaceableCallbacks(UReplaceableCallbacks* callbacks) {
|
||||
callbacks->length = Xlength;
|
||||
callbacks->charAt = XcharAt;
|
||||
callbacks->char32At = Xchar32At;
|
||||
callbacks->replace = Xreplace;
|
||||
callbacks->extract = Xextract;
|
||||
callbacks->copy = Xcopy;
|
||||
}
|
||||
|
||||
|
@ -420,6 +430,24 @@ static void TestFilter() {
|
|||
utrans_close(hex);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the UReplaceableCallback extractBetween support. We use a
|
||||
* transliterator known to rely on this call.
|
||||
*/
|
||||
static void TestExtractBetween() {
|
||||
|
||||
UTransliterator *trans;
|
||||
UErrorCode status = U_ZERO_ERROR;
|
||||
UParseError parseErr;
|
||||
|
||||
trans = utrans_open("Lower", UTRANS_FORWARD, NULL, -1,
|
||||
&parseErr, &status);
|
||||
|
||||
_expect(trans, "ABC", "abc");
|
||||
|
||||
utrans_close(trans);
|
||||
}
|
||||
|
||||
static void _expectRules(const char* crules,
|
||||
const char* cfrom,
|
||||
const char* cto) {
|
||||
|
|
Loading…
Add table
Reference in a new issue