{"id":16696661,"url":"https://github.com/atry/fastring","last_synced_at":"2025-04-04T12:07:00.424Z","repository":{"id":5640025,"uuid":"6848464","full_name":"Atry/fastring","owner":"Atry","description":"Extremely fast string formatting","archived":false,"fork":false,"pushed_at":"2024-10-13T01:18:36.000Z","size":316,"stargazers_count":127,"open_issues_count":7,"forks_count":10,"subscribers_count":8,"default_branch":"master","last_synced_at":"2024-10-13T17:45:23.031Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Scala","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Atry.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2012-11-25T07:09:25.000Z","updated_at":"2024-08-26T10:58:25.000Z","dependencies_parsed_at":"2024-10-25T18:34:08.732Z","dependency_job_id":"66db8770-a576-4841-a3e6-b270e95181b0","html_url":"https://github.com/Atry/fastring","commit_stats":{"total_commits":326,"total_committers":11,"mean_commits":"29.636363636363637","dds":"0.42944785276073616","last_synced_commit":"c69fab04ebde497f0e33e0856c251c9a6675f295"},"previous_names":[],"tags_count":10,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Atry%2Ffastring","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Atry%2Ffastring/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Atry%2Ffastring/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Atry%2Ffastring/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Atry","download_url":"https://codeload.github.com/Atry/fastring/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247174415,"owners_count":20896078,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-12T17:44:29.879Z","updated_at":"2025-04-04T12:07:00.407Z","avatar_url":"https://github.com/Atry.png","language":"Scala","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Fastring\n\n[![Join the chat at https://gitter.im/Atry/fastring](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/Atry/fastring?utm_source=badge\u0026utm_medium=badge\u0026utm_campaign=pr-badge\u0026utm_content=badge)\n[![Scala CI](https://github.com/Atry/fastring/actions/workflows/scala.yml/badge.svg)](https://github.com/Atry/fastring/actions/workflows/scala.yml)\n[![Latest version](https://index.scala-lang.org/atry/fastring/fastring/latest.svg)](https://index.scala-lang.org/atry/fastring/fastring)\n[![Scaladoc](https://javadoc.io/badge/com.dongxiguo/fastring_2.12.svg?label=scaladoc)](https://javadoc.io/page/com.dongxiguo/fastring_2.12/latest/com/dongxiguo/fastring/index.html)\n\n\n**Fastring** is a string formatting library for [Scala](http://www.scala-lang.org/).\n`Fastring` is also designed to be a template engine,\nand it is an excellent replacement of JSP, [Scalate](https://scalate.github.io/scalate/) or [FreeMarker](http://freemarker.sourceforge.net/).\n\n## It's simple to use\n\n`Fastring` uses [string interpolation](http://docs.scala-lang.org/sips/pending/string-interpolation.html) syntax.\nFor example, if you are writing a CGI page:\n\n    import com.dongxiguo.fastring.Fastring.Implicits._\n    def printHtml(link: java.net.URL) {\n      val fastHtml = fast\"\u003chtml\u003e\u003cbody\u003e\u003ca href='$link'\u003eClick Me!\u003c/a\u003e\u003c/body\u003e\u003c/html\u003e\"\n      print(fastHtml)\n    }\n\n## It's extremely fast\n\nI made a [benchmark](https://github.com/Atry/fastring/blob/master/benchmark/src/main/scala/com/dongxiguo/fastring/benchmark/FastringBenchmark.scala).\nI used 4 different ways to create a 545-characters string.\n\n1. Fastring (`fast\"Concat with $something\"` syntax);\n2. String concatenation (`s\"Concat with $something\"` syntax);\n3. Handwritten `StringBuilder` (`stringBuilder ++= \"Build from \" ++= something` syntax);\n4. `java.util.Formatter` (`f\"Format with $something\"` syntax).\n\nThis is the result from my Intel i5-3450 computer:\n\n\u003ctable\u003e\n\u003ctr\u003e\n\u003cth\u003e\nFastring\n\u003c/th\u003e\n\u003ctd\u003e\n\u003cpre\u003e\u003ccode\u003eimport com.dongxiguo.fastring.Fastring.Implicits._\ndef fast(a: Int) =\n  fast\"head ${\n    (for (j \u0026lt;- 0 until 10 view) yield {\n      fast\"baz$j $a foo ${\n        (for (i \u0026lt;- 0 until 4 view) yield {\n          fast\"$a i=$i\"\n        }).mkFastring(\",\")\n      } bar\\n\"\n    }).mkFastring(\"\u0026lt;hr/\u0026gt;\")\n  } tail\"\n\nfast(0).toString\u003c/code\u003e\u003c/pre\u003e\n\u003c/td\u003e\n\u003ctd\u003e\nTook 669 nanoseconds to generate a 545-characters string.\u003cbr/\u003e(Simple and fast)\n\u003c/td\u003e\n\u003c/tr\u003e\n\u003ctr\u003e\n\u003cth\u003e\nString concatenation\n\u003c/th\u003e\n\u003ctd\u003e\n\u003cpre\u003e\u003ccode\u003edef s(a: Int) =\n  s\"head ${\n    (for (j \u0026lt;- 0 until 10 view) yield {\n      s\"baz$j $a foo ${\n        (for (i \u0026lt;- 0 until 4 view) yield {\n          s\"$a i=$i\"\n        }).mkString(\",\")\n      } bar\\n\"\n    }).mkString(\"\u0026lt;hr/\u0026gt;\")\n  } tail\"\n\ns(0)\u003c/code\u003e\u003c/pre\u003e\n\u003c/td\u003e\n\u003ctd\u003e\nTook 1738 nanoseconds to generate a 545-characters string.\u003cbr/\u003e(Simple but slow)\n\u003c/td\u003e\n\u003c/tr\u003e\n\u003ctr\u003e\n\u003cth\u003e\nHandwritten \u003ccode\u003eStringBuilder\u003c/code\u003e\n\u003c/th\u003e\n\u003ctd\u003e\n\u003cpre\u003e\u003ccode\u003edef sb(sb: StringBuilder, a: Int) {\n  sb ++= \"head \"\n  var first = true\n  for (j \u0026lt;- 0 until 10 view) {\n    if (first) {\n      first = false\n    } else {\n      sb ++= \"\"\u0026lt;hr/\u0026gt;\"\"\n    }\n    sb ++=\n      \"baz\" ++= j.toString ++=\n      \" \" ++= a.toString ++= \" foo \";\n    {\n      var first = true\n      for (i \u0026lt;- 0 until 4 view) {\n        if (first) {\n          first = false\n        } else {\n          sb ++= \",\"\n        }\n        sb ++= a.toString\n        sb ++= \" i=\"\n        sb ++= i.toString\n      }\n    }\n    sb ++= \" bar\\n\"\n  }\n  sb ++= \" tail\"\n  sb\n}\n\nval s = new StringBuilder\nsb(s, 0)\ns.toString\u003c/code\u003e\u003c/pre\u003e\n\u003c/td\u003e\n\u003ctd\u003e\nTook 537 nanoseconds to generate a 545-characters string.\u003cbr/\u003e(Fast but too trivial)\n\u003c/td\u003e\n\u003c/tr\u003e\n\u003ctr\u003e\n\u003cth\u003e\n\u003ccode\u003ejava.util.Formatter\u003c/code\u003e\n\u003c/th\u003e\n\u003ctd\u003e\n\u003cpre\u003e\u003ccode\u003edef f(a: Int) =\n    f\"head ${\n      (for (j \u0026lt;- 0 until 10 view) yield {\n        f\"baz$j $a foo ${\n          (for (i \u0026lt;- 0 until 4 view) yield {\n            f\"$a i=$i\"\n          }).mkString(\",\")\n        } bar\\n\"\n      }).mkString(\"\u0026lt;hr/\u0026gt;\")\n    } tail\"\n\nf(0)\u003c/code\u003e\u003c/pre\u003e\n\u003c/td\u003e\n\u003ctd\u003e\nTook 7436 nanoseconds to generate a 545-characters string.\u003cbr/\u003e(Simple but extremely slow)\n\u003c/td\u003e\n\u003c/tr\u003e\n\u003c/table\u003e\n\n`Fastring` is so fast because it is **lazily** evaluated.\nIt avoids coping content for nested String Interpolation.\nThus, `Fastring` is very suitable to generate complex text content(e.g. HTML, JSON).\n\nFor example, in the previous benchmark for `Fastring`, the most of time was spent on invoking `toString`.\nYou can avoid these overhead if you do not need a whole string. For example:\n\n    // Faster than: print(fast\"My lazy string from $something\")\n    fast\"My lazy string from $something\".foreach(print)\n\nYou can invoke `foreach` because `Fastring` is just a `Traversable[String]`.\n\n## Utilities\n\nThere is a `mkFastring` method for `Seq`:\n\n    // Enable mkFastring method\n    import com.dongxiguo.fastring.Fastring.Implicits._\n    \n    // Got Fastring(\"Seq.mkFastring: Hello, world\")\n    fast\"Seq.mkFastring: ${Seq(\"Hello\", \"world\").mkFastring(\", \")}\"\n    \n    // Also works, but slower:\n    // Got Fastring(\"Seq.mkString: Hello, world\")\n    fast\"Seq.mkString: ${Seq(\"Hello\", \"world\").mkString(\", \")}\"\n\nAnd a `leftPad` method for `Byte`, `Short`, `Int` and `Long`:\n\n    // Enable leftPad method\n    import com.dongxiguo.fastring.Fastring.Implicits._\n    \n    // Got Fastring(\"Int.leftPad:   123\")\n    fast\"Int.leftPad: ${123.leftPad(5)}\"\n    \n    // Got Fastring(\"Int.leftPad: 00123\")\n    fast\"Int.leftPad: ${123.leftPad(5, '0')}\"\n\n## Installation\n\nPut these lines in your `build.sbt` if you use [Sbt](http://www.scala-sbt.org/):\n\n    libraryDependencies += \"com.dongxiguo\" %% \"fastring\" % \"latest.release\"\n\nSee http://mvnrepository.com/artifact/com.dongxiguo/fastring_2.12 if you use [Maven](http://maven.apache.org/)\nor other build systems.\n\nNote that `Fastring` requires [Scala](http://www.scala-lang.org/) `2.10`, `2.11` or `2.12`.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fatry%2Ffastring","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fatry%2Ffastring","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fatry%2Ffastring/lists"}