/literal

A utility for parsing / formatting backslash-escaped String literals of common languages

Primary LanguageScala

literal

A neurotic and general library for formatting and parsing String literals accommodating and generating literals with a wide variety of escape conventions.

Within Scala object com.mchange.sc.v2.literal.StringLiteral are methods like...

  def formatCStringLiteral( raw : String )                 : String = ...
  def formatGCCStringLiteral( raw : String )               : String = ...
  def formatAsciiJavaStringLiteral ( raw : String )        : String = ...
  def formatUnicodeJavaStringLiteral ( raw : String )      : String = ...
  def formatAsciiScalaStringLiteral( raw : String )        : String = ...
  def formatUnicodeScalaStringLiteral( raw : String )      : String = ...
  def formatAsciiPermissiveStringLiteral( raw : String )   : String = ...
  def formatUnicodePermissiveStringLiteral( raw : String ) : String = ...

  def formatJavaStringLiteral( raw : String )       : String = ...
  def formatScalaStringLiteral( raw : String )      : String = ...
  def formatPermissiveStringLiteral( raw : String ) : String = ...

The word "permissive" is, a bit counterintuitively, from the perspective of formatting, not parsing. A "permissive" literal is one that can be widely used because it escapes everything that might cause trouble.

Even within all these classes, there is some discretion over how much to escape. To format literals with maximum escapage, try variations like EscapeMaximizing.formatJavaStringLiteral(...). (EscapeMaximizing is an object nested within com.mchange.sc.v2.literal.StringLiteral.)

For all of the formatters, there are matching parsers. If you printed with formatXXX, parse with parseXXX.

  case class Parse( endQuoteIndex : Int, parsed : String )

  def parseCStringLiteral( source : String, startQuoteIndex : Int = 0 )          : StringLiteral.Parse = ...
  def parseGCCStringLiteral( source : String, startQuoteIndex : Int = 0 )        : StringLiteral.Parse = ...
  def parseJavaStringLiteral( source : String, startQuoteIndex : Int = 0 )       : StringLiteral.Parse = ...
  def parseScalaStringLiteral( source : String, startQuoteIndex : Int = 0 )      : StringLiteral.Parse = ...
  def parsePermissiveStringLiteral( source : String, startQuoteIndex : Int = 0 ) : StringLiteral.Parse = ...

That's it!