fastring


License

License

GroupId

GroupId

com.dongxiguo
ArtifactId

ArtifactId

fastring_sjs0.6_2.11
Last Version

Last Version

1.0.0+197-156d0aae
Release Date

Release Date

Type

Type

jar
Description

Description

fastring
fastring
Project URL

Project URL

https://github.com/Atry/fastring
Project Organization

Project Organization

com.dongxiguo
Source Code Management

Source Code Management

https://github.com/Atry/fastring

Download fastring_sjs0.6_2.11

How to add to project

<!-- https://jarcasting.com/artifacts/com.dongxiguo/fastring_sjs0.6_2.11/ -->
<dependency>
    <groupId>com.dongxiguo</groupId>
    <artifactId>fastring_sjs0.6_2.11</artifactId>
    <version>1.0.0+197-156d0aae</version>
</dependency>
// https://jarcasting.com/artifacts/com.dongxiguo/fastring_sjs0.6_2.11/
implementation 'com.dongxiguo:fastring_sjs0.6_2.11:1.0.0+197-156d0aae'
// https://jarcasting.com/artifacts/com.dongxiguo/fastring_sjs0.6_2.11/
implementation ("com.dongxiguo:fastring_sjs0.6_2.11:1.0.0+197-156d0aae")
'com.dongxiguo:fastring_sjs0.6_2.11:jar:1.0.0+197-156d0aae'
<dependency org="com.dongxiguo" name="fastring_sjs0.6_2.11" rev="1.0.0+197-156d0aae">
  <artifact name="fastring_sjs0.6_2.11" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.dongxiguo', module='fastring_sjs0.6_2.11', version='1.0.0+197-156d0aae')
)
libraryDependencies += "com.dongxiguo" % "fastring_sjs0.6_2.11" % "1.0.0+197-156d0aae"
[com.dongxiguo/fastring_sjs0.6_2.11 "1.0.0+197-156d0aae"]

Dependencies

compile (3)

Group / Artifact Type Version
org.scala-lang : scala-library jar 2.11.12
org.scala-js : scalajs-library_2.11 jar 0.6.33
org.scala-lang : scala-reflect jar 2.11.12

test (2)

Group / Artifact Type Version
org.scala-js : scalajs-test-bridge_2.11 jar 0.6.33
com.novocode : junit-interface jar 0.11

Project Modules

There are no modules declared in this project.

Fastring

Join the chat at https://gitter.im/Atry/fastring Build Status Codacy Badge Latest version Scaladoc

Fastring is a string formatting library for Scala. Fastring is also designed to be a template engine, and it is an excellent replacement of JSP, Scalate or FreeMarker.

It's simple to use

Fastring uses string interpolation syntax. For example, if you are writing a CGI page:

import com.dongxiguo.fastring.Fastring.Implicits._
def printHtml(link: java.net.URL) {
  val fastHtml = fast"<html><body><a href='$link'>Click Me!</a></body></html>"
  print(fastHtml)
}

It's extremely fast

I made a benchmark. I used 4 different ways to create a 545-characters string.

  1. Fastring (fast"Concat with $something" syntax);
  2. String concatenation (s"Concat with $something" syntax);
  3. Handwritten StringBuilder (stringBuilder ++= "Build from " ++= something syntax);
  4. java.util.Formatter (f"Format with $something" syntax).

This is the result from my Intel i5-3450 computer:

Fastring
import com.dongxiguo.fastring.Fastring.Implicits._
def fast(a: Int) =
  fast"head ${
    (for (j <- 0 until 10 view) yield {
      fast"baz$j $a foo ${
        (for (i <- 0 until 4 view) yield {
          fast"$a i=$i"
        }).mkFastring(",")
      } bar\n"
    }).mkFastring("<hr/>")
  } tail"

fast(0).toString

Took 669 nanoseconds to generate a 545-characters string.
(Simple and fast)
String concatenation
def s(a: Int) =
  s"head ${
    (for (j <- 0 until 10 view) yield {
      s"baz$j $a foo ${
        (for (i <- 0 until 4 view) yield {
          s"$a i=$i"
        }).mkString(",")
      } bar\n"
    }).mkString("<hr/>")
  } tail"

s(0)

Took 1738 nanoseconds to generate a 545-characters string.
(Simple but slow)
Handwritten StringBuilder
def sb(sb: StringBuilder, a: Int) {
  sb ++= "head "
  var first = true
  for (j <- 0 until 10 view) {
    if (first) {
      first = false
    } else {
      sb ++= ""<hr/>""
    }
    sb ++=
      "baz" ++= j.toString ++=
      " " ++= a.toString ++= " foo ";
    {
      var first = true
      for (i <- 0 until 4 view) {
        if (first) {
          first = false
        } else {
          sb ++= ","
        }
        sb ++= a.toString
        sb ++= " i="
        sb ++= i.toString
      }
    }
    sb ++= " bar\n"
  }
  sb ++= " tail"
  sb
}

val s = new StringBuilder sb(s, 0) s.toString

Took 537 nanoseconds to generate a 545-characters string.
(Fast but too trivial)
java.util.Formatter
def f(a: Int) =
    f"head ${
      (for (j <- 0 until 10 view) yield {
        f"baz$j $a foo ${
          (for (i <- 0 until 4 view) yield {
            f"$a i=$i"
          }).mkString(",")
        } bar\n"
      }).mkString("<hr/>")
    } tail"

f(0)

Took 7436 nanoseconds to generate a 545-characters string.
(Simple but extremely slow)

Fastring is so fast because it is lazily evaluated. It avoids coping content for nested String Interpolation. Thus, Fastring is very suitable to generate complex text content(e.g. HTML, JSON).

For example, in the previous benchmark for Fastring, the most of time was spent on invoking toString. You can avoid these overhead if you do not need a whole string. For example:

// Faster than: print(fast"My lazy string from $something")
fast"My lazy string from $something".foreach(print)

You can invoke foreach because Fastring is just a Traversable[String].

Utilities

There is a mkFastring method for Seq:

// Enable mkFastring method
import com.dongxiguo.fastring.Fastring.Implicits._

// Got Fastring("Seq.mkFastring: Hello, world")
fast"Seq.mkFastring: ${Seq("Hello", "world").mkFastring(", ")}"

// Also works, but slower:
// Got Fastring("Seq.mkString: Hello, world")
fast"Seq.mkString: ${Seq("Hello", "world").mkString(", ")}"

And a leftPad method for Byte, Short, Int and Long:

// Enable leftPad method
import com.dongxiguo.fastring.Fastring.Implicits._

// Got Fastring("Int.leftPad:   123")
fast"Int.leftPad: ${123.leftPad(5)}"

// Got Fastring("Int.leftPad: 00123")
fast"Int.leftPad: ${123.leftPad(5, '0')}"

Installation

Put these lines in your build.sbt if you use Sbt:

libraryDependencies += "com.dongxiguo" %% "fastring" % "latest.release"

See http://mvnrepository.com/artifact/com.dongxiguo/fastring_2.12 if you use Maven or other build systems.

Note that Fastring requires Scala 2.10, 2.11 or 2.12.

Versions

Version
1.0.0+197-156d0aae
1.0.0+197-dd44d416
1.0.0+196-f4d4097d
1.0.0+195-156085bc
1.0.0+194-b7cee8af
1.0.0+193-3d11a29d
1.0.0+193-bbfda858
1.0.0+192-19901a18
1.0.0+191-290222ee
1.0.0+191-d59e6ff1
1.0.0+191-c5203393
1.0.0+190-cb295e7d
1.0.0+189-43aa7b2c
1.0.0+189-e2224b11
1.0.0+188-315e8955
1.0.0+188-9fa030dc
1.0.0+187-75c0ba94
1.0.0+186-6b048707
1.0.0+186-acee7752
1.0.0+185-2134ea53
1.0.0+185-90a1fe3d
1.0.0+185-9de767f7
1.0.0+184-10ae4ce5
1.0.0+184-4f7bde73
1.0.0+184-2f8c49b7
1.0.0+184-f4dc6f9d
1.0.0+184-e5d6d507
1.0.0+184-a26ba2b5
1.0.0+183-077b533e
1.0.0+181-ef5d0a4d
1.0.0+158-d99c3be7
1.0.0+150-5a30c1c0
1.0.0+149-e0a34b4c
1.0.0+148-36844679
1.0.0+148-13b80a83
1.0.0+148-c767be02
1.0.0+148-c77cb7af
1.0.0+148-b6502835
1.0.0+148-a6f0b0a7
1.0.0+146-2ade4102
1.0.0+146-f43ecfdc
1.0.0+146-be03b080
1.0.0+145-cd4d3027
1.0.0+145-c652332e
1.0.0+145-c70152d5
1.0.0+144-e2284c16
1.0.0
0.3.1
0.3.0