-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update source mappings to exclude filtered files.
- Loading branch information
Han van Venrooij
committed
Dec 26, 2015
1 parent
1ebf123
commit 174a366
Showing
4 changed files
with
178 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* | ||
* Copyright 2015 Han van Venrooij | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.irundaia.base64 | ||
|
||
import scala.annotation.tailrec | ||
|
||
object Base64VLQ { | ||
|
||
private val baseShift = 5 | ||
private val base = 1 << baseShift | ||
private val baseMask = base - 1 | ||
private val continuationBit = base | ||
private val base64Chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" | ||
private val base64CharMap = base64Chars.zipWithIndex.toMap | ||
|
||
def decode(encoded: String): Seq[Int] = { | ||
val digits = encoded.map(base64CharMap) | ||
|
||
digits.foldLeft((Seq[Int](), 0, 0)){ | ||
case ((values, currentValue, shift), nextDigit) => | ||
val resultValue = currentValue + (decodedValue(nextDigit) << shift) | ||
|
||
if (shouldContinueDecode(nextDigit)) { | ||
(values, resultValue, shift + baseShift) | ||
} else { | ||
if (shouldNegateDecodedValue(resultValue)) { | ||
(values ++ Seq(-(resultValue >> 1)), 0, 0) | ||
} else { | ||
(values ++ Seq(resultValue >> 1), 0, 0) | ||
} | ||
} | ||
}._1 | ||
} | ||
|
||
def encode(value: Int): String = { | ||
val toEncodeValue = if (value < 0) | ||
((-value) << 1) | 1 | ||
else | ||
value << 1 | ||
|
||
encodePartial(toEncodeValue) | ||
} | ||
|
||
@tailrec private def encodePartial(value: Int, acc: String = ""): String = { | ||
val digit = value & baseMask | ||
val nextValue = value >>> baseShift | ||
val base64Char = base64Chars(if (nextValue > 0) digit | continuationBit else digit) | ||
|
||
if (nextValue > 0) | ||
encodePartial(nextValue, acc + base64Char) | ||
else | ||
acc + base64Char | ||
} | ||
|
||
private def shouldContinueDecode(digit: Int): Boolean = (digit & continuationBit) > 0 | ||
private def shouldNegateDecodedValue(value: Int): Boolean = (value & 1) > 0 | ||
private def decodedValue(digit: Int): Int = digit & baseMask | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* Copyright 2015 Han van Venrooij | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.irundaia.sourcemap | ||
|
||
import org.irundaia.base64.Base64VLQ | ||
|
||
case class SourceMapping(sourceFileIndex: Int, sourceLine: Int, sourceColumn: Int, targetLine: Int, targetColumn: Int) | ||
|
||
object SourceMapping { | ||
private val lineSeparator = ";" | ||
private val segmentSeparator = "," | ||
|
||
def decode(input: String): Seq[SourceMapping] = { | ||
input.split(lineSeparator).toSeq.zipWithIndex.map(decodeLine).reduce(_ ++ _) | ||
} | ||
|
||
private def doDecodeLine(line: String, targetLine: Int): Seq[SourceMapping] = { | ||
if (line.isEmpty) | ||
Seq() | ||
else { | ||
val segments = line.split(segmentSeparator).toSeq.map(decodeSegment) | ||
segments.foldLeft(Seq[SourceMapping]()) { | ||
case | ||
(mappings, (targetColumnIncrement, sourceFileIncrement, sourceLineIncrement, sourceColumnIncrement)) => | ||
val prevMapping = mappings.headOption.getOrElse(SourceMapping(0, 0, 0, targetLine, 0)) | ||
val nextTargetColumn = prevMapping.targetColumn + targetColumnIncrement | ||
val nextSourceIndex = prevMapping.sourceFileIndex + sourceFileIncrement | ||
val nextSourceLine = prevMapping.sourceLine + sourceLineIncrement | ||
val nextSourceColumn = prevMapping.sourceColumn + sourceColumnIncrement | ||
|
||
SourceMapping(nextSourceIndex, nextSourceLine, nextSourceColumn, targetLine, nextTargetColumn) +: mappings | ||
}.reverse | ||
} | ||
} | ||
private def decodeLine = (doDecodeLine _).tupled | ||
|
||
private def decodeSegment(segment: String): (Int, Int, Int, Int) = { | ||
val foundValues = Base64VLQ.decode(segment) | ||
|
||
(foundValues(0), foundValues(1), foundValues(2), foundValues(3)) | ||
} | ||
|
||
def encode(mappings: Seq[SourceMapping]): String = mappings.groupBy(_.targetLine).mapValues(encodeLine).values.mkString(lineSeparator) | ||
|
||
private def encodeLine(mappings: Seq[SourceMapping]): String = | ||
mappings.foldLeft((Seq[String](), SourceMapping(0, 0, 0, 0, 0))){ | ||
case ((encodedSegments, previousMapping), nextMapping) => (encodeSegment(nextMapping, previousMapping) +: encodedSegments, nextMapping) | ||
}._1.reverse.mkString(segmentSeparator) | ||
|
||
private def encodeSegment(currentMapping: SourceMapping, previousMapping: SourceMapping): String = { | ||
val toBeEncodedValues = Seq( | ||
currentMapping.targetColumn - previousMapping.targetColumn, // Target column increment | ||
currentMapping.sourceFileIndex - previousMapping.sourceFileIndex, // Source file increment | ||
currentMapping.sourceLine - previousMapping.sourceLine, // Source line increment | ||
currentMapping.sourceColumn - previousMapping.sourceColumn // Source column increment | ||
) | ||
|
||
toBeEncodedValues.map(Base64VLQ.encode).mkString("") | ||
} | ||
} |