1999-12-28 23:39:02 +00:00
|
|
|
/*
|
2001-03-21 20:44:20 +00:00
|
|
|
**************************************************************************
|
2012-11-09 19:34:53 +00:00
|
|
|
* Copyright (C) 1999-2012, International Business Machines Corporation and
|
1999-12-28 23:39:02 +00:00
|
|
|
* others. All Rights Reserved.
|
2001-03-21 20:44:20 +00:00
|
|
|
**************************************************************************
|
1999-12-28 23:39:02 +00:00
|
|
|
* 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.
|
2001-03-21 20:44:20 +00:00
|
|
|
**************************************************************************
|
1999-12-28 23:39:02 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef REP_H
|
|
|
|
#define REP_H
|
|
|
|
|
2002-06-27 01:19:20 +00:00
|
|
|
#include "unicode/uobject.h"
|
|
|
|
|
2005-06-22 22:39:36 +00:00
|
|
|
/**
|
|
|
|
* \file
|
|
|
|
* \brief C++ API: Replaceable String
|
|
|
|
*/
|
|
|
|
|
2001-10-08 23:26:58 +00:00
|
|
|
U_NAMESPACE_BEGIN
|
1999-12-28 23:39:02 +00:00
|
|
|
|
|
|
|
class UnicodeString;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* <code>Replaceable</code> 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
|
2002-07-03 01:03:46 +00:00
|
|
|
* change a piece of text while retaining metadata. Metadata is data
|
|
|
|
* other than the Unicode characters returned by char32At(). One
|
|
|
|
* example of metadata is style attributes; another is an edit
|
|
|
|
* history, marking each character with an author and revision number.
|
1999-12-28 23:39:02 +00:00
|
|
|
*
|
2002-07-03 01:03:46 +00:00
|
|
|
* <p>An implicit aspect of the <code>Replaceable</code> API is that
|
|
|
|
* during a replace operation, new characters take on the metadata of
|
|
|
|
* the old characters. For example, if the string "the <b>bold</b>
|
|
|
|
* font" has range (4, 8) replaced with "strong", then it becomes "the
|
|
|
|
* <b>strong</b> font".
|
|
|
|
*
|
|
|
|
* <p><code>Replaceable</code> specifies ranges using a start
|
1999-12-28 23:39:02 +00:00
|
|
|
* offset and a limit offset. The range of characters thus specified
|
2002-07-03 01:03:46 +00:00
|
|
|
* includes the characters at offset start..limit-1. That is, the
|
1999-12-28 23:39:02 +00:00
|
|
|
* start offset is inclusive, and the limit offset is exclusive.
|
|
|
|
*
|
|
|
|
* <p><code>Replaceable</code> also includes API to access characters
|
2002-07-03 01:03:46 +00:00
|
|
|
* in the string: <code>length()</code>, <code>charAt()</code>,
|
|
|
|
* <code>char32At()</code>, and <code>extractBetween()</code>.
|
1999-12-28 23:39:02 +00:00
|
|
|
*
|
2002-07-03 01:03:46 +00:00
|
|
|
* <p>For a subclass to support metadata, typical behavior of
|
|
|
|
* <code>replace()</code> is the following:
|
2001-11-21 21:15:16 +00:00
|
|
|
* <ul>
|
2002-07-03 01:03:46 +00:00
|
|
|
* <li>Set the metadata of the new text to the metadata of the first
|
|
|
|
* character replaced</li>
|
|
|
|
* <li>If no characters are replaced, use the metadata of the
|
|
|
|
* previous character</li>
|
|
|
|
* <li>If there is no previous character (i.e. start == 0), use the
|
|
|
|
* following character</li>
|
|
|
|
* <li>If there is no following character (i.e. the replaceable was
|
|
|
|
* empty), use default metadata.<br>
|
2002-11-14 01:44:57 +00:00
|
|
|
* <li>If the code point U+FFFF is seen, it should be interpreted as
|
|
|
|
* a special marker having no metadata<li>
|
2001-11-21 21:15:16 +00:00
|
|
|
* </li>
|
|
|
|
* </ul>
|
|
|
|
* If this is not the behavior, the subclass should document any differences.
|
1999-12-28 23:39:02 +00:00
|
|
|
* @author Alan Liu
|
2002-12-04 23:39:56 +00:00
|
|
|
* @stable ICU 2.0
|
1999-12-28 23:39:02 +00:00
|
|
|
*/
|
2002-06-27 01:19:20 +00:00
|
|
|
class U_COMMON_API Replaceable : public UObject {
|
1999-12-28 23:39:02 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* Destructor.
|
2002-12-04 23:39:56 +00:00
|
|
|
* @stable ICU 2.0
|
1999-12-28 23:39:02 +00:00
|
|
|
*/
|
|
|
|
virtual ~Replaceable();
|
|
|
|
|
|
|
|
/**
|
2002-07-03 01:03:46 +00:00
|
|
|
* Returns the number of 16-bit code units in the text.
|
|
|
|
* @return number of 16-bit code units in text
|
2002-12-19 00:43:21 +00:00
|
|
|
* @stable ICU 1.8
|
1999-12-28 23:39:02 +00:00
|
|
|
*/
|
2001-02-14 18:02:44 +00:00
|
|
|
inline int32_t length() const;
|
1999-12-28 23:39:02 +00:00
|
|
|
|
|
|
|
/**
|
2002-07-03 01:03:46 +00:00
|
|
|
* Returns the 16-bit code unit at the given offset into the text.
|
1999-12-28 23:39:02 +00:00
|
|
|
* @param offset an integer between 0 and <code>length()</code>-1
|
|
|
|
* inclusive
|
2002-07-03 01:03:46 +00:00
|
|
|
* @return 16-bit code unit of text at given offset
|
2002-12-19 00:43:21 +00:00
|
|
|
* @stable ICU 1.8
|
1999-12-28 23:39:02 +00:00
|
|
|
*/
|
2002-03-12 01:32:42 +00:00
|
|
|
inline UChar charAt(int32_t offset) const;
|
1999-12-28 23:39:02 +00:00
|
|
|
|
2000-04-01 00:21:57 +00:00
|
|
|
/**
|
2002-07-03 01:03:46 +00:00
|
|
|
* Returns the 32-bit code point at the given 16-bit offset into
|
|
|
|
* the text. This assumes the text is stored as 16-bit code units
|
|
|
|
* with surrogate pairs intermixed. If the offset of a leading or
|
|
|
|
* trailing code unit of a surrogate pair is given, return the
|
|
|
|
* code point of the surrogate pair.
|
|
|
|
*
|
2000-04-01 00:21:57 +00:00
|
|
|
* @param offset an integer between 0 and <code>length()</code>-1
|
2002-07-03 01:03:46 +00:00
|
|
|
* inclusive
|
|
|
|
* @return 32-bit code point of text at given offset
|
2002-12-19 00:43:21 +00:00
|
|
|
* @stable ICU 1.8
|
2000-04-01 00:21:57 +00:00
|
|
|
*/
|
2002-03-12 01:32:42 +00:00
|
|
|
inline UChar32 char32At(int32_t offset) const;
|
2000-04-01 00:21:57 +00:00
|
|
|
|
1999-12-28 23:39:02 +00:00
|
|
|
/**
|
2002-07-03 01:03:46 +00:00
|
|
|
* Copies characters in the range [<tt>start</tt>, <tt>limit</tt>)
|
2002-02-26 17:50:59 +00:00
|
|
|
* 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>
|
2002-12-19 00:43:21 +00:00
|
|
|
* @stable ICU 2.1
|
2000-03-22 18:31:40 +00:00
|
|
|
*/
|
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 = 0;
|
1999-12-28 23:39:02 +00:00
|
|
|
|
|
|
|
/**
|
2002-07-03 01:03:46 +00:00
|
|
|
* Replaces a substring of this object with the given text. If the
|
|
|
|
* characters being replaced have metadata, the new characters
|
|
|
|
* that replace them should be given the same metadata.
|
1999-12-28 23:39:02 +00:00
|
|
|
*
|
2001-11-21 21:15:16 +00:00
|
|
|
* <p>Subclasses must ensure that if the text between start and
|
|
|
|
* limit is equal to the replacement text, that replace has no
|
2002-07-03 01:03:46 +00:00
|
|
|
* effect. That is, any metadata
|
|
|
|
* should be unaffected. In addition, subclasses are encouraged to
|
2001-11-21 21:15:16 +00:00
|
|
|
* check for initial and trailing identical characters, and make a
|
|
|
|
* smaller replacement if possible. This will preserve as much
|
2002-07-03 01:03:46 +00:00
|
|
|
* metadata as possible.
|
1999-12-28 23:39:02 +00:00
|
|
|
* @param start the beginning index, inclusive; <code>0 <= start
|
|
|
|
* <= limit</code>.
|
|
|
|
* @param limit the ending index, exclusive; <code>start <= limit
|
|
|
|
* <= length()</code>.
|
|
|
|
* @param text the text to replace characters <code>start</code>
|
2000-03-22 18:31:40 +00:00
|
|
|
* to <code>limit - 1</code>
|
2002-12-04 23:39:56 +00:00
|
|
|
* @stable ICU 2.0
|
2000-03-22 18:31:40 +00:00
|
|
|
*/
|
2002-03-12 01:32:42 +00:00
|
|
|
virtual void handleReplaceBetween(int32_t start,
|
|
|
|
int32_t limit,
|
1999-12-28 23:39:02 +00:00
|
|
|
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 <operation>, and
|
|
|
|
// those taking start/limit are named <operationBetween>. The
|
|
|
|
// 'handle' is added because 'replaceBetween' and
|
|
|
|
// 'doReplaceBetween' are already taken.
|
|
|
|
|
2000-05-20 04:41:12 +00:00
|
|
|
/**
|
2002-07-03 01:03:46 +00:00
|
|
|
* Copies a substring of this object, retaining metadata.
|
|
|
|
* This method is used to duplicate or reorder substrings.
|
2000-05-20 04:41:12 +00:00
|
|
|
* The destination index must not overlap the source range.
|
|
|
|
*
|
|
|
|
* @param start the beginning index, inclusive; <code>0 <= start <=
|
|
|
|
* limit</code>.
|
|
|
|
* @param limit the ending index, exclusive; <code>start <= limit <=
|
|
|
|
* length()</code>.
|
|
|
|
* @param dest the destination index. The characters from
|
|
|
|
* <code>start..limit-1</code> will be copied to <code>dest</code>.
|
|
|
|
* Implementations of this method may assume that <code>dest <= start ||
|
|
|
|
* dest >= limit</code>.
|
2002-12-04 23:39:56 +00:00
|
|
|
* @stable ICU 2.0
|
2002-07-03 01:03:46 +00:00
|
|
|
*/
|
2000-05-20 04:41:12 +00:00
|
|
|
virtual void copy(int32_t start, int32_t limit, int32_t dest) = 0;
|
|
|
|
|
2002-07-03 01:03:46 +00:00
|
|
|
/**
|
|
|
|
* Returns true if this object contains metadata. If a
|
|
|
|
* Replaceable object has metadata, calls to the Replaceable API
|
|
|
|
* must be made so as to preserve metadata. If it does not, calls
|
|
|
|
* to the Replaceable API may be optimized to improve performance.
|
|
|
|
* The default implementation returns true.
|
|
|
|
* @return true if this object contains metadata
|
2002-12-19 00:43:21 +00:00
|
|
|
* @stable ICU 2.2
|
2002-07-03 01:03:46 +00:00
|
|
|
*/
|
|
|
|
virtual UBool hasMetaData() const;
|
|
|
|
|
2003-04-30 22:08:57 +00:00
|
|
|
/**
|
|
|
|
* Clone this object, an instance of a subclass of Replaceable.
|
|
|
|
* Clones can be used concurrently in multiple threads.
|
|
|
|
* If a subclass does not implement clone(), or if an error occurs,
|
|
|
|
* then NULL is returned.
|
|
|
|
* The clone functions in all subclasses return a pointer to a Replaceable
|
|
|
|
* because some compilers do not support covariant (same-as-this)
|
|
|
|
* return types; cast to the appropriate subclass if necessary.
|
|
|
|
* The caller must delete the clone.
|
|
|
|
*
|
|
|
|
* @return a clone of this object
|
|
|
|
*
|
|
|
|
* @see getDynamicClassID
|
2004-11-02 07:28:07 +00:00
|
|
|
* @stable ICU 2.6
|
2003-04-30 22:08:57 +00:00
|
|
|
*/
|
|
|
|
virtual Replaceable *clone() const;
|
|
|
|
|
1999-12-28 23:39:02 +00:00
|
|
|
protected:
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Default constructor.
|
2004-03-26 19:42:04 +00:00
|
|
|
* @stable ICU 2.4
|
1999-12-28 23:39:02 +00:00
|
|
|
*/
|
2012-11-09 19:34:53 +00:00
|
|
|
inline Replaceable();
|
2001-02-14 18:02:44 +00:00
|
|
|
|
2003-05-11 23:27:07 +00:00
|
|
|
/*
|
|
|
|
* Assignment operator not declared. The compiler will provide one
|
|
|
|
* which does nothing since this class does not contain any data members.
|
|
|
|
* API/code coverage may show the assignment operator as present and
|
|
|
|
* untested - ignore.
|
|
|
|
* Subclasses need this assignment operator if they use compiler-provided
|
|
|
|
* assignment operators of their own. An alternative to not declaring one
|
|
|
|
* here would be to declare and empty-implement a protected or public one.
|
|
|
|
Replaceable &Replaceable::operator=(const Replaceable &);
|
|
|
|
*/
|
2003-05-10 23:10:33 +00:00
|
|
|
|
2001-02-14 18:02:44 +00:00
|
|
|
/**
|
2001-07-02 20:58:14 +00:00
|
|
|
* Virtual version of length().
|
2004-03-26 19:42:04 +00:00
|
|
|
* @stable ICU 2.4
|
2001-07-02 20:58:14 +00:00
|
|
|
*/
|
|
|
|
virtual int32_t getLength() const = 0;
|
2001-02-14 18:02:44 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Virtual version of charAt().
|
2004-03-26 19:42:04 +00:00
|
|
|
* @stable ICU 2.4
|
2001-02-14 18:02:44 +00:00
|
|
|
*/
|
2002-03-12 01:32:42 +00:00
|
|
|
virtual UChar getCharAt(int32_t offset) const = 0;
|
2001-02-14 18:02:44 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Virtual version of char32At().
|
2004-03-26 19:42:04 +00:00
|
|
|
* @stable ICU 2.4
|
2001-02-14 18:02:44 +00:00
|
|
|
*/
|
2002-03-12 01:32:42 +00:00
|
|
|
virtual UChar32 getChar32At(int32_t offset) const = 0;
|
1999-12-28 23:39:02 +00:00
|
|
|
};
|
|
|
|
|
2012-11-09 19:34:53 +00:00
|
|
|
inline Replaceable::Replaceable() {}
|
|
|
|
|
2001-02-14 18:02:44 +00:00
|
|
|
inline int32_t
|
|
|
|
Replaceable::length() const {
|
2001-07-02 20:58:14 +00:00
|
|
|
return getLength();
|
2001-02-14 18:02:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline UChar
|
2002-03-12 01:32:42 +00:00
|
|
|
Replaceable::charAt(int32_t offset) const {
|
2001-02-14 18:02:44 +00:00
|
|
|
return getCharAt(offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline UChar32
|
2002-03-12 01:32:42 +00:00
|
|
|
Replaceable::char32At(int32_t offset) const {
|
2001-02-14 18:02:44 +00:00
|
|
|
return getChar32At(offset);
|
|
|
|
}
|
|
|
|
|
2003-04-30 22:08:57 +00:00
|
|
|
// There is no rep.cpp, see unistr.cpp for Replaceable function implementations.
|
2002-07-03 01:03:46 +00:00
|
|
|
|
2001-10-08 23:26:58 +00:00
|
|
|
U_NAMESPACE_END
|
|
|
|
|
1999-12-28 23:39:02 +00:00
|
|
|
#endif
|