From 925f2cf8c1c874bd8b1cb582500ea0bf3caaf5d5 Mon Sep 17 00:00:00 2001 From: Alan Liu Date: Fri, 19 Nov 1999 23:13:41 +0000 Subject: [PATCH] ICU-114 Make UnicodeString inherit from Replaceable X-SVN-Rev: 191 --- icu4c/source/common/common.dsp | 27 ++++++++ icu4c/source/common/rep.h | 123 +++++++++++++++++++++++++++++++++ icu4c/source/common/unistr.cpp | 14 ++++ icu4c/source/common/unistr.h | 20 +++++- 4 files changed, 183 insertions(+), 1 deletion(-) create mode 100644 icu4c/source/common/rep.h diff --git a/icu4c/source/common/common.dsp b/icu4c/source/common/common.dsp index fab8a128e36..1409e8ce85f 100644 --- a/icu4c/source/common/common.dsp +++ b/icu4c/source/common/common.dsp @@ -505,6 +505,33 @@ SOURCE=.\rbread.h # End Source File # Begin Source File +SOURCE=.\rep.h + +!IF "$(CFG)" == "common - Win32 Release" + +# Begin Custom Build +InputPath=.\rep.h + +"..\..\include\rep.h" : $(SOURCE) "$(INTDIR)" "$(OUTDIR)" + copy rep.h ..\..\include + +# End Custom Build + +!ELSEIF "$(CFG)" == "common - Win32 Debug" + +# Begin Custom Build +InputPath=.\rep.h + +"..\..\include\rep.h" : $(SOURCE) "$(INTDIR)" "$(OUTDIR)" + copy rep.h ..\..\include + +# End Custom Build + +!ENDIF + +# End Source File +# Begin Source File + SOURCE=.\resbund.h !IF "$(CFG)" == "common - Win32 Release" diff --git a/icu4c/source/common/rep.h b/icu4c/source/common/rep.h new file mode 100644 index 00000000000..064b4f79d90 --- /dev/null +++ b/icu4c/source/common/rep.h @@ -0,0 +1,123 @@ +/* +********************************************************************** +* Copyright (C) 1999, International Business Machines Corporation and +* others. All Rights Reserved. +********************************************************************** +* Date Name Description +* 11/17/99 aliu Creation. Ported from java. Modified to +* match current UnicodeString API. Forced +* to use name "handleReplaceBetween" because +* of existing methods in UnicodeString. +********************************************************************** +*/ + +#ifndef REP_H +#define REP_H + +#include "utypes.h" + +class UnicodeString; + +/** + * Replaceable is an abstract base class representing a + * string of characters that supports the replacement of a range of + * itself with a new string of characters. It is used by APIs that + * change a piece of text while retaining style attributes. In other + * words, an implicit aspect of the Replaceable API is + * that during a replace operation, new characters take on the + * attributes, if any, of the old characters. For example, if the + * string "the bold font" has range (4, 8) replaced with + * "strong", then it becomes "the strong font". + * + *

Replaceable specifies ranges using an initial + * offset and a limit offset. The range of characters thus specified + * includes the characters at offset initial..limit-1. That is, the + * start offset is inclusive, and the limit offset is exclusive. + * + *

Replaceable also includes API to access characters + * in the string: length(), charAt(), and + * extractBetween(). + * + * @author Alan Liu + */ +class U_COMMON_API Replaceable { + +public: + + /** + * Destructor. + */ + virtual ~Replaceable(); + + /** + * Return the number of characters in the text. + * @return number of characters in text + */ + virtual int32_t length() const = 0; + + /** + * Return the character at the given offset into the text. + * @param offset an integer between 0 and length()-1 + * inclusive + * @return character of text at given offset + */ + virtual UChar charAt(UTextOffset offset) const = 0; + + /** + * Copy characters from this object into the destination character + * array. The first character to be copied is at index + * srcStart; the last character to be copied is at + * index srcLimit-1 (thus the total number of + * characters to be copied is srcLimit-srcStart). The + * characters are copied into the subarray of dst + * starting at index dstStart and ending at index + * dstStart + (srcLimit-srcStart) - 1. + * + * @param srcStart the beginning index to copy, inclusive; 0 + * <= srcStart <= srcLimit. + * @param srcLimit the ending index to copy, exclusive; + * srcStart <= srcLimit <= length(). + * @param dst the destination array. + * @param dstStart the start offset in the destination array. */ + virtual void extractBetween(UTextOffset srcStart, + UTextOffset srcLimit, + UChar* dst, + UTextOffset dstStart = 0) const = 0; + + /** + * Replace a substring of this object with the given text. If the + * characters being replaced have attributes, the new characters + * that replace them should be given the same attributes. + * + * @param start the beginning index, inclusive; 0 <= start + * <= limit. + * @param limit the ending index, exclusive; start <= limit + * <= length(). + * @param text the text to replace characters start + * to limit - 1 */ + virtual void handleReplaceBetween(UTextOffset start, + UTextOffset limit, + const UnicodeString& text) = 0; + // Note: All other methods in this class take the names of + // existing UnicodeString methods. This method is the exception. + // It is named differently because all replace methods of + // UnicodeString return a UnicodeString&. The 'between' is + // required in order to conform to the UnicodeString naming + // convention; API taking start/length are named , and + // those taking start/limit are named . The + // 'handle' is added because 'replaceBetween' and + // 'doReplaceBetween' are already taken. + +protected: + + /** + * Default constructor. + */ + Replaceable(); +}; + +inline Replaceable::Replaceable() {} + +inline Replaceable::~Replaceable() {} + +#endif diff --git a/icu4c/source/common/unistr.cpp b/icu4c/source/common/unistr.cpp index 4c6855a7263..0f3134267e6 100644 --- a/icu4c/source/common/unistr.cpp +++ b/icu4c/source/common/unistr.cpp @@ -2,6 +2,8 @@ ******************************************************************************* * * * COPYRIGHT: * +* Copyright (C) 1999, International Business Machines Corporation and * +* others. All Rights Reserved. * * (C) Copyright International Business Machines Corporation, 1998-1999 * * Licensed Material - Program-Property of IBM - All Rights Reserved. * * US Government Users Restricted Rights - Use, duplication, or disclosure * @@ -17,6 +19,8 @@ * 09/25/98 stephen Creation. * 04/20/99 stephen Overhauled per 4/16 code review. * 07/09/99 stephen Renamed {hi,lo},{byte,word} to icu_X for HP/UX +* 11/18/99 aliu Added handleReplaceBetween() to make inherit from +* Replaceable. ******************************************************************************* */ @@ -656,6 +660,16 @@ UnicodeString::doReplace(UTextOffset start, return *this; } +/** + * Replaceable API + */ +void +UnicodeString::handleReplaceBetween(UTextOffset start, + UTextOffset limit, + const UnicodeString& text) { + replaceBetween(start, limit, text); +} + UnicodeString& UnicodeString::doReverse(UTextOffset start, int32_t length) diff --git a/icu4c/source/common/unistr.h b/icu4c/source/common/unistr.h index f655cc8f679..567a8bc8f5d 100644 --- a/icu4c/source/common/unistr.h +++ b/icu4c/source/common/unistr.h @@ -2,6 +2,8 @@ ******************************************************************************* * * * COPYRIGHT: * +* Copyright (C) 1999, International Business Machines Corporation and * +* others. All Rights Reserved. * * (C) Copyright International Business Machines Corporation, 1998-1999 * * Licensed Material - Program-Property of IBM - All Rights Reserved. * * US Government Users Restricted Rights - Use, duplication, or disclosure * @@ -17,6 +19,8 @@ * 09/25/98 stephen Creation. * 11/11/98 stephen Changed per 11/9 code review. * 04/20/99 stephen Overhauled per 4/16 code review. +* 11/18/99 aliu Made to inherit from Replaceable. Added method +* handleReplaceBetween(); other methods unchanged. ******************************************************************************* */ @@ -28,6 +32,7 @@ #include "utypes.h" #include "unicode.h" #include "ucnv.h" +#include "rep.h" // Size of stack buffer for small strings #define US_STACKBUF_SIZE 10 @@ -40,7 +45,7 @@ class UCharReference; * UnicodeText. UnicodeString performs codeset conversion from char* * data based on the type of data specified. */ -class U_COMMON_API UnicodeString +class U_COMMON_API UnicodeString : public Replaceable { public: @@ -1073,6 +1078,19 @@ public: UTextOffset srcStart, UTextOffset srcLimit); + /** + * Replace a substring of this object with the given text. + * @param start the beginning index, inclusive; 0 <= start + * <= limit. + * @param limit the ending index, exclusive; start <= limit + * <= length(). + * @param text the text to replace characters start + * to limit - 1 + */ + virtual void handleReplaceBetween(UTextOffset start, + UTextOffset limit, + const UnicodeString& text); + /* Search and replace operations */