001/** 002 * Logback: the reliable, generic, fast and flexible logging framework. 003 * Copyright (C) 1999-2015, QOS.ch. All rights reserved. 004 * 005 * This program and the accompanying materials are dual-licensed under 006 * either the terms of the Eclipse Public License v1.0 as published by 007 * the Eclipse Foundation 008 * 009 * or (per the licensee's choosing) 010 * 011 * under the terms of the GNU Lesser General Public License version 2.1 012 * as published by the Free Software Foundation. 013 */ 014package ch.qos.logback.core.pattern; 015 016public class SpacePadder { 017 018 final static String[] SPACES = { " ", " ", " ", " ", // 1,2,4,8 019 // spaces 020 " ", // 16 spaces 021 " " }; // 32 spaces 022 023 final static public void leftPad(StringBuilder buf, String s, int desiredLength) { 024 int actualLen = 0; 025 if (s != null) { 026 actualLen = s.length(); 027 } 028 if (actualLen < desiredLength) { 029 spacePad(buf, desiredLength - actualLen); 030 } 031 if (s != null) { 032 buf.append(s); 033 } 034 } 035 036 final static public void rightPad(StringBuilder buf, String s, int desiredLength) { 037 int actualLen = 0; 038 if (s != null) { 039 actualLen = s.length(); 040 } 041 if (s != null) { 042 buf.append(s); 043 } 044 if (actualLen < desiredLength) { 045 spacePad(buf, desiredLength - actualLen); 046 } 047 } 048 049 /** 050 * Fast space padding method. 051 */ 052 final static public void spacePad(StringBuilder sbuf, int length) { 053 while (length >= 32) { 054 sbuf.append(SPACES[5]); 055 length -= 32; 056 } 057 058 for (int i = 4; i >= 0; i--) { 059 if ((length & (1 << i)) != 0) { 060 sbuf.append(SPACES[i]); 061 } 062 } 063 } 064}