Sample

int-b568ad36b676-I13

Lower every eligible classic and enhanced for loop to a while loop

Matched original source

Lines138

Scores

MethodScoreScale
Posnett00–1; higher is more readable
Scalabrino0.8850–1; higher is more readable
Dorn0.270–1; higher is more readable
Mi0.4680–1; higher is more readable
LLOC32Logical lines; fewer is more readable
DeepSeek V4 Pro17.330–20; mean of three runs
GPT-6.1 Sol180–20; mean of three runs
BTTF0.5890–1; higher is more readable

Views

Source code

package com.google.common.escape; import static com.google.common.base.Preconditions.checkNotNull; import com.google.common.annotations.GwtCompatible; import java.util.Map; import org.jspecify.annotations.Nullable; /** * A {@link CharEscaper} that uses an array to quickly look up replacement characters for a given * {@code char} value. An additional safe range is provided that determines whether {@code char} * values without specific replacements are to be considered safe and left unescaped or should be * escaped in a general way. * * <p>A good example of usage of this class is for Java source code escaping where the replacement * array contains information about special ASCII characters such as {@code \\t} and {@code \\n} * while {@link #escapeUnsafe} is overridden to handle general escaping of the form {@code \\uxxxx}. * * <p>The size of the data structure used by {@link ArrayBasedCharEscaper} is proportional to the * highest valued character that requires escaping. For example a replacement map containing the * single character '{@code \}{@code u1000}' will require approximately 16K of memory. If you need * to create multiple escaper instances that have the same character replacement mapping consider * using {@link ArrayBasedEscaperMap}. * * @author Sven Mawson * @author David Beaumont * @since 15.0 */ @GwtCompatible public abstract class ArrayBasedCharEscaper extends CharEscaper { // The replacement array (see ArrayBasedEscaperMap). private final char[][] replacements; // The number of elements in the replacement array. private final int replacementsLength; // The first character in the safe range. private final char safeMin; // The last character in the safe range. private final char safeMax; /** * Creates a new ArrayBasedCharEscaper instance with the given replacement map and specified safe * range. If {@code safeMax < safeMin} then no characters are considered safe. * * <p>If a character has no mapped replacement then it is checked against the safe range. If it * lies outside that, then {@link #escapeUnsafe} is called, otherwise no escaping is performed. * * @param replacementMap a map of characters to their escaped representations * @param safeMin the lowest character value in the safe range * @param safeMax the highest character value in the safe range */ protected ArrayBasedCharEscaper( Map<Character, String> replacementMap, char safeMin, char safeMax) { this(ArrayBasedEscaperMap.create(replacementMap), safeMin, safeMax); } /** * Creates a new ArrayBasedCharEscaper instance with the given replacement map and specified safe * range. If {@code safeMax < safeMin} then no characters are considered safe. This initializer is * useful when explicit instances of ArrayBasedEscaperMap are used to allow the sharing of large * replacement mappings. * * <p>If a character has no mapped replacement then it is checked against the safe range. If it * lies outside that, then {@link #escapeUnsafe} is called, otherwise no escaping is performed. * * @param escaperMap the mapping of characters to be escaped * @param safeMin the lowest character value in the safe range * @param safeMax the highest character value in the safe range */ protected ArrayBasedCharEscaper(ArrayBasedEscaperMap escaperMap, char safeMin, char safeMax) { checkNotNull(escaperMap); // GWT specific check (do not optimize) this.replacements = escaperMap.getReplacementArray(); this.replacementsLength = replacements.length; if (safeMax < safeMin) { // If the safe range is empty, set the range limits to opposite extremes // to ensure the first test of either value will (almost certainly) fail. safeMax = Character.MIN_VALUE; safeMin = Character.MAX_VALUE; } this.safeMin = safeMin; this.safeMax = safeMax; } /* * This is overridden to improve performance. Rough benchmarking shows that this almost doubles * the speed when processing strings that do not require any escaping. */ @Override public final String escape(String s) { checkNotNull(s); // GWT specific check (do not optimize). { int i = 0; while (i < s.length()) { char c = s.charAt(i); if ((c < replacementsLength && replacements[c] != null) || c > safeMax || c < safeMin) { return escapeSlow(s, i); } i++; } } return s; } /** * Escapes a single character using the replacement array and safe range values. If the given * character does not have an explicit replacement and lies outside the safe range then {@link * #escapeUnsafe} is called. * * @return the replacement characters, or {@code null} if no escaping was required */ @Override protected final char @Nullable [] escape(char c) { if (c < replacementsLength) { char[] chars = replacements[c]; if (chars != null) { return chars; } } if (c >= safeMin && c <= safeMax) { return null; } return escapeUnsafe(c); } /** * Escapes a {@code char} value that has no direct explicit value in the replacement array and * lies outside the stated safe range. Subclasses should override this method to provide * generalized escaping for characters. * * <p>Note that arrays returned by this method must not be modified once they have been returned. * However it is acceptable to return the same array multiple times (even for different input * characters). * * @param c the character to escape * @return the replacement characters, or {@code null} if no escaping was required */ // TODO(dbeaumont,cpovirk): Rename this something better once refactoring done protected abstract char @Nullable [] escapeUnsafe(char c); }

Loading feature diagnosis…

Source regions

Feature contributions

Feature contributions

FeatureValueContribution
(log-odds)
Halstead volumeHow much symbolic information the code contains. It combines the total number of operators and values with the number of distinct ones.2148.942−70.915
Source linesThe number of non-empty source lines.138+55.2
Byte entropyHow varied the bytes in the source text are. Repeated bytes give a lower value; a more even mixture gives a higher value.4.599−6.898

Intercept 8.87 + feature contributions = -13.743 (log-odds).
Score = sigmoid(-13.743) = 0.

Score0.885

Feature contributions

FeatureValueContribution
(log-odds)
Comment layout frequencyHow frequently the layout switches between lines containing only comments or whitespace and lines containing code.139−3.885
Indentation frequencyHow rapidly indentation changes from line to line.68+1.615
Identifier layout (vertical)How rapidly the visual pattern of identifier characters changes from top to bottom.137.911+1.518
Number layout frequencyHow rapidly the number of numeric values per line changes through the code.68−1.14
Comment layout (horizontal)How rapidly the visual pattern of comment characters changes from left to right.97.101+0.762
Operator visual areaThe share of the code’s visual area occupied by operators such as +, =, and *.0.032+0.632
Identifier layout (horizontal)How rapidly the visual pattern of identifier characters changes from left to right. It measures spatial frequency, not the meaning of the names.99.957−0.569

Intercept 0.07 + feature contributions = -0.996 (log-odds).
Score = sigmoid(-0.996) = 0.27.

Score0.468
Score32
Run 1

The code is highly readable. It has clear logical structure with fields, constructors, and methods well-organized, supported by meaningful Javadoc. Nesting is minimal and shallow; the escape method uses a small block that slightly affects flow but remains easy to follow. Intent is explicit through descriptive names, comments, and documentation. The code is concise without sacrificing clarity—each method performs a single focused action. Action granularity is appropriate, generally one logical step per line, and reading flow is natural. The only minor drawback is the extra enclosing block in the escape method, which adds slight visual clutter but is not confusing.

Run 2

The code is highly readable with clear structural organization, comprehensive Javadoc documentation, and logical separation of concerns. The class follows a clean constructor pattern with two overloaded implementations that maintain a flat, non-nested structure. Methods like `escape(String)` and `escape(char)` demonstrate excellent action granularity with one operation per line and minimal nesting depth. The intent is immediately clear through well-named methods and descriptive comments explaining performance optimizations and safe range logic. Code length is appropriate for the complexity of the escaping functionality, avoiding unnecessary verbosity while maintaining clarity. The reading flow progresses naturally from field declarations through constructors to core methods. Minor deductions stem from slightly dense expressions in the escape loop condition and the odd empty block in `escape(String)`, but these are negligible compared to the overall quality.

Run 3

The code is well-structured and logically organized, with clear separation of fields, constructors, and methods. Documentation comments thoroughly explain purpose and edge cases. Nesting is shallow, generally limited to one or two levels. Intent is clearly conveyed through meaningful names like 'replacementsLength', 'safeMin', and 'escapeUnsafe'. Length is appropriate for the complexity, though the constructors have slight parameter duplication. Action granularity is mostly one action per line, though the constructor contains a multi-step if-block. Reading flow is natural and the logic is easy to follow. Minor deductions: the @Nullable syntax can be visually noisy, and the performance comment before 'escape' slightly interrupts flow.

Run 1

Code Structure: Fields, constructors, and escaping methods are logically organized, with clear separation of responsibilities. Nesting: Generally shallow, although escape(String) introduces an unnecessary scope block and escape(char) uses a small nested conditional. Clarity of intent: Descriptive names and thorough documentation explain replacement precedence, safe ranges, and performance choices; the compound scan condition requires some attention. Code length: Implementation is concise, but constructor documentation repeats information and adds bulk. Action granularity: Statements generally perform one action per line, making initialization and control flow easy to follow. Reading flow: Consistent formatting, early returns, and method ordering support natural reading; the unusual nullable-array syntax and redundant scope block slightly interrupt it.

Run 2

Code Structure: Fields, constructors, and escaping methods are logically organized, with clear separation of responsibilities. Nesting: Generally shallow, although escape(String) contains an unnecessary scope block and escape(char) uses a small nested conditional. Clarity of intent: Descriptive names and comprehensive documentation explain replacement lookup, safe ranges, and subclass behavior; the compound escape condition requires modest attention. Code length: Implementation is concise, but repeated constructor documentation and extensive comments add some redundancy. Action granularity: Statements consistently perform one action per line, making state changes easy to follow. Reading flow: Constructor delegation and early returns create a natural progression from initialization to fast-path scanning and character handling. Minor formatting issues, the redundant block, and the unusual nullable-array annotation syntax slightly interrupt an otherwise clear reading experience.

Run 3

Code Structure: Fields, constructors, and escaping methods are logically organized with clear separation of responsibilities. Nesting: Generally shallow, though the extra scope block in escape(String) adds unnecessary indentation. Clarity of intent: Descriptive names and detailed documentation explain replacement precedence, safe ranges, and subclass behavior; the compound escape condition requires some attention. Code length: Implementation is concise, but constructor documentation repeats information and some field comments restate names. Action granularity: Statements consistently perform one action per line, making state changes easy to follow. Reading flow: Constructor delegation and early returns create a natural progression from initialization to fast-path scanning and character-level handling. Minor distractions include unusual nullable-array annotation syntax and a stale-looking naming TODO.