001: /***
002: * Retrotranslator: a Java bytecode transformer that translates Java classes
003: * compiled with JDK 5.0 into classes that can be run on JVM 1.4.
004: *
005: * Copyright (c) 2005 - 2008 Taras Puchko
006: * All rights reserved.
007: *
008: * Redistribution and use in source and binary forms, with or without
009: * modification, are permitted provided that the following conditions
010: * are met:
011: * 1. Redistributions of source code must retain the above copyright
012: * notice, this list of conditions and the following disclaimer.
013: * 2. Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in the
015: * documentation and/or other materials provided with the distribution.
016: * 3. Neither the name of the copyright holders nor the names of its
017: * contributors may be used to endorse or promote products derived from
018: * this software without specific prior written permission.
019: *
020: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
021: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
022: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
023: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
024: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
025: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
026: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
027: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
028: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
029: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
030: * THE POSSIBILITY OF SUCH DAMAGE.
031: */package net.sf.retrotranslator.runtime.java.lang;
032:
033: import java.util.*;
034:
035: /**
036: * @author Taras Puchko
037: */
038: public class _String {
039:
040: public static String convertConstructorArguments(int[] codePoints,
041: int offset, int count) {
042: if (offset < 0) {
043: throw new StringIndexOutOfBoundsException(offset);
044: }
045: if (count < 0) {
046: throw new StringIndexOutOfBoundsException(count);
047: }
048: if (offset + count > codePoints.length
049: || offset + count < offset) {
050: throw new StringIndexOutOfBoundsException(offset + count);
051: }
052: char[] chars = new char[count * 2 >= 0 ? count * 2
053: : Integer.MAX_VALUE];
054: int index = 0;
055: for (int i = 0; i < count; i++) {
056: index += _Character.toChars(codePoints[offset + i], chars,
057: index);
058: }
059: return new String(chars, 0, index);
060: }
061:
062: public static int codePointAt(String s, int index) {
063: return _Character.codePointAt(s, index);
064: }
065:
066: public static int codePointBefore(String s, int index) {
067: return _Character.codePointBefore(s, index);
068: }
069:
070: public static int codePointCount(String s, int beginIndex,
071: int endIndex) {
072: return _Character.codePointCount(s, beginIndex, endIndex);
073: }
074:
075: public static boolean contains(String s, CharSequence cs) {
076: return s.indexOf(cs.toString()) >= 0;
077: }
078:
079: public static boolean contentEquals(String s, CharSequence cs) {
080: return s.length() == cs.length() && s.equals(cs.toString());
081: }
082:
083: public static String format(String format, Object... args) {
084: return new Formatter().format(format, args).toString();
085: }
086:
087: public static String format(Locale locale, String format,
088: Object... args) {
089: return new Formatter(locale).format(format, args).toString();
090: }
091:
092: public static int offsetByCodePoints(String s, int index,
093: int codePointOffset) {
094: return _Character.offsetByCodePoints(s, index, codePointOffset);
095: }
096:
097: public static String replace(String s, CharSequence target,
098: CharSequence replacement) {
099: String pattern = target.toString();
100: int patternIndex = s.indexOf(pattern);
101: if (patternIndex < 0)
102: return s;
103: if (pattern.length() > 0) {
104: StringBuilder builder = new StringBuilder();
105: int startIndex = 0;
106: do {
107: builder.append(s.substring(startIndex, patternIndex))
108: .append(replacement);
109: startIndex = patternIndex + pattern.length();
110: patternIndex = s.indexOf(pattern, startIndex);
111: } while (patternIndex >= 0);
112: return builder.append(s.substring(startIndex)).toString();
113: } else {
114: StringBuilder builder = new StringBuilder();
115: for (int i = 0; i < s.length(); i++) {
116: builder.append(replacement).append(s.charAt(i));
117: }
118: return builder.append(replacement).toString();
119: }
120: }
121:
122: /**
123: * @since JDK 1.6
124: */
125: public static boolean isEmpty(String s) {
126: return s.length() == 0;
127: }
128:
129: }
|