本文整理汇总了Scala中scala.collection.mutable.StringBuilder类的典型用法代码示例。如果您正苦于以下问题:Scala StringBuilder类的具体用法?Scala StringBuilder怎么用?Scala StringBuilder使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了StringBuilder类的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: Interpreter
//设置package包名称以及导入依赖的类
package co.technius.brainfp
import scala.collection.mutable.StringBuilder
import Command._
object Interpreter {
def eval(commands: Seq[Command]): String = {
val stack = Array.fill[Byte](100000)(0)
val output = new StringBuilder()
var ptr = 0
def doEval(next: Seq[Command]): Unit = {
next foreach {
case RightShiftPointer => ptr = ptr + 1
case LeftShiftPointer => ptr = ptr - 1
case IncrementCell => stack(ptr) = (stack(ptr) + 1).toByte
case DecrementCell => stack(ptr) = (stack(ptr) - 1).toByte
case OutputCharacter => output.append(stack(ptr).toChar)
case InputCharacter =>
val input = io.StdIn.readChar().toByte
stack(ptr) = input
case Loop(commands) =>
while (stack(ptr) > 0) doEval(commands)
}
}
doEval(commands)
output.toString
}
}
示例2: CodePrinter
//设置package包名称以及导入依赖的类
package scalisp
import scala.collection.mutable.{ListBuffer, StringBuilder}
class CodePrinter {
private var id = 0
private var blocks = new ListBuffer[StringBuilder]
def print(): String = {
val b = new StringBuilder
for (block <- blocks) b ++= block
return b.result
}
def putBlock(header: String, code: Code): String = {
val label = s"[$id $header]"
val block = new StringBuilder
id += 1
blocks += block
block ++= label ++= "\n"
for (inst <- code) block ++= " " ++= putInst(inst) ++= "\n"
return label
}
def putInst(inst: Inst): String = inst match {
case Ldc(constant) => "ldc " + constant.inspect
case Ldv(variable) => s"ldv $variable"
case Ldf(pattern, code) =>
val label = putBlock(s"fun $pattern", code)
s"ldf $label"
case Ldm(pattern, code) =>
val label = putBlock(s"macro $pattern", code)
s"ldm $label"
case Ldb(name) => s"ldb $name"
case Sel(a, b) =>
val al = putBlock("then", a)
val bl = putBlock("else", b)
s"sel $al $bl"
case App(argc) => s"app $argc"
case Leave => "leave"
case Pop => "pop"
case Def(variable) => s"def $variable"
case Set(variable) => s"set $variable"
}
}
object CodePrinter {
def print(code: Code): String = {
val printer = new CodePrinter
printer.putBlock("entry", code)
printer.print()
}
}
object Code {
def unapply(code: Code): Option[String] = Some(CodePrinter.print(code))
}
示例3: fromEnigmaFormat
//设置package包名称以及导入依赖的类
package mlesiewski.enigmainscala.core
import scala.collection.mutable
import scala.collection.mutable.StringBuilder
def fromEnigmaFormat (setting: (Char, Char, Char), inEnigmaFormat: String): Message =
new Message (setting, toPrettyText (inEnigmaFormat), inEnigmaFormat)
private[core] def toPrettyText (inEnigmaFormat: String): String = {
implicit val allowed = Enigma.acceptedLetters
val result = valid (inEnigmaFormat.toUpperCase)
s"Y[$lettersEncodingNumbers]+".r.replaceAllIn (result, m => letter2number (m.matched))
}
// 48 is 0 in ASCII
private def letter2number (letters: String): String = {
letters.substring (1).foldLeft (new mutable.StringBuilder ()) { (acc: mutable.StringBuilder, c: Char) =>
acc += (lettersEncodingNumbers.indexOf (c) + 48).toChar
}.toString
}
private def valid (input: String)(implicit allowed: String): String = {
input.map (c => if (!allowed.contains (c)) throw new IllegalArgumentException (s"illegal character found '$c'") else c).toString
}
}