Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: jruby/jruby
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: f9d03ff86c1d
Choose a base ref
...
head repository: jruby/jruby
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 23a66696f659
Choose a head ref
  • 2 commits
  • 1 file changed
  • 1 contributor

Commits on Jan 2, 2016

  1. Copy the full SHA
    f996bd9 View commit details
  2. Copy the full SHA
    23a6669 View commit details
Showing with 56 additions and 23 deletions.
  1. +56 −23 truffle/src/main/java/org/jruby/truffle/format/parser/PackCompiler.java
Original file line number Diff line number Diff line change
@@ -81,50 +81,83 @@ public CallTarget compile(String format) {
* complicated than that.
*/
public static String recoverLoop(String format) {
int break_point = 0;
// The index is the point in the format string where we look backwards for loops from

while (break_point < format.length()) {
if ("0123456789*".indexOf(format.charAt(break_point)) != -1) {
break_point++;
int index = 0;

// Keep going until we reach the end of hte format string

while (index < format.length()) {
// If we aren't at the start of a new directive, step forward one

if ("CSLQcslqInNvVUwDdFfEeFfAaZBbHhuMmpPXx@".indexOf(format.charAt(index)) == -1) {
index++;
continue;
}

int repeated_length = 1;
int max_repeated_length = -1;
// The length of the string that will be tried to be looped - initially trying just one character

int tryLengthOfLoopedString = 1;

// The length of the string that will be looped, where there was actually a loop found

int successfulLengthOfLoopedString = -1;

// Increase the size of the string that will be tried to belooped - but only as far as there is that much
// string both before and after the index

while (tryLengthOfLoopedString <= index && index + tryLengthOfLoopedString <= format.length()) {
// If that length of string exists both before and after the index then that's a successful length
// to use for looping

final String beforeIndex = format.substring(index - tryLengthOfLoopedString, index);
final String afterIndex = format.substring(index, index + tryLengthOfLoopedString);

while (repeated_length <= break_point && break_point + repeated_length <= format.length()) {
if (format.substring(break_point - repeated_length, break_point)
.equals(format.substring(break_point, break_point + repeated_length))) {
max_repeated_length = repeated_length;
if (beforeIndex.equals(afterIndex)) {
successfulLengthOfLoopedString = tryLengthOfLoopedString;
}

repeated_length++;
tryLengthOfLoopedString++;
}

if (max_repeated_length == -1) {
break_point++;
// Were any lengths of looped string we tried successful?

if (successfulLengthOfLoopedString == -1) {
// None were - just move onto the next character and try again

index++;
} else {
final String repeated = format.substring(break_point, break_point + max_repeated_length);
final String repeated = format.substring(index, index + successfulLengthOfLoopedString);

// The number of times to repeat - 2 initially - before and after the index

int repetitionsCount = 2;

int count = 2;
int rep_point = break_point + max_repeated_length;
// Where in the string the 2 repititions end

while (rep_point + max_repeated_length <= format.length()) {
if (!format.substring(rep_point, rep_point + max_repeated_length).equals(repeated)) {
int indexOfEndOfRepititions = index + successfulLengthOfLoopedString;

// Loop to find out how many times the string appears after the 2 initial instances

while (indexOfEndOfRepititions + successfulLengthOfLoopedString <= format.length()) {
if (!format.substring(indexOfEndOfRepititions, indexOfEndOfRepititions + successfulLengthOfLoopedString).equals(repeated)) {
break;
}

count++;
rep_point += max_repeated_length;
repetitionsCount++;
indexOfEndOfRepititions += successfulLengthOfLoopedString;
}

// Replace 'nnn' with 'n3'

final StringBuilder builder = new StringBuilder();
builder.append(format.substring(0, break_point - max_repeated_length));
builder.append(format.substring(0, index - successfulLengthOfLoopedString));
builder.append('(');
builder.append(repeated);
builder.append(')');
builder.append(count);
builder.append(format.substring(rep_point));
builder.append(repetitionsCount);
builder.append(format.substring(indexOfEndOfRepititions));

format = builder.toString();
}