-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit f1430db
Showing
8 changed files
with
200 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
.idea | ||
target |
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,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2018 Chang Liu | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
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,40 @@ | ||
# varint | ||
|
||
[![standard-readme compliant](https://img.shields.io/badge/standard--readme-OK-green.svg?style=flat-square)](https://github.com/RichardLitt/standard-readme) | ||
|
||
> Scala Implementation of [Varint](https://github.com/multiformats/unsigned-varint). | ||
## Table of Contents | ||
|
||
- [Install](#install) | ||
- [Usage](#usage) | ||
- [API](#api) | ||
- [Maintainers](#maintainers) | ||
- [Contribute](#contribute) | ||
- [License](#license) | ||
|
||
## Install | ||
|
||
``` | ||
``` | ||
|
||
## Usage | ||
|
||
``` | ||
``` | ||
|
||
## API | ||
|
||
## Maintainers | ||
|
||
[@fluency03](https://github.com/fluency03) | ||
|
||
## Contribute | ||
|
||
PRs accepted. | ||
|
||
Small note: If editing the README, please conform to the [standard-readme](https://github.com/RichardLitt/standard-readme) specification. | ||
|
||
## License | ||
|
||
MIT © 2018 Chang Liu |
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,54 @@ | ||
name := "varint" | ||
|
||
version := "0.0.1" | ||
|
||
organization := "com.github.fluency03" | ||
organizationName := "fluency03" | ||
organizationHomepage := Some(url("https://github.com/fluency03")) | ||
|
||
scalaVersion := "2.12.7" | ||
|
||
val scalaTestVersion = "3.0.5" | ||
|
||
val testDependencies = Seq( | ||
"org.scalactic" %% "scalactic" % scalaTestVersion, | ||
"org.scalatest" %% "scalatest" % scalaTestVersion % Test | ||
) | ||
|
||
libraryDependencies ++= testDependencies | ||
|
||
sonatypeProfileName := "com.github.fluency03" | ||
|
||
publishMavenStyle := true | ||
|
||
publishTo := { | ||
val nexus = "https://oss.sonatype.org/" | ||
if (isSnapshot.value) | ||
Some("snapshots" at nexus + "content/repositories/snapshots") | ||
else | ||
Some("releases" at nexus + "service/local/staging/deploy/maven2") | ||
} | ||
|
||
scmInfo := Some( | ||
ScmInfo( | ||
url("https://github.com/fluency03/varint"), | ||
"scm:git@github.com:fluency03/varint.git" | ||
) | ||
) | ||
|
||
developers := List( | ||
Developer( | ||
id = "fluency03", | ||
name = "Chang Liu", | ||
email = "fluency.03@gmail.com", | ||
url = url("https://github.com/fluency03") | ||
) | ||
) | ||
|
||
description := "Scala Implementation of Varint." | ||
|
||
licenses := Seq("MIT" -> url("https://raw.githubusercontent.com/fluency03/varint/master/LICENSE")) | ||
|
||
homepage := Some(url("https://github.com/fluency03/varint")) | ||
|
||
pomIncludeRepository := { _ => false } |
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 @@ | ||
sbt.version = 1.2.6 |
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,2 @@ | ||
addSbtPlugin("com.jsuereth" % "sbt-pgp" % "2.0.0-M2") | ||
addSbtPlugin("org.xerial.sbt" % "sbt-sonatype" % "2.0") |
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,51 @@ | ||
package com.github.fluency03.varint | ||
|
||
import scala.annotation.tailrec | ||
|
||
object Varint { | ||
|
||
val MSB = 0x80 | ||
val LOW_7_BITS = 0x7F | ||
val INT_REST_BITES = 0xFFFFFF80 | ||
val LONG_REST_BITES = 0xFFFFFFFFFFFFFF80L | ||
|
||
// TODO (fluency03): set max size | ||
|
||
def encodeInt(num: Int): Array[Byte] = { | ||
@tailrec | ||
def rec(value: Int, acc: Array[Byte]): Array[Byte] = | ||
if ((value & INT_REST_BITES) == 0) acc :+ (value & LOW_7_BITS).toByte | ||
else rec(value >>> 7, acc :+ ((value & LOW_7_BITS) | MSB).toByte) | ||
|
||
rec(num, Array()) | ||
} | ||
|
||
def encodeLong(num: Long): Array[Byte] = { | ||
@tailrec | ||
def rec(value: Long, acc: Array[Byte]): Array[Byte] = | ||
if ((value & LONG_REST_BITES) == 0) acc :+ (value & LOW_7_BITS).toByte | ||
else rec(value >>> 7, acc :+ ((value & LOW_7_BITS) | MSB).toByte) | ||
|
||
rec(num, Array()) | ||
} | ||
|
||
|
||
def decodeToInt(bytes: Array[Byte]): Int = { | ||
@tailrec | ||
def rec(index: Int, shift: Int, acc: Int): Int = | ||
if (index >= bytes.length || (bytes(index) & MSB) == 0) acc | (bytes(index) << shift) | ||
else rec(index + 1, shift + 7, acc | ((bytes(index) & LOW_7_BITS) << shift)) | ||
|
||
rec(0, 0, 0) | ||
} | ||
|
||
def decodeToLong(bytes: Array[Byte]): Long = { | ||
@tailrec | ||
def rec(index: Int, shift: Long, acc: Long): Long = | ||
if (index >= bytes.length || (bytes(index) & MSB) == 0) acc | (bytes(index).toLong << shift) | ||
else rec(index + 1, shift + 7, acc | ((bytes(index).toLong & LOW_7_BITS) << shift)) | ||
|
||
rec(0, 0L, 0L) | ||
} | ||
|
||
} |
29 changes: 29 additions & 0 deletions
29
src/test/scala/com/github/fluency03/varint/VarintTest.scala
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,29 @@ | ||
package com.github.fluency03.varint | ||
|
||
import org.scalatest.{FlatSpec, Matchers} | ||
|
||
class VarintTest extends FlatSpec with Matchers { | ||
|
||
val rand = new scala.util.Random | ||
|
||
it should "encode an Int to Varint Bytes and decode it back." in { | ||
for (i <- 1 to 100) { | ||
val num = rand.nextInt(Int.MaxValue) | ||
Varint.decodeToInt(Varint.encodeInt(num)) shouldBe num | ||
} | ||
|
||
Varint.encodeInt(300) shouldBe Array(0xac.toByte, 0x02.toByte) | ||
} | ||
|
||
it should "encode an Long to Varint Bytes and decode it back." in { | ||
for (i <- 1 to 200) { | ||
val num = rand.nextLong | ||
if (num > 0) Varint.decodeToLong(Varint.encodeLong(num)) shouldBe num | ||
} | ||
|
||
Varint.decodeToLong(Varint.encodeLong(278734309057836877L)) shouldBe 278734309057836877L | ||
Varint.decodeToLong(Varint.encodeLong(100L)) shouldBe 100L | ||
Varint.decodeToLong(Varint.encodeLong(10000000000L)) shouldBe 10000000000L | ||
} | ||
|
||
} |