2 *******************************************************************************
\r
3 * Copyright (C) 1996-2009, International Business Machines Corporation and *
\r
4 * others. All Rights Reserved. *
\r
5 *******************************************************************************
\r
7 package com.ibm.icu.text;
\r
10 * <code>ReplaceableString</code> is an adapter class that implements the
\r
11 * <code>Replaceable</code> API around an ordinary <code>StringBuffer</code>.
\r
13 * <p><em>Note:</em> This class does not support attributes and is not
\r
14 * intended for general use. Most clients will need to implement
\r
15 * {@link Replaceable} in their text representation class.
\r
17 * <p>Copyright © IBM Corporation 1999. All rights reserved.
\r
23 public class ReplaceableString implements Replaceable {
\r
24 private StringBuffer buf;
\r
27 * Construct a new object with the given initial contents.
\r
28 * @param str initial contents
\r
31 public ReplaceableString(String str) {
\r
32 buf = new StringBuffer(str);
\r
36 * Construct a new object using <code>buf</code> for internal
\r
37 * storage. The contents of <code>buf</code> at the time of
\r
38 * construction are used as the initial contents. <em>Note!
\r
39 * Modifications to <code>buf</code> will modify this object, and
\r
41 * @param buf object to be used as internal storage
\r
44 public ReplaceableString(StringBuffer buf) {
\r
49 * Construct a new empty object.
\r
52 public ReplaceableString() {
\r
53 buf = new StringBuffer();
\r
57 * Return the contents of this object as a <code>String</code>.
\r
58 * @return string contents of this object
\r
61 public String toString() {
\r
62 return buf.toString();
\r
66 * Return a substring of the given string.
\r
69 public String substring(int start, int limit) {
\r
70 return buf.substring(start, limit);
\r
74 * Return the number of characters contained in this object.
\r
75 * <code>Replaceable</code> API.
\r
78 public int length() {
\r
79 return buf.length();
\r
83 * Return the character at the given position in this object.
\r
84 * <code>Replaceable</code> API.
\r
85 * @param offset offset into the contents, from 0 to
\r
86 * <code>length()</code> - 1
\r
89 public char charAt(int offset) {
\r
90 return buf.charAt(offset);
\r
94 * Return the 32-bit code point at the given 16-bit offset into
\r
95 * the text. This assumes the text is stored as 16-bit code units
\r
96 * with surrogate pairs intermixed. If the offset of a leading or
\r
97 * trailing code unit of a surrogate pair is given, return the
\r
98 * code point of the surrogate pair.
\r
99 * @param offset an integer between 0 and <code>length()</code>-1
\r
101 * @return 32-bit code point of text at given offset
\r
104 public int char32At(int offset) {
\r
105 return UTF16.charAt(buf, offset);
\r
109 * Copies characters from this object into the destination
\r
110 * character array. The first character to be copied is at index
\r
111 * <code>srcStart</code>; the last character to be copied is at
\r
112 * index <code>srcLimit-1</code> (thus the total number of
\r
113 * characters to be copied is <code>srcLimit-srcStart</code>). The
\r
114 * characters are copied into the subarray of <code>dst</code>
\r
115 * starting at index <code>dstStart</code> and ending at index
\r
116 * <code>dstStart + (srcLimit-srcStart) - 1</code>.
\r
118 * @param srcStart the beginning index to copy, inclusive; <code>0
\r
119 * <= start <= limit</code>.
\r
120 * @param srcLimit the ending index to copy, exclusive;
\r
121 * <code>start <= limit <= length()</code>.
\r
122 * @param dst the destination array.
\r
123 * @param dstStart the start offset in the destination array.
\r
126 public void getChars(int srcStart, int srcLimit, char dst[], int dstStart) {
\r
127 if (srcStart != srcLimit) {
\r
128 buf.getChars(srcStart, srcLimit, dst, dstStart);
\r
133 * Replace zero or more characters with new characters.
\r
134 * <code>Replaceable</code> API.
\r
135 * @param start the beginning index, inclusive; <code>0 <= start
\r
137 * @param limit the ending index, exclusive; <code>start <= limit
\r
138 * <= length()</code>.
\r
139 * @param text new text to replace characters <code>start</code> to
\r
140 * <code>limit - 1</code>
\r
143 public void replace(int start, int limit, String text) {
\r
144 buf.replace(start, limit, text);
\r
148 * Replace a substring of this object with the given text.
\r
149 * @param start the beginning index, inclusive; <code>0 <= start
\r
151 * @param limit the ending index, exclusive; <code>start <= limit
\r
152 * <= length()</code>.
\r
153 * @param chars the text to replace characters <code>start</code>
\r
154 * to <code>limit - 1</code>
\r
155 * @param charsStart the beginning index into <code>chars</code>,
\r
156 * inclusive; <code>0 <= start <= limit</code>.
\r
157 * @param charsLen the number of characters of <code>chars</code>.
\r
160 public void replace(int start, int limit, char[] chars,
\r
161 int charsStart, int charsLen) {
\r
162 buf.delete(start, limit);
\r
163 buf.insert(start, chars, charsStart, charsLen);
\r
167 * Copy a substring of this object, retaining attribute (out-of-band)
\r
168 * information. This method is used to duplicate or reorder substrings.
\r
169 * The destination index must not overlap the source range.
\r
171 * @param start the beginning index, inclusive; <code>0 <= start <=
\r
173 * @param limit the ending index, exclusive; <code>start <= limit <=
\r
175 * @param dest the destination index. The characters from
\r
176 * <code>start..limit-1</code> will be copied to <code>dest</code>.
\r
177 * Implementations of this method may assume that <code>dest <= start ||
\r
178 * dest >= limit</code>.
\r
181 public void copy(int start, int limit, int dest) {
\r
182 if (start == limit && start >= 0 && start <= buf.length()) {
\r
185 char[] text = new char[limit - start];
\r
186 getChars(start, limit, text, 0);
\r
187 replace(dest, dest, text, 0, limit - start);
\r
191 * Implements Replaceable
\r
194 public boolean hasMetaData() {
\r