PageRenderTime 44ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/src/orc/values/Format.scala

https://github.com/laurenyew/cOrcS
Scala | 58 lines | 24 code | 7 blank | 27 comment | 2 complexity | e2b01d4c43c3a7321a0fda887491fbef MD5 | raw file
Possible License(s): BSD-3-Clause
  1. //
  2. // Format.scala -- Scala object Format
  3. // Project OrcScala
  4. //
  5. // $Id: Format.scala 2933 2011-12-15 16:26:02Z jthywissen $
  6. //
  7. // Created by dkitchin on Jul 10, 2010.
  8. //
  9. // Copyright (c) 2011 The University of Texas at Austin. All rights reserved.
  10. //
  11. // Use and redistribution of this file is governed by the license terms in
  12. // the LICENSE file found in the project's top-level directory and also found at
  13. // URL: http://orc.csres.utexas.edu/license.shtml .
  14. //
  15. package orc.values
  16. /**
  17. * Format values (given as Scala's type AnyRef)
  18. *
  19. * A value which can be written in an Orc program is formatted as a string which
  20. * the parser would parse as an expression evaluating to that value.
  21. *
  22. * A value which cannot be written in an Orc program is given back in some
  23. * suitable pseudo-syntax.
  24. *
  25. * @author dkitchin
  26. */
  27. object Format {
  28. def formatValue(v: Any): String = {
  29. // Escape strings by default
  30. formatValue(v, true)
  31. }
  32. def formatValue(v: Any, escapeStrings: Boolean): String =
  33. v match {
  34. case null => "null"
  35. case l: List[_] => "[" + formatSequence(l) + "]"
  36. case s: String => if (escapeStrings) { unparseString(s) } else s
  37. case orcv: OrcValue => orcv.toOrcSyntax()
  38. case other => other.toString()
  39. }
  40. // For Java callers:
  41. def formatValueR(v: AnyRef): String = formatValue(v)
  42. def formatValueR(v: AnyRef, escapeStrings: Boolean): String = formatValue(v, escapeStrings)
  43. def formatSequence(vs: List[_]) =
  44. vs match {
  45. case Nil => ""
  46. case _ => (vs map { formatValue }) reduceRight { _ + ", " + _ }
  47. }
  48. def unparseString(s: String) = {
  49. "\"" + s.replace("\\", "\\\\").replace("\"", "\\\"").replace("\f", "\\f").replace("\n", "\\n").replace("\r", "\\r").replace("\t", "\\t") + "\""
  50. }
  51. }