|
Scala example source code file (NameTransformer.scala)
The Scala NameTransformer.scala source code/* __ *\ ** ________ ___ / / ___ Scala API ** ** / __/ __// _ | / / / _ | (c) 2003-2011, LAMP/EPFL ** ** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ ** ** /____/\___/_/ |_/____/_/ | | ** ** |/ ** \* */ package scala.reflect /** * @author Martin Odersky */ object NameTransformer { private val nops = 128 private val ncodes = 26 * 26 private class OpCodes(val op: Char, val code: String, val next: OpCodes) private val op2code = new Array[String](nops) private val code2op = new Array[OpCodes](ncodes) private def enterOp(op: Char, code: String) = { op2code(op) = code val c = (code.charAt(1) - 'a') * 26 + code.charAt(2) - 'a' code2op(c) = new OpCodes(op, code, code2op(c)) } /* Note: decoding assumes opcodes are only ever lowercase. */ enterOp('~', "$tilde") enterOp('=', "$eq") enterOp('<', "$less") enterOp('>', "$greater") enterOp('!', "$bang") enterOp('#', "$hash") enterOp('%', "$percent") enterOp('^', "$up") enterOp('&', "$amp") enterOp('|', "$bar") enterOp('*', "$times") enterOp('/', "$div") enterOp('+', "$plus") enterOp('-', "$minus") enterOp(':', "$colon") enterOp('\\', "$bslash") enterOp('?', "$qmark") enterOp('@', "$at") /** Replace operator symbols by corresponding `\$opname`. * * @param name the string to encode * @return the string with all recognized opchars replaced with their encoding */ def encode(name: String): String = { var buf: StringBuilder = null val len = name.length() var i = 0 while (i < len) { val c = name charAt i if (c < nops && (op2code(c) ne null)) { if (buf eq null) { buf = new StringBuilder() buf.append(name.substring(0, i)) } buf.append(op2code(c)) /* Handle glyphs that are not valid Java/JVM identifiers */ } else if (!Character.isJavaIdentifierPart(c)) { if (buf eq null) { buf = new StringBuilder() buf.append(name.substring(0, i)) } buf.append("$u%04X".format(c.toInt)) } else if (buf ne null) { buf.append(c) } i += 1 } if (buf eq null) name else buf.toString() } /** Replace `\$opname` by corresponding operator symbol. * * @param name0 the string to decode * @return the string with all recognized operator symbol encodings replaced with their name */ def decode(name0: String): String = { //System.out.println("decode: " + name);//DEBUG val name = if (name0.endsWith("<init>")) name0.substring(0, name0.length() - (" Other Scala examples (source code examples)Here is a short list of links related to this Scala NameTransformer.scala source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
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.