You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@felix.apache.org by da...@apache.org on 2017/12/01 08:20:24 UTC
svn commit: r1816804 - in /felix/trunk/converter/converter/src:
main/java/org/osgi/util/converter/Util.java
test/java/org/osgi/util/converter/UtilTest.java
Author: davidb
Date: Fri Dec 1 08:20:24 2017
New Revision: 1816804
URL: http://svn.apache.org/viewvc?rev=1816804&view=rev
Log:
[Converter] Improved name mangling implementation committed on behalf of Peter Kriens with many thanks
Added:
felix/trunk/converter/converter/src/test/java/org/osgi/util/converter/UtilTest.java
Modified:
felix/trunk/converter/converter/src/main/java/org/osgi/util/converter/Util.java
Modified: felix/trunk/converter/converter/src/main/java/org/osgi/util/converter/Util.java
URL: http://svn.apache.org/viewvc/felix/trunk/converter/converter/src/main/java/org/osgi/util/converter/Util.java?rev=1816804&r1=1816803&r2=1816804&view=diff
==============================================================================
--- felix/trunk/converter/converter/src/main/java/org/osgi/util/converter/Util.java (original)
+++ felix/trunk/converter/converter/src/main/java/org/osgi/util/converter/Util.java Fri Dec 1 08:20:24 2017
@@ -345,18 +345,48 @@ class Util {
}
static String unMangleName(String prefix, String key) {
- String res = key;
- res = res.replace("$$", "\b"); // park double dollar as backspace char
- res = res.replace("$_$", "-");
- res = res.replaceAll("_\\$", ".");
- res = res.replace("__", "\f"); // park double underscore as formfeed
- // char
- res = res.replace('_', '.');
- res = res.replace("$", "");
- res = res.replace('\f', '_'); // convert formfeed char back to single
- // underscore
- res = res.replace('\b', '$'); // convert backspace char back go dollar
// TODO handle Java keywords
- return prefix + res;
+ return prefix + mangleMethodName(key);
+ }
+
+ static String mangleMethodName(String id) {
+ char[] array = id.toCharArray();
+ int out = 0;
+
+ boolean changed = false;
+ for (int i = 0; i < array.length; i++) {
+ if (match("$$", array, i) || match("__", array, i)) {
+ array[out++] = array[i++];
+ changed = true;
+ } else if (match("$_$", array, i)) {
+ array[out++] = '-';
+ i += 2;
+ } else {
+ char c = array[i];
+ if (c == '_') {
+ array[out++] = '.';
+ changed = true;
+ } else if (c == '$') {
+ changed = true;
+ } else {
+ array[out++] = c;
+ }
+ }
+ }
+ if (id.length() != out || changed)
+ return new String(array, 0, out);
+
+ return id;
+ }
+
+ private static boolean match(String pattern, char[] array, int i) {
+ for (int j = 0; j < pattern.length(); j++, i++) {
+ if (i >= array.length)
+ return false;
+
+ if (pattern.charAt(j) != array[i])
+ return false;
+ }
+ return true;
}
}
Added: felix/trunk/converter/converter/src/test/java/org/osgi/util/converter/UtilTest.java
URL: http://svn.apache.org/viewvc/felix/trunk/converter/converter/src/test/java/org/osgi/util/converter/UtilTest.java?rev=1816804&view=auto
==============================================================================
--- felix/trunk/converter/converter/src/test/java/org/osgi/util/converter/UtilTest.java (added)
+++ felix/trunk/converter/converter/src/test/java/org/osgi/util/converter/UtilTest.java Fri Dec 1 08:20:24 2017
@@ -0,0 +1,62 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.osgi.util.converter;
+
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+
+public class UtilTest {
+ @Test
+ public void testMangling() {
+ assertMangle("", "");
+ assertMangle("a", "a");
+ assertMangle("ab", "ab");
+ assertMangle("abc", "abc");
+ assertMangle("a\u0008bc", "a\bbc");
+
+ assertMangle("$_$", "-");
+ assertMangle("$_", ".");
+ assertMangle("_$", ".");
+ assertMangle("x$_$", "x-");
+ assertMangle("$_$x", "-x");
+ assertMangle("abc$_$abc", "abc-abc");
+ assertMangle("$$_$x", "$.x");
+ assertMangle("$_$$", "-");
+ assertMangle("$_$$$", "-$");
+ assertMangle("$", "");
+ assertMangle("$$", "$");
+ assertMangle("_", ".");
+ assertMangle("$_", ".");
+
+ assertMangle("myProperty143", "myProperty143");
+ assertMangle("$new", "new");
+ assertMangle("n$ew", "new");
+ assertMangle("new$", "new");
+ assertMangle("my$$prop", "my$prop");
+ assertMangle("dot_prop", "dot.prop");
+ assertMangle("_secret", ".secret");
+ assertMangle("another__prop", "another_prop");
+ assertMangle("three___prop", "three_.prop");
+ assertMangle("four_$__prop", "four._prop");
+ assertMangle("five_$_prop", "five..prop");
+ }
+
+ private void assertMangle(String methodName, String key) {
+ assertEquals(Util.mangleMethodName(methodName), key);
+ }
+}