alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Scala example source code file (Names.scala)

This example Scala source code file (Names.scala) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Scala tags/keywords

array, array, names, names, string, string, stringbuffer, stringbuffer

The Scala Names.scala source code

/*     ___ ____ ___   __   ___   ___
**    / _// __// _ | / /  / _ | / _ \    Scala classfile decoder
**  __\ \/ /__/ __ |/ /__/ __ |/ ___/    (c) 2003-2011, LAMP/EPFL
** /____/\___/_/ |_/____/_/ |_/_/        http://scala-lang.org/
**
*/


package scala.tools.scalap


object Names {

  val operatorName = new Array[String](128)
  operatorName('$') = "$"
  operatorName('~') = "$tilde"
  operatorName('=') = "$eq"
  operatorName('<') = "$less"
  operatorName('>') = "$greater"
  operatorName('!') = "$bang"
  operatorName('#') = "$hash"
  operatorName('%') = "$percent"
  operatorName('^') = "$up"
  operatorName('&') = "$amp"
  operatorName('|') = "$bar"
  operatorName('*') = "$times"
  operatorName('/') = "$div"
  operatorName('\\') = "$bslash"
  operatorName('+') = "$plus"
  operatorName('-') = "$minus"
  operatorName(':') = "$colon"

  /** Replace operator symbols by corresponding "$op_name" in names.
   */
  def encode(name: String): String = {
    var i = 0
    val len = name.length()
    val res = new StringBuffer()
    while (i < len) {
      val c = name.charAt(i)
      if (c < 128) {
        val nop = operatorName(c)
        if (nop == null)
          res.append(c)
        else
          res.append(nop)
      } else
        res.append(c)
      i = i + 1
     }
     res.toString()
  }

  /** Replace "$op_name" by corresponding operator symbols in names.
   */
  def decode(name: String): String = {
    var i = 0
    val len = name.length()
    val res = new StringBuffer()
    while (i < len) {
      val c = name.charAt(i)
      if (c == '$') {
        var j = len
        while (j > i) {
          val prefix = name.substring(i, j)
          val c = lookup(prefix)
          if (c != null) {
            i = j
            res.append(c)
          } else
            j = j - 1
        }
      } else {
        i = i + 1
        res.append(c)
      }
    }
    res.toString()
  }

  /** Looks up the array entry for the operator name.
   */
  def lookup(string: String): String = {
    var i = 0
    var res: String = null
    while (i < 128) {
      if (string.equals(operatorName(i))) {
        res = String.valueOf(i.asInstanceOf[Char])
        i = 128
      }
      i = i + 1
    }
    res
  }

}

Other Scala examples (source code examples)

Here is a short list of links related to this Scala Names.scala source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.