2 *******************************************************************************
\r
3 * Copyright (C) 1996-2007, International Business Machines Corporation and *
\r
4 * others. All Rights Reserved. *
\r
5 *******************************************************************************
\r
7 package com.ibm.icu.text;
\r
9 import com.ibm.icu.impl.Utility;
\r
12 * <code>ReplaceableString</code> is an adapter class that implements the
\r
13 * <code>Replaceable</code> API around an ordinary <code>StringBuffer</code>.
\r
15 * <p><em>Note:</em> This class does not support attributes and is not
\r
16 * intended for general use. Most clients will need to implement
\r
17 * {@link Replaceable} in their text representation class.
\r
19 * <p>Copyright © IBM Corporation 1999. All rights reserved.
\r
25 public class ReplaceableString implements Replaceable {
\r
26 private StringBuffer buf;
\r
29 * Construct a new object with the given initial contents.
\r
30 * @param str initial contents
\r
33 public ReplaceableString(String str) {
\r
34 buf = new StringBuffer(str);
\r
38 * Construct a new object using <code>buf</code> for internal
\r
39 * storage. The contents of <code>buf</code> at the time of
\r
40 * construction are used as the initial contents. <em>Note!
\r
41 * Modifications to <code>buf</code> will modify this object, and
\r
43 * @param buf object to be used as internal storage
\r
46 public ReplaceableString(StringBuffer buf) {
\r
51 * Construct a new empty object.
\r
54 public ReplaceableString() {
\r
55 buf = new StringBuffer();
\r
59 * Return the contents of this object as a <code>String</code>.
\r
60 * @return string contents of this object
\r
63 public String toString() {
\r
64 return buf.toString();
\r
68 * Return a substring of the given string.
\r
71 public String substring(int start, int limit) {
\r
72 return buf.substring(start, limit);
\r
76 * Return the number of characters contained in this object.
\r
77 * <code>Replaceable</code> API.
\r
80 public int length() {
\r
81 return buf.length();
\r
85 * Return the character at the given position in this object.
\r
86 * <code>Replaceable</code> API.
\r
87 * @param offset offset into the contents, from 0 to
\r
88 * <code>length()</code> - 1
\r
91 public char charAt(int offset) {
\r
92 return buf.charAt(offset);
\r
96 * Return the 32-bit code point at the given 16-bit offset into
\r
97 * the text. This assumes the text is stored as 16-bit code units
\r
98 * with surrogate pairs intermixed. If the offset of a leading or
\r
99 * trailing code unit of a surrogate pair is given, return the
\r
100 * code point of the surrogate pair.
\r
101 * @param offset an integer between 0 and <code>length()</code>-1
\r
103 * @return 32-bit code point of text at given offset
\r
106 public int char32At(int offset) {
\r
107 return UTF16.charAt(buf, offset);
\r
111 * Copies characters from this object into the destination
\r
112 * character array. The first character to be copied is at index
\r
113 * <code>srcStart</code>; the last character to be copied is at
\r
114 * index <code>srcLimit-1</code> (thus the total number of
\r
115 * characters to be copied is <code>srcLimit-srcStart</code>). The
\r
116 * characters are copied into the subarray of <code>dst</code>
\r
117 * starting at index <code>dstStart</code> and ending at index
\r
118 * <code>dstStart + (srcLimit-srcStart) - 1</code>.
\r
120 * @param srcStart the beginning index to copy, inclusive; <code>0
\r
121 * <= start <= limit</code>.
\r
122 * @param srcLimit the ending index to copy, exclusive;
\r
123 * <code>start <= limit <= length()</code>.
\r
124 * @param dst the destination array.
\r
125 * @param dstStart the start offset in the destination array.
\r
128 public void getChars(int srcStart, int srcLimit, char dst[], int dstStart) {
\r
129 Utility.getChars(buf, 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