001/**
002 * Copyright (c) 2004-2011 QOS.ch
003 * All rights reserved.
004 *
005 * Permission is hereby granted, free  of charge, to any person obtaining
006 * a  copy  of this  software  and  associated  documentation files  (the
007 * "Software"), to  deal in  the Software without  restriction, including
008 * without limitation  the rights to  use, copy, modify,  merge, publish,
009 * distribute,  sublicense, and/or sell  copies of  the Software,  and to
010 * permit persons to whom the Software  is furnished to do so, subject to
011 * the following conditions:
012 *
013 * The  above  copyright  notice  and  this permission  notice  shall  be
014 * included in all copies or substantial portions of the Software.
015 *
016 * THE  SOFTWARE IS  PROVIDED  "AS  IS", WITHOUT  WARRANTY  OF ANY  KIND,
017 * EXPRESS OR  IMPLIED, INCLUDING  BUT NOT LIMITED  TO THE  WARRANTIES OF
018 * MERCHANTABILITY,    FITNESS    FOR    A   PARTICULAR    PURPOSE    AND
019 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
020 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
021 * OF CONTRACT, TORT OR OTHERWISE,  ARISING FROM, OUT OF OR IN CONNECTION
022 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
023 *
024 */
025package org.slf4j.profiler;
026
027public class SpacePadder {
028    public static final String LINE_SEP = System.getProperty("line.separator");
029
030    final static String[] SPACES = { " ", "  ", "    ", "        ", // 1,2,4,8
031            // spaces
032            "                ", // 16 spaces
033            "                                " }; // 32 spaces
034
035    @Deprecated
036    final static public void leftPad(StringBuffer buf, String s, int desiredLength) {
037        int actualLen = 0;
038        if (s != null) {
039            actualLen = s.length();
040        }
041        if (actualLen < desiredLength) {
042            spacePad(buf, desiredLength - actualLen);
043        }
044        if (s != null) {
045            buf.append(s);
046        }
047    }
048
049    final static public void leftPad(StringBuilder buf, String s, int desiredLength) {
050        int actualLen = 0;
051        if (s != null) {
052            actualLen = s.length();
053        }
054        if (actualLen < desiredLength) {
055            spacePad(buf, desiredLength - actualLen);
056        }
057        if (s != null) {
058            buf.append(s);
059        }
060    }
061
062    @Deprecated
063    final static public void rightPad(StringBuffer buf, String s, int desiredLength) {
064        int actualLen = 0;
065        if (s != null) {
066            actualLen = s.length();
067        }
068        if (s != null) {
069            buf.append(s);
070        }
071        if (actualLen < desiredLength) {
072            spacePad(buf, desiredLength - actualLen);
073        }
074    }
075
076    final static public void rightPad(StringBuilder buf, String s, int desiredLength) {
077        int actualLen = 0;
078        if (s != null) {
079            actualLen = s.length();
080        }
081        if (s != null) {
082            buf.append(s);
083        }
084        if (actualLen < desiredLength) {
085            spacePad(buf, desiredLength - actualLen);
086        }
087    }
088
089    /**
090     * Fast space padding method.
091     * 
092     * @param sbuf the buffer to pad
093     * @param length the target size of the buffer after padding
094     */
095    @Deprecated
096    final static public void spacePad(StringBuffer sbuf, int length) {
097        while (length >= 32) {
098            sbuf.append(SPACES[5]);
099            length -= 32;
100        }
101
102        for (int i = 4; i >= 0; i--) {
103            if ((length & (1 << i)) != 0) {
104                sbuf.append(SPACES[i]);
105            }
106        }
107    }
108
109    /**
110     * Fast space padding method.
111     * 
112     * @param sbuf the buffer to pad
113     * @param length the target size of the buffer after padding
114     */
115    final static public void spacePad(StringBuilder sbuf, int length) {
116        while (length >= 32) {
117            sbuf.append(SPACES[5]);
118            length -= 32;
119        }
120
121        for (int i = 4; i >= 0; i--) {
122            if ((length & (1 << i)) != 0) {
123                sbuf.append(SPACES[i]);
124            }
125        }
126    }
127}