scala.collection.mutable.StringBuilder Scala Examples

The following examples show how to use scala.collection.mutable.StringBuilder. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.
Example 1
Source File: Tag.scala    From sctags   with Apache License 2.0 5 votes vote down vote up
package sctags

import scala.collection.mutable.StringBuilder

case class Tag(val name: String, pos: TagPosition, fields: Product2[String, String]*) {
  private def fieldString(field: Product2[String, String]) =
    if (field._1 == "kind") escapeValue(field._2)
      else field._1 + ":" + escapeValue(field._2)

  private def escapeValue(value: String) =
    (value.foldLeft(new StringBuilder) {(b, c) =>
      c match {
        case '\t' => b.append("\\t")
        case '\r' => b.append("\\r")
        case '\n' => b.append("\\n")
        case '\\' => b.append("\\\\")
        case x    => b.append(x)
      }
    }).toString

  def fieldsString: String =
    if (fields.isEmpty) {
      ""
    } else {
      (fields ++ Seq("line"->pos.line.toString)).
        map(fieldString).mkString(";\"\t", "\t", "")
    }
  override def toString =
    name + "\t" + "\t" + pos.content + fieldsString
} 
Example 2
Source File: LibSVMRequestRowSerializerUtils.scala    From sagemaker-spark   with Apache License 2.0 5 votes vote down vote up
package com.amazonaws.services.sagemaker.sparksdk.transformation.serializers

import scala.collection.mutable.StringBuilder

import org.apache.spark.ml.linalg.Vector

private[serializers]object LibSVMRequestRowSerializerUtils {

   def serializeLabeledFeatureVector(label : Double, features : Vector): Array[Byte] = {
    val sb = new StringBuilder(label.toString)
    features.foreachActive {
      case (index, value) =>
        sb ++= s" ${index + 1}:$value"
    }
    sb ++= "\n"
    sb.toString().getBytes
  }
}