From 8947a5900e390c19524ab3013b33f0a8ec07ea43 Mon Sep 17 00:00:00 2001 From: Aditya Naik Date: Fri, 2 Aug 2024 15:20:25 -0700 Subject: Update Module.scala --- core/src/main/scala/chisel3/util/Naming.scala | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 core/src/main/scala/chisel3/util/Naming.scala (limited to 'core/src/main/scala/chisel3/util') diff --git a/core/src/main/scala/chisel3/util/Naming.scala b/core/src/main/scala/chisel3/util/Naming.scala new file mode 100644 index 00000000..a78744b2 --- /dev/null +++ b/core/src/main/scala/chisel3/util/Naming.scala @@ -0,0 +1,23 @@ +package chisel3.util + +/* Generates a safe 'simple class name' from the given class, avoiding `Malformed class name` exceptions from `getClass.getSimpleName` + * when Java 8 is used. + */ +object simpleClassName { + + def apply[T](clazz: Class[T]): String = { + /* The default class name is derived from the Java reflection derived class name. */ + val baseName = clazz.getName + + /* A sequence of string filters applied to the name */ + val filters: Seq[String => String] = + Seq(((a: String) => raw"\$$+anon".r.replaceAllIn(a, "_Anon")) // Merge the "$$anon" name with previous name + ) + + filters + .foldLeft(baseName) { case (str, filter) => filter(str) } // 1. Apply filters to baseName + .split("\\.|\\$") // 2. Split string at '.' or '$' + .filterNot(_.forall(_.isDigit)) // 3. Drop purely numeric names + .last // 4. Use the last name + } +} -- cgit v1.2.3