/*
 * @(#)Strings.java  1.2  2002-05-10
 *
 * Copyright (c) 1999 Werner Randelshofer, Immensee, Switzerland.
 * All rights reserved.
 *
 * You may not use, copy or modify this file, except in compliance with the
 * license agreement you entered into with Werner Randelshofer.
 * For details see accompanying license terms.
 */
package ch.randelshofer.util;

/**
 * This class contains various methods for manipulating strings.
 *
 * @author Werner Randelshofer, Hausmatt 10, Immensee, CH-6405, Switzerland
 * @version 1.2 2002-05-10 Method replace(String, String, String) added.
 * <br>1.1 2002-02-05
 */
public class Strings extends Object {

    /**
     * Translates all occurences of characters in the
     * char array 'from' to the characters in 'to'.
     *
     * @param s The string to be translated.
     * @param from The characters to be replaced.
     * @param to   The replacement characters.
     *
     * @return The translated String.
     */
    public static String translate(String s, char[] from, char[] to) {
        char[] map = new char[256];

        int i;

        for (i = 0; i < 256; i++) {
            map[i] = (char) i;
        }

        for (i = 0; i < from.length; i++) {
            map[from[i]] = to[i];
        }


        char[] result = new char[s.length()];
        for (i = 0; i < result.length; i++) {
            result[i] = map[s.charAt(i)];
        }

        return new String(result);
    }

    /**
     * Translates all occurences of char 'from' to char 'to'.
     *
     * @param str The string to be translated.
     * @param from The character to be replaced.
     * @param to   The replacement character.
     *
     * @return The translated String.
     */
    public static String translate(String str, char from, char to) {
        char[] chars = str.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            if (chars[i] == from) {
                chars[i] = to;
            }
        }
        return new String(chars);
    }

    /**
     * Replaces all occurences of 'from' by 'to'.
     *
     * @param str The string to be processed.
     * @param from The text to be replaced.
     * @param to   The replacement text.
     *
     * @return The translated String.
     */
    public static String replace(String str, String from, String to) {
        int p0 = 0, p1 = 0;
        StringBuffer buf = new StringBuffer();

        while ((p1 = str.indexOf(from, p0)) != -1) {
            buf.append(str.substring(p0, p1));
            buf.append(to);
            p0 = p1 + from.length();
        }
        buf.append(str.substring(p0, str.length()));

        return buf.toString();
    }

    public static String camelToWords(String str) {

        /** Convert camel case into multiple words. */
        StringBuilder buf = new StringBuilder();
        boolean prevIsUC = true;
        for (int i = 0, n = str.length(); i < n; i++) {
            char c = str.charAt(i);
            boolean isUC = Character.isUpperCase(c);

            if (i > 0 && isUC &&//
                    (!prevIsUC //
                    || i < n - 1 && !Character.isUpperCase(str.charAt(i + 1)))//
                    ) {
                buf.append(' ');
            }
            prevIsUC = isUC;
            buf.append(c == '_' ? ' ' : c);
        }
        return buf.toString();
    }
}
