当前位置: 首页>>代码示例>>Scala>>正文


Scala StringBuilder类代码示例

本文整理汇总了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
  }
} 
开发者ID:Technius,项目名称:brainfp,代码行数:31,代码来源:Interpreter.scala

示例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))
} 
开发者ID:yubrot,项目名称:scalisp,代码行数:58,代码来源:Printer.scala

示例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
  }
} 
开发者ID:tirpitz-verus,项目名称:enigma-in-scala,代码行数:28,代码来源:Message.scala


注:本文中的scala.collection.mutable.StringBuilder类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。