• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Scala StringBuilder类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了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;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Scala ZipOutputStream类代码示例发布时间:2022-05-23
下一篇:
Scala LogisticRegressionWithLBFGS类代码示例发布时间:2022-05-23
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap