printfの自作
Posted feedbacks - Java
やっつけで解いてみました。 実行結果 ------------------ abcdefg abc%defg abc1010e\fg %c = あ. %f = 10.5. %8ld = 10. %8d = 10. %s = abcdefg. %08s = 0abcdefg. %08x = 0000000a. %08X = 0000000A. ------------------
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 | import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class PrintFormat {
private static final Pattern PATTERN = Pattern.compile("%%|%0??[0-9]*?l??[dfcsxX]");
public static String format(String aFormat, Object... aValues) {
StringBuilder tBuffer = new StringBuilder();
int tValueIndex = 0;
Matcher tMatcher = PATTERN.matcher(aFormat);
int tPreviousIndex = 0;
while (tMatcher.find()) {
tBuffer.append(aFormat, tPreviousIndex, tMatcher.start());
tPreviousIndex = tMatcher.end();
String tMatch = tMatcher.group();
if (tMatch.equals("%%")) {
tBuffer.append('%'); // エスケープ
continue;
}
boolean tZeroPadding = tMatch.startsWith("%0");
boolean tLongType = tMatch.indexOf('l') >= 0;
int tDigitNumber = 0;
if (Character.isDigit(tMatch.charAt(1))) {
tDigitNumber = Integer.parseInt(tMatch.substring(tZeroPadding ? 2 : 1, tMatch.length() - (tLongType ? 2 : 1)));
}
Object tValue = aValues[tValueIndex++];
if (tMatch.endsWith("c")) {
tBuffer.append(tValue);
} else if (tMatch.endsWith("f")) {
tBuffer.append(padding(tValue.toString(), tDigitNumber, tZeroPadding));
} else if (tMatch.endsWith("d")) {
tBuffer.append(padding(tValue.toString(), tDigitNumber, tZeroPadding));
} else if (tMatch.endsWith("s")) {
tBuffer.append(padding(tValue.toString(), tDigitNumber, tZeroPadding));
} else if (tMatch.endsWith("x")) {
if (tLongType) {
tBuffer.append(padding(Long.toHexString((Long) tValue), tDigitNumber, tZeroPadding));
} else {
tBuffer.append(padding(Integer.toHexString((Integer) tValue), tDigitNumber, tZeroPadding));
}
} else if (tMatch.endsWith("X")) {
if (tLongType) {
tBuffer.append(padding(Long.toHexString((Long) tValue).toUpperCase(), tDigitNumber, tZeroPadding));
} else {
tBuffer.append(padding(Integer.toHexString((Integer) tValue).toUpperCase(), tDigitNumber, tZeroPadding));
}
}
}
tBuffer.append(aFormat, tPreviousIndex, aFormat.length());
return new String(tBuffer);
}
private static String padding(String aText, int aLength, boolean aZeroPadding) {
if (aLength == 0) {
return aText; // 長さ指定なし
}
int tDelta = aLength - aText.length();
if (tDelta < 0) {
return aText.substring(-tDelta); // 長すぎる
} else if (tDelta == 0) {
return aText; // ぴったり
}
StringBuilder tBuilder = new StringBuilder(aLength);
for (int i = 0; i < tDelta; i++) {
tBuilder.append(aZeroPadding ? '0' : ' ');
}
tBuilder.append(aText);
return new String(tBuilder);
}
public static void main(String[] args) {
System.out.println(format("abcdefg", new Object[] {}));
System.out.println(format("abc%%defg", new Object[] {}));
System.out.println(format("abc%ld%de\\fg", new Object[] { 10L, 10 }));
System.out.println(format("%%c = %c.", new Object[] { 'あ' }));
System.out.println(format("%%f = %f.", new Object[] { 10.5 }));
System.out.println(format("%%8ld = %8ld.", new Object[] { 10L }));
System.out.println(format("%%8d = %8d.", new Object[] { 10 }));
System.out.println(format("%%s = %s.", new Object[] { "abcdefg" }));
System.out.println(format("%%08s = %08s.", new Object[] { "abcdefg" }));
System.out.println(format("%%08x = %08x.", new Object[] { 10 }));
System.out.println(format("%%08X = %08X.", new Object[] { 10 }));
}
}
|


yappy
#4119()
[
C
]
Rating-4/18=-0.22
Rating-4/18=-0.22-0+
[ reply ]