| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.google.debugging.sourcemap; |
| 18 |
|
| 19 |
import com.google.common.collect.Lists; |
| 20 |
|
| 21 |
import java.util.List; |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
class SourceMapLineDecoder { |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
static List<Integer> decodeLine(String lineSource) { |
| 35 |
return decodeLine(new StringParser(lineSource)); |
| 36 |
} |
| 37 |
|
| 38 |
private SourceMapLineDecoder() {} |
| 39 |
|
| 40 |
static LineEntry decodeLineEntry(String in, int lastId) { |
| 41 |
return decodeLineEntry(new StringParser(in), lastId); |
| 42 |
} |
| 43 |
|
| 44 |
private static LineEntry decodeLineEntry(StringParser reader, int lastId) { |
| 45 |
int repDigits = 0; |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
for (char peek = reader.peek(); peek == '!'; peek = reader.peek()) { |
| 50 |
repDigits++; |
| 51 |
reader.next(); |
| 52 |
} |
| 53 |
|
| 54 |
int idDigits = 0; |
| 55 |
int reps = 0; |
| 56 |
if (repDigits == 0) { |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
char digit = reader.next(); |
| 61 |
int value = addBase64Digit(digit, 0); |
| 62 |
reps = (value >> 2); |
| 63 |
idDigits = (value & 3); |
| 64 |
} else { |
| 65 |
char digit = reader.next(); |
| 66 |
idDigits = addBase64Digit(digit, 0); |
| 67 |
|
| 68 |
int value = 0; |
| 69 |
for (int i = 0; i < repDigits; i++) { |
| 70 |
digit = reader.next(); |
| 71 |
value = addBase64Digit(digit, value); |
| 72 |
} |
| 73 |
reps = value; |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
reps += 1; |
| 78 |
idDigits += 1; |
| 79 |
|
| 80 |
|
| 81 |
int value = 0; |
| 82 |
for (int i = 0; i < idDigits; i++) { |
| 83 |
char digit = reader.next(); |
| 84 |
value = addBase64Digit(digit, value); |
| 85 |
} |
| 86 |
int mappingId = getIdFromRelativeId(value, idDigits, lastId); |
| 87 |
return new LineEntry(mappingId, reps); |
| 88 |
} |
| 89 |
|
| 90 |
private static List<Integer> decodeLine(StringParser reader) { |
| 91 |
List<Integer> result = Lists.newArrayListWithCapacity(512); |
| 92 |
int lastId = 0; |
| 93 |
while (reader.hasNext()) { |
| 94 |
LineEntry entry = decodeLineEntry(reader, lastId); |
| 95 |
lastId = entry.id; |
| 96 |
|
| 97 |
for (int i=0; i < entry.reps; i++) { |
| 98 |
result.add(entry.id); |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
return result; |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
private static int addBase64Digit(char digit, int previousValue) { |
| 109 |
return (previousValue * 64) + Base64.fromBase64(digit); |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
static int getIdFromRelativeId(int rawId, int digits, int lastId) { |
| 116 |
|
| 117 |
int base = 1 << (digits * 6); |
| 118 |
return ((rawId >= base/2) ? rawId - base : rawId) + lastId; |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
static class LineEntry { |
| 125 |
final int id; |
| 126 |
final int reps; |
| 127 |
public LineEntry(int id, int reps) { |
| 128 |
this.id = id; |
| 129 |
this.reps = reps; |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
static class StringParser { |
| 138 |
final String content; |
| 139 |
int current = 0; |
| 140 |
|
| 141 |
StringParser(String content) { |
| 142 |
this.content = content; |
| 143 |
} |
| 144 |
|
| 145 |
char next() { |
| 146 |
return content.charAt(current++); |
| 147 |
} |
| 148 |
|
| 149 |
char peek() { |
| 150 |
return content.charAt(current); |
| 151 |
} |
| 152 |
|
| 153 |
boolean hasNext() { |
| 154 |
return current < content.length() -1; |
| 155 |
} |
| 156 |
} |
| 157 |
} |