MCPcopy
hub / github.com/libgdx/libgdx / simpleFormat

Method simpleFormat

gdx/src/com/badlogic/gdx/utils/TextFormatter.java:115–154  ·  view source on GitHub ↗

Formats the given pattern replacing any placeholder of the form {0}, {1}, {2} and so on with the corresponding object from args converted to a string with toString(), so without taking into account the locale. This method only implements a small subset of the grammar supp

(String pattern, Object... args)

Source from the content-addressed store, hash-verified

113 * @return the formatted pattern
114 * @exception IllegalArgumentException if the pattern is invalid */
115 private String simpleFormat (String pattern, Object... args) {
116 buffer.setLength(0);
117 boolean changed = false;
118 int placeholder = -1;
119 int patternLength = pattern.length();
120 for (int i = 0; i < patternLength; ++i) {
121 char ch = pattern.charAt(i);
122 if (placeholder < 0) { // processing constant part
123 if (ch == '{') {
124 changed = true;
125 if (i + 1 < patternLength && pattern.charAt(i + 1) == '{') {
126 buffer.append(ch); // handle escaped '{'
127 ++i;
128 } else {
129 placeholder = 0; // switch to placeholder part
130 }
131 } else {
132 buffer.append(ch);
133 }
134 } else { // processing placeholder part
135 if (ch == '}') {
136 if (placeholder >= args.length) throw new IllegalArgumentException("Argument index out of bounds: " + placeholder);
137 if (pattern.charAt(i - 1) == '{')
138 throw new IllegalArgumentException("Missing argument index after a left curly brace");
139 if (args[placeholder] == null)
140 buffer.append("null"); // append null argument
141 else
142 buffer.append(args[placeholder].toString()); // append actual argument
143 placeholder = -1; // switch to constant part
144 } else {
145 if (ch < '0' || ch > '9')
146 throw new IllegalArgumentException("Unexpected '" + ch + "' while parsing argument index");
147 placeholder = placeholder * 10 + (ch - '0');
148 }
149 }
150 }
151 if (placeholder >= 0) throw new IllegalArgumentException("Unmatched braces in the pattern.");
152
153 return changed ? buffer.toString() : pattern;
154 }
155}

Callers 1

formatMethod · 0.95

Calls 5

setLengthMethod · 0.65
lengthMethod · 0.45
charAtMethod · 0.45
appendMethod · 0.45
toStringMethod · 0.45

Tested by

no test coverage detected